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 StackTy::reverse_iterator reverse_iterator;
165 
166   DSAVarData getDSA(reverse_iterator &Iter, ValueDecl *D);
167 
168   /// \brief Checks if the variable is a local for OpenMP region.
169   bool isOpenMPLocal(VarDecl *D, 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(
329       const llvm::function_ref<bool(
330           OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
331           DPred,
332       bool FromParent);
333 
334   /// \brief Returns currently analyzed directive.
335   OpenMPDirectiveKind getCurrentDirective() const {
336     return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
337   }
338   /// \brief Returns directive kind at specified level.
339   OpenMPDirectiveKind getDirective(unsigned Level) const {
340     assert(!isStackEmpty() && "No directive at specified level.");
341     return Stack.back().first[Level].Directive;
342   }
343   /// \brief Returns parent directive.
344   OpenMPDirectiveKind getParentDirective() const {
345     if (isStackEmpty() || Stack.back().first.size() == 1)
346       return OMPD_unknown;
347     return std::next(Stack.back().first.rbegin())->Directive;
348   }
349 
350   /// \brief Set default data sharing attribute to none.
351   void setDefaultDSANone(SourceLocation Loc) {
352     assert(!isStackEmpty());
353     Stack.back().first.back().DefaultAttr = DSA_none;
354     Stack.back().first.back().DefaultAttrLoc = Loc;
355   }
356   /// \brief Set default data sharing attribute to shared.
357   void setDefaultDSAShared(SourceLocation Loc) {
358     assert(!isStackEmpty());
359     Stack.back().first.back().DefaultAttr = DSA_shared;
360     Stack.back().first.back().DefaultAttrLoc = Loc;
361   }
362   /// Set default data mapping attribute to 'tofrom:scalar'.
363   void setDefaultDMAToFromScalar(SourceLocation Loc) {
364     assert(!isStackEmpty());
365     Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
366     Stack.back().first.back().DefaultMapAttrLoc = Loc;
367   }
368 
369   DefaultDataSharingAttributes getDefaultDSA() const {
370     return isStackEmpty() ? DSA_unspecified
371                           : Stack.back().first.back().DefaultAttr;
372   }
373   SourceLocation getDefaultDSALocation() const {
374     return isStackEmpty() ? SourceLocation()
375                           : Stack.back().first.back().DefaultAttrLoc;
376   }
377   DefaultMapAttributes getDefaultDMA() const {
378     return isStackEmpty() ? DMA_unspecified
379                           : Stack.back().first.back().DefaultMapAttr;
380   }
381   DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
382     return Stack.back().first[Level].DefaultMapAttr;
383   }
384   SourceLocation getDefaultDMALocation() const {
385     return isStackEmpty() ? SourceLocation()
386                           : Stack.back().first.back().DefaultMapAttrLoc;
387   }
388 
389   /// \brief Checks if the specified variable is a threadprivate.
390   bool isThreadPrivate(VarDecl *D) {
391     DSAVarData DVar = getTopDSA(D, false);
392     return isOpenMPThreadPrivate(DVar.CKind);
393   }
394 
395   /// \brief Marks current region as ordered (it has an 'ordered' clause).
396   void setOrderedRegion(bool IsOrdered, Expr *Param) {
397     assert(!isStackEmpty());
398     Stack.back().first.back().OrderedRegion.setInt(IsOrdered);
399     Stack.back().first.back().OrderedRegion.setPointer(Param);
400   }
401   /// \brief Returns true, if parent region is ordered (has associated
402   /// 'ordered' clause), false - otherwise.
403   bool isParentOrderedRegion() const {
404     if (isStackEmpty() || Stack.back().first.size() == 1)
405       return false;
406     return std::next(Stack.back().first.rbegin())->OrderedRegion.getInt();
407   }
408   /// \brief Returns optional parameter for the ordered region.
409   Expr *getParentOrderedRegionParam() const {
410     if (isStackEmpty() || Stack.back().first.size() == 1)
411       return nullptr;
412     return std::next(Stack.back().first.rbegin())->OrderedRegion.getPointer();
413   }
414   /// \brief Marks current region as nowait (it has a 'nowait' clause).
415   void setNowaitRegion(bool IsNowait = true) {
416     assert(!isStackEmpty());
417     Stack.back().first.back().NowaitRegion = IsNowait;
418   }
419   /// \brief Returns true, if parent region is nowait (has associated
420   /// 'nowait' clause), false - otherwise.
421   bool isParentNowaitRegion() const {
422     if (isStackEmpty() || Stack.back().first.size() == 1)
423       return false;
424     return std::next(Stack.back().first.rbegin())->NowaitRegion;
425   }
426   /// \brief Marks parent region as cancel region.
427   void setParentCancelRegion(bool Cancel = true) {
428     if (!isStackEmpty() && Stack.back().first.size() > 1) {
429       auto &StackElemRef = *std::next(Stack.back().first.rbegin());
430       StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
431     }
432   }
433   /// \brief Return true if current region has inner cancel construct.
434   bool isCancelRegion() const {
435     return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
436   }
437 
438   /// \brief Set collapse value for the region.
439   void setAssociatedLoops(unsigned Val) {
440     assert(!isStackEmpty());
441     Stack.back().first.back().AssociatedLoops = Val;
442   }
443   /// \brief Return collapse value for region.
444   unsigned getAssociatedLoops() const {
445     return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
446   }
447 
448   /// \brief Marks current target region as one with closely nested teams
449   /// region.
450   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
451     if (!isStackEmpty() && Stack.back().first.size() > 1) {
452       std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
453           TeamsRegionLoc;
454     }
455   }
456   /// \brief Returns true, if current region has closely nested teams region.
457   bool hasInnerTeamsRegion() const {
458     return getInnerTeamsRegionLoc().isValid();
459   }
460   /// \brief Returns location of the nested teams region (if any).
461   SourceLocation getInnerTeamsRegionLoc() const {
462     return isStackEmpty() ? SourceLocation()
463                           : Stack.back().first.back().InnerTeamsRegionLoc;
464   }
465 
466   Scope *getCurScope() const {
467     return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
468   }
469   Scope *getCurScope() {
470     return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
471   }
472   SourceLocation getConstructLoc() {
473     return isStackEmpty() ? SourceLocation()
474                           : Stack.back().first.back().ConstructLoc;
475   }
476 
477   /// Do the check specified in \a Check to all component lists and return true
478   /// if any issue is found.
479   bool checkMappableExprComponentListsForDecl(
480       ValueDecl *VD, bool CurrentRegionOnly,
481       const llvm::function_ref<
482           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
483                OpenMPClauseKind)>
484           Check) {
485     if (isStackEmpty())
486       return false;
487     auto SI = Stack.back().first.rbegin();
488     auto SE = Stack.back().first.rend();
489 
490     if (SI == SE)
491       return false;
492 
493     if (CurrentRegionOnly) {
494       SE = std::next(SI);
495     } else {
496       ++SI;
497     }
498 
499     for (; SI != SE; ++SI) {
500       auto MI = SI->MappedExprComponents.find(VD);
501       if (MI != SI->MappedExprComponents.end())
502         for (auto &L : MI->second.Components)
503           if (Check(L, MI->second.Kind))
504             return true;
505     }
506     return false;
507   }
508 
509   /// Do the check specified in \a Check to all component lists at a given level
510   /// and return true if any issue is found.
511   bool checkMappableExprComponentListsForDeclAtLevel(
512       ValueDecl *VD, unsigned Level,
513       const llvm::function_ref<
514           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
515                OpenMPClauseKind)>
516           Check) {
517     if (isStackEmpty())
518       return false;
519 
520     auto StartI = Stack.back().first.begin();
521     auto EndI = Stack.back().first.end();
522     if (std::distance(StartI, EndI) <= (int)Level)
523       return false;
524     std::advance(StartI, Level);
525 
526     auto MI = StartI->MappedExprComponents.find(VD);
527     if (MI != StartI->MappedExprComponents.end())
528       for (auto &L : MI->second.Components)
529         if (Check(L, MI->second.Kind))
530           return true;
531     return false;
532   }
533 
534   /// Create a new mappable expression component list associated with a given
535   /// declaration and initialize it with the provided list of components.
536   void addMappableExpressionComponents(
537       ValueDecl *VD,
538       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
539       OpenMPClauseKind WhereFoundClauseKind) {
540     assert(!isStackEmpty() &&
541            "Not expecting to retrieve components from a empty stack!");
542     auto &MEC = Stack.back().first.back().MappedExprComponents[VD];
543     // Create new entry and append the new components there.
544     MEC.Components.resize(MEC.Components.size() + 1);
545     MEC.Components.back().append(Components.begin(), Components.end());
546     MEC.Kind = WhereFoundClauseKind;
547   }
548 
549   unsigned getNestingLevel() const {
550     assert(!isStackEmpty());
551     return Stack.back().first.size() - 1;
552   }
553   void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
554     assert(!isStackEmpty() && Stack.back().first.size() > 1);
555     auto &StackElem = *std::next(Stack.back().first.rbegin());
556     assert(isOpenMPWorksharingDirective(StackElem.Directive));
557     StackElem.DoacrossDepends.insert({C, OpsOffs});
558   }
559   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
560   getDoacrossDependClauses() const {
561     assert(!isStackEmpty());
562     auto &StackElem = Stack.back().first.back();
563     if (isOpenMPWorksharingDirective(StackElem.Directive)) {
564       auto &Ref = StackElem.DoacrossDepends;
565       return llvm::make_range(Ref.begin(), Ref.end());
566     }
567     return llvm::make_range(StackElem.DoacrossDepends.end(),
568                             StackElem.DoacrossDepends.end());
569   }
570 };
571 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
572   return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
573          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
574 }
575 } // namespace
576 
577 static Expr *getExprAsWritten(Expr *E) {
578   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
579     E = ExprTemp->getSubExpr();
580 
581   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
582     E = MTE->GetTemporaryExpr();
583 
584   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
585     E = Binder->getSubExpr();
586 
587   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
588     E = ICE->getSubExprAsWritten();
589   return E->IgnoreParens();
590 }
591 
592 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
593   if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
594     if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
595       D = ME->getMemberDecl();
596   auto *VD = dyn_cast<VarDecl>(D);
597   auto *FD = dyn_cast<FieldDecl>(D);
598   if (VD != nullptr) {
599     VD = VD->getCanonicalDecl();
600     D = VD;
601   } else {
602     assert(FD);
603     FD = FD->getCanonicalDecl();
604     D = FD;
605   }
606   return D;
607 }
608 
609 DSAStackTy::DSAVarData DSAStackTy::getDSA(reverse_iterator &Iter,
610                                           ValueDecl *D) {
611   D = getCanonicalDecl(D);
612   auto *VD = dyn_cast<VarDecl>(D);
613   auto *FD = dyn_cast<FieldDecl>(D);
614   DSAVarData DVar;
615   if (isStackEmpty() || Iter == Stack.back().first.rend()) {
616     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
617     // in a region but not in construct]
618     //  File-scope or namespace-scope variables referenced in called routines
619     //  in the region are shared unless they appear in a threadprivate
620     //  directive.
621     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
622       DVar.CKind = OMPC_shared;
623 
624     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
625     // in a region but not in construct]
626     //  Variables with static storage duration that are declared in called
627     //  routines in the region are shared.
628     if (VD && VD->hasGlobalStorage())
629       DVar.CKind = OMPC_shared;
630 
631     // Non-static data members are shared by default.
632     if (FD)
633       DVar.CKind = OMPC_shared;
634 
635     return DVar;
636   }
637 
638   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
639   // in a Construct, C/C++, predetermined, p.1]
640   // Variables with automatic storage duration that are declared in a scope
641   // inside the construct are private.
642   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
643       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
644     DVar.CKind = OMPC_private;
645     return DVar;
646   }
647 
648   DVar.DKind = Iter->Directive;
649   // Explicitly specified attributes and local variables with predetermined
650   // attributes.
651   if (Iter->SharingMap.count(D)) {
652     DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
653     DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
654     DVar.CKind = Iter->SharingMap[D].Attributes;
655     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
656     return DVar;
657   }
658 
659   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
660   // in a Construct, C/C++, implicitly determined, p.1]
661   //  In a parallel or task construct, the data-sharing attributes of these
662   //  variables are determined by the default clause, if present.
663   switch (Iter->DefaultAttr) {
664   case DSA_shared:
665     DVar.CKind = OMPC_shared;
666     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
667     return DVar;
668   case DSA_none:
669     return DVar;
670   case DSA_unspecified:
671     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
672     // in a Construct, implicitly determined, p.2]
673     //  In a parallel construct, if no default clause is present, these
674     //  variables are shared.
675     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
676     if (isOpenMPParallelDirective(DVar.DKind) ||
677         isOpenMPTeamsDirective(DVar.DKind)) {
678       DVar.CKind = OMPC_shared;
679       return DVar;
680     }
681 
682     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
683     // in a Construct, implicitly determined, p.4]
684     //  In a task construct, if no default clause is present, a variable that in
685     //  the enclosing context is determined to be shared by all implicit tasks
686     //  bound to the current team is shared.
687     if (isOpenMPTaskingDirective(DVar.DKind)) {
688       DSAVarData DVarTemp;
689       auto I = Iter, E = Stack.back().first.rend();
690       do {
691         ++I;
692         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
693         // Referenced in a Construct, implicitly determined, p.6]
694         //  In a task construct, if no default clause is present, a variable
695         //  whose data-sharing attribute is not determined by the rules above is
696         //  firstprivate.
697         DVarTemp = getDSA(I, D);
698         if (DVarTemp.CKind != OMPC_shared) {
699           DVar.RefExpr = nullptr;
700           DVar.CKind = OMPC_firstprivate;
701           return DVar;
702         }
703       } while (I != E && !isParallelOrTaskRegion(I->Directive));
704       DVar.CKind =
705           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
706       return DVar;
707     }
708   }
709   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
710   // in a Construct, implicitly determined, p.3]
711   //  For constructs other than task, if no default clause is present, these
712   //  variables inherit their data-sharing attributes from the enclosing
713   //  context.
714   return getDSA(++Iter, D);
715 }
716 
717 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
718   assert(!isStackEmpty() && "Data sharing attributes stack is empty");
719   D = getCanonicalDecl(D);
720   auto &StackElem = Stack.back().first.back();
721   auto It = StackElem.AlignedMap.find(D);
722   if (It == StackElem.AlignedMap.end()) {
723     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
724     StackElem.AlignedMap[D] = NewDE;
725     return nullptr;
726   } else {
727     assert(It->second && "Unexpected nullptr expr in the aligned map");
728     return It->second;
729   }
730   return nullptr;
731 }
732 
733 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
734   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
735   D = getCanonicalDecl(D);
736   auto &StackElem = Stack.back().first.back();
737   StackElem.LCVMap.insert(
738       {D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture)});
739 }
740 
741 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
742   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
743   D = getCanonicalDecl(D);
744   auto &StackElem = Stack.back().first.back();
745   auto It = StackElem.LCVMap.find(D);
746   if (It != StackElem.LCVMap.end())
747     return It->second;
748   return {0, nullptr};
749 }
750 
751 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
752   assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
753          "Data-sharing attributes stack is empty");
754   D = getCanonicalDecl(D);
755   auto &StackElem = *std::next(Stack.back().first.rbegin());
756   auto It = StackElem.LCVMap.find(D);
757   if (It != StackElem.LCVMap.end())
758     return It->second;
759   return {0, nullptr};
760 }
761 
762 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
763   assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
764          "Data-sharing attributes stack is empty");
765   auto &StackElem = *std::next(Stack.back().first.rbegin());
766   if (StackElem.LCVMap.size() < I)
767     return nullptr;
768   for (auto &Pair : StackElem.LCVMap)
769     if (Pair.second.first == I)
770       return Pair.first;
771   return nullptr;
772 }
773 
774 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
775                         DeclRefExpr *PrivateCopy) {
776   D = getCanonicalDecl(D);
777   if (A == OMPC_threadprivate) {
778     auto &Data = Threadprivates[D];
779     Data.Attributes = A;
780     Data.RefExpr.setPointer(E);
781     Data.PrivateCopy = nullptr;
782   } else {
783     assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
784     auto &Data = Stack.back().first.back().SharingMap[D];
785     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
786            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
787            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
788            (isLoopControlVariable(D).first && A == OMPC_private));
789     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
790       Data.RefExpr.setInt(/*IntVal=*/true);
791       return;
792     }
793     const bool IsLastprivate =
794         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
795     Data.Attributes = A;
796     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
797     Data.PrivateCopy = PrivateCopy;
798     if (PrivateCopy) {
799       auto &Data = Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
800       Data.Attributes = A;
801       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
802       Data.PrivateCopy = nullptr;
803     }
804   }
805 }
806 
807 /// \brief Build a variable declaration for OpenMP loop iteration variable.
808 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
809                              StringRef Name, const AttrVec *Attrs = nullptr,
810                              DeclRefExpr *OrigRef = nullptr) {
811   DeclContext *DC = SemaRef.CurContext;
812   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
813   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
814   VarDecl *Decl =
815       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
816   if (Attrs) {
817     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
818          I != E; ++I)
819       Decl->addAttr(*I);
820   }
821   Decl->setImplicit();
822   if (OrigRef) {
823     Decl->addAttr(
824         OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
825   }
826   return Decl;
827 }
828 
829 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
830                                      SourceLocation Loc,
831                                      bool RefersToCapture = false) {
832   D->setReferenced();
833   D->markUsed(S.Context);
834   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
835                              SourceLocation(), D, RefersToCapture, Loc, Ty,
836                              VK_LValue);
837 }
838 
839 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
840                                            BinaryOperatorKind BOK) {
841   D = getCanonicalDecl(D);
842   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
843   assert(
844       Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
845       "Additional reduction info may be specified only for reduction items.");
846   auto &ReductionData = Stack.back().first.back().ReductionMap[D];
847   assert(ReductionData.ReductionRange.isInvalid() &&
848          Stack.back().first.back().Directive == OMPD_taskgroup &&
849          "Additional reduction info may be specified only once for reduction "
850          "items.");
851   ReductionData.set(BOK, SR);
852   Expr *&TaskgroupReductionRef =
853       Stack.back().first.back().TaskgroupReductionRef;
854   if (!TaskgroupReductionRef) {
855     auto *VD = buildVarDecl(SemaRef, SR.getBegin(),
856                             SemaRef.Context.VoidPtrTy, ".task_red.");
857     TaskgroupReductionRef =
858         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
859   }
860 }
861 
862 void DSAStackTy::addTaskgroupReductionData(ValueDecl *D, SourceRange SR,
863                                            const Expr *ReductionRef) {
864   D = getCanonicalDecl(D);
865   assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
866   assert(
867       Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
868       "Additional reduction info may be specified only for reduction items.");
869   auto &ReductionData = Stack.back().first.back().ReductionMap[D];
870   assert(ReductionData.ReductionRange.isInvalid() &&
871          Stack.back().first.back().Directive == OMPD_taskgroup &&
872          "Additional reduction info may be specified only once for reduction "
873          "items.");
874   ReductionData.set(ReductionRef, SR);
875   Expr *&TaskgroupReductionRef =
876       Stack.back().first.back().TaskgroupReductionRef;
877   if (!TaskgroupReductionRef) {
878     auto *VD = buildVarDecl(SemaRef, SR.getBegin(), SemaRef.Context.VoidPtrTy,
879                             ".task_red.");
880     TaskgroupReductionRef =
881         buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
882   }
883 }
884 
885 DSAStackTy::DSAVarData
886 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
887                                              BinaryOperatorKind &BOK,
888                                              Expr *&TaskgroupDescriptor) {
889   D = getCanonicalDecl(D);
890   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
891   if (Stack.back().first.empty())
892       return DSAVarData();
893   for (auto I = std::next(Stack.back().first.rbegin(), 1),
894             E = Stack.back().first.rend();
895        I != E; std::advance(I, 1)) {
896     auto &Data = I->SharingMap[D];
897     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
898       continue;
899     auto &ReductionData = I->ReductionMap[D];
900     if (!ReductionData.ReductionOp ||
901         ReductionData.ReductionOp.is<const Expr *>())
902       return DSAVarData();
903     SR = ReductionData.ReductionRange;
904     BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
905     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
906                                        "expression for the descriptor is not "
907                                        "set.");
908     TaskgroupDescriptor = I->TaskgroupReductionRef;
909     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
910                       Data.PrivateCopy, I->DefaultAttrLoc);
911   }
912   return DSAVarData();
913 }
914 
915 DSAStackTy::DSAVarData
916 DSAStackTy::getTopMostTaskgroupReductionData(ValueDecl *D, SourceRange &SR,
917                                              const Expr *&ReductionRef,
918                                              Expr *&TaskgroupDescriptor) {
919   D = getCanonicalDecl(D);
920   assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
921   if (Stack.back().first.empty())
922       return DSAVarData();
923   for (auto I = std::next(Stack.back().first.rbegin(), 1),
924             E = Stack.back().first.rend();
925        I != E; std::advance(I, 1)) {
926     auto &Data = I->SharingMap[D];
927     if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
928       continue;
929     auto &ReductionData = I->ReductionMap[D];
930     if (!ReductionData.ReductionOp ||
931         !ReductionData.ReductionOp.is<const Expr *>())
932       return DSAVarData();
933     SR = ReductionData.ReductionRange;
934     ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
935     assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
936                                        "expression for the descriptor is not "
937                                        "set.");
938     TaskgroupDescriptor = I->TaskgroupReductionRef;
939     return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
940                       Data.PrivateCopy, I->DefaultAttrLoc);
941   }
942   return DSAVarData();
943 }
944 
945 bool DSAStackTy::isOpenMPLocal(VarDecl *D, reverse_iterator Iter) {
946   D = D->getCanonicalDecl();
947   if (!isStackEmpty()) {
948     reverse_iterator I = Iter, E = Stack.back().first.rend();
949     Scope *TopScope = nullptr;
950     while (I != E && !isParallelOrTaskRegion(I->Directive) &&
951            !isOpenMPTargetExecutionDirective(I->Directive))
952       ++I;
953     if (I == E)
954       return false;
955     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
956     Scope *CurScope = getCurScope();
957     while (CurScope != TopScope && !CurScope->isDeclScope(D))
958       CurScope = CurScope->getParent();
959     return CurScope != TopScope;
960   }
961   return false;
962 }
963 
964 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
965   D = getCanonicalDecl(D);
966   DSAVarData DVar;
967 
968   auto *VD = dyn_cast<VarDecl>(D);
969   auto TI = Threadprivates.find(D);
970   if (TI != Threadprivates.end()) {
971     DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
972     DVar.CKind = OMPC_threadprivate;
973     return DVar;
974   } else if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
975     DVar.RefExpr = buildDeclRefExpr(
976         SemaRef, VD, D->getType().getNonReferenceType(),
977         VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
978     DVar.CKind = OMPC_threadprivate;
979     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
980     return DVar;
981   }
982   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
983   // in a Construct, C/C++, predetermined, p.1]
984   //  Variables appearing in threadprivate directives are threadprivate.
985   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
986        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
987          SemaRef.getLangOpts().OpenMPUseTLS &&
988          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
989       (VD && VD->getStorageClass() == SC_Register &&
990        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
991     DVar.RefExpr = buildDeclRefExpr(
992         SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
993     DVar.CKind = OMPC_threadprivate;
994     addDSA(D, DVar.RefExpr, OMPC_threadprivate);
995     return DVar;
996   }
997   if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
998       VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
999       !isLoopControlVariable(D).first) {
1000     auto IterTarget =
1001         std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1002                      [](const SharingMapTy &Data) {
1003                        return isOpenMPTargetExecutionDirective(Data.Directive);
1004                      });
1005     if (IterTarget != Stack.back().first.rend()) {
1006       auto ParentIterTarget = std::next(IterTarget, 1);
1007       auto Iter = Stack.back().first.rbegin();
1008       while (Iter != ParentIterTarget) {
1009         if (isOpenMPLocal(VD, Iter)) {
1010           DVar.RefExpr =
1011               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1012                                D->getLocation());
1013           DVar.CKind = OMPC_threadprivate;
1014           return DVar;
1015         }
1016         std::advance(Iter, 1);
1017       }
1018       if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1019         auto DSAIter = IterTarget->SharingMap.find(D);
1020         if (DSAIter != IterTarget->SharingMap.end() &&
1021             isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1022           DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1023           DVar.CKind = OMPC_threadprivate;
1024           return DVar;
1025         } else if (!SemaRef.IsOpenMPCapturedByRef(
1026                        D, std::distance(ParentIterTarget,
1027                                         Stack.back().first.rend()))) {
1028           DVar.RefExpr =
1029               buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1030                                IterTarget->ConstructLoc);
1031           DVar.CKind = OMPC_threadprivate;
1032           return DVar;
1033         }
1034       }
1035     }
1036   }
1037 
1038   if (isStackEmpty())
1039     // Not in OpenMP execution region and top scope was already checked.
1040     return DVar;
1041 
1042   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1043   // in a Construct, C/C++, predetermined, p.4]
1044   //  Static data members are shared.
1045   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1046   // in a Construct, C/C++, predetermined, p.7]
1047   //  Variables with static storage duration that are declared in a scope
1048   //  inside the construct are shared.
1049   auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
1050   if (VD && VD->isStaticDataMember()) {
1051     DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
1052     if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1053       return DVar;
1054 
1055     DVar.CKind = OMPC_shared;
1056     return DVar;
1057   }
1058 
1059   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
1060   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1061   Type = SemaRef.getASTContext().getBaseElementType(Type);
1062   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1063   // in a Construct, C/C++, predetermined, p.6]
1064   //  Variables with const qualified type having no mutable member are
1065   //  shared.
1066   CXXRecordDecl *RD =
1067       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
1068   if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1069     if (auto *CTD = CTSD->getSpecializedTemplate())
1070       RD = CTD->getTemplatedDecl();
1071   if (IsConstant &&
1072       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1073         RD->hasMutableFields())) {
1074     // Variables with const-qualified type having no mutable member may be
1075     // listed in a firstprivate clause, even if they are static data members.
1076     DSAVarData DVarTemp = hasDSA(
1077         D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
1078         MatchesAlways, FromParent);
1079     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
1080       return DVarTemp;
1081 
1082     DVar.CKind = OMPC_shared;
1083     return DVar;
1084   }
1085 
1086   // Explicitly specified attributes and local variables with predetermined
1087   // attributes.
1088   auto I = Stack.back().first.rbegin();
1089   auto EndI = Stack.back().first.rend();
1090   if (FromParent && I != EndI)
1091     std::advance(I, 1);
1092   if (I->SharingMap.count(D)) {
1093     DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
1094     DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
1095     DVar.CKind = I->SharingMap[D].Attributes;
1096     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
1097     DVar.DKind = I->Directive;
1098   }
1099 
1100   return DVar;
1101 }
1102 
1103 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1104                                                   bool FromParent) {
1105   if (isStackEmpty()) {
1106     reverse_iterator I;
1107     return getDSA(I, D);
1108   }
1109   D = getCanonicalDecl(D);
1110   auto StartI = Stack.back().first.rbegin();
1111   auto EndI = Stack.back().first.rend();
1112   if (FromParent && StartI != EndI)
1113     std::advance(StartI, 1);
1114   return getDSA(StartI, D);
1115 }
1116 
1117 DSAStackTy::DSAVarData
1118 DSAStackTy::hasDSA(ValueDecl *D,
1119                    const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1120                    const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1121                    bool FromParent) {
1122   if (isStackEmpty())
1123     return {};
1124   D = getCanonicalDecl(D);
1125   auto I = Stack.back().first.rbegin();
1126   auto EndI = Stack.back().first.rend();
1127   if (FromParent && I != EndI)
1128     std::advance(I, 1);
1129   for (; I != EndI; std::advance(I, 1)) {
1130     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
1131       continue;
1132     auto NewI = I;
1133     DSAVarData DVar = getDSA(NewI, D);
1134     if (I == NewI && CPred(DVar.CKind))
1135       return DVar;
1136   }
1137   return {};
1138 }
1139 
1140 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
1141     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1142     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1143     bool FromParent) {
1144   if (isStackEmpty())
1145     return {};
1146   D = getCanonicalDecl(D);
1147   auto StartI = Stack.back().first.rbegin();
1148   auto EndI = Stack.back().first.rend();
1149   if (FromParent && StartI != EndI)
1150     std::advance(StartI, 1);
1151   if (StartI == EndI || !DPred(StartI->Directive))
1152     return {};
1153   auto NewI = StartI;
1154   DSAVarData DVar = getDSA(NewI, D);
1155   return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
1156 }
1157 
1158 bool DSAStackTy::hasExplicitDSA(
1159     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1160     unsigned Level, bool NotLastprivate) {
1161   if (isStackEmpty())
1162     return false;
1163   D = getCanonicalDecl(D);
1164   auto StartI = Stack.back().first.begin();
1165   auto EndI = Stack.back().first.end();
1166   if (std::distance(StartI, EndI) <= (int)Level)
1167     return false;
1168   std::advance(StartI, Level);
1169   return (StartI->SharingMap.count(D) > 0) &&
1170          StartI->SharingMap[D].RefExpr.getPointer() &&
1171          CPred(StartI->SharingMap[D].Attributes) &&
1172          (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
1173 }
1174 
1175 bool DSAStackTy::hasExplicitDirective(
1176     const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred, unsigned Level) {
1177   if (isStackEmpty())
1178     return false;
1179   auto StartI = Stack.back().first.begin();
1180   auto EndI = Stack.back().first.end();
1181   if (std::distance(StartI, EndI) <= (int)Level)
1182     return false;
1183   std::advance(StartI, Level);
1184   return DPred(StartI->Directive);
1185 }
1186 
1187 bool DSAStackTy::hasDirective(
1188     const llvm::function_ref<bool(OpenMPDirectiveKind,
1189                                   const DeclarationNameInfo &, SourceLocation)>
1190         DPred,
1191     bool FromParent) {
1192   // We look only in the enclosing region.
1193   if (isStackEmpty())
1194     return false;
1195   auto StartI = std::next(Stack.back().first.rbegin());
1196   auto EndI = Stack.back().first.rend();
1197   if (FromParent && StartI != EndI)
1198     StartI = std::next(StartI);
1199   for (auto I = StartI, EE = EndI; I != EE; ++I) {
1200     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1201       return true;
1202   }
1203   return false;
1204 }
1205 
1206 void Sema::InitDataSharingAttributesStack() {
1207   VarDataSharingAttributesStack = new DSAStackTy(*this);
1208 }
1209 
1210 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1211 
1212 void Sema::pushOpenMPFunctionRegion() {
1213   DSAStack->pushFunction();
1214 }
1215 
1216 void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1217   DSAStack->popFunction(OldFSI);
1218 }
1219 
1220 static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy>
1221 isDeclareTargetDeclaration(const ValueDecl *VD) {
1222   for (const auto *D : VD->redecls()) {
1223     if (!D->hasAttrs())
1224       continue;
1225     if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>())
1226       return Attr->getMapType();
1227   }
1228   return llvm::None;
1229 }
1230 
1231 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
1232   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1233 
1234   auto &Ctx = getASTContext();
1235   bool IsByRef = true;
1236 
1237   // Find the directive that is associated with the provided scope.
1238   D = cast<ValueDecl>(D->getCanonicalDecl());
1239   auto Ty = D->getType();
1240 
1241   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
1242     // This table summarizes how a given variable should be passed to the device
1243     // given its type and the clauses where it appears. This table is based on
1244     // the description in OpenMP 4.5 [2.10.4, target Construct] and
1245     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1246     //
1247     // =========================================================================
1248     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
1249     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
1250     // =========================================================================
1251     // | scl  |               |     |       |       -       |          | bycopy|
1252     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
1253     // | scl  |               |  x  |   -   |       -       |     -    | null  |
1254     // | scl  |       x       |     |       |       -       |          | byref |
1255     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
1256     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
1257     // | scl  |               |  -  |   -   |       -       |     x    | byref |
1258     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
1259     //
1260     // | agg  |      n.a.     |     |       |       -       |          | byref |
1261     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
1262     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1263     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1264     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
1265     //
1266     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
1267     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
1268     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
1269     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
1270     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
1271     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
1272     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
1273     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
1274     // =========================================================================
1275     // Legend:
1276     //  scl - scalar
1277     //  ptr - pointer
1278     //  agg - aggregate
1279     //  x - applies
1280     //  - - invalid in this combination
1281     //  [] - mapped with an array section
1282     //  byref - should be mapped by reference
1283     //  byval - should be mapped by value
1284     //  null - initialize a local variable to null on the device
1285     //
1286     // Observations:
1287     //  - All scalar declarations that show up in a map clause have to be passed
1288     //    by reference, because they may have been mapped in the enclosing data
1289     //    environment.
1290     //  - If the scalar value does not fit the size of uintptr, it has to be
1291     //    passed by reference, regardless the result in the table above.
1292     //  - For pointers mapped by value that have either an implicit map or an
1293     //    array section, the runtime library may pass the NULL value to the
1294     //    device instead of the value passed to it by the compiler.
1295 
1296     if (Ty->isReferenceType())
1297       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
1298 
1299     // Locate map clauses and see if the variable being captured is referred to
1300     // in any of those clauses. Here we only care about variables, not fields,
1301     // because fields are part of aggregates.
1302     bool IsVariableUsedInMapClause = false;
1303     bool IsVariableAssociatedWithSection = false;
1304 
1305     DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1306         D, Level, [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
1307                 MapExprComponents,
1308             OpenMPClauseKind WhereFoundClauseKind) {
1309           // Only the map clause information influences how a variable is
1310           // captured. E.g. is_device_ptr does not require changing the default
1311           // behavior.
1312           if (WhereFoundClauseKind != OMPC_map)
1313             return false;
1314 
1315           auto EI = MapExprComponents.rbegin();
1316           auto EE = MapExprComponents.rend();
1317 
1318           assert(EI != EE && "Invalid map expression!");
1319 
1320           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1321             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1322 
1323           ++EI;
1324           if (EI == EE)
1325             return false;
1326 
1327           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1328               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1329               isa<MemberExpr>(EI->getAssociatedExpression())) {
1330             IsVariableAssociatedWithSection = true;
1331             // There is nothing more we need to know about this variable.
1332             return true;
1333           }
1334 
1335           // Keep looking for more map info.
1336           return false;
1337         });
1338 
1339     if (IsVariableUsedInMapClause) {
1340       // If variable is identified in a map clause it is always captured by
1341       // reference except if it is a pointer that is dereferenced somehow.
1342       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1343     } else {
1344       // By default, all the data that has a scalar type is mapped by copy
1345       // (except for reduction variables).
1346       IsByRef =
1347           !Ty->isScalarType() ||
1348           DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1349           DSAStack->hasExplicitDSA(
1350               D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
1351     }
1352   }
1353 
1354   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
1355     IsByRef =
1356         !DSAStack->hasExplicitDSA(
1357             D,
1358             [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1359             Level, /*NotLastprivate=*/true) &&
1360         // If the variable is artificial and must be captured by value - try to
1361         // capture by value.
1362         !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1363           !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
1364   }
1365 
1366   // When passing data by copy, we need to make sure it fits the uintptr size
1367   // and alignment, because the runtime library only deals with uintptr types.
1368   // If it does not fit the uintptr size, we need to pass the data by reference
1369   // instead.
1370   if (!IsByRef &&
1371       (Ctx.getTypeSizeInChars(Ty) >
1372            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
1373        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
1374     IsByRef = true;
1375   }
1376 
1377   return IsByRef;
1378 }
1379 
1380 unsigned Sema::getOpenMPNestingLevel() const {
1381   assert(getLangOpts().OpenMP);
1382   return DSAStack->getNestingLevel();
1383 }
1384 
1385 bool Sema::isInOpenMPTargetExecutionDirective() const {
1386   return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1387           !DSAStack->isClauseParsingMode()) ||
1388          DSAStack->hasDirective(
1389              [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1390                 SourceLocation) -> bool {
1391                return isOpenMPTargetExecutionDirective(K);
1392              },
1393              false);
1394 }
1395 
1396 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
1397   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1398   D = getCanonicalDecl(D);
1399 
1400   // If we are attempting to capture a global variable in a directive with
1401   // 'target' we return true so that this global is also mapped to the device.
1402   //
1403   auto *VD = dyn_cast<VarDecl>(D);
1404   if (VD && !VD->hasLocalStorage() && isInOpenMPTargetExecutionDirective()) {
1405     // If the declaration is enclosed in a 'declare target' directive,
1406     // then it should not be captured.
1407     //
1408     if (isDeclareTargetDeclaration(VD))
1409       return nullptr;
1410     return VD;
1411   }
1412 
1413   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1414       (!DSAStack->isClauseParsingMode() ||
1415        DSAStack->getParentDirective() != OMPD_unknown)) {
1416     auto &&Info = DSAStack->isLoopControlVariable(D);
1417     if (Info.first ||
1418         (VD && VD->hasLocalStorage() &&
1419          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
1420         (VD && DSAStack->isForceVarCapturing()))
1421       return VD ? VD : Info.second;
1422     auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1423     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1424       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1425     DVarPrivate = DSAStack->hasDSA(
1426         D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1427         DSAStack->isClauseParsingMode());
1428     if (DVarPrivate.CKind != OMPC_unknown)
1429       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1430   }
1431   return nullptr;
1432 }
1433 
1434 void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1435                                         unsigned Level) const {
1436   SmallVector<OpenMPDirectiveKind, 4> Regions;
1437   getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1438   FunctionScopesIndex -= Regions.size();
1439 }
1440 
1441 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
1442   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1443   return DSAStack->hasExplicitDSA(
1444              D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; },
1445              Level) ||
1446          (DSAStack->isClauseParsingMode() &&
1447           DSAStack->getClauseParsingMode() == OMPC_private) ||
1448          // Consider taskgroup reduction descriptor variable a private to avoid
1449          // possible capture in the region.
1450          (DSAStack->hasExplicitDirective(
1451               [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1452               Level) &&
1453           DSAStack->isTaskgroupReductionRef(D, Level));
1454 }
1455 
1456 void Sema::setOpenMPCaptureKind(FieldDecl *FD, ValueDecl *D, unsigned Level) {
1457   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1458   D = getCanonicalDecl(D);
1459   OpenMPClauseKind OMPC = OMPC_unknown;
1460   for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1461     const unsigned NewLevel = I - 1;
1462     if (DSAStack->hasExplicitDSA(D,
1463                                  [&OMPC](const OpenMPClauseKind K) {
1464                                    if (isOpenMPPrivate(K)) {
1465                                      OMPC = K;
1466                                      return true;
1467                                    }
1468                                    return false;
1469                                  },
1470                                  NewLevel))
1471       break;
1472     if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1473             D, NewLevel,
1474             [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1475                OpenMPClauseKind) { return true; })) {
1476       OMPC = OMPC_map;
1477       break;
1478     }
1479     if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1480                                        NewLevel)) {
1481       OMPC = OMPC_map;
1482       if (D->getType()->isScalarType() &&
1483           DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1484               DefaultMapAttributes::DMA_tofrom_scalar)
1485         OMPC = OMPC_firstprivate;
1486       break;
1487     }
1488   }
1489   if (OMPC != OMPC_unknown)
1490     FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1491 }
1492 
1493 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
1494   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1495   // Return true if the current level is no longer enclosed in a target region.
1496 
1497   auto *VD = dyn_cast<VarDecl>(D);
1498   return VD && !VD->hasLocalStorage() &&
1499          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1500                                         Level);
1501 }
1502 
1503 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1504 
1505 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1506                                const DeclarationNameInfo &DirName,
1507                                Scope *CurScope, SourceLocation Loc) {
1508   DSAStack->push(DKind, DirName, CurScope, Loc);
1509   PushExpressionEvaluationContext(
1510       ExpressionEvaluationContext::PotentiallyEvaluated);
1511 }
1512 
1513 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1514   DSAStack->setClauseParsingMode(K);
1515 }
1516 
1517 void Sema::EndOpenMPClause() {
1518   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1519 }
1520 
1521 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1522   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1523   //  A variable of class type (or array thereof) that appears in a lastprivate
1524   //  clause requires an accessible, unambiguous default constructor for the
1525   //  class type, unless the list item is also specified in a firstprivate
1526   //  clause.
1527   if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1528     for (auto *C : D->clauses()) {
1529       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1530         SmallVector<Expr *, 8> PrivateCopies;
1531         for (auto *DE : Clause->varlists()) {
1532           if (DE->isValueDependent() || DE->isTypeDependent()) {
1533             PrivateCopies.push_back(nullptr);
1534             continue;
1535           }
1536           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1537           VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1538           QualType Type = VD->getType().getNonReferenceType();
1539           auto DVar = DSAStack->getTopDSA(VD, false);
1540           if (DVar.CKind == OMPC_lastprivate) {
1541             // Generate helper private variable and initialize it with the
1542             // default value. The address of the original variable is replaced
1543             // by the address of the new private variable in CodeGen. This new
1544             // variable is not added to IdResolver, so the code in the OpenMP
1545             // region uses original variable for proper diagnostics.
1546             auto *VDPrivate = buildVarDecl(
1547                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1548                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
1549             ActOnUninitializedDecl(VDPrivate);
1550             if (VDPrivate->isInvalidDecl())
1551               continue;
1552             PrivateCopies.push_back(buildDeclRefExpr(
1553                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1554           } else {
1555             // The variable is also a firstprivate, so initialization sequence
1556             // for private copy is generated already.
1557             PrivateCopies.push_back(nullptr);
1558           }
1559         }
1560         // Set initializers to private copies if no errors were found.
1561         if (PrivateCopies.size() == Clause->varlist_size())
1562           Clause->setPrivateCopies(PrivateCopies);
1563       }
1564     }
1565   }
1566 
1567   DSAStack->pop();
1568   DiscardCleanupsInEvaluationContext();
1569   PopExpressionEvaluationContext();
1570 }
1571 
1572 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1573                                      Expr *NumIterations, Sema &SemaRef,
1574                                      Scope *S, DSAStackTy *Stack);
1575 
1576 namespace {
1577 
1578 class VarDeclFilterCCC : public CorrectionCandidateCallback {
1579 private:
1580   Sema &SemaRef;
1581 
1582 public:
1583   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1584   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1585     NamedDecl *ND = Candidate.getCorrectionDecl();
1586     if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1587       return VD->hasGlobalStorage() &&
1588              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1589                                    SemaRef.getCurScope());
1590     }
1591     return false;
1592   }
1593 };
1594 
1595 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1596 private:
1597   Sema &SemaRef;
1598 
1599 public:
1600   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1601   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1602     NamedDecl *ND = Candidate.getCorrectionDecl();
1603     if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
1604       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1605                                    SemaRef.getCurScope());
1606     }
1607     return false;
1608   }
1609 };
1610 
1611 } // namespace
1612 
1613 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1614                                          CXXScopeSpec &ScopeSpec,
1615                                          const DeclarationNameInfo &Id) {
1616   LookupResult Lookup(*this, Id, LookupOrdinaryName);
1617   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1618 
1619   if (Lookup.isAmbiguous())
1620     return ExprError();
1621 
1622   VarDecl *VD;
1623   if (!Lookup.isSingleResult()) {
1624     if (TypoCorrection Corrected = CorrectTypo(
1625             Id, LookupOrdinaryName, CurScope, nullptr,
1626             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1627       diagnoseTypo(Corrected,
1628                    PDiag(Lookup.empty()
1629                              ? diag::err_undeclared_var_use_suggest
1630                              : diag::err_omp_expected_var_arg_suggest)
1631                        << Id.getName());
1632       VD = Corrected.getCorrectionDeclAs<VarDecl>();
1633     } else {
1634       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1635                                        : diag::err_omp_expected_var_arg)
1636           << Id.getName();
1637       return ExprError();
1638     }
1639   } else {
1640     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1641       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1642       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1643       return ExprError();
1644     }
1645   }
1646   Lookup.suppressDiagnostics();
1647 
1648   // OpenMP [2.9.2, Syntax, C/C++]
1649   //   Variables must be file-scope, namespace-scope, or static block-scope.
1650   if (!VD->hasGlobalStorage()) {
1651     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1652         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1653     bool IsDecl =
1654         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1655     Diag(VD->getLocation(),
1656          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1657         << VD;
1658     return ExprError();
1659   }
1660 
1661   VarDecl *CanonicalVD = VD->getCanonicalDecl();
1662   NamedDecl *ND = CanonicalVD;
1663   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1664   //   A threadprivate directive for file-scope variables must appear outside
1665   //   any definition or declaration.
1666   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1667       !getCurLexicalContext()->isTranslationUnit()) {
1668     Diag(Id.getLoc(), diag::err_omp_var_scope)
1669         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1670     bool IsDecl =
1671         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1672     Diag(VD->getLocation(),
1673          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1674         << VD;
1675     return ExprError();
1676   }
1677   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1678   //   A threadprivate directive for static class member variables must appear
1679   //   in the class definition, in the same scope in which the member
1680   //   variables are declared.
1681   if (CanonicalVD->isStaticDataMember() &&
1682       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1683     Diag(Id.getLoc(), diag::err_omp_var_scope)
1684         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1685     bool IsDecl =
1686         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1687     Diag(VD->getLocation(),
1688          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1689         << VD;
1690     return ExprError();
1691   }
1692   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1693   //   A threadprivate directive for namespace-scope variables must appear
1694   //   outside any definition or declaration other than the namespace
1695   //   definition itself.
1696   if (CanonicalVD->getDeclContext()->isNamespace() &&
1697       (!getCurLexicalContext()->isFileContext() ||
1698        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1699     Diag(Id.getLoc(), diag::err_omp_var_scope)
1700         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1701     bool IsDecl =
1702         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1703     Diag(VD->getLocation(),
1704          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1705         << VD;
1706     return ExprError();
1707   }
1708   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1709   //   A threadprivate directive for static block-scope variables must appear
1710   //   in the scope of the variable and not in a nested scope.
1711   if (CanonicalVD->isStaticLocal() && CurScope &&
1712       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1713     Diag(Id.getLoc(), diag::err_omp_var_scope)
1714         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1715     bool IsDecl =
1716         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1717     Diag(VD->getLocation(),
1718          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1719         << VD;
1720     return ExprError();
1721   }
1722 
1723   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1724   //   A threadprivate directive must lexically precede all references to any
1725   //   of the variables in its list.
1726   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1727     Diag(Id.getLoc(), diag::err_omp_var_used)
1728         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1729     return ExprError();
1730   }
1731 
1732   QualType ExprType = VD->getType().getNonReferenceType();
1733   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1734                              SourceLocation(), VD,
1735                              /*RefersToEnclosingVariableOrCapture=*/false,
1736                              Id.getLoc(), ExprType, VK_LValue);
1737 }
1738 
1739 Sema::DeclGroupPtrTy
1740 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1741                                         ArrayRef<Expr *> VarList) {
1742   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1743     CurContext->addDecl(D);
1744     return DeclGroupPtrTy::make(DeclGroupRef(D));
1745   }
1746   return nullptr;
1747 }
1748 
1749 namespace {
1750 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1751   Sema &SemaRef;
1752 
1753 public:
1754   bool VisitDeclRefExpr(const DeclRefExpr *E) {
1755     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1756       if (VD->hasLocalStorage()) {
1757         SemaRef.Diag(E->getLocStart(),
1758                      diag::err_omp_local_var_in_threadprivate_init)
1759             << E->getSourceRange();
1760         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1761             << VD << VD->getSourceRange();
1762         return true;
1763       }
1764     }
1765     return false;
1766   }
1767   bool VisitStmt(const Stmt *S) {
1768     for (auto Child : S->children()) {
1769       if (Child && Visit(Child))
1770         return true;
1771     }
1772     return false;
1773   }
1774   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1775 };
1776 } // namespace
1777 
1778 OMPThreadPrivateDecl *
1779 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1780   SmallVector<Expr *, 8> Vars;
1781   for (auto &RefExpr : VarList) {
1782     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1783     VarDecl *VD = cast<VarDecl>(DE->getDecl());
1784     SourceLocation ILoc = DE->getExprLoc();
1785 
1786     // Mark variable as used.
1787     VD->setReferenced();
1788     VD->markUsed(Context);
1789 
1790     QualType QType = VD->getType();
1791     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1792       // It will be analyzed later.
1793       Vars.push_back(DE);
1794       continue;
1795     }
1796 
1797     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1798     //   A threadprivate variable must not have an incomplete type.
1799     if (RequireCompleteType(ILoc, VD->getType(),
1800                             diag::err_omp_threadprivate_incomplete_type)) {
1801       continue;
1802     }
1803 
1804     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1805     //   A threadprivate variable must not have a reference type.
1806     if (VD->getType()->isReferenceType()) {
1807       Diag(ILoc, diag::err_omp_ref_type_arg)
1808           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1809       bool IsDecl =
1810           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1811       Diag(VD->getLocation(),
1812            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1813           << VD;
1814       continue;
1815     }
1816 
1817     // Check if this is a TLS variable. If TLS is not being supported, produce
1818     // the corresponding diagnostic.
1819     if ((VD->getTLSKind() != VarDecl::TLS_None &&
1820          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1821            getLangOpts().OpenMPUseTLS &&
1822            getASTContext().getTargetInfo().isTLSSupported())) ||
1823         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1824          !VD->isLocalVarDecl())) {
1825       Diag(ILoc, diag::err_omp_var_thread_local)
1826           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1827       bool IsDecl =
1828           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1829       Diag(VD->getLocation(),
1830            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1831           << VD;
1832       continue;
1833     }
1834 
1835     // Check if initial value of threadprivate variable reference variable with
1836     // local storage (it is not supported by runtime).
1837     if (auto Init = VD->getAnyInitializer()) {
1838       LocalVarRefChecker Checker(*this);
1839       if (Checker.Visit(Init))
1840         continue;
1841     }
1842 
1843     Vars.push_back(RefExpr);
1844     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1845     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1846         Context, SourceRange(Loc, Loc)));
1847     if (auto *ML = Context.getASTMutationListener())
1848       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1849   }
1850   OMPThreadPrivateDecl *D = nullptr;
1851   if (!Vars.empty()) {
1852     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1853                                      Vars);
1854     D->setAccess(AS_public);
1855   }
1856   return D;
1857 }
1858 
1859 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1860                               const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1861                               bool IsLoopIterVar = false) {
1862   if (DVar.RefExpr) {
1863     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1864         << getOpenMPClauseName(DVar.CKind);
1865     return;
1866   }
1867   enum {
1868     PDSA_StaticMemberShared,
1869     PDSA_StaticLocalVarShared,
1870     PDSA_LoopIterVarPrivate,
1871     PDSA_LoopIterVarLinear,
1872     PDSA_LoopIterVarLastprivate,
1873     PDSA_ConstVarShared,
1874     PDSA_GlobalVarShared,
1875     PDSA_TaskVarFirstprivate,
1876     PDSA_LocalVarPrivate,
1877     PDSA_Implicit
1878   } Reason = PDSA_Implicit;
1879   bool ReportHint = false;
1880   auto ReportLoc = D->getLocation();
1881   auto *VD = dyn_cast<VarDecl>(D);
1882   if (IsLoopIterVar) {
1883     if (DVar.CKind == OMPC_private)
1884       Reason = PDSA_LoopIterVarPrivate;
1885     else if (DVar.CKind == OMPC_lastprivate)
1886       Reason = PDSA_LoopIterVarLastprivate;
1887     else
1888       Reason = PDSA_LoopIterVarLinear;
1889   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1890              DVar.CKind == OMPC_firstprivate) {
1891     Reason = PDSA_TaskVarFirstprivate;
1892     ReportLoc = DVar.ImplicitDSALoc;
1893   } else if (VD && VD->isStaticLocal())
1894     Reason = PDSA_StaticLocalVarShared;
1895   else if (VD && VD->isStaticDataMember())
1896     Reason = PDSA_StaticMemberShared;
1897   else if (VD && VD->isFileVarDecl())
1898     Reason = PDSA_GlobalVarShared;
1899   else if (D->getType().isConstant(SemaRef.getASTContext()))
1900     Reason = PDSA_ConstVarShared;
1901   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1902     ReportHint = true;
1903     Reason = PDSA_LocalVarPrivate;
1904   }
1905   if (Reason != PDSA_Implicit) {
1906     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1907         << Reason << ReportHint
1908         << getOpenMPDirectiveName(Stack->getCurrentDirective());
1909   } else if (DVar.ImplicitDSALoc.isValid()) {
1910     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1911         << getOpenMPClauseName(DVar.CKind);
1912   }
1913 }
1914 
1915 namespace {
1916 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1917   DSAStackTy *Stack;
1918   Sema &SemaRef;
1919   bool ErrorFound;
1920   CapturedStmt *CS;
1921   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1922   llvm::SmallVector<Expr *, 8> ImplicitMap;
1923   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1924   llvm::DenseSet<ValueDecl *> ImplicitDeclarations;
1925 
1926 public:
1927   void VisitDeclRefExpr(DeclRefExpr *E) {
1928     if (E->isTypeDependent() || E->isValueDependent() ||
1929         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1930       return;
1931     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1932       VD = VD->getCanonicalDecl();
1933       // Skip internally declared variables.
1934       if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
1935         return;
1936 
1937       auto DVar = Stack->getTopDSA(VD, false);
1938       // Check if the variable has explicit DSA set and stop analysis if it so.
1939       if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
1940         return;
1941 
1942       // Skip internally declared static variables.
1943       llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
1944           isDeclareTargetDeclaration(VD);
1945       if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
1946           (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
1947         return;
1948 
1949       auto ELoc = E->getExprLoc();
1950       auto DKind = Stack->getCurrentDirective();
1951       // The default(none) clause requires that each variable that is referenced
1952       // in the construct, and does not have a predetermined data-sharing
1953       // attribute, must have its data-sharing attribute explicitly determined
1954       // by being listed in a data-sharing attribute clause.
1955       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1956           isParallelOrTaskRegion(DKind) &&
1957           VarsWithInheritedDSA.count(VD) == 0) {
1958         VarsWithInheritedDSA[VD] = E;
1959         return;
1960       }
1961 
1962       if (isOpenMPTargetExecutionDirective(DKind) &&
1963           !Stack->isLoopControlVariable(VD).first) {
1964         if (!Stack->checkMappableExprComponentListsForDecl(
1965                 VD, /*CurrentRegionOnly=*/true,
1966                 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
1967                        StackComponents,
1968                    OpenMPClauseKind) {
1969                   // Variable is used if it has been marked as an array, array
1970                   // section or the variable iself.
1971                   return StackComponents.size() == 1 ||
1972                          std::all_of(
1973                              std::next(StackComponents.rbegin()),
1974                              StackComponents.rend(),
1975                              [](const OMPClauseMappableExprCommon::
1976                                     MappableComponent &MC) {
1977                                return MC.getAssociatedDeclaration() ==
1978                                           nullptr &&
1979                                       (isa<OMPArraySectionExpr>(
1980                                            MC.getAssociatedExpression()) ||
1981                                        isa<ArraySubscriptExpr>(
1982                                            MC.getAssociatedExpression()));
1983                              });
1984                 })) {
1985           bool IsFirstprivate = false;
1986           // By default lambdas are captured as firstprivates.
1987           if (const auto *RD =
1988                   VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
1989             IsFirstprivate = RD->isLambda();
1990           IsFirstprivate =
1991               IsFirstprivate ||
1992               (VD->getType().getNonReferenceType()->isScalarType() &&
1993                Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
1994           if (IsFirstprivate)
1995             ImplicitFirstprivate.emplace_back(E);
1996           else
1997             ImplicitMap.emplace_back(E);
1998           return;
1999         }
2000       }
2001 
2002       // OpenMP [2.9.3.6, Restrictions, p.2]
2003       //  A list item that appears in a reduction clause of the innermost
2004       //  enclosing worksharing or parallel construct may not be accessed in an
2005       //  explicit task.
2006       DVar = Stack->hasInnermostDSA(
2007           VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
2008           [](OpenMPDirectiveKind K) -> bool {
2009             return isOpenMPParallelDirective(K) ||
2010                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2011           },
2012           /*FromParent=*/true);
2013       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2014         ErrorFound = true;
2015         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2016         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
2017         return;
2018       }
2019 
2020       // Define implicit data-sharing attributes for task.
2021       DVar = Stack->getImplicitDSA(VD, false);
2022       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2023           !Stack->isLoopControlVariable(VD).first)
2024         ImplicitFirstprivate.push_back(E);
2025     }
2026   }
2027   void VisitMemberExpr(MemberExpr *E) {
2028     if (E->isTypeDependent() || E->isValueDependent() ||
2029         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2030       return;
2031     auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
2032     OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
2033     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
2034       if (!FD)
2035         return;
2036       auto DVar = Stack->getTopDSA(FD, false);
2037       // Check if the variable has explicit DSA set and stop analysis if it
2038       // so.
2039       if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2040         return;
2041 
2042       if (isOpenMPTargetExecutionDirective(DKind) &&
2043           !Stack->isLoopControlVariable(FD).first &&
2044           !Stack->checkMappableExprComponentListsForDecl(
2045               FD, /*CurrentRegionOnly=*/true,
2046               [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2047                      StackComponents,
2048                  OpenMPClauseKind) {
2049                 return isa<CXXThisExpr>(
2050                     cast<MemberExpr>(
2051                         StackComponents.back().getAssociatedExpression())
2052                         ->getBase()
2053                         ->IgnoreParens());
2054               })) {
2055         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2056         //  A bit-field cannot appear in a map clause.
2057         //
2058         if (FD->isBitField())
2059           return;
2060         ImplicitMap.emplace_back(E);
2061         return;
2062       }
2063 
2064       auto ELoc = E->getExprLoc();
2065       // OpenMP [2.9.3.6, Restrictions, p.2]
2066       //  A list item that appears in a reduction clause of the innermost
2067       //  enclosing worksharing or parallel construct may not be accessed in
2068       //  an  explicit task.
2069       DVar = Stack->hasInnermostDSA(
2070           FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
2071           [](OpenMPDirectiveKind K) -> bool {
2072             return isOpenMPParallelDirective(K) ||
2073                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2074           },
2075           /*FromParent=*/true);
2076       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2077         ErrorFound = true;
2078         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
2079         ReportOriginalDSA(SemaRef, Stack, FD, DVar);
2080         return;
2081       }
2082 
2083       // Define implicit data-sharing attributes for task.
2084       DVar = Stack->getImplicitDSA(FD, false);
2085       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2086           !Stack->isLoopControlVariable(FD).first)
2087         ImplicitFirstprivate.push_back(E);
2088       return;
2089     }
2090     if (isOpenMPTargetExecutionDirective(DKind)) {
2091       OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
2092       if (!CheckMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
2093                                         /*NoDiagnose=*/true))
2094         return;
2095       auto *VD = cast<ValueDecl>(
2096           CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2097       if (!Stack->checkMappableExprComponentListsForDecl(
2098               VD, /*CurrentRegionOnly=*/true,
2099               [&CurComponents](
2100                   OMPClauseMappableExprCommon::MappableExprComponentListRef
2101                       StackComponents,
2102                   OpenMPClauseKind) {
2103                 auto CCI = CurComponents.rbegin();
2104                 auto CCE = CurComponents.rend();
2105                 for (const auto &SC : llvm::reverse(StackComponents)) {
2106                   // Do both expressions have the same kind?
2107                   if (CCI->getAssociatedExpression()->getStmtClass() !=
2108                       SC.getAssociatedExpression()->getStmtClass())
2109                     if (!(isa<OMPArraySectionExpr>(
2110                               SC.getAssociatedExpression()) &&
2111                           isa<ArraySubscriptExpr>(
2112                               CCI->getAssociatedExpression())))
2113                       return false;
2114 
2115                   Decl *CCD = CCI->getAssociatedDeclaration();
2116                   Decl *SCD = SC.getAssociatedDeclaration();
2117                   CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2118                   SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2119                   if (SCD != CCD)
2120                     return false;
2121                   std::advance(CCI, 1);
2122                   if (CCI == CCE)
2123                     break;
2124                 }
2125                 return true;
2126               })) {
2127         Visit(E->getBase());
2128       }
2129     } else
2130       Visit(E->getBase());
2131   }
2132   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
2133     for (auto *C : S->clauses()) {
2134       // Skip analysis of arguments of implicitly defined firstprivate clause
2135       // for task|target directives.
2136       // Skip analysis of arguments of implicitly defined map clause for target
2137       // directives.
2138       if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2139                  C->isImplicit())) {
2140         for (auto *CC : C->children()) {
2141           if (CC)
2142             Visit(CC);
2143         }
2144       }
2145     }
2146   }
2147   void VisitStmt(Stmt *S) {
2148     for (auto *C : S->children()) {
2149       if (C && !isa<OMPExecutableDirective>(C))
2150         Visit(C);
2151     }
2152   }
2153 
2154   bool isErrorFound() { return ErrorFound; }
2155   ArrayRef<Expr *> getImplicitFirstprivate() const {
2156     return ImplicitFirstprivate;
2157   }
2158   ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
2159   llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
2160     return VarsWithInheritedDSA;
2161   }
2162 
2163   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2164       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
2165 };
2166 } // namespace
2167 
2168 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
2169   switch (DKind) {
2170   case OMPD_parallel:
2171   case OMPD_parallel_for:
2172   case OMPD_parallel_for_simd:
2173   case OMPD_parallel_sections:
2174   case OMPD_teams:
2175   case OMPD_teams_distribute:
2176   case OMPD_teams_distribute_simd: {
2177     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2178     QualType KmpInt32PtrTy =
2179         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2180     Sema::CapturedParamNameType Params[] = {
2181         std::make_pair(".global_tid.", KmpInt32PtrTy),
2182         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2183         std::make_pair(StringRef(), QualType()) // __context with shared vars
2184     };
2185     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2186                              Params);
2187     break;
2188   }
2189   case OMPD_target_teams:
2190   case OMPD_target_parallel:
2191   case OMPD_target_parallel_for:
2192   case OMPD_target_parallel_for_simd:
2193   case OMPD_target_teams_distribute:
2194   case OMPD_target_teams_distribute_simd: {
2195     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2196     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2197     QualType KmpInt32PtrTy =
2198         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2199     QualType Args[] = {VoidPtrTy};
2200     FunctionProtoType::ExtProtoInfo EPI;
2201     EPI.Variadic = true;
2202     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2203     Sema::CapturedParamNameType Params[] = {
2204         std::make_pair(".global_tid.", KmpInt32Ty),
2205         std::make_pair(".part_id.", KmpInt32PtrTy),
2206         std::make_pair(".privates.", VoidPtrTy),
2207         std::make_pair(
2208             ".copy_fn.",
2209             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2210         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2211         std::make_pair(StringRef(), QualType()) // __context with shared vars
2212     };
2213     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2214                              Params);
2215     // Mark this captured region as inlined, because we don't use outlined
2216     // function directly.
2217     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2218         AlwaysInlineAttr::CreateImplicit(
2219             Context, AlwaysInlineAttr::Keyword_forceinline));
2220     Sema::CapturedParamNameType ParamsTarget[] = {
2221         std::make_pair(StringRef(), QualType()) // __context with shared vars
2222     };
2223     // Start a captured region for 'target' with no implicit parameters.
2224     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2225                              ParamsTarget);
2226     Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
2227         std::make_pair(".global_tid.", KmpInt32PtrTy),
2228         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2229         std::make_pair(StringRef(), QualType()) // __context with shared vars
2230     };
2231     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2232     // the same implicit parameters.
2233     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2234                              ParamsTeamsOrParallel);
2235     break;
2236   }
2237   case OMPD_target:
2238   case OMPD_target_simd: {
2239     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2240     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2241     QualType KmpInt32PtrTy =
2242         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2243     QualType Args[] = {VoidPtrTy};
2244     FunctionProtoType::ExtProtoInfo EPI;
2245     EPI.Variadic = true;
2246     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2247     Sema::CapturedParamNameType Params[] = {
2248         std::make_pair(".global_tid.", KmpInt32Ty),
2249         std::make_pair(".part_id.", KmpInt32PtrTy),
2250         std::make_pair(".privates.", VoidPtrTy),
2251         std::make_pair(
2252             ".copy_fn.",
2253             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2254         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2255         std::make_pair(StringRef(), QualType()) // __context with shared vars
2256     };
2257     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2258                              Params);
2259     // Mark this captured region as inlined, because we don't use outlined
2260     // function directly.
2261     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2262         AlwaysInlineAttr::CreateImplicit(
2263             Context, AlwaysInlineAttr::Keyword_forceinline));
2264     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2265                              std::make_pair(StringRef(), QualType()));
2266     break;
2267   }
2268   case OMPD_simd:
2269   case OMPD_for:
2270   case OMPD_for_simd:
2271   case OMPD_sections:
2272   case OMPD_section:
2273   case OMPD_single:
2274   case OMPD_master:
2275   case OMPD_critical:
2276   case OMPD_taskgroup:
2277   case OMPD_distribute:
2278   case OMPD_distribute_simd:
2279   case OMPD_ordered:
2280   case OMPD_atomic:
2281   case OMPD_target_data: {
2282     Sema::CapturedParamNameType Params[] = {
2283         std::make_pair(StringRef(), QualType()) // __context with shared vars
2284     };
2285     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2286                              Params);
2287     break;
2288   }
2289   case OMPD_task: {
2290     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2291     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2292     QualType KmpInt32PtrTy =
2293         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2294     QualType Args[] = {VoidPtrTy};
2295     FunctionProtoType::ExtProtoInfo EPI;
2296     EPI.Variadic = true;
2297     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2298     Sema::CapturedParamNameType Params[] = {
2299         std::make_pair(".global_tid.", KmpInt32Ty),
2300         std::make_pair(".part_id.", KmpInt32PtrTy),
2301         std::make_pair(".privates.", VoidPtrTy),
2302         std::make_pair(
2303             ".copy_fn.",
2304             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2305         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2306         std::make_pair(StringRef(), QualType()) // __context with shared vars
2307     };
2308     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2309                              Params);
2310     // Mark this captured region as inlined, because we don't use outlined
2311     // function directly.
2312     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2313         AlwaysInlineAttr::CreateImplicit(
2314             Context, AlwaysInlineAttr::Keyword_forceinline));
2315     break;
2316   }
2317   case OMPD_taskloop:
2318   case OMPD_taskloop_simd: {
2319     QualType KmpInt32Ty =
2320         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2321             .withConst();
2322     QualType KmpUInt64Ty =
2323         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2324             .withConst();
2325     QualType KmpInt64Ty =
2326         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2327             .withConst();
2328     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2329     QualType KmpInt32PtrTy =
2330         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2331     QualType Args[] = {VoidPtrTy};
2332     FunctionProtoType::ExtProtoInfo EPI;
2333     EPI.Variadic = true;
2334     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2335     Sema::CapturedParamNameType Params[] = {
2336         std::make_pair(".global_tid.", KmpInt32Ty),
2337         std::make_pair(".part_id.", KmpInt32PtrTy),
2338         std::make_pair(".privates.", VoidPtrTy),
2339         std::make_pair(
2340             ".copy_fn.",
2341             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2342         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2343         std::make_pair(".lb.", KmpUInt64Ty),
2344         std::make_pair(".ub.", KmpUInt64Ty),
2345         std::make_pair(".st.", KmpInt64Ty),
2346         std::make_pair(".liter.", KmpInt32Ty),
2347         std::make_pair(".reductions.", VoidPtrTy),
2348         std::make_pair(StringRef(), QualType()) // __context with shared vars
2349     };
2350     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2351                              Params);
2352     // Mark this captured region as inlined, because we don't use outlined
2353     // function directly.
2354     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2355         AlwaysInlineAttr::CreateImplicit(
2356             Context, AlwaysInlineAttr::Keyword_forceinline));
2357     break;
2358   }
2359   case OMPD_distribute_parallel_for_simd:
2360   case OMPD_distribute_parallel_for: {
2361     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2362     QualType KmpInt32PtrTy =
2363         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2364     Sema::CapturedParamNameType Params[] = {
2365         std::make_pair(".global_tid.", KmpInt32PtrTy),
2366         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2367         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2368         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
2369         std::make_pair(StringRef(), QualType()) // __context with shared vars
2370     };
2371     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2372                              Params);
2373     break;
2374   }
2375   case OMPD_target_teams_distribute_parallel_for:
2376   case OMPD_target_teams_distribute_parallel_for_simd: {
2377     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2378     QualType KmpInt32PtrTy =
2379         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2380     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2381 
2382     QualType Args[] = {VoidPtrTy};
2383     FunctionProtoType::ExtProtoInfo EPI;
2384     EPI.Variadic = true;
2385     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2386     Sema::CapturedParamNameType Params[] = {
2387         std::make_pair(".global_tid.", KmpInt32Ty),
2388         std::make_pair(".part_id.", KmpInt32PtrTy),
2389         std::make_pair(".privates.", VoidPtrTy),
2390         std::make_pair(
2391             ".copy_fn.",
2392             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2393         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2394         std::make_pair(StringRef(), QualType()) // __context with shared vars
2395     };
2396     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2397                              Params);
2398     // Mark this captured region as inlined, because we don't use outlined
2399     // function directly.
2400     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2401         AlwaysInlineAttr::CreateImplicit(
2402             Context, AlwaysInlineAttr::Keyword_forceinline));
2403     Sema::CapturedParamNameType ParamsTarget[] = {
2404         std::make_pair(StringRef(), QualType()) // __context with shared vars
2405     };
2406     // Start a captured region for 'target' with no implicit parameters.
2407     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2408                              ParamsTarget);
2409 
2410     Sema::CapturedParamNameType ParamsTeams[] = {
2411         std::make_pair(".global_tid.", KmpInt32PtrTy),
2412         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2413         std::make_pair(StringRef(), QualType()) // __context with shared vars
2414     };
2415     // Start a captured region for 'target' with no implicit parameters.
2416     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2417                              ParamsTeams);
2418 
2419     Sema::CapturedParamNameType ParamsParallel[] = {
2420         std::make_pair(".global_tid.", KmpInt32PtrTy),
2421         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2422         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2423         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
2424         std::make_pair(StringRef(), QualType()) // __context with shared vars
2425     };
2426     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2427     // the same implicit parameters.
2428     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2429                              ParamsParallel);
2430     break;
2431   }
2432 
2433   case OMPD_teams_distribute_parallel_for:
2434   case OMPD_teams_distribute_parallel_for_simd: {
2435     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2436     QualType KmpInt32PtrTy =
2437         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2438 
2439     Sema::CapturedParamNameType ParamsTeams[] = {
2440         std::make_pair(".global_tid.", KmpInt32PtrTy),
2441         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2442         std::make_pair(StringRef(), QualType()) // __context with shared vars
2443     };
2444     // Start a captured region for 'target' with no implicit parameters.
2445     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2446                              ParamsTeams);
2447 
2448     Sema::CapturedParamNameType ParamsParallel[] = {
2449         std::make_pair(".global_tid.", KmpInt32PtrTy),
2450         std::make_pair(".bound_tid.", KmpInt32PtrTy),
2451         std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2452         std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
2453         std::make_pair(StringRef(), QualType()) // __context with shared vars
2454     };
2455     // Start a captured region for 'teams' or 'parallel'.  Both regions have
2456     // the same implicit parameters.
2457     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2458                              ParamsParallel);
2459     break;
2460   }
2461   case OMPD_target_update:
2462   case OMPD_target_enter_data:
2463   case OMPD_target_exit_data: {
2464     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2465     QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2466     QualType KmpInt32PtrTy =
2467         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2468     QualType Args[] = {VoidPtrTy};
2469     FunctionProtoType::ExtProtoInfo EPI;
2470     EPI.Variadic = true;
2471     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2472     Sema::CapturedParamNameType Params[] = {
2473         std::make_pair(".global_tid.", KmpInt32Ty),
2474         std::make_pair(".part_id.", KmpInt32PtrTy),
2475         std::make_pair(".privates.", VoidPtrTy),
2476         std::make_pair(
2477             ".copy_fn.",
2478             Context.getPointerType(CopyFnType).withConst().withRestrict()),
2479         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2480         std::make_pair(StringRef(), QualType()) // __context with shared vars
2481     };
2482     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2483                              Params);
2484     // Mark this captured region as inlined, because we don't use outlined
2485     // function directly.
2486     getCurCapturedRegion()->TheCapturedDecl->addAttr(
2487         AlwaysInlineAttr::CreateImplicit(
2488             Context, AlwaysInlineAttr::Keyword_forceinline));
2489     break;
2490   }
2491   case OMPD_threadprivate:
2492   case OMPD_taskyield:
2493   case OMPD_barrier:
2494   case OMPD_taskwait:
2495   case OMPD_cancellation_point:
2496   case OMPD_cancel:
2497   case OMPD_flush:
2498   case OMPD_declare_reduction:
2499   case OMPD_declare_simd:
2500   case OMPD_declare_target:
2501   case OMPD_end_declare_target:
2502     llvm_unreachable("OpenMP Directive is not allowed");
2503   case OMPD_unknown:
2504     llvm_unreachable("Unknown OpenMP directive");
2505   }
2506 }
2507 
2508 int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2509   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2510   getOpenMPCaptureRegions(CaptureRegions, DKind);
2511   return CaptureRegions.size();
2512 }
2513 
2514 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
2515                                              Expr *CaptureExpr, bool WithInit,
2516                                              bool AsExpression) {
2517   assert(CaptureExpr);
2518   ASTContext &C = S.getASTContext();
2519   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
2520   QualType Ty = Init->getType();
2521   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
2522     if (S.getLangOpts().CPlusPlus) {
2523       Ty = C.getLValueReferenceType(Ty);
2524     } else {
2525       Ty = C.getPointerType(Ty);
2526       ExprResult Res =
2527           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2528       if (!Res.isUsable())
2529         return nullptr;
2530       Init = Res.get();
2531     }
2532     WithInit = true;
2533   }
2534   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
2535                                           CaptureExpr->getLocStart());
2536   if (!WithInit)
2537     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
2538   S.CurContext->addHiddenDecl(CED);
2539   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
2540   return CED;
2541 }
2542 
2543 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2544                                  bool WithInit) {
2545   OMPCapturedExprDecl *CD;
2546   if (auto *VD = S.IsOpenMPCapturedDecl(D)) {
2547     CD = cast<OMPCapturedExprDecl>(VD);
2548   } else {
2549     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2550                           /*AsExpression=*/false);
2551   }
2552   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2553                           CaptureExpr->getExprLoc());
2554 }
2555 
2556 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
2557   CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
2558   if (!Ref) {
2559     OMPCapturedExprDecl *CD = buildCaptureDecl(
2560         S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2561         /*WithInit=*/true, /*AsExpression=*/true);
2562     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2563                            CaptureExpr->getExprLoc());
2564   }
2565   ExprResult Res = Ref;
2566   if (!S.getLangOpts().CPlusPlus &&
2567       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
2568       Ref->getType()->isPointerType()) {
2569     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
2570     if (!Res.isUsable())
2571       return ExprError();
2572   }
2573   return S.DefaultLvalueConversion(Res.get());
2574 }
2575 
2576 namespace {
2577 // OpenMP directives parsed in this section are represented as a
2578 // CapturedStatement with an associated statement.  If a syntax error
2579 // is detected during the parsing of the associated statement, the
2580 // compiler must abort processing and close the CapturedStatement.
2581 //
2582 // Combined directives such as 'target parallel' have more than one
2583 // nested CapturedStatements.  This RAII ensures that we unwind out
2584 // of all the nested CapturedStatements when an error is found.
2585 class CaptureRegionUnwinderRAII {
2586 private:
2587   Sema &S;
2588   bool &ErrorFound;
2589   OpenMPDirectiveKind DKind;
2590 
2591 public:
2592   CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2593                             OpenMPDirectiveKind DKind)
2594       : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2595   ~CaptureRegionUnwinderRAII() {
2596     if (ErrorFound) {
2597       int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2598       while (--ThisCaptureLevel >= 0)
2599         S.ActOnCapturedRegionError();
2600     }
2601   }
2602 };
2603 } // namespace
2604 
2605 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2606                                       ArrayRef<OMPClause *> Clauses) {
2607   bool ErrorFound = false;
2608   CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2609       *this, ErrorFound, DSAStack->getCurrentDirective());
2610   if (!S.isUsable()) {
2611     ErrorFound = true;
2612     return StmtError();
2613   }
2614 
2615   SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2616   getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
2617   OMPOrderedClause *OC = nullptr;
2618   OMPScheduleClause *SC = nullptr;
2619   SmallVector<OMPLinearClause *, 4> LCs;
2620   SmallVector<OMPClauseWithPreInit *, 8> PICs;
2621   // This is required for proper codegen.
2622   for (auto *Clause : Clauses) {
2623     if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2624         Clause->getClauseKind() == OMPC_in_reduction) {
2625       // Capture taskgroup task_reduction descriptors inside the tasking regions
2626       // with the corresponding in_reduction items.
2627       auto *IRC = cast<OMPInReductionClause>(Clause);
2628       for (auto *E : IRC->taskgroup_descriptors())
2629         if (E)
2630           MarkDeclarationsReferencedInExpr(E);
2631     }
2632     if (isOpenMPPrivate(Clause->getClauseKind()) ||
2633         Clause->getClauseKind() == OMPC_copyprivate ||
2634         (getLangOpts().OpenMPUseTLS &&
2635          getASTContext().getTargetInfo().isTLSSupported() &&
2636          Clause->getClauseKind() == OMPC_copyin)) {
2637       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
2638       // Mark all variables in private list clauses as used in inner region.
2639       for (auto *VarRef : Clause->children()) {
2640         if (auto *E = cast_or_null<Expr>(VarRef)) {
2641           MarkDeclarationsReferencedInExpr(E);
2642         }
2643       }
2644       DSAStack->setForceVarCapturing(/*V=*/false);
2645     } else if (CaptureRegions.size() > 1 ||
2646                CaptureRegions.back() != OMPD_unknown) {
2647       if (auto *C = OMPClauseWithPreInit::get(Clause))
2648         PICs.push_back(C);
2649       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
2650         if (auto *E = C->getPostUpdateExpr())
2651           MarkDeclarationsReferencedInExpr(E);
2652       }
2653     }
2654     if (Clause->getClauseKind() == OMPC_schedule)
2655       SC = cast<OMPScheduleClause>(Clause);
2656     else if (Clause->getClauseKind() == OMPC_ordered)
2657       OC = cast<OMPOrderedClause>(Clause);
2658     else if (Clause->getClauseKind() == OMPC_linear)
2659       LCs.push_back(cast<OMPLinearClause>(Clause));
2660   }
2661   // OpenMP, 2.7.1 Loop Construct, Restrictions
2662   // The nonmonotonic modifier cannot be specified if an ordered clause is
2663   // specified.
2664   if (SC &&
2665       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2666        SC->getSecondScheduleModifier() ==
2667            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2668       OC) {
2669     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2670              ? SC->getFirstScheduleModifierLoc()
2671              : SC->getSecondScheduleModifierLoc(),
2672          diag::err_omp_schedule_nonmonotonic_ordered)
2673         << SourceRange(OC->getLocStart(), OC->getLocEnd());
2674     ErrorFound = true;
2675   }
2676   if (!LCs.empty() && OC && OC->getNumForLoops()) {
2677     for (auto *C : LCs) {
2678       Diag(C->getLocStart(), diag::err_omp_linear_ordered)
2679           << SourceRange(OC->getLocStart(), OC->getLocEnd());
2680     }
2681     ErrorFound = true;
2682   }
2683   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2684       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2685       OC->getNumForLoops()) {
2686     Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
2687         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2688     ErrorFound = true;
2689   }
2690   if (ErrorFound) {
2691     return StmtError();
2692   }
2693   StmtResult SR = S;
2694   for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
2695     // Mark all variables in private list clauses as used in inner region.
2696     // Required for proper codegen of combined directives.
2697     // TODO: add processing for other clauses.
2698     if (ThisCaptureRegion != OMPD_unknown) {
2699       for (auto *C : PICs) {
2700         OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2701         // Find the particular capture region for the clause if the
2702         // directive is a combined one with multiple capture regions.
2703         // If the directive is not a combined one, the capture region
2704         // associated with the clause is OMPD_unknown and is generated
2705         // only once.
2706         if (CaptureRegion == ThisCaptureRegion ||
2707             CaptureRegion == OMPD_unknown) {
2708           if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
2709             for (auto *D : DS->decls())
2710               MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2711           }
2712         }
2713       }
2714     }
2715     SR = ActOnCapturedRegionEnd(SR.get());
2716   }
2717   return SR;
2718 }
2719 
2720 static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2721                               OpenMPDirectiveKind CancelRegion,
2722                               SourceLocation StartLoc) {
2723   // CancelRegion is only needed for cancel and cancellation_point.
2724   if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2725     return false;
2726 
2727   if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2728       CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2729     return false;
2730 
2731   SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2732       << getOpenMPDirectiveName(CancelRegion);
2733   return true;
2734 }
2735 
2736 static bool checkNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
2737                                   OpenMPDirectiveKind CurrentRegion,
2738                                   const DeclarationNameInfo &CurrentName,
2739                                   OpenMPDirectiveKind CancelRegion,
2740                                   SourceLocation StartLoc) {
2741   if (Stack->getCurScope()) {
2742     auto ParentRegion = Stack->getParentDirective();
2743     auto OffendingRegion = ParentRegion;
2744     bool NestingProhibited = false;
2745     bool CloseNesting = true;
2746     bool OrphanSeen = false;
2747     enum {
2748       NoRecommend,
2749       ShouldBeInParallelRegion,
2750       ShouldBeInOrderedRegion,
2751       ShouldBeInTargetRegion,
2752       ShouldBeInTeamsRegion
2753     } Recommend = NoRecommend;
2754     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
2755       // OpenMP [2.16, Nesting of Regions]
2756       // OpenMP constructs may not be nested inside a simd region.
2757       // OpenMP [2.8.1,simd Construct, Restrictions]
2758       // An ordered construct with the simd clause is the only OpenMP
2759       // construct that can appear in the simd region.
2760       // Allowing a SIMD construct nested in another SIMD construct is an
2761       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2762       // message.
2763       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2764                                  ? diag::err_omp_prohibited_region_simd
2765                                  : diag::warn_omp_nesting_simd);
2766       return CurrentRegion != OMPD_simd;
2767     }
2768     if (ParentRegion == OMPD_atomic) {
2769       // OpenMP [2.16, Nesting of Regions]
2770       // OpenMP constructs may not be nested inside an atomic region.
2771       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2772       return true;
2773     }
2774     if (CurrentRegion == OMPD_section) {
2775       // OpenMP [2.7.2, sections Construct, Restrictions]
2776       // Orphaned section directives are prohibited. That is, the section
2777       // directives must appear within the sections construct and must not be
2778       // encountered elsewhere in the sections region.
2779       if (ParentRegion != OMPD_sections &&
2780           ParentRegion != OMPD_parallel_sections) {
2781         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2782             << (ParentRegion != OMPD_unknown)
2783             << getOpenMPDirectiveName(ParentRegion);
2784         return true;
2785       }
2786       return false;
2787     }
2788     // Allow some constructs (except teams) to be orphaned (they could be
2789     // used in functions, called from OpenMP regions with the required
2790     // preconditions).
2791     if (ParentRegion == OMPD_unknown &&
2792         !isOpenMPNestingTeamsDirective(CurrentRegion))
2793       return false;
2794     if (CurrentRegion == OMPD_cancellation_point ||
2795         CurrentRegion == OMPD_cancel) {
2796       // OpenMP [2.16, Nesting of Regions]
2797       // A cancellation point construct for which construct-type-clause is
2798       // taskgroup must be nested inside a task construct. A cancellation
2799       // point construct for which construct-type-clause is not taskgroup must
2800       // be closely nested inside an OpenMP construct that matches the type
2801       // specified in construct-type-clause.
2802       // A cancel construct for which construct-type-clause is taskgroup must be
2803       // nested inside a task construct. A cancel construct for which
2804       // construct-type-clause is not taskgroup must be closely nested inside an
2805       // OpenMP construct that matches the type specified in
2806       // construct-type-clause.
2807       NestingProhibited =
2808           !((CancelRegion == OMPD_parallel &&
2809              (ParentRegion == OMPD_parallel ||
2810               ParentRegion == OMPD_target_parallel)) ||
2811             (CancelRegion == OMPD_for &&
2812              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2813               ParentRegion == OMPD_target_parallel_for ||
2814               ParentRegion == OMPD_distribute_parallel_for ||
2815               ParentRegion == OMPD_teams_distribute_parallel_for ||
2816               ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
2817             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2818             (CancelRegion == OMPD_sections &&
2819              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2820               ParentRegion == OMPD_parallel_sections)));
2821     } else if (CurrentRegion == OMPD_master) {
2822       // OpenMP [2.16, Nesting of Regions]
2823       // A master region may not be closely nested inside a worksharing,
2824       // atomic, or explicit task region.
2825       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2826                           isOpenMPTaskingDirective(ParentRegion);
2827     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2828       // OpenMP [2.16, Nesting of Regions]
2829       // A critical region may not be nested (closely or otherwise) inside a
2830       // critical region with the same name. Note that this restriction is not
2831       // sufficient to prevent deadlock.
2832       SourceLocation PreviousCriticalLoc;
2833       bool DeadLock = Stack->hasDirective(
2834           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2835                                               const DeclarationNameInfo &DNI,
2836                                               SourceLocation Loc) -> bool {
2837             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2838               PreviousCriticalLoc = Loc;
2839               return true;
2840             } else
2841               return false;
2842           },
2843           false /* skip top directive */);
2844       if (DeadLock) {
2845         SemaRef.Diag(StartLoc,
2846                      diag::err_omp_prohibited_region_critical_same_name)
2847             << CurrentName.getName();
2848         if (PreviousCriticalLoc.isValid())
2849           SemaRef.Diag(PreviousCriticalLoc,
2850                        diag::note_omp_previous_critical_region);
2851         return true;
2852       }
2853     } else if (CurrentRegion == OMPD_barrier) {
2854       // OpenMP [2.16, Nesting of Regions]
2855       // A barrier region may not be closely nested inside a worksharing,
2856       // explicit task, critical, ordered, atomic, or master region.
2857       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2858                           isOpenMPTaskingDirective(ParentRegion) ||
2859                           ParentRegion == OMPD_master ||
2860                           ParentRegion == OMPD_critical ||
2861                           ParentRegion == OMPD_ordered;
2862     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
2863                !isOpenMPParallelDirective(CurrentRegion) &&
2864                !isOpenMPTeamsDirective(CurrentRegion)) {
2865       // OpenMP [2.16, Nesting of Regions]
2866       // A worksharing region may not be closely nested inside a worksharing,
2867       // explicit task, critical, ordered, atomic, or master region.
2868       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2869                           isOpenMPTaskingDirective(ParentRegion) ||
2870                           ParentRegion == OMPD_master ||
2871                           ParentRegion == OMPD_critical ||
2872                           ParentRegion == OMPD_ordered;
2873       Recommend = ShouldBeInParallelRegion;
2874     } else if (CurrentRegion == OMPD_ordered) {
2875       // OpenMP [2.16, Nesting of Regions]
2876       // An ordered region may not be closely nested inside a critical,
2877       // atomic, or explicit task region.
2878       // An ordered region must be closely nested inside a loop region (or
2879       // parallel loop region) with an ordered clause.
2880       // OpenMP [2.8.1,simd Construct, Restrictions]
2881       // An ordered construct with the simd clause is the only OpenMP construct
2882       // that can appear in the simd region.
2883       NestingProhibited = ParentRegion == OMPD_critical ||
2884                           isOpenMPTaskingDirective(ParentRegion) ||
2885                           !(isOpenMPSimdDirective(ParentRegion) ||
2886                             Stack->isParentOrderedRegion());
2887       Recommend = ShouldBeInOrderedRegion;
2888     } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
2889       // OpenMP [2.16, Nesting of Regions]
2890       // If specified, a teams construct must be contained within a target
2891       // construct.
2892       NestingProhibited = ParentRegion != OMPD_target;
2893       OrphanSeen = ParentRegion == OMPD_unknown;
2894       Recommend = ShouldBeInTargetRegion;
2895     }
2896     if (!NestingProhibited &&
2897         !isOpenMPTargetExecutionDirective(CurrentRegion) &&
2898         !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
2899         (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
2900       // OpenMP [2.16, Nesting of Regions]
2901       // distribute, parallel, parallel sections, parallel workshare, and the
2902       // parallel loop and parallel loop SIMD constructs are the only OpenMP
2903       // constructs that can be closely nested in the teams region.
2904       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2905                           !isOpenMPDistributeDirective(CurrentRegion);
2906       Recommend = ShouldBeInParallelRegion;
2907     }
2908     if (!NestingProhibited &&
2909         isOpenMPNestingDistributeDirective(CurrentRegion)) {
2910       // OpenMP 4.5 [2.17 Nesting of Regions]
2911       // The region associated with the distribute construct must be strictly
2912       // nested inside a teams region
2913       NestingProhibited =
2914           (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
2915       Recommend = ShouldBeInTeamsRegion;
2916     }
2917     if (!NestingProhibited &&
2918         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2919          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2920       // OpenMP 4.5 [2.17 Nesting of Regions]
2921       // If a target, target update, target data, target enter data, or
2922       // target exit data construct is encountered during execution of a
2923       // target region, the behavior is unspecified.
2924       NestingProhibited = Stack->hasDirective(
2925           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
2926                              SourceLocation) -> bool {
2927             if (isOpenMPTargetExecutionDirective(K)) {
2928               OffendingRegion = K;
2929               return true;
2930             } else
2931               return false;
2932           },
2933           false /* don't skip top directive */);
2934       CloseNesting = false;
2935     }
2936     if (NestingProhibited) {
2937       if (OrphanSeen) {
2938         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
2939             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
2940       } else {
2941         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2942             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2943             << Recommend << getOpenMPDirectiveName(CurrentRegion);
2944       }
2945       return true;
2946     }
2947   }
2948   return false;
2949 }
2950 
2951 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2952                            ArrayRef<OMPClause *> Clauses,
2953                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2954   bool ErrorFound = false;
2955   unsigned NamedModifiersNumber = 0;
2956   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2957       OMPD_unknown + 1);
2958   SmallVector<SourceLocation, 4> NameModifierLoc;
2959   for (const auto *C : Clauses) {
2960     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2961       // At most one if clause without a directive-name-modifier can appear on
2962       // the directive.
2963       OpenMPDirectiveKind CurNM = IC->getNameModifier();
2964       if (FoundNameModifiers[CurNM]) {
2965         S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2966             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2967             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2968         ErrorFound = true;
2969       } else if (CurNM != OMPD_unknown) {
2970         NameModifierLoc.push_back(IC->getNameModifierLoc());
2971         ++NamedModifiersNumber;
2972       }
2973       FoundNameModifiers[CurNM] = IC;
2974       if (CurNM == OMPD_unknown)
2975         continue;
2976       // Check if the specified name modifier is allowed for the current
2977       // directive.
2978       // At most one if clause with the particular directive-name-modifier can
2979       // appear on the directive.
2980       bool MatchFound = false;
2981       for (auto NM : AllowedNameModifiers) {
2982         if (CurNM == NM) {
2983           MatchFound = true;
2984           break;
2985         }
2986       }
2987       if (!MatchFound) {
2988         S.Diag(IC->getNameModifierLoc(),
2989                diag::err_omp_wrong_if_directive_name_modifier)
2990             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2991         ErrorFound = true;
2992       }
2993     }
2994   }
2995   // If any if clause on the directive includes a directive-name-modifier then
2996   // all if clauses on the directive must include a directive-name-modifier.
2997   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2998     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2999       S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3000              diag::err_omp_no_more_if_clause);
3001     } else {
3002       std::string Values;
3003       std::string Sep(", ");
3004       unsigned AllowedCnt = 0;
3005       unsigned TotalAllowedNum =
3006           AllowedNameModifiers.size() - NamedModifiersNumber;
3007       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3008            ++Cnt) {
3009         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3010         if (!FoundNameModifiers[NM]) {
3011           Values += "'";
3012           Values += getOpenMPDirectiveName(NM);
3013           Values += "'";
3014           if (AllowedCnt + 2 == TotalAllowedNum)
3015             Values += " or ";
3016           else if (AllowedCnt + 1 != TotalAllowedNum)
3017             Values += Sep;
3018           ++AllowedCnt;
3019         }
3020       }
3021       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3022              diag::err_omp_unnamed_if_clause)
3023           << (TotalAllowedNum > 1) << Values;
3024     }
3025     for (auto Loc : NameModifierLoc) {
3026       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3027     }
3028     ErrorFound = true;
3029   }
3030   return ErrorFound;
3031 }
3032 
3033 StmtResult Sema::ActOnOpenMPExecutableDirective(
3034     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3035     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3036     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3037   StmtResult Res = StmtError();
3038   // First check CancelRegion which is then used in checkNestingOfRegions.
3039   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3040       checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3041                             StartLoc))
3042     return StmtError();
3043 
3044   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3045   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
3046   bool ErrorFound = false;
3047   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3048   if (AStmt && !CurContext->isDependentContext()) {
3049     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3050 
3051     // Check default data sharing attributes for referenced variables.
3052     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3053     int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3054     Stmt *S = AStmt;
3055     while (--ThisCaptureLevel >= 0)
3056       S = cast<CapturedStmt>(S)->getCapturedStmt();
3057     DSAChecker.Visit(S);
3058     if (DSAChecker.isErrorFound())
3059       return StmtError();
3060     // Generate list of implicitly defined firstprivate variables.
3061     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3062 
3063     SmallVector<Expr *, 4> ImplicitFirstprivates(
3064         DSAChecker.getImplicitFirstprivate().begin(),
3065         DSAChecker.getImplicitFirstprivate().end());
3066     SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3067                                         DSAChecker.getImplicitMap().end());
3068     // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
3069     for (auto *C : Clauses) {
3070       if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
3071         for (auto *E : IRC->taskgroup_descriptors())
3072           if (E)
3073             ImplicitFirstprivates.emplace_back(E);
3074       }
3075     }
3076     if (!ImplicitFirstprivates.empty()) {
3077       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3078               ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3079               SourceLocation())) {
3080         ClausesWithImplicit.push_back(Implicit);
3081         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3082                      ImplicitFirstprivates.size();
3083       } else
3084         ErrorFound = true;
3085     }
3086     if (!ImplicitMaps.empty()) {
3087       if (OMPClause *Implicit = ActOnOpenMPMapClause(
3088               OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3089               SourceLocation(), SourceLocation(), ImplicitMaps,
3090               SourceLocation(), SourceLocation(), SourceLocation())) {
3091         ClausesWithImplicit.emplace_back(Implicit);
3092         ErrorFound |=
3093             cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
3094       } else
3095         ErrorFound = true;
3096     }
3097   }
3098 
3099   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3100   switch (Kind) {
3101   case OMPD_parallel:
3102     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3103                                        EndLoc);
3104     AllowedNameModifiers.push_back(OMPD_parallel);
3105     break;
3106   case OMPD_simd:
3107     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3108                                    VarsWithInheritedDSA);
3109     break;
3110   case OMPD_for:
3111     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3112                                   VarsWithInheritedDSA);
3113     break;
3114   case OMPD_for_simd:
3115     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3116                                       EndLoc, VarsWithInheritedDSA);
3117     break;
3118   case OMPD_sections:
3119     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3120                                        EndLoc);
3121     break;
3122   case OMPD_section:
3123     assert(ClausesWithImplicit.empty() &&
3124            "No clauses are allowed for 'omp section' directive");
3125     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3126     break;
3127   case OMPD_single:
3128     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3129                                      EndLoc);
3130     break;
3131   case OMPD_master:
3132     assert(ClausesWithImplicit.empty() &&
3133            "No clauses are allowed for 'omp master' directive");
3134     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3135     break;
3136   case OMPD_critical:
3137     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3138                                        StartLoc, EndLoc);
3139     break;
3140   case OMPD_parallel_for:
3141     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3142                                           EndLoc, VarsWithInheritedDSA);
3143     AllowedNameModifiers.push_back(OMPD_parallel);
3144     break;
3145   case OMPD_parallel_for_simd:
3146     Res = ActOnOpenMPParallelForSimdDirective(
3147         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3148     AllowedNameModifiers.push_back(OMPD_parallel);
3149     break;
3150   case OMPD_parallel_sections:
3151     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3152                                                StartLoc, EndLoc);
3153     AllowedNameModifiers.push_back(OMPD_parallel);
3154     break;
3155   case OMPD_task:
3156     Res =
3157         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3158     AllowedNameModifiers.push_back(OMPD_task);
3159     break;
3160   case OMPD_taskyield:
3161     assert(ClausesWithImplicit.empty() &&
3162            "No clauses are allowed for 'omp taskyield' directive");
3163     assert(AStmt == nullptr &&
3164            "No associated statement allowed for 'omp taskyield' directive");
3165     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3166     break;
3167   case OMPD_barrier:
3168     assert(ClausesWithImplicit.empty() &&
3169            "No clauses are allowed for 'omp barrier' directive");
3170     assert(AStmt == nullptr &&
3171            "No associated statement allowed for 'omp barrier' directive");
3172     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3173     break;
3174   case OMPD_taskwait:
3175     assert(ClausesWithImplicit.empty() &&
3176            "No clauses are allowed for 'omp taskwait' directive");
3177     assert(AStmt == nullptr &&
3178            "No associated statement allowed for 'omp taskwait' directive");
3179     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3180     break;
3181   case OMPD_taskgroup:
3182     Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3183                                         EndLoc);
3184     break;
3185   case OMPD_flush:
3186     assert(AStmt == nullptr &&
3187            "No associated statement allowed for 'omp flush' directive");
3188     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3189     break;
3190   case OMPD_ordered:
3191     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3192                                       EndLoc);
3193     break;
3194   case OMPD_atomic:
3195     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3196                                      EndLoc);
3197     break;
3198   case OMPD_teams:
3199     Res =
3200         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3201     break;
3202   case OMPD_target:
3203     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3204                                      EndLoc);
3205     AllowedNameModifiers.push_back(OMPD_target);
3206     break;
3207   case OMPD_target_parallel:
3208     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3209                                              StartLoc, EndLoc);
3210     AllowedNameModifiers.push_back(OMPD_target);
3211     AllowedNameModifiers.push_back(OMPD_parallel);
3212     break;
3213   case OMPD_target_parallel_for:
3214     Res = ActOnOpenMPTargetParallelForDirective(
3215         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3216     AllowedNameModifiers.push_back(OMPD_target);
3217     AllowedNameModifiers.push_back(OMPD_parallel);
3218     break;
3219   case OMPD_cancellation_point:
3220     assert(ClausesWithImplicit.empty() &&
3221            "No clauses are allowed for 'omp cancellation point' directive");
3222     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3223                                "cancellation point' directive");
3224     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3225     break;
3226   case OMPD_cancel:
3227     assert(AStmt == nullptr &&
3228            "No associated statement allowed for 'omp cancel' directive");
3229     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3230                                      CancelRegion);
3231     AllowedNameModifiers.push_back(OMPD_cancel);
3232     break;
3233   case OMPD_target_data:
3234     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3235                                          EndLoc);
3236     AllowedNameModifiers.push_back(OMPD_target_data);
3237     break;
3238   case OMPD_target_enter_data:
3239     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3240                                               EndLoc, AStmt);
3241     AllowedNameModifiers.push_back(OMPD_target_enter_data);
3242     break;
3243   case OMPD_target_exit_data:
3244     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3245                                              EndLoc, AStmt);
3246     AllowedNameModifiers.push_back(OMPD_target_exit_data);
3247     break;
3248   case OMPD_taskloop:
3249     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3250                                        EndLoc, VarsWithInheritedDSA);
3251     AllowedNameModifiers.push_back(OMPD_taskloop);
3252     break;
3253   case OMPD_taskloop_simd:
3254     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3255                                            EndLoc, VarsWithInheritedDSA);
3256     AllowedNameModifiers.push_back(OMPD_taskloop);
3257     break;
3258   case OMPD_distribute:
3259     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3260                                          EndLoc, VarsWithInheritedDSA);
3261     break;
3262   case OMPD_target_update:
3263     Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3264                                            EndLoc, AStmt);
3265     AllowedNameModifiers.push_back(OMPD_target_update);
3266     break;
3267   case OMPD_distribute_parallel_for:
3268     Res = ActOnOpenMPDistributeParallelForDirective(
3269         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3270     AllowedNameModifiers.push_back(OMPD_parallel);
3271     break;
3272   case OMPD_distribute_parallel_for_simd:
3273     Res = ActOnOpenMPDistributeParallelForSimdDirective(
3274         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3275     AllowedNameModifiers.push_back(OMPD_parallel);
3276     break;
3277   case OMPD_distribute_simd:
3278     Res = ActOnOpenMPDistributeSimdDirective(
3279         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3280     break;
3281   case OMPD_target_parallel_for_simd:
3282     Res = ActOnOpenMPTargetParallelForSimdDirective(
3283         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3284     AllowedNameModifiers.push_back(OMPD_target);
3285     AllowedNameModifiers.push_back(OMPD_parallel);
3286     break;
3287   case OMPD_target_simd:
3288     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3289                                          EndLoc, VarsWithInheritedDSA);
3290     AllowedNameModifiers.push_back(OMPD_target);
3291     break;
3292   case OMPD_teams_distribute:
3293     Res = ActOnOpenMPTeamsDistributeDirective(
3294         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3295     break;
3296   case OMPD_teams_distribute_simd:
3297     Res = ActOnOpenMPTeamsDistributeSimdDirective(
3298         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3299     break;
3300   case OMPD_teams_distribute_parallel_for_simd:
3301     Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3302         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3303     AllowedNameModifiers.push_back(OMPD_parallel);
3304     break;
3305   case OMPD_teams_distribute_parallel_for:
3306     Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3307         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3308     AllowedNameModifiers.push_back(OMPD_parallel);
3309     break;
3310   case OMPD_target_teams:
3311     Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3312                                           EndLoc);
3313     AllowedNameModifiers.push_back(OMPD_target);
3314     break;
3315   case OMPD_target_teams_distribute:
3316     Res = ActOnOpenMPTargetTeamsDistributeDirective(
3317         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3318     AllowedNameModifiers.push_back(OMPD_target);
3319     break;
3320   case OMPD_target_teams_distribute_parallel_for:
3321     Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3322         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3323     AllowedNameModifiers.push_back(OMPD_target);
3324     AllowedNameModifiers.push_back(OMPD_parallel);
3325     break;
3326   case OMPD_target_teams_distribute_parallel_for_simd:
3327     Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3328         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3329     AllowedNameModifiers.push_back(OMPD_target);
3330     AllowedNameModifiers.push_back(OMPD_parallel);
3331     break;
3332   case OMPD_target_teams_distribute_simd:
3333     Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3334         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3335     AllowedNameModifiers.push_back(OMPD_target);
3336     break;
3337   case OMPD_declare_target:
3338   case OMPD_end_declare_target:
3339   case OMPD_threadprivate:
3340   case OMPD_declare_reduction:
3341   case OMPD_declare_simd:
3342     llvm_unreachable("OpenMP Directive is not allowed");
3343   case OMPD_unknown:
3344     llvm_unreachable("Unknown OpenMP directive");
3345   }
3346 
3347   for (auto P : VarsWithInheritedDSA) {
3348     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3349         << P.first << P.second->getSourceRange();
3350   }
3351   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3352 
3353   if (!AllowedNameModifiers.empty())
3354     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3355                  ErrorFound;
3356 
3357   if (ErrorFound)
3358     return StmtError();
3359   return Res;
3360 }
3361 
3362 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3363     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3364     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3365     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3366     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3367   assert(Aligneds.size() == Alignments.size());
3368   assert(Linears.size() == LinModifiers.size());
3369   assert(Linears.size() == Steps.size());
3370   if (!DG || DG.get().isNull())
3371     return DeclGroupPtrTy();
3372 
3373   if (!DG.get().isSingleDecl()) {
3374     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3375     return DG;
3376   }
3377   auto *ADecl = DG.get().getSingleDecl();
3378   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3379     ADecl = FTD->getTemplatedDecl();
3380 
3381   auto *FD = dyn_cast<FunctionDecl>(ADecl);
3382   if (!FD) {
3383     Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3384     return DeclGroupPtrTy();
3385   }
3386 
3387   // OpenMP [2.8.2, declare simd construct, Description]
3388   // The parameter of the simdlen clause must be a constant positive integer
3389   // expression.
3390   ExprResult SL;
3391   if (Simdlen)
3392     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3393   // OpenMP [2.8.2, declare simd construct, Description]
3394   // The special this pointer can be used as if was one of the arguments to the
3395   // function in any of the linear, aligned, or uniform clauses.
3396   // The uniform clause declares one or more arguments to have an invariant
3397   // value for all concurrent invocations of the function in the execution of a
3398   // single SIMD loop.
3399   llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3400   Expr *UniformedLinearThis = nullptr;
3401   for (auto *E : Uniforms) {
3402     E = E->IgnoreParenImpCasts();
3403     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3404       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3405         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3406             FD->getParamDecl(PVD->getFunctionScopeIndex())
3407                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3408           UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
3409           continue;
3410         }
3411     if (isa<CXXThisExpr>(E)) {
3412       UniformedLinearThis = E;
3413       continue;
3414     }
3415     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3416         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3417   }
3418   // OpenMP [2.8.2, declare simd construct, Description]
3419   // The aligned clause declares that the object to which each list item points
3420   // is aligned to the number of bytes expressed in the optional parameter of
3421   // the aligned clause.
3422   // The special this pointer can be used as if was one of the arguments to the
3423   // function in any of the linear, aligned, or uniform clauses.
3424   // The type of list items appearing in the aligned clause must be array,
3425   // pointer, reference to array, or reference to pointer.
3426   llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3427   Expr *AlignedThis = nullptr;
3428   for (auto *E : Aligneds) {
3429     E = E->IgnoreParenImpCasts();
3430     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3431       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3432         auto *CanonPVD = PVD->getCanonicalDecl();
3433         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3434             FD->getParamDecl(PVD->getFunctionScopeIndex())
3435                     ->getCanonicalDecl() == CanonPVD) {
3436           // OpenMP  [2.8.1, simd construct, Restrictions]
3437           // A list-item cannot appear in more than one aligned clause.
3438           if (AlignedArgs.count(CanonPVD) > 0) {
3439             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3440                 << 1 << E->getSourceRange();
3441             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3442                  diag::note_omp_explicit_dsa)
3443                 << getOpenMPClauseName(OMPC_aligned);
3444             continue;
3445           }
3446           AlignedArgs[CanonPVD] = E;
3447           QualType QTy = PVD->getType()
3448                              .getNonReferenceType()
3449                              .getUnqualifiedType()
3450                              .getCanonicalType();
3451           const Type *Ty = QTy.getTypePtrOrNull();
3452           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3453             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3454                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3455             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3456           }
3457           continue;
3458         }
3459       }
3460     if (isa<CXXThisExpr>(E)) {
3461       if (AlignedThis) {
3462         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3463             << 2 << E->getSourceRange();
3464         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3465             << getOpenMPClauseName(OMPC_aligned);
3466       }
3467       AlignedThis = E;
3468       continue;
3469     }
3470     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3471         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3472   }
3473   // The optional parameter of the aligned clause, alignment, must be a constant
3474   // positive integer expression. If no optional parameter is specified,
3475   // implementation-defined default alignments for SIMD instructions on the
3476   // target platforms are assumed.
3477   SmallVector<Expr *, 4> NewAligns;
3478   for (auto *E : Alignments) {
3479     ExprResult Align;
3480     if (E)
3481       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3482     NewAligns.push_back(Align.get());
3483   }
3484   // OpenMP [2.8.2, declare simd construct, Description]
3485   // The linear clause declares one or more list items to be private to a SIMD
3486   // lane and to have a linear relationship with respect to the iteration space
3487   // of a loop.
3488   // The special this pointer can be used as if was one of the arguments to the
3489   // function in any of the linear, aligned, or uniform clauses.
3490   // When a linear-step expression is specified in a linear clause it must be
3491   // either a constant integer expression or an integer-typed parameter that is
3492   // specified in a uniform clause on the directive.
3493   llvm::DenseMap<Decl *, Expr *> LinearArgs;
3494   const bool IsUniformedThis = UniformedLinearThis != nullptr;
3495   auto MI = LinModifiers.begin();
3496   for (auto *E : Linears) {
3497     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3498     ++MI;
3499     E = E->IgnoreParenImpCasts();
3500     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3501       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3502         auto *CanonPVD = PVD->getCanonicalDecl();
3503         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3504             FD->getParamDecl(PVD->getFunctionScopeIndex())
3505                     ->getCanonicalDecl() == CanonPVD) {
3506           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
3507           // A list-item cannot appear in more than one linear clause.
3508           if (LinearArgs.count(CanonPVD) > 0) {
3509             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3510                 << getOpenMPClauseName(OMPC_linear)
3511                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3512             Diag(LinearArgs[CanonPVD]->getExprLoc(),
3513                  diag::note_omp_explicit_dsa)
3514                 << getOpenMPClauseName(OMPC_linear);
3515             continue;
3516           }
3517           // Each argument can appear in at most one uniform or linear clause.
3518           if (UniformedArgs.count(CanonPVD) > 0) {
3519             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3520                 << getOpenMPClauseName(OMPC_linear)
3521                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3522             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3523                  diag::note_omp_explicit_dsa)
3524                 << getOpenMPClauseName(OMPC_uniform);
3525             continue;
3526           }
3527           LinearArgs[CanonPVD] = E;
3528           if (E->isValueDependent() || E->isTypeDependent() ||
3529               E->isInstantiationDependent() ||
3530               E->containsUnexpandedParameterPack())
3531             continue;
3532           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3533                                       PVD->getOriginalType());
3534           continue;
3535         }
3536       }
3537     if (isa<CXXThisExpr>(E)) {
3538       if (UniformedLinearThis) {
3539         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3540             << getOpenMPClauseName(OMPC_linear)
3541             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3542             << E->getSourceRange();
3543         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3544             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3545                                                    : OMPC_linear);
3546         continue;
3547       }
3548       UniformedLinearThis = E;
3549       if (E->isValueDependent() || E->isTypeDependent() ||
3550           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3551         continue;
3552       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3553                                   E->getType());
3554       continue;
3555     }
3556     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3557         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3558   }
3559   Expr *Step = nullptr;
3560   Expr *NewStep = nullptr;
3561   SmallVector<Expr *, 4> NewSteps;
3562   for (auto *E : Steps) {
3563     // Skip the same step expression, it was checked already.
3564     if (Step == E || !E) {
3565       NewSteps.push_back(E ? NewStep : nullptr);
3566       continue;
3567     }
3568     Step = E;
3569     if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3570       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3571         auto *CanonPVD = PVD->getCanonicalDecl();
3572         if (UniformedArgs.count(CanonPVD) == 0) {
3573           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3574               << Step->getSourceRange();
3575         } else if (E->isValueDependent() || E->isTypeDependent() ||
3576                    E->isInstantiationDependent() ||
3577                    E->containsUnexpandedParameterPack() ||
3578                    CanonPVD->getType()->hasIntegerRepresentation())
3579           NewSteps.push_back(Step);
3580         else {
3581           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3582               << Step->getSourceRange();
3583         }
3584         continue;
3585       }
3586     NewStep = Step;
3587     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3588         !Step->isInstantiationDependent() &&
3589         !Step->containsUnexpandedParameterPack()) {
3590       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3591                     .get();
3592       if (NewStep)
3593         NewStep = VerifyIntegerConstantExpression(NewStep).get();
3594     }
3595     NewSteps.push_back(NewStep);
3596   }
3597   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3598       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
3599       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3600       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3601       const_cast<Expr **>(Linears.data()), Linears.size(),
3602       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3603       NewSteps.data(), NewSteps.size(), SR);
3604   ADecl->addAttr(NewAttr);
3605   return ConvertDeclToDeclGroup(ADecl);
3606 }
3607 
3608 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3609                                               Stmt *AStmt,
3610                                               SourceLocation StartLoc,
3611                                               SourceLocation EndLoc) {
3612   if (!AStmt)
3613     return StmtError();
3614 
3615   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3616   // 1.2.2 OpenMP Language Terminology
3617   // Structured block - An executable statement with a single entry at the
3618   // top and a single exit at the bottom.
3619   // The point of exit cannot be a branch out of the structured block.
3620   // longjmp() and throw() must not violate the entry/exit criteria.
3621   CS->getCapturedDecl()->setNothrow();
3622 
3623   setFunctionHasBranchProtectedScope();
3624 
3625   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3626                                       DSAStack->isCancelRegion());
3627 }
3628 
3629 namespace {
3630 /// \brief Helper class for checking canonical form of the OpenMP loops and
3631 /// extracting iteration space of each loop in the loop nest, that will be used
3632 /// for IR generation.
3633 class OpenMPIterationSpaceChecker {
3634   /// \brief Reference to Sema.
3635   Sema &SemaRef;
3636   /// \brief A location for diagnostics (when there is no some better location).
3637   SourceLocation DefaultLoc;
3638   /// \brief A location for diagnostics (when increment is not compatible).
3639   SourceLocation ConditionLoc;
3640   /// \brief A source location for referring to loop init later.
3641   SourceRange InitSrcRange;
3642   /// \brief A source location for referring to condition later.
3643   SourceRange ConditionSrcRange;
3644   /// \brief A source location for referring to increment later.
3645   SourceRange IncrementSrcRange;
3646   /// \brief Loop variable.
3647   ValueDecl *LCDecl = nullptr;
3648   /// \brief Reference to loop variable.
3649   Expr *LCRef = nullptr;
3650   /// \brief Lower bound (initializer for the var).
3651   Expr *LB = nullptr;
3652   /// \brief Upper bound.
3653   Expr *UB = nullptr;
3654   /// \brief Loop step (increment).
3655   Expr *Step = nullptr;
3656   /// \brief This flag is true when condition is one of:
3657   ///   Var <  UB
3658   ///   Var <= UB
3659   ///   UB  >  Var
3660   ///   UB  >= Var
3661   bool TestIsLessOp = false;
3662   /// \brief This flag is true when condition is strict ( < or > ).
3663   bool TestIsStrictOp = false;
3664   /// \brief This flag is true when step is subtracted on each iteration.
3665   bool SubtractStep = false;
3666 
3667 public:
3668   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3669       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
3670   /// \brief Check init-expr for canonical loop form and save loop counter
3671   /// variable - #Var and its initialization value - #LB.
3672   bool CheckInit(Stmt *S, bool EmitDiags = true);
3673   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3674   /// for less/greater and for strict/non-strict comparison.
3675   bool CheckCond(Expr *S);
3676   /// \brief Check incr-expr for canonical loop form and return true if it
3677   /// does not conform, otherwise save loop step (#Step).
3678   bool CheckInc(Expr *S);
3679   /// \brief Return the loop counter variable.
3680   ValueDecl *GetLoopDecl() const { return LCDecl; }
3681   /// \brief Return the reference expression to loop counter variable.
3682   Expr *GetLoopDeclRefExpr() const { return LCRef; }
3683   /// \brief Source range of the loop init.
3684   SourceRange GetInitSrcRange() const { return InitSrcRange; }
3685   /// \brief Source range of the loop condition.
3686   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3687   /// \brief Source range of the loop increment.
3688   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3689   /// \brief True if the step should be subtracted.
3690   bool ShouldSubtractStep() const { return SubtractStep; }
3691   /// \brief Build the expression to calculate the number of iterations.
3692   Expr *
3693   BuildNumIterations(Scope *S, const bool LimitedType,
3694                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3695   /// \brief Build the precondition expression for the loops.
3696   Expr *BuildPreCond(Scope *S, Expr *Cond,
3697                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3698   /// \brief Build reference expression to the counter be used for codegen.
3699   DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3700                                DSAStackTy &DSA) const;
3701   /// \brief Build reference expression to the private counter be used for
3702   /// codegen.
3703   Expr *BuildPrivateCounterVar() const;
3704   /// \brief Build initialization of the counter be used for codegen.
3705   Expr *BuildCounterInit() const;
3706   /// \brief Build step of the counter be used for codegen.
3707   Expr *BuildCounterStep() const;
3708   /// \brief Return true if any expression is dependent.
3709   bool Dependent() const;
3710 
3711 private:
3712   /// \brief Check the right-hand side of an assignment in the increment
3713   /// expression.
3714   bool CheckIncRHS(Expr *RHS);
3715   /// \brief Helper to set loop counter variable and its initializer.
3716   bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3717   /// \brief Helper to set upper bound.
3718   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
3719              SourceLocation SL);
3720   /// \brief Helper to set loop increment.
3721   bool SetStep(Expr *NewStep, bool Subtract);
3722 };
3723 
3724 bool OpenMPIterationSpaceChecker::Dependent() const {
3725   if (!LCDecl) {
3726     assert(!LB && !UB && !Step);
3727     return false;
3728   }
3729   return LCDecl->getType()->isDependentType() ||
3730          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3731          (Step && Step->isValueDependent());
3732 }
3733 
3734 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3735                                                  Expr *NewLCRefExpr,
3736                                                  Expr *NewLB) {
3737   // State consistency checking to ensure correct usage.
3738   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
3739          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3740   if (!NewLCDecl || !NewLB)
3741     return true;
3742   LCDecl = getCanonicalDecl(NewLCDecl);
3743   LCRef = NewLCRefExpr;
3744   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3745     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3746       if ((Ctor->isCopyOrMoveConstructor() ||
3747            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3748           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3749         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
3750   LB = NewLB;
3751   return false;
3752 }
3753 
3754 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
3755                                         SourceRange SR, SourceLocation SL) {
3756   // State consistency checking to ensure correct usage.
3757   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3758          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3759   if (!NewUB)
3760     return true;
3761   UB = NewUB;
3762   TestIsLessOp = LessOp;
3763   TestIsStrictOp = StrictOp;
3764   ConditionSrcRange = SR;
3765   ConditionLoc = SL;
3766   return false;
3767 }
3768 
3769 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3770   // State consistency checking to ensure correct usage.
3771   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
3772   if (!NewStep)
3773     return true;
3774   if (!NewStep->isValueDependent()) {
3775     // Check that the step is integer expression.
3776     SourceLocation StepLoc = NewStep->getLocStart();
3777     ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3778         StepLoc, getExprAsWritten(NewStep));
3779     if (Val.isInvalid())
3780       return true;
3781     NewStep = Val.get();
3782 
3783     // OpenMP [2.6, Canonical Loop Form, Restrictions]
3784     //  If test-expr is of form var relational-op b and relational-op is < or
3785     //  <= then incr-expr must cause var to increase on each iteration of the
3786     //  loop. If test-expr is of form var relational-op b and relational-op is
3787     //  > or >= then incr-expr must cause var to decrease on each iteration of
3788     //  the loop.
3789     //  If test-expr is of form b relational-op var and relational-op is < or
3790     //  <= then incr-expr must cause var to decrease on each iteration of the
3791     //  loop. If test-expr is of form b relational-op var and relational-op is
3792     //  > or >= then incr-expr must cause var to increase on each iteration of
3793     //  the loop.
3794     llvm::APSInt Result;
3795     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3796     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3797     bool IsConstNeg =
3798         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
3799     bool IsConstPos =
3800         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
3801     bool IsConstZero = IsConstant && !Result.getBoolValue();
3802     if (UB && (IsConstZero ||
3803                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
3804                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
3805       SemaRef.Diag(NewStep->getExprLoc(),
3806                    diag::err_omp_loop_incr_not_compatible)
3807           << LCDecl << TestIsLessOp << NewStep->getSourceRange();
3808       SemaRef.Diag(ConditionLoc,
3809                    diag::note_omp_loop_cond_requres_compatible_incr)
3810           << TestIsLessOp << ConditionSrcRange;
3811       return true;
3812     }
3813     if (TestIsLessOp == Subtract) {
3814       NewStep =
3815           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3816               .get();
3817       Subtract = !Subtract;
3818     }
3819   }
3820 
3821   Step = NewStep;
3822   SubtractStep = Subtract;
3823   return false;
3824 }
3825 
3826 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
3827   // Check init-expr for canonical loop form and save loop counter
3828   // variable - #Var and its initialization value - #LB.
3829   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3830   //   var = lb
3831   //   integer-type var = lb
3832   //   random-access-iterator-type var = lb
3833   //   pointer-type var = lb
3834   //
3835   if (!S) {
3836     if (EmitDiags) {
3837       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3838     }
3839     return true;
3840   }
3841   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3842     if (!ExprTemp->cleanupsHaveSideEffects())
3843       S = ExprTemp->getSubExpr();
3844 
3845   InitSrcRange = S->getSourceRange();
3846   if (Expr *E = dyn_cast<Expr>(S))
3847     S = E->IgnoreParens();
3848   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3849     if (BO->getOpcode() == BO_Assign) {
3850       auto *LHS = BO->getLHS()->IgnoreParens();
3851       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3852         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3853           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3854             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3855         return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3856       }
3857       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3858         if (ME->isArrow() &&
3859             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3860           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3861       }
3862     }
3863   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
3864     if (DS->isSingleDecl()) {
3865       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
3866         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
3867           // Accept non-canonical init form here but emit ext. warning.
3868           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
3869             SemaRef.Diag(S->getLocStart(),
3870                          diag::ext_omp_loop_not_canonical_init)
3871                 << S->getSourceRange();
3872           return SetLCDeclAndLB(Var, nullptr, Var->getInit());
3873         }
3874       }
3875     }
3876   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3877     if (CE->getOperator() == OO_Equal) {
3878       auto *LHS = CE->getArg(0);
3879       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3880         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3881           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3882             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3883         return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3884       }
3885       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3886         if (ME->isArrow() &&
3887             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3888           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3889       }
3890     }
3891   }
3892 
3893   if (Dependent() || SemaRef.CurContext->isDependentContext())
3894     return false;
3895   if (EmitDiags) {
3896     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3897         << S->getSourceRange();
3898   }
3899   return true;
3900 }
3901 
3902 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
3903 /// variable (which may be the loop variable) if possible.
3904 static const ValueDecl *GetInitLCDecl(Expr *E) {
3905   if (!E)
3906     return nullptr;
3907   E = getExprAsWritten(E);
3908   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3909     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3910       if ((Ctor->isCopyOrMoveConstructor() ||
3911            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3912           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3913         E = CE->getArg(0)->IgnoreParenImpCasts();
3914   if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3915     if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
3916       return getCanonicalDecl(VD);
3917   }
3918   if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3919     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3920       return getCanonicalDecl(ME->getMemberDecl());
3921   return nullptr;
3922 }
3923 
3924 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3925   // Check test-expr for canonical form, save upper-bound UB, flags for
3926   // less/greater and for strict/non-strict comparison.
3927   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3928   //   var relational-op b
3929   //   b relational-op var
3930   //
3931   if (!S) {
3932     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
3933     return true;
3934   }
3935   S = getExprAsWritten(S);
3936   SourceLocation CondLoc = S->getLocStart();
3937   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
3938     if (BO->isRelationalOp()) {
3939       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3940         return SetUB(BO->getRHS(),
3941                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3942                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3943                      BO->getSourceRange(), BO->getOperatorLoc());
3944       if (GetInitLCDecl(BO->getRHS()) == LCDecl)
3945         return SetUB(BO->getLHS(),
3946                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3947                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3948                      BO->getSourceRange(), BO->getOperatorLoc());
3949     }
3950   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3951     if (CE->getNumArgs() == 2) {
3952       auto Op = CE->getOperator();
3953       switch (Op) {
3954       case OO_Greater:
3955       case OO_GreaterEqual:
3956       case OO_Less:
3957       case OO_LessEqual:
3958         if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3959           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3960                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3961                        CE->getOperatorLoc());
3962         if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
3963           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3964                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3965                        CE->getOperatorLoc());
3966         break;
3967       default:
3968         break;
3969       }
3970     }
3971   }
3972   if (Dependent() || SemaRef.CurContext->isDependentContext())
3973     return false;
3974   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3975       << S->getSourceRange() << LCDecl;
3976   return true;
3977 }
3978 
3979 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3980   // RHS of canonical loop form increment can be:
3981   //   var + incr
3982   //   incr + var
3983   //   var - incr
3984   //
3985   RHS = RHS->IgnoreParenImpCasts();
3986   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
3987     if (BO->isAdditiveOp()) {
3988       bool IsAdd = BO->getOpcode() == BO_Add;
3989       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3990         return SetStep(BO->getRHS(), !IsAdd);
3991       if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
3992         return SetStep(BO->getLHS(), false);
3993     }
3994   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3995     bool IsAdd = CE->getOperator() == OO_Plus;
3996     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3997       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3998         return SetStep(CE->getArg(1), !IsAdd);
3999       if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
4000         return SetStep(CE->getArg(0), false);
4001     }
4002   }
4003   if (Dependent() || SemaRef.CurContext->isDependentContext())
4004     return false;
4005   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
4006       << RHS->getSourceRange() << LCDecl;
4007   return true;
4008 }
4009 
4010 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4011   // Check incr-expr for canonical loop form and return true if it
4012   // does not conform.
4013   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4014   //   ++var
4015   //   var++
4016   //   --var
4017   //   var--
4018   //   var += incr
4019   //   var -= incr
4020   //   var = var + incr
4021   //   var = incr + var
4022   //   var = var - incr
4023   //
4024   if (!S) {
4025     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
4026     return true;
4027   }
4028   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4029     if (!ExprTemp->cleanupsHaveSideEffects())
4030       S = ExprTemp->getSubExpr();
4031 
4032   IncrementSrcRange = S->getSourceRange();
4033   S = S->IgnoreParens();
4034   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
4035     if (UO->isIncrementDecrementOp() &&
4036         GetInitLCDecl(UO->getSubExpr()) == LCDecl)
4037       return SetStep(SemaRef
4038                          .ActOnIntegerConstant(UO->getLocStart(),
4039                                                (UO->isDecrementOp() ? -1 : 1))
4040                          .get(),
4041                      false);
4042   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4043     switch (BO->getOpcode()) {
4044     case BO_AddAssign:
4045     case BO_SubAssign:
4046       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4047         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4048       break;
4049     case BO_Assign:
4050       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4051         return CheckIncRHS(BO->getRHS());
4052       break;
4053     default:
4054       break;
4055     }
4056   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4057     switch (CE->getOperator()) {
4058     case OO_PlusPlus:
4059     case OO_MinusMinus:
4060       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4061         return SetStep(SemaRef
4062                            .ActOnIntegerConstant(
4063                                CE->getLocStart(),
4064                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4065                            .get(),
4066                        false);
4067       break;
4068     case OO_PlusEqual:
4069     case OO_MinusEqual:
4070       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4071         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4072       break;
4073     case OO_Equal:
4074       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4075         return CheckIncRHS(CE->getArg(1));
4076       break;
4077     default:
4078       break;
4079     }
4080   }
4081   if (Dependent() || SemaRef.CurContext->isDependentContext())
4082     return false;
4083   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
4084       << S->getSourceRange() << LCDecl;
4085   return true;
4086 }
4087 
4088 static ExprResult
4089 tryBuildCapture(Sema &SemaRef, Expr *Capture,
4090                 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4091   if (SemaRef.CurContext->isDependentContext())
4092     return ExprResult(Capture);
4093   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4094     return SemaRef.PerformImplicitConversion(
4095         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4096         /*AllowExplicit=*/true);
4097   auto I = Captures.find(Capture);
4098   if (I != Captures.end())
4099     return buildCapture(SemaRef, Capture, I->second);
4100   DeclRefExpr *Ref = nullptr;
4101   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4102   Captures[Capture] = Ref;
4103   return Res;
4104 }
4105 
4106 /// \brief Build the expression to calculate the number of iterations.
4107 Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4108     Scope *S, const bool LimitedType,
4109     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4110   ExprResult Diff;
4111   auto VarType = LCDecl->getType().getNonReferenceType();
4112   if (VarType->isIntegerType() || VarType->isPointerType() ||
4113       SemaRef.getLangOpts().CPlusPlus) {
4114     // Upper - Lower
4115     auto *UBExpr = TestIsLessOp ? UB : LB;
4116     auto *LBExpr = TestIsLessOp ? LB : UB;
4117     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4118     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
4119     if (!Upper || !Lower)
4120       return nullptr;
4121 
4122     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4123 
4124     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4125       // BuildBinOp already emitted error, this one is to point user to upper
4126       // and lower bound, and to tell what is passed to 'operator-'.
4127       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4128           << Upper->getSourceRange() << Lower->getSourceRange();
4129       return nullptr;
4130     }
4131   }
4132 
4133   if (!Diff.isUsable())
4134     return nullptr;
4135 
4136   // Upper - Lower [- 1]
4137   if (TestIsStrictOp)
4138     Diff = SemaRef.BuildBinOp(
4139         S, DefaultLoc, BO_Sub, Diff.get(),
4140         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4141   if (!Diff.isUsable())
4142     return nullptr;
4143 
4144   // Upper - Lower [- 1] + Step
4145   auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4146   if (!NewStep.isUsable())
4147     return nullptr;
4148   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
4149   if (!Diff.isUsable())
4150     return nullptr;
4151 
4152   // Parentheses (for dumping/debugging purposes only).
4153   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4154   if (!Diff.isUsable())
4155     return nullptr;
4156 
4157   // (Upper - Lower [- 1] + Step) / Step
4158   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4159   if (!Diff.isUsable())
4160     return nullptr;
4161 
4162   // OpenMP runtime requires 32-bit or 64-bit loop variables.
4163   QualType Type = Diff.get()->getType();
4164   auto &C = SemaRef.Context;
4165   bool UseVarType = VarType->hasIntegerRepresentation() &&
4166                     C.getTypeSize(Type) > C.getTypeSize(VarType);
4167   if (!Type->isIntegerType() || UseVarType) {
4168     unsigned NewSize =
4169         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4170     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4171                                : Type->hasSignedIntegerRepresentation();
4172     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
4173     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4174       Diff = SemaRef.PerformImplicitConversion(
4175           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4176       if (!Diff.isUsable())
4177         return nullptr;
4178     }
4179   }
4180   if (LimitedType) {
4181     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4182     if (NewSize != C.getTypeSize(Type)) {
4183       if (NewSize < C.getTypeSize(Type)) {
4184         assert(NewSize == 64 && "incorrect loop var size");
4185         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4186             << InitSrcRange << ConditionSrcRange;
4187       }
4188       QualType NewType = C.getIntTypeForBitwidth(
4189           NewSize, Type->hasSignedIntegerRepresentation() ||
4190                        C.getTypeSize(Type) < NewSize);
4191       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4192         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4193                                                  Sema::AA_Converting, true);
4194         if (!Diff.isUsable())
4195           return nullptr;
4196       }
4197     }
4198   }
4199 
4200   return Diff.get();
4201 }
4202 
4203 Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4204     Scope *S, Expr *Cond,
4205     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4206   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4207   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4208   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4209 
4210   auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4211   auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4212   if (!NewLB.isUsable() || !NewUB.isUsable())
4213     return nullptr;
4214 
4215   auto CondExpr = SemaRef.BuildBinOp(
4216       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4217                                   : (TestIsStrictOp ? BO_GT : BO_GE),
4218       NewLB.get(), NewUB.get());
4219   if (CondExpr.isUsable()) {
4220     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4221                                                 SemaRef.Context.BoolTy))
4222       CondExpr = SemaRef.PerformImplicitConversion(
4223           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4224           /*AllowExplicit=*/true);
4225   }
4226   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4227   // Otherwise use original loop conditon and evaluate it in runtime.
4228   return CondExpr.isUsable() ? CondExpr.get() : Cond;
4229 }
4230 
4231 /// \brief Build reference expression to the counter be used for codegen.
4232 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
4233     llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
4234   auto *VD = dyn_cast<VarDecl>(LCDecl);
4235   if (!VD) {
4236     VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4237     auto *Ref = buildDeclRefExpr(
4238         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4239     DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4240     // If the loop control decl is explicitly marked as private, do not mark it
4241     // as captured again.
4242     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4243       Captures.insert(std::make_pair(LCRef, Ref));
4244     return Ref;
4245   }
4246   return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4247                           DefaultLoc);
4248 }
4249 
4250 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
4251   if (LCDecl && !LCDecl->isInvalidDecl()) {
4252     auto Type = LCDecl->getType().getNonReferenceType();
4253     auto *PrivateVar = buildVarDecl(
4254         SemaRef, DefaultLoc, Type, LCDecl->getName(),
4255         LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4256         isa<VarDecl>(LCDecl)
4257             ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4258             : nullptr);
4259     if (PrivateVar->isInvalidDecl())
4260       return nullptr;
4261     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4262   }
4263   return nullptr;
4264 }
4265 
4266 /// \brief Build initialization of the counter to be used for codegen.
4267 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4268 
4269 /// \brief Build step of the counter be used for codegen.
4270 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4271 
4272 /// \brief Iteration space of a single for loop.
4273 struct LoopIterationSpace final {
4274   /// \brief Condition of the loop.
4275   Expr *PreCond = nullptr;
4276   /// \brief This expression calculates the number of iterations in the loop.
4277   /// It is always possible to calculate it before starting the loop.
4278   Expr *NumIterations = nullptr;
4279   /// \brief The loop counter variable.
4280   Expr *CounterVar = nullptr;
4281   /// \brief Private loop counter variable.
4282   Expr *PrivateCounterVar = nullptr;
4283   /// \brief This is initializer for the initial value of #CounterVar.
4284   Expr *CounterInit = nullptr;
4285   /// \brief This is step for the #CounterVar used to generate its update:
4286   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4287   Expr *CounterStep = nullptr;
4288   /// \brief Should step be subtracted?
4289   bool Subtract = false;
4290   /// \brief Source range of the loop init.
4291   SourceRange InitSrcRange;
4292   /// \brief Source range of the loop condition.
4293   SourceRange CondSrcRange;
4294   /// \brief Source range of the loop increment.
4295   SourceRange IncSrcRange;
4296 };
4297 
4298 } // namespace
4299 
4300 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4301   assert(getLangOpts().OpenMP && "OpenMP is not active.");
4302   assert(Init && "Expected loop in canonical form.");
4303   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4304   if (AssociatedLoops > 0 &&
4305       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4306     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4307     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4308       if (auto *D = ISC.GetLoopDecl()) {
4309         auto *VD = dyn_cast<VarDecl>(D);
4310         if (!VD) {
4311           if (auto *Private = IsOpenMPCapturedDecl(D))
4312             VD = Private;
4313           else {
4314             auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4315                                      /*WithInit=*/false);
4316             VD = cast<VarDecl>(Ref->getDecl());
4317           }
4318         }
4319         DSAStack->addLoopControlVariable(D, VD);
4320       }
4321     }
4322     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4323   }
4324 }
4325 
4326 /// \brief Called on a for stmt to check and extract its iteration space
4327 /// for further processing (such as collapsing).
4328 static bool CheckOpenMPIterationSpace(
4329     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4330     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4331     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
4332     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4333     LoopIterationSpace &ResultIterSpace,
4334     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4335   // OpenMP [2.6, Canonical Loop Form]
4336   //   for (init-expr; test-expr; incr-expr) structured-block
4337   auto *For = dyn_cast_or_null<ForStmt>(S);
4338   if (!For) {
4339     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
4340         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4341         << getOpenMPDirectiveName(DKind) << NestedLoopCount
4342         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4343     if (NestedLoopCount > 1) {
4344       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4345         SemaRef.Diag(DSA.getConstructLoc(),
4346                      diag::note_omp_collapse_ordered_expr)
4347             << 2 << CollapseLoopCountExpr->getSourceRange()
4348             << OrderedLoopCountExpr->getSourceRange();
4349       else if (CollapseLoopCountExpr)
4350         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4351                      diag::note_omp_collapse_ordered_expr)
4352             << 0 << CollapseLoopCountExpr->getSourceRange();
4353       else
4354         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4355                      diag::note_omp_collapse_ordered_expr)
4356             << 1 << OrderedLoopCountExpr->getSourceRange();
4357     }
4358     return true;
4359   }
4360   assert(For->getBody());
4361 
4362   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4363 
4364   // Check init.
4365   auto Init = For->getInit();
4366   if (ISC.CheckInit(Init))
4367     return true;
4368 
4369   bool HasErrors = false;
4370 
4371   // Check loop variable's type.
4372   if (auto *LCDecl = ISC.GetLoopDecl()) {
4373     auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
4374 
4375     // OpenMP [2.6, Canonical Loop Form]
4376     // Var is one of the following:
4377     //   A variable of signed or unsigned integer type.
4378     //   For C++, a variable of a random access iterator type.
4379     //   For C, a variable of a pointer type.
4380     auto VarType = LCDecl->getType().getNonReferenceType();
4381     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4382         !VarType->isPointerType() &&
4383         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4384       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4385           << SemaRef.getLangOpts().CPlusPlus;
4386       HasErrors = true;
4387     }
4388 
4389     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4390     // a Construct
4391     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4392     // parallel for construct is (are) private.
4393     // The loop iteration variable in the associated for-loop of a simd
4394     // construct with just one associated for-loop is linear with a
4395     // constant-linear-step that is the increment of the associated for-loop.
4396     // Exclude loop var from the list of variables with implicitly defined data
4397     // sharing attributes.
4398     VarsWithImplicitDSA.erase(LCDecl);
4399 
4400     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4401     // in a Construct, C/C++].
4402     // The loop iteration variable in the associated for-loop of a simd
4403     // construct with just one associated for-loop may be listed in a linear
4404     // clause with a constant-linear-step that is the increment of the
4405     // associated for-loop.
4406     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4407     // parallel for construct may be listed in a private or lastprivate clause.
4408     DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4409     // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4410     // declared in the loop and it is predetermined as a private.
4411     auto PredeterminedCKind =
4412         isOpenMPSimdDirective(DKind)
4413             ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4414             : OMPC_private;
4415     if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4416           DVar.CKind != PredeterminedCKind) ||
4417          ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4418            isOpenMPDistributeDirective(DKind)) &&
4419           !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4420           DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4421         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4422       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4423           << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4424           << getOpenMPClauseName(PredeterminedCKind);
4425       if (DVar.RefExpr == nullptr)
4426         DVar.CKind = PredeterminedCKind;
4427       ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4428       HasErrors = true;
4429     } else if (LoopDeclRefExpr != nullptr) {
4430       // Make the loop iteration variable private (for worksharing constructs),
4431       // linear (for simd directives with the only one associated loop) or
4432       // lastprivate (for simd directives with several collapsed or ordered
4433       // loops).
4434       if (DVar.CKind == OMPC_unknown)
4435         DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4436                           [](OpenMPDirectiveKind) -> bool { return true; },
4437                           /*FromParent=*/false);
4438       DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4439     }
4440 
4441     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4442 
4443     // Check test-expr.
4444     HasErrors |= ISC.CheckCond(For->getCond());
4445 
4446     // Check incr-expr.
4447     HasErrors |= ISC.CheckInc(For->getInc());
4448   }
4449 
4450   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4451     return HasErrors;
4452 
4453   // Build the loop's iteration space representation.
4454   ResultIterSpace.PreCond =
4455       ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4456   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
4457       DSA.getCurScope(),
4458       (isOpenMPWorksharingDirective(DKind) ||
4459        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4460       Captures);
4461   ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
4462   ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
4463   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4464   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4465   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4466   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4467   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4468   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4469 
4470   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4471                 ResultIterSpace.NumIterations == nullptr ||
4472                 ResultIterSpace.CounterVar == nullptr ||
4473                 ResultIterSpace.PrivateCounterVar == nullptr ||
4474                 ResultIterSpace.CounterInit == nullptr ||
4475                 ResultIterSpace.CounterStep == nullptr);
4476 
4477   return HasErrors;
4478 }
4479 
4480 /// \brief Build 'VarRef = Start.
4481 static ExprResult
4482 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4483                  ExprResult Start,
4484                  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4485   // Build 'VarRef = Start.
4486   auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4487   if (!NewStart.isUsable())
4488     return ExprError();
4489   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4490                                    VarRef.get()->getType())) {
4491     NewStart = SemaRef.PerformImplicitConversion(
4492         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4493         /*AllowExplicit=*/true);
4494     if (!NewStart.isUsable())
4495       return ExprError();
4496   }
4497 
4498   auto Init =
4499       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4500   return Init;
4501 }
4502 
4503 /// \brief Build 'VarRef = Start + Iter * Step'.
4504 static ExprResult
4505 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4506                    ExprResult VarRef, ExprResult Start, ExprResult Iter,
4507                    ExprResult Step, bool Subtract,
4508                    llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
4509   // Add parentheses (for debugging purposes only).
4510   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4511   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4512       !Step.isUsable())
4513     return ExprError();
4514 
4515   ExprResult NewStep = Step;
4516   if (Captures)
4517     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4518   if (NewStep.isInvalid())
4519     return ExprError();
4520   ExprResult Update =
4521       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4522   if (!Update.isUsable())
4523     return ExprError();
4524 
4525   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4526   // 'VarRef = Start (+|-) Iter * Step'.
4527   ExprResult NewStart = Start;
4528   if (Captures)
4529     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4530   if (NewStart.isInvalid())
4531     return ExprError();
4532 
4533   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4534   ExprResult SavedUpdate = Update;
4535   ExprResult UpdateVal;
4536   if (VarRef.get()->getType()->isOverloadableType() ||
4537       NewStart.get()->getType()->isOverloadableType() ||
4538       Update.get()->getType()->isOverloadableType()) {
4539     bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4540     SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4541     Update =
4542         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4543     if (Update.isUsable()) {
4544       UpdateVal =
4545           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4546                              VarRef.get(), SavedUpdate.get());
4547       if (UpdateVal.isUsable()) {
4548         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4549                                             UpdateVal.get());
4550       }
4551     }
4552     SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4553   }
4554 
4555   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4556   if (!Update.isUsable() || !UpdateVal.isUsable()) {
4557     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4558                                 NewStart.get(), SavedUpdate.get());
4559     if (!Update.isUsable())
4560       return ExprError();
4561 
4562     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4563                                      VarRef.get()->getType())) {
4564       Update = SemaRef.PerformImplicitConversion(
4565           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4566       if (!Update.isUsable())
4567         return ExprError();
4568     }
4569 
4570     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4571   }
4572   return Update;
4573 }
4574 
4575 /// \brief Convert integer expression \a E to make it have at least \a Bits
4576 /// bits.
4577 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
4578   if (E == nullptr)
4579     return ExprError();
4580   auto &C = SemaRef.Context;
4581   QualType OldType = E->getType();
4582   unsigned HasBits = C.getTypeSize(OldType);
4583   if (HasBits >= Bits)
4584     return ExprResult(E);
4585   // OK to convert to signed, because new type has more bits than old.
4586   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4587   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4588                                            true);
4589 }
4590 
4591 /// \brief Check if the given expression \a E is a constant integer that fits
4592 /// into \a Bits bits.
4593 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4594   if (E == nullptr)
4595     return false;
4596   llvm::APSInt Result;
4597   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4598     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4599   return false;
4600 }
4601 
4602 /// Build preinits statement for the given declarations.
4603 static Stmt *buildPreInits(ASTContext &Context,
4604                            MutableArrayRef<Decl *> PreInits) {
4605   if (!PreInits.empty()) {
4606     return new (Context) DeclStmt(
4607         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4608         SourceLocation(), SourceLocation());
4609   }
4610   return nullptr;
4611 }
4612 
4613 /// Build preinits statement for the given declarations.
4614 static Stmt *
4615 buildPreInits(ASTContext &Context,
4616               const llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4617   if (!Captures.empty()) {
4618     SmallVector<Decl *, 16> PreInits;
4619     for (auto &Pair : Captures)
4620       PreInits.push_back(Pair.second->getDecl());
4621     return buildPreInits(Context, PreInits);
4622   }
4623   return nullptr;
4624 }
4625 
4626 /// Build postupdate expression for the given list of postupdates expressions.
4627 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4628   Expr *PostUpdate = nullptr;
4629   if (!PostUpdates.empty()) {
4630     for (auto *E : PostUpdates) {
4631       Expr *ConvE = S.BuildCStyleCastExpr(
4632                          E->getExprLoc(),
4633                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4634                          E->getExprLoc(), E)
4635                         .get();
4636       PostUpdate = PostUpdate
4637                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4638                                               PostUpdate, ConvE)
4639                              .get()
4640                        : ConvE;
4641     }
4642   }
4643   return PostUpdate;
4644 }
4645 
4646 /// \brief Called on a for stmt to check itself and nested loops (if any).
4647 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4648 /// number of collapsed loops otherwise.
4649 static unsigned
4650 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4651                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4652                 DSAStackTy &DSA,
4653                 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4654                 OMPLoopDirective::HelperExprs &Built) {
4655   unsigned NestedLoopCount = 1;
4656   if (CollapseLoopCountExpr) {
4657     // Found 'collapse' clause - calculate collapse number.
4658     llvm::APSInt Result;
4659     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
4660       NestedLoopCount = Result.getLimitedValue();
4661   }
4662   if (OrderedLoopCountExpr) {
4663     // Found 'ordered' clause - calculate collapse number.
4664     llvm::APSInt Result;
4665     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4666       if (Result.getLimitedValue() < NestedLoopCount) {
4667         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4668                      diag::err_omp_wrong_ordered_loop_count)
4669             << OrderedLoopCountExpr->getSourceRange();
4670         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4671                      diag::note_collapse_loop_count)
4672             << CollapseLoopCountExpr->getSourceRange();
4673       }
4674       NestedLoopCount = Result.getLimitedValue();
4675     }
4676   }
4677   // This is helper routine for loop directives (e.g., 'for', 'simd',
4678   // 'for simd', etc.).
4679   llvm::MapVector<Expr *, DeclRefExpr *> Captures;
4680   SmallVector<LoopIterationSpace, 4> IterSpaces;
4681   IterSpaces.resize(NestedLoopCount);
4682   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
4683   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
4684     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
4685                                   NestedLoopCount, CollapseLoopCountExpr,
4686                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
4687                                   IterSpaces[Cnt], Captures))
4688       return 0;
4689     // Move on to the next nested for loop, or to the loop body.
4690     // OpenMP [2.8.1, simd construct, Restrictions]
4691     // All loops associated with the construct must be perfectly nested; that
4692     // is, there must be no intervening code nor any OpenMP directive between
4693     // any two loops.
4694     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4695   }
4696 
4697   Built.clear(/* size */ NestedLoopCount);
4698 
4699   if (SemaRef.CurContext->isDependentContext())
4700     return NestedLoopCount;
4701 
4702   // An example of what is generated for the following code:
4703   //
4704   //   #pragma omp simd collapse(2) ordered(2)
4705   //   for (i = 0; i < NI; ++i)
4706   //     for (k = 0; k < NK; ++k)
4707   //       for (j = J0; j < NJ; j+=2) {
4708   //         <loop body>
4709   //       }
4710   //
4711   // We generate the code below.
4712   // Note: the loop body may be outlined in CodeGen.
4713   // Note: some counters may be C++ classes, operator- is used to find number of
4714   // iterations and operator+= to calculate counter value.
4715   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4716   // or i64 is currently supported).
4717   //
4718   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4719   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4720   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4721   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4722   //     // similar updates for vars in clauses (e.g. 'linear')
4723   //     <loop body (using local i and j)>
4724   //   }
4725   //   i = NI; // assign final values of counters
4726   //   j = NJ;
4727   //
4728 
4729   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4730   // the iteration counts of the collapsed for loops.
4731   // Precondition tests if there is at least one iteration (all conditions are
4732   // true).
4733   auto PreCond = ExprResult(IterSpaces[0].PreCond);
4734   auto N0 = IterSpaces[0].NumIterations;
4735   ExprResult LastIteration32 = WidenIterationCount(
4736       32 /* Bits */, SemaRef
4737                          .PerformImplicitConversion(
4738                              N0->IgnoreImpCasts(), N0->getType(),
4739                              Sema::AA_Converting, /*AllowExplicit=*/true)
4740                          .get(),
4741       SemaRef);
4742   ExprResult LastIteration64 = WidenIterationCount(
4743       64 /* Bits */, SemaRef
4744                          .PerformImplicitConversion(
4745                              N0->IgnoreImpCasts(), N0->getType(),
4746                              Sema::AA_Converting, /*AllowExplicit=*/true)
4747                          .get(),
4748       SemaRef);
4749 
4750   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4751     return NestedLoopCount;
4752 
4753   auto &C = SemaRef.Context;
4754   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4755 
4756   Scope *CurScope = DSA.getCurScope();
4757   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
4758     if (PreCond.isUsable()) {
4759       PreCond =
4760           SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
4761                              PreCond.get(), IterSpaces[Cnt].PreCond);
4762     }
4763     auto N = IterSpaces[Cnt].NumIterations;
4764     SourceLocation Loc = N->getExprLoc();
4765     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4766     if (LastIteration32.isUsable())
4767       LastIteration32 = SemaRef.BuildBinOp(
4768           CurScope, Loc, BO_Mul, LastIteration32.get(),
4769           SemaRef
4770               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4771                                          Sema::AA_Converting,
4772                                          /*AllowExplicit=*/true)
4773               .get());
4774     if (LastIteration64.isUsable())
4775       LastIteration64 = SemaRef.BuildBinOp(
4776           CurScope, Loc, BO_Mul, LastIteration64.get(),
4777           SemaRef
4778               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4779                                          Sema::AA_Converting,
4780                                          /*AllowExplicit=*/true)
4781               .get());
4782   }
4783 
4784   // Choose either the 32-bit or 64-bit version.
4785   ExprResult LastIteration = LastIteration64;
4786   if (LastIteration32.isUsable() &&
4787       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4788       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4789        FitsInto(
4790            32 /* Bits */,
4791            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4792            LastIteration64.get(), SemaRef)))
4793     LastIteration = LastIteration32;
4794   QualType VType = LastIteration.get()->getType();
4795   QualType RealVType = VType;
4796   QualType StrideVType = VType;
4797   if (isOpenMPTaskLoopDirective(DKind)) {
4798     VType =
4799         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4800     StrideVType =
4801         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4802   }
4803 
4804   if (!LastIteration.isUsable())
4805     return 0;
4806 
4807   // Save the number of iterations.
4808   ExprResult NumIterations = LastIteration;
4809   {
4810     LastIteration = SemaRef.BuildBinOp(
4811         CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
4812         LastIteration.get(),
4813         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4814     if (!LastIteration.isUsable())
4815       return 0;
4816   }
4817 
4818   // Calculate the last iteration number beforehand instead of doing this on
4819   // each iteration. Do not do this if the number of iterations may be kfold-ed.
4820   llvm::APSInt Result;
4821   bool IsConstant =
4822       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4823   ExprResult CalcLastIteration;
4824   if (!IsConstant) {
4825     ExprResult SaveRef =
4826         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
4827     LastIteration = SaveRef;
4828 
4829     // Prepare SaveRef + 1.
4830     NumIterations = SemaRef.BuildBinOp(
4831         CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
4832         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4833     if (!NumIterations.isUsable())
4834       return 0;
4835   }
4836 
4837   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4838 
4839   // Build variables passed into runtime, necessary for worksharing directives.
4840   ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
4841   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4842       isOpenMPDistributeDirective(DKind)) {
4843     // Lower bound variable, initialized with zero.
4844     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4845     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
4846     SemaRef.AddInitializerToDecl(LBDecl,
4847                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4848                                  /*DirectInit*/ false);
4849 
4850     // Upper bound variable, initialized with last iteration number.
4851     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4852     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
4853     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4854                                  /*DirectInit*/ false);
4855 
4856     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4857     // This will be used to implement clause 'lastprivate'.
4858     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
4859     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4860     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
4861     SemaRef.AddInitializerToDecl(ILDecl,
4862                                  SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4863                                  /*DirectInit*/ false);
4864 
4865     // Stride variable returned by runtime (we initialize it to 1 by default).
4866     VarDecl *STDecl =
4867         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4868     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
4869     SemaRef.AddInitializerToDecl(STDecl,
4870                                  SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4871                                  /*DirectInit*/ false);
4872 
4873     // Build expression: UB = min(UB, LastIteration)
4874     // It is necessary for CodeGen of directives with static scheduling.
4875     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4876                                                 UB.get(), LastIteration.get());
4877     ExprResult CondOp = SemaRef.ActOnConditionalOp(
4878         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4879     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4880                              CondOp.get());
4881     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4882 
4883     // If we have a combined directive that combines 'distribute', 'for' or
4884     // 'simd' we need to be able to access the bounds of the schedule of the
4885     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
4886     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
4887     if (isOpenMPLoopBoundSharingDirective(DKind)) {
4888 
4889       // Lower bound variable, initialized with zero.
4890       VarDecl *CombLBDecl =
4891           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
4892       CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
4893       SemaRef.AddInitializerToDecl(
4894           CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4895           /*DirectInit*/ false);
4896 
4897       // Upper bound variable, initialized with last iteration number.
4898       VarDecl *CombUBDecl =
4899           buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
4900       CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
4901       SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
4902                                    /*DirectInit*/ false);
4903 
4904       ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
4905           CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
4906       ExprResult CombCondOp =
4907           SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
4908                                      LastIteration.get(), CombUB.get());
4909       CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
4910                                    CombCondOp.get());
4911       CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
4912 
4913       auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
4914       // We expect to have at least 2 more parameters than the 'parallel'
4915       // directive does - the lower and upper bounds of the previous schedule.
4916       assert(CD->getNumParams() >= 4 &&
4917              "Unexpected number of parameters in loop combined directive");
4918 
4919       // Set the proper type for the bounds given what we learned from the
4920       // enclosed loops.
4921       auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
4922       auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
4923 
4924       // Previous lower and upper bounds are obtained from the region
4925       // parameters.
4926       PrevLB =
4927           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
4928       PrevUB =
4929           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
4930     }
4931   }
4932 
4933   // Build the iteration variable and its initialization before loop.
4934   ExprResult IV;
4935   ExprResult Init, CombInit;
4936   {
4937     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4938     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
4939     Expr *RHS =
4940         (isOpenMPWorksharingDirective(DKind) ||
4941          isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4942             ? LB.get()
4943             : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4944     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4945     Init = SemaRef.ActOnFinishFullExpr(Init.get());
4946 
4947     if (isOpenMPLoopBoundSharingDirective(DKind)) {
4948       Expr *CombRHS =
4949           (isOpenMPWorksharingDirective(DKind) ||
4950            isOpenMPTaskLoopDirective(DKind) ||
4951            isOpenMPDistributeDirective(DKind))
4952               ? CombLB.get()
4953               : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4954       CombInit =
4955           SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
4956       CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
4957     }
4958   }
4959 
4960   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
4961   SourceLocation CondLoc = AStmt->getLocStart();
4962   ExprResult Cond =
4963       (isOpenMPWorksharingDirective(DKind) ||
4964        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4965           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4966           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4967                                NumIterations.get());
4968   ExprResult CombCond;
4969   if (isOpenMPLoopBoundSharingDirective(DKind)) {
4970     CombCond =
4971         SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
4972   }
4973   // Loop increment (IV = IV + 1)
4974   SourceLocation IncLoc = AStmt->getLocStart();
4975   ExprResult Inc =
4976       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4977                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4978   if (!Inc.isUsable())
4979     return 0;
4980   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
4981   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4982   if (!Inc.isUsable())
4983     return 0;
4984 
4985   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4986   // Used for directives with static scheduling.
4987   // In combined construct, add combined version that use CombLB and CombUB
4988   // base variables for the update
4989   ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
4990   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4991       isOpenMPDistributeDirective(DKind)) {
4992     // LB + ST
4993     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4994     if (!NextLB.isUsable())
4995       return 0;
4996     // LB = LB + ST
4997     NextLB =
4998         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4999     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5000     if (!NextLB.isUsable())
5001       return 0;
5002     // UB + ST
5003     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5004     if (!NextUB.isUsable())
5005       return 0;
5006     // UB = UB + ST
5007     NextUB =
5008         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5009     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5010     if (!NextUB.isUsable())
5011       return 0;
5012     if (isOpenMPLoopBoundSharingDirective(DKind)) {
5013       CombNextLB =
5014           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5015       if (!NextLB.isUsable())
5016         return 0;
5017       // LB = LB + ST
5018       CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5019                                       CombNextLB.get());
5020       CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5021       if (!CombNextLB.isUsable())
5022         return 0;
5023       // UB + ST
5024       CombNextUB =
5025           SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5026       if (!CombNextUB.isUsable())
5027         return 0;
5028       // UB = UB + ST
5029       CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5030                                       CombNextUB.get());
5031       CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5032       if (!CombNextUB.isUsable())
5033         return 0;
5034     }
5035   }
5036 
5037   // Create increment expression for distribute loop when combined in a same
5038   // directive with for as IV = IV + ST; ensure upper bound expression based
5039   // on PrevUB instead of NumIterations - used to implement 'for' when found
5040   // in combination with 'distribute', like in 'distribute parallel for'
5041   SourceLocation DistIncLoc = AStmt->getLocStart();
5042   ExprResult DistCond, DistInc, PrevEUB;
5043   if (isOpenMPLoopBoundSharingDirective(DKind)) {
5044     DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5045     assert(DistCond.isUsable() && "distribute cond expr was not built");
5046 
5047     DistInc =
5048         SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5049     assert(DistInc.isUsable() && "distribute inc expr was not built");
5050     DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5051                                  DistInc.get());
5052     DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5053     assert(DistInc.isUsable() && "distribute inc expr was not built");
5054 
5055     // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5056     // construct
5057     SourceLocation DistEUBLoc = AStmt->getLocStart();
5058     ExprResult IsUBGreater =
5059         SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5060     ExprResult CondOp = SemaRef.ActOnConditionalOp(
5061         DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5062     PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5063                                  CondOp.get());
5064     PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
5065   }
5066 
5067   // Build updates and final values of the loop counters.
5068   bool HasErrors = false;
5069   Built.Counters.resize(NestedLoopCount);
5070   Built.Inits.resize(NestedLoopCount);
5071   Built.Updates.resize(NestedLoopCount);
5072   Built.Finals.resize(NestedLoopCount);
5073   SmallVector<Expr *, 4> LoopMultipliers;
5074   {
5075     ExprResult Div;
5076     // Go from inner nested loop to outer.
5077     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5078       LoopIterationSpace &IS = IterSpaces[Cnt];
5079       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5080       // Build: Iter = (IV / Div) % IS.NumIters
5081       // where Div is product of previous iterations' IS.NumIters.
5082       ExprResult Iter;
5083       if (Div.isUsable()) {
5084         Iter =
5085             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5086       } else {
5087         Iter = IV;
5088         assert((Cnt == (int)NestedLoopCount - 1) &&
5089                "unusable div expected on first iteration only");
5090       }
5091 
5092       if (Cnt != 0 && Iter.isUsable())
5093         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5094                                   IS.NumIterations);
5095       if (!Iter.isUsable()) {
5096         HasErrors = true;
5097         break;
5098       }
5099 
5100       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
5101       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5102       auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5103                                           IS.CounterVar->getExprLoc(),
5104                                           /*RefersToCapture=*/true);
5105       ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
5106                                          IS.CounterInit, Captures);
5107       if (!Init.isUsable()) {
5108         HasErrors = true;
5109         break;
5110       }
5111       ExprResult Update = BuildCounterUpdate(
5112           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5113           IS.CounterStep, IS.Subtract, &Captures);
5114       if (!Update.isUsable()) {
5115         HasErrors = true;
5116         break;
5117       }
5118 
5119       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5120       ExprResult Final = BuildCounterUpdate(
5121           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
5122           IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
5123       if (!Final.isUsable()) {
5124         HasErrors = true;
5125         break;
5126       }
5127 
5128       // Build Div for the next iteration: Div <- Div * IS.NumIters
5129       if (Cnt != 0) {
5130         if (Div.isUnset())
5131           Div = IS.NumIterations;
5132         else
5133           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5134                                    IS.NumIterations);
5135 
5136         // Add parentheses (for debugging purposes only).
5137         if (Div.isUsable())
5138           Div = tryBuildCapture(SemaRef, Div.get(), Captures);
5139         if (!Div.isUsable()) {
5140           HasErrors = true;
5141           break;
5142         }
5143         LoopMultipliers.push_back(Div.get());
5144       }
5145       if (!Update.isUsable() || !Final.isUsable()) {
5146         HasErrors = true;
5147         break;
5148       }
5149       // Save results
5150       Built.Counters[Cnt] = IS.CounterVar;
5151       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
5152       Built.Inits[Cnt] = Init.get();
5153       Built.Updates[Cnt] = Update.get();
5154       Built.Finals[Cnt] = Final.get();
5155     }
5156   }
5157 
5158   if (HasErrors)
5159     return 0;
5160 
5161   // Save results
5162   Built.IterationVarRef = IV.get();
5163   Built.LastIteration = LastIteration.get();
5164   Built.NumIterations = NumIterations.get();
5165   Built.CalcLastIteration =
5166       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
5167   Built.PreCond = PreCond.get();
5168   Built.PreInits = buildPreInits(C, Captures);
5169   Built.Cond = Cond.get();
5170   Built.Init = Init.get();
5171   Built.Inc = Inc.get();
5172   Built.LB = LB.get();
5173   Built.UB = UB.get();
5174   Built.IL = IL.get();
5175   Built.ST = ST.get();
5176   Built.EUB = EUB.get();
5177   Built.NLB = NextLB.get();
5178   Built.NUB = NextUB.get();
5179   Built.PrevLB = PrevLB.get();
5180   Built.PrevUB = PrevUB.get();
5181   Built.DistInc = DistInc.get();
5182   Built.PrevEUB = PrevEUB.get();
5183   Built.DistCombinedFields.LB = CombLB.get();
5184   Built.DistCombinedFields.UB = CombUB.get();
5185   Built.DistCombinedFields.EUB = CombEUB.get();
5186   Built.DistCombinedFields.Init = CombInit.get();
5187   Built.DistCombinedFields.Cond = CombCond.get();
5188   Built.DistCombinedFields.NLB = CombNextLB.get();
5189   Built.DistCombinedFields.NUB = CombNextUB.get();
5190 
5191   Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5192   // Fill data for doacross depend clauses.
5193   for (auto Pair : DSA.getDoacrossDependClauses()) {
5194     if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5195       Pair.first->setCounterValue(CounterVal);
5196     else {
5197       if (NestedLoopCount != Pair.second.size() ||
5198           NestedLoopCount != LoopMultipliers.size() + 1) {
5199         // Erroneous case - clause has some problems.
5200         Pair.first->setCounterValue(CounterVal);
5201         continue;
5202       }
5203       assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5204       auto I = Pair.second.rbegin();
5205       auto IS = IterSpaces.rbegin();
5206       auto ILM = LoopMultipliers.rbegin();
5207       Expr *UpCounterVal = CounterVal;
5208       Expr *Multiplier = nullptr;
5209       for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5210         if (I->first) {
5211           assert(IS->CounterStep);
5212           Expr *NormalizedOffset =
5213               SemaRef
5214                   .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5215                               I->first, IS->CounterStep)
5216                   .get();
5217           if (Multiplier) {
5218             NormalizedOffset =
5219                 SemaRef
5220                     .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5221                                 NormalizedOffset, Multiplier)
5222                     .get();
5223           }
5224           assert(I->second == OO_Plus || I->second == OO_Minus);
5225           BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5226           UpCounterVal = SemaRef
5227                              .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5228                                          UpCounterVal, NormalizedOffset)
5229                              .get();
5230         }
5231         Multiplier = *ILM;
5232         ++I;
5233         ++IS;
5234         ++ILM;
5235       }
5236       Pair.first->setCounterValue(UpCounterVal);
5237     }
5238   }
5239 
5240   return NestedLoopCount;
5241 }
5242 
5243 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
5244   auto CollapseClauses =
5245       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5246   if (CollapseClauses.begin() != CollapseClauses.end())
5247     return (*CollapseClauses.begin())->getNumForLoops();
5248   return nullptr;
5249 }
5250 
5251 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
5252   auto OrderedClauses =
5253       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5254   if (OrderedClauses.begin() != OrderedClauses.end())
5255     return (*OrderedClauses.begin())->getNumForLoops();
5256   return nullptr;
5257 }
5258 
5259 static bool checkSimdlenSafelenSpecified(Sema &S,
5260                                          const ArrayRef<OMPClause *> Clauses) {
5261   OMPSafelenClause *Safelen = nullptr;
5262   OMPSimdlenClause *Simdlen = nullptr;
5263 
5264   for (auto *Clause : Clauses) {
5265     if (Clause->getClauseKind() == OMPC_safelen)
5266       Safelen = cast<OMPSafelenClause>(Clause);
5267     else if (Clause->getClauseKind() == OMPC_simdlen)
5268       Simdlen = cast<OMPSimdlenClause>(Clause);
5269     if (Safelen && Simdlen)
5270       break;
5271   }
5272 
5273   if (Simdlen && Safelen) {
5274     llvm::APSInt SimdlenRes, SafelenRes;
5275     auto SimdlenLength = Simdlen->getSimdlen();
5276     auto SafelenLength = Safelen->getSafelen();
5277     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5278         SimdlenLength->isInstantiationDependent() ||
5279         SimdlenLength->containsUnexpandedParameterPack())
5280       return false;
5281     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5282         SafelenLength->isInstantiationDependent() ||
5283         SafelenLength->containsUnexpandedParameterPack())
5284       return false;
5285     SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5286     SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5287     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5288     // If both simdlen and safelen clauses are specified, the value of the
5289     // simdlen parameter must be less than or equal to the value of the safelen
5290     // parameter.
5291     if (SimdlenRes > SafelenRes) {
5292       S.Diag(SimdlenLength->getExprLoc(),
5293              diag::err_omp_wrong_simdlen_safelen_values)
5294           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5295       return true;
5296     }
5297   }
5298   return false;
5299 }
5300 
5301 StmtResult Sema::ActOnOpenMPSimdDirective(
5302     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5303     SourceLocation EndLoc,
5304     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5305   if (!AStmt)
5306     return StmtError();
5307 
5308   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5309   OMPLoopDirective::HelperExprs B;
5310   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5311   // define the nested loops number.
5312   unsigned NestedLoopCount = CheckOpenMPLoop(
5313       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5314       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5315   if (NestedLoopCount == 0)
5316     return StmtError();
5317 
5318   assert((CurContext->isDependentContext() || B.builtAll()) &&
5319          "omp simd loop exprs were not built");
5320 
5321   if (!CurContext->isDependentContext()) {
5322     // Finalize the clauses that need pre-built expressions for CodeGen.
5323     for (auto C : Clauses) {
5324       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5325         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5326                                      B.NumIterations, *this, CurScope,
5327                                      DSAStack))
5328           return StmtError();
5329     }
5330   }
5331 
5332   if (checkSimdlenSafelenSpecified(*this, Clauses))
5333     return StmtError();
5334 
5335   setFunctionHasBranchProtectedScope();
5336   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5337                                   Clauses, AStmt, B);
5338 }
5339 
5340 StmtResult Sema::ActOnOpenMPForDirective(
5341     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5342     SourceLocation EndLoc,
5343     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5344   if (!AStmt)
5345     return StmtError();
5346 
5347   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5348   OMPLoopDirective::HelperExprs B;
5349   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5350   // define the nested loops number.
5351   unsigned NestedLoopCount = CheckOpenMPLoop(
5352       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5353       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5354   if (NestedLoopCount == 0)
5355     return StmtError();
5356 
5357   assert((CurContext->isDependentContext() || B.builtAll()) &&
5358          "omp for loop exprs were not built");
5359 
5360   if (!CurContext->isDependentContext()) {
5361     // Finalize the clauses that need pre-built expressions for CodeGen.
5362     for (auto C : Clauses) {
5363       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5364         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5365                                      B.NumIterations, *this, CurScope,
5366                                      DSAStack))
5367           return StmtError();
5368     }
5369   }
5370 
5371   setFunctionHasBranchProtectedScope();
5372   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5373                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
5374 }
5375 
5376 StmtResult Sema::ActOnOpenMPForSimdDirective(
5377     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5378     SourceLocation EndLoc,
5379     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5380   if (!AStmt)
5381     return StmtError();
5382 
5383   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5384   OMPLoopDirective::HelperExprs B;
5385   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5386   // define the nested loops number.
5387   unsigned NestedLoopCount =
5388       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5389                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5390                       VarsWithImplicitDSA, B);
5391   if (NestedLoopCount == 0)
5392     return StmtError();
5393 
5394   assert((CurContext->isDependentContext() || B.builtAll()) &&
5395          "omp for simd loop exprs were not built");
5396 
5397   if (!CurContext->isDependentContext()) {
5398     // Finalize the clauses that need pre-built expressions for CodeGen.
5399     for (auto C : Clauses) {
5400       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5401         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5402                                      B.NumIterations, *this, CurScope,
5403                                      DSAStack))
5404           return StmtError();
5405     }
5406   }
5407 
5408   if (checkSimdlenSafelenSpecified(*this, Clauses))
5409     return StmtError();
5410 
5411   setFunctionHasBranchProtectedScope();
5412   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5413                                      Clauses, AStmt, B);
5414 }
5415 
5416 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5417                                               Stmt *AStmt,
5418                                               SourceLocation StartLoc,
5419                                               SourceLocation EndLoc) {
5420   if (!AStmt)
5421     return StmtError();
5422 
5423   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5424   auto BaseStmt = AStmt;
5425   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5426     BaseStmt = CS->getCapturedStmt();
5427   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5428     auto S = C->children();
5429     if (S.begin() == S.end())
5430       return StmtError();
5431     // All associated statements must be '#pragma omp section' except for
5432     // the first one.
5433     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5434       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5435         if (SectionStmt)
5436           Diag(SectionStmt->getLocStart(),
5437                diag::err_omp_sections_substmt_not_section);
5438         return StmtError();
5439       }
5440       cast<OMPSectionDirective>(SectionStmt)
5441           ->setHasCancel(DSAStack->isCancelRegion());
5442     }
5443   } else {
5444     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5445     return StmtError();
5446   }
5447 
5448   setFunctionHasBranchProtectedScope();
5449 
5450   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5451                                       DSAStack->isCancelRegion());
5452 }
5453 
5454 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5455                                              SourceLocation StartLoc,
5456                                              SourceLocation EndLoc) {
5457   if (!AStmt)
5458     return StmtError();
5459 
5460   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5461 
5462   setFunctionHasBranchProtectedScope();
5463   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5464 
5465   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5466                                      DSAStack->isCancelRegion());
5467 }
5468 
5469 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5470                                             Stmt *AStmt,
5471                                             SourceLocation StartLoc,
5472                                             SourceLocation EndLoc) {
5473   if (!AStmt)
5474     return StmtError();
5475 
5476   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5477 
5478   setFunctionHasBranchProtectedScope();
5479 
5480   // OpenMP [2.7.3, single Construct, Restrictions]
5481   // The copyprivate clause must not be used with the nowait clause.
5482   OMPClause *Nowait = nullptr;
5483   OMPClause *Copyprivate = nullptr;
5484   for (auto *Clause : Clauses) {
5485     if (Clause->getClauseKind() == OMPC_nowait)
5486       Nowait = Clause;
5487     else if (Clause->getClauseKind() == OMPC_copyprivate)
5488       Copyprivate = Clause;
5489     if (Copyprivate && Nowait) {
5490       Diag(Copyprivate->getLocStart(),
5491            diag::err_omp_single_copyprivate_with_nowait);
5492       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5493       return StmtError();
5494     }
5495   }
5496 
5497   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5498 }
5499 
5500 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5501                                             SourceLocation StartLoc,
5502                                             SourceLocation EndLoc) {
5503   if (!AStmt)
5504     return StmtError();
5505 
5506   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5507 
5508   setFunctionHasBranchProtectedScope();
5509 
5510   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5511 }
5512 
5513 StmtResult Sema::ActOnOpenMPCriticalDirective(
5514     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5515     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5516   if (!AStmt)
5517     return StmtError();
5518 
5519   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5520 
5521   bool ErrorFound = false;
5522   llvm::APSInt Hint;
5523   SourceLocation HintLoc;
5524   bool DependentHint = false;
5525   for (auto *C : Clauses) {
5526     if (C->getClauseKind() == OMPC_hint) {
5527       if (!DirName.getName()) {
5528         Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5529         ErrorFound = true;
5530       }
5531       Expr *E = cast<OMPHintClause>(C)->getHint();
5532       if (E->isTypeDependent() || E->isValueDependent() ||
5533           E->isInstantiationDependent())
5534         DependentHint = true;
5535       else {
5536         Hint = E->EvaluateKnownConstInt(Context);
5537         HintLoc = C->getLocStart();
5538       }
5539     }
5540   }
5541   if (ErrorFound)
5542     return StmtError();
5543   auto Pair = DSAStack->getCriticalWithHint(DirName);
5544   if (Pair.first && DirName.getName() && !DependentHint) {
5545     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5546       Diag(StartLoc, diag::err_omp_critical_with_hint);
5547       if (HintLoc.isValid()) {
5548         Diag(HintLoc, diag::note_omp_critical_hint_here)
5549             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5550       } else
5551         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5552       if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5553         Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5554             << 1
5555             << C->getHint()->EvaluateKnownConstInt(Context).toString(
5556                    /*Radix=*/10, /*Signed=*/false);
5557       } else
5558         Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5559     }
5560   }
5561 
5562   setFunctionHasBranchProtectedScope();
5563 
5564   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5565                                            Clauses, AStmt);
5566   if (!Pair.first && DirName.getName() && !DependentHint)
5567     DSAStack->addCriticalWithHint(Dir, Hint);
5568   return Dir;
5569 }
5570 
5571 StmtResult Sema::ActOnOpenMPParallelForDirective(
5572     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5573     SourceLocation EndLoc,
5574     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5575   if (!AStmt)
5576     return StmtError();
5577 
5578   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5579   // 1.2.2 OpenMP Language Terminology
5580   // Structured block - An executable statement with a single entry at the
5581   // top and a single exit at the bottom.
5582   // The point of exit cannot be a branch out of the structured block.
5583   // longjmp() and throw() must not violate the entry/exit criteria.
5584   CS->getCapturedDecl()->setNothrow();
5585 
5586   OMPLoopDirective::HelperExprs B;
5587   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5588   // define the nested loops number.
5589   unsigned NestedLoopCount =
5590       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5591                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5592                       VarsWithImplicitDSA, B);
5593   if (NestedLoopCount == 0)
5594     return StmtError();
5595 
5596   assert((CurContext->isDependentContext() || B.builtAll()) &&
5597          "omp parallel for loop exprs were not built");
5598 
5599   if (!CurContext->isDependentContext()) {
5600     // Finalize the clauses that need pre-built expressions for CodeGen.
5601     for (auto C : Clauses) {
5602       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5603         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5604                                      B.NumIterations, *this, CurScope,
5605                                      DSAStack))
5606           return StmtError();
5607     }
5608   }
5609 
5610   setFunctionHasBranchProtectedScope();
5611   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5612                                          NestedLoopCount, Clauses, AStmt, B,
5613                                          DSAStack->isCancelRegion());
5614 }
5615 
5616 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5617     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5618     SourceLocation EndLoc,
5619     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5620   if (!AStmt)
5621     return StmtError();
5622 
5623   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5624   // 1.2.2 OpenMP Language Terminology
5625   // Structured block - An executable statement with a single entry at the
5626   // top and a single exit at the bottom.
5627   // The point of exit cannot be a branch out of the structured block.
5628   // longjmp() and throw() must not violate the entry/exit criteria.
5629   CS->getCapturedDecl()->setNothrow();
5630 
5631   OMPLoopDirective::HelperExprs B;
5632   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5633   // define the nested loops number.
5634   unsigned NestedLoopCount =
5635       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5636                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5637                       VarsWithImplicitDSA, B);
5638   if (NestedLoopCount == 0)
5639     return StmtError();
5640 
5641   if (!CurContext->isDependentContext()) {
5642     // Finalize the clauses that need pre-built expressions for CodeGen.
5643     for (auto C : Clauses) {
5644       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5645         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5646                                      B.NumIterations, *this, CurScope,
5647                                      DSAStack))
5648           return StmtError();
5649     }
5650   }
5651 
5652   if (checkSimdlenSafelenSpecified(*this, Clauses))
5653     return StmtError();
5654 
5655   setFunctionHasBranchProtectedScope();
5656   return OMPParallelForSimdDirective::Create(
5657       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5658 }
5659 
5660 StmtResult
5661 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5662                                            Stmt *AStmt, SourceLocation StartLoc,
5663                                            SourceLocation EndLoc) {
5664   if (!AStmt)
5665     return StmtError();
5666 
5667   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5668   auto BaseStmt = AStmt;
5669   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5670     BaseStmt = CS->getCapturedStmt();
5671   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5672     auto S = C->children();
5673     if (S.begin() == S.end())
5674       return StmtError();
5675     // All associated statements must be '#pragma omp section' except for
5676     // the first one.
5677     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5678       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5679         if (SectionStmt)
5680           Diag(SectionStmt->getLocStart(),
5681                diag::err_omp_parallel_sections_substmt_not_section);
5682         return StmtError();
5683       }
5684       cast<OMPSectionDirective>(SectionStmt)
5685           ->setHasCancel(DSAStack->isCancelRegion());
5686     }
5687   } else {
5688     Diag(AStmt->getLocStart(),
5689          diag::err_omp_parallel_sections_not_compound_stmt);
5690     return StmtError();
5691   }
5692 
5693   setFunctionHasBranchProtectedScope();
5694 
5695   return OMPParallelSectionsDirective::Create(
5696       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
5697 }
5698 
5699 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5700                                           Stmt *AStmt, SourceLocation StartLoc,
5701                                           SourceLocation EndLoc) {
5702   if (!AStmt)
5703     return StmtError();
5704 
5705   auto *CS = cast<CapturedStmt>(AStmt);
5706   // 1.2.2 OpenMP Language Terminology
5707   // Structured block - An executable statement with a single entry at the
5708   // top and a single exit at the bottom.
5709   // The point of exit cannot be a branch out of the structured block.
5710   // longjmp() and throw() must not violate the entry/exit criteria.
5711   CS->getCapturedDecl()->setNothrow();
5712 
5713   setFunctionHasBranchProtectedScope();
5714 
5715   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5716                                   DSAStack->isCancelRegion());
5717 }
5718 
5719 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5720                                                SourceLocation EndLoc) {
5721   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5722 }
5723 
5724 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5725                                              SourceLocation EndLoc) {
5726   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5727 }
5728 
5729 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5730                                               SourceLocation EndLoc) {
5731   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5732 }
5733 
5734 StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5735                                                Stmt *AStmt,
5736                                                SourceLocation StartLoc,
5737                                                SourceLocation EndLoc) {
5738   if (!AStmt)
5739     return StmtError();
5740 
5741   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5742 
5743   setFunctionHasBranchProtectedScope();
5744 
5745   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
5746                                        AStmt,
5747                                        DSAStack->getTaskgroupReductionRef());
5748 }
5749 
5750 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5751                                            SourceLocation StartLoc,
5752                                            SourceLocation EndLoc) {
5753   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5754   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5755 }
5756 
5757 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5758                                              Stmt *AStmt,
5759                                              SourceLocation StartLoc,
5760                                              SourceLocation EndLoc) {
5761   OMPClause *DependFound = nullptr;
5762   OMPClause *DependSourceClause = nullptr;
5763   OMPClause *DependSinkClause = nullptr;
5764   bool ErrorFound = false;
5765   OMPThreadsClause *TC = nullptr;
5766   OMPSIMDClause *SC = nullptr;
5767   for (auto *C : Clauses) {
5768     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5769       DependFound = C;
5770       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5771         if (DependSourceClause) {
5772           Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5773               << getOpenMPDirectiveName(OMPD_ordered)
5774               << getOpenMPClauseName(OMPC_depend) << 2;
5775           ErrorFound = true;
5776         } else
5777           DependSourceClause = C;
5778         if (DependSinkClause) {
5779           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5780               << 0;
5781           ErrorFound = true;
5782         }
5783       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5784         if (DependSourceClause) {
5785           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5786               << 1;
5787           ErrorFound = true;
5788         }
5789         DependSinkClause = C;
5790       }
5791     } else if (C->getClauseKind() == OMPC_threads)
5792       TC = cast<OMPThreadsClause>(C);
5793     else if (C->getClauseKind() == OMPC_simd)
5794       SC = cast<OMPSIMDClause>(C);
5795   }
5796   if (!ErrorFound && !SC &&
5797       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
5798     // OpenMP [2.8.1,simd Construct, Restrictions]
5799     // An ordered construct with the simd clause is the only OpenMP construct
5800     // that can appear in the simd region.
5801     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5802     ErrorFound = true;
5803   } else if (DependFound && (TC || SC)) {
5804     Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5805         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5806     ErrorFound = true;
5807   } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5808     Diag(DependFound->getLocStart(),
5809          diag::err_omp_ordered_directive_without_param);
5810     ErrorFound = true;
5811   } else if (TC || Clauses.empty()) {
5812     if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5813       SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5814       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5815           << (TC != nullptr);
5816       Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5817       ErrorFound = true;
5818     }
5819   }
5820   if ((!AStmt && !DependFound) || ErrorFound)
5821     return StmtError();
5822 
5823   if (AStmt) {
5824     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5825 
5826     setFunctionHasBranchProtectedScope();
5827   }
5828 
5829   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5830 }
5831 
5832 namespace {
5833 /// \brief Helper class for checking expression in 'omp atomic [update]'
5834 /// construct.
5835 class OpenMPAtomicUpdateChecker {
5836   /// \brief Error results for atomic update expressions.
5837   enum ExprAnalysisErrorCode {
5838     /// \brief A statement is not an expression statement.
5839     NotAnExpression,
5840     /// \brief Expression is not builtin binary or unary operation.
5841     NotABinaryOrUnaryExpression,
5842     /// \brief Unary operation is not post-/pre- increment/decrement operation.
5843     NotAnUnaryIncDecExpression,
5844     /// \brief An expression is not of scalar type.
5845     NotAScalarType,
5846     /// \brief A binary operation is not an assignment operation.
5847     NotAnAssignmentOp,
5848     /// \brief RHS part of the binary operation is not a binary expression.
5849     NotABinaryExpression,
5850     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5851     /// expression.
5852     NotABinaryOperator,
5853     /// \brief RHS binary operation does not have reference to the updated LHS
5854     /// part.
5855     NotAnUpdateExpression,
5856     /// \brief No errors is found.
5857     NoError
5858   };
5859   /// \brief Reference to Sema.
5860   Sema &SemaRef;
5861   /// \brief A location for note diagnostics (when error is found).
5862   SourceLocation NoteLoc;
5863   /// \brief 'x' lvalue part of the source atomic expression.
5864   Expr *X;
5865   /// \brief 'expr' rvalue part of the source atomic expression.
5866   Expr *E;
5867   /// \brief Helper expression of the form
5868   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5869   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5870   Expr *UpdateExpr;
5871   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5872   /// important for non-associative operations.
5873   bool IsXLHSInRHSPart;
5874   BinaryOperatorKind Op;
5875   SourceLocation OpLoc;
5876   /// \brief true if the source expression is a postfix unary operation, false
5877   /// if it is a prefix unary operation.
5878   bool IsPostfixUpdate;
5879 
5880 public:
5881   OpenMPAtomicUpdateChecker(Sema &SemaRef)
5882       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
5883         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
5884   /// \brief Check specified statement that it is suitable for 'atomic update'
5885   /// constructs and extract 'x', 'expr' and Operation from the original
5886   /// expression. If DiagId and NoteId == 0, then only check is performed
5887   /// without error notification.
5888   /// \param DiagId Diagnostic which should be emitted if error is found.
5889   /// \param NoteId Diagnostic note for the main error message.
5890   /// \return true if statement is not an update expression, false otherwise.
5891   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
5892   /// \brief Return the 'x' lvalue part of the source atomic expression.
5893   Expr *getX() const { return X; }
5894   /// \brief Return the 'expr' rvalue part of the source atomic expression.
5895   Expr *getExpr() const { return E; }
5896   /// \brief Return the update expression used in calculation of the updated
5897   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5898   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5899   Expr *getUpdateExpr() const { return UpdateExpr; }
5900   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5901   /// false otherwise.
5902   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5903 
5904   /// \brief true if the source expression is a postfix unary operation, false
5905   /// if it is a prefix unary operation.
5906   bool isPostfixUpdate() const { return IsPostfixUpdate; }
5907 
5908 private:
5909   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5910                             unsigned NoteId = 0);
5911 };
5912 } // namespace
5913 
5914 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5915     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5916   ExprAnalysisErrorCode ErrorFound = NoError;
5917   SourceLocation ErrorLoc, NoteLoc;
5918   SourceRange ErrorRange, NoteRange;
5919   // Allowed constructs are:
5920   //  x = x binop expr;
5921   //  x = expr binop x;
5922   if (AtomicBinOp->getOpcode() == BO_Assign) {
5923     X = AtomicBinOp->getLHS();
5924     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5925             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5926       if (AtomicInnerBinOp->isMultiplicativeOp() ||
5927           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5928           AtomicInnerBinOp->isBitwiseOp()) {
5929         Op = AtomicInnerBinOp->getOpcode();
5930         OpLoc = AtomicInnerBinOp->getOperatorLoc();
5931         auto *LHS = AtomicInnerBinOp->getLHS();
5932         auto *RHS = AtomicInnerBinOp->getRHS();
5933         llvm::FoldingSetNodeID XId, LHSId, RHSId;
5934         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5935                                           /*Canonical=*/true);
5936         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5937                                             /*Canonical=*/true);
5938         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5939                                             /*Canonical=*/true);
5940         if (XId == LHSId) {
5941           E = RHS;
5942           IsXLHSInRHSPart = true;
5943         } else if (XId == RHSId) {
5944           E = LHS;
5945           IsXLHSInRHSPart = false;
5946         } else {
5947           ErrorLoc = AtomicInnerBinOp->getExprLoc();
5948           ErrorRange = AtomicInnerBinOp->getSourceRange();
5949           NoteLoc = X->getExprLoc();
5950           NoteRange = X->getSourceRange();
5951           ErrorFound = NotAnUpdateExpression;
5952         }
5953       } else {
5954         ErrorLoc = AtomicInnerBinOp->getExprLoc();
5955         ErrorRange = AtomicInnerBinOp->getSourceRange();
5956         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5957         NoteRange = SourceRange(NoteLoc, NoteLoc);
5958         ErrorFound = NotABinaryOperator;
5959       }
5960     } else {
5961       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5962       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5963       ErrorFound = NotABinaryExpression;
5964     }
5965   } else {
5966     ErrorLoc = AtomicBinOp->getExprLoc();
5967     ErrorRange = AtomicBinOp->getSourceRange();
5968     NoteLoc = AtomicBinOp->getOperatorLoc();
5969     NoteRange = SourceRange(NoteLoc, NoteLoc);
5970     ErrorFound = NotAnAssignmentOp;
5971   }
5972   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5973     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5974     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5975     return true;
5976   } else if (SemaRef.CurContext->isDependentContext())
5977     E = X = UpdateExpr = nullptr;
5978   return ErrorFound != NoError;
5979 }
5980 
5981 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5982                                                unsigned NoteId) {
5983   ExprAnalysisErrorCode ErrorFound = NoError;
5984   SourceLocation ErrorLoc, NoteLoc;
5985   SourceRange ErrorRange, NoteRange;
5986   // Allowed constructs are:
5987   //  x++;
5988   //  x--;
5989   //  ++x;
5990   //  --x;
5991   //  x binop= expr;
5992   //  x = x binop expr;
5993   //  x = expr binop x;
5994   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5995     AtomicBody = AtomicBody->IgnoreParenImpCasts();
5996     if (AtomicBody->getType()->isScalarType() ||
5997         AtomicBody->isInstantiationDependent()) {
5998       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5999               AtomicBody->IgnoreParenImpCasts())) {
6000         // Check for Compound Assignment Operation
6001         Op = BinaryOperator::getOpForCompoundAssignment(
6002             AtomicCompAssignOp->getOpcode());
6003         OpLoc = AtomicCompAssignOp->getOperatorLoc();
6004         E = AtomicCompAssignOp->getRHS();
6005         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
6006         IsXLHSInRHSPart = true;
6007       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6008                      AtomicBody->IgnoreParenImpCasts())) {
6009         // Check for Binary Operation
6010         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6011           return true;
6012       } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
6013                      AtomicBody->IgnoreParenImpCasts())) {
6014         // Check for Unary Operation
6015         if (AtomicUnaryOp->isIncrementDecrementOp()) {
6016           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
6017           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6018           OpLoc = AtomicUnaryOp->getOperatorLoc();
6019           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
6020           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6021           IsXLHSInRHSPart = true;
6022         } else {
6023           ErrorFound = NotAnUnaryIncDecExpression;
6024           ErrorLoc = AtomicUnaryOp->getExprLoc();
6025           ErrorRange = AtomicUnaryOp->getSourceRange();
6026           NoteLoc = AtomicUnaryOp->getOperatorLoc();
6027           NoteRange = SourceRange(NoteLoc, NoteLoc);
6028         }
6029       } else if (!AtomicBody->isInstantiationDependent()) {
6030         ErrorFound = NotABinaryOrUnaryExpression;
6031         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6032         NoteRange = ErrorRange = AtomicBody->getSourceRange();
6033       }
6034     } else {
6035       ErrorFound = NotAScalarType;
6036       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6037       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6038     }
6039   } else {
6040     ErrorFound = NotAnExpression;
6041     NoteLoc = ErrorLoc = S->getLocStart();
6042     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6043   }
6044   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6045     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6046     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6047     return true;
6048   } else if (SemaRef.CurContext->isDependentContext())
6049     E = X = UpdateExpr = nullptr;
6050   if (ErrorFound == NoError && E && X) {
6051     // Build an update expression of form 'OpaqueValueExpr(x) binop
6052     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6053     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6054     auto *OVEX = new (SemaRef.getASTContext())
6055         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6056     auto *OVEExpr = new (SemaRef.getASTContext())
6057         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6058     auto Update =
6059         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6060                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
6061     if (Update.isInvalid())
6062       return true;
6063     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6064                                                Sema::AA_Casting);
6065     if (Update.isInvalid())
6066       return true;
6067     UpdateExpr = Update.get();
6068   }
6069   return ErrorFound != NoError;
6070 }
6071 
6072 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6073                                             Stmt *AStmt,
6074                                             SourceLocation StartLoc,
6075                                             SourceLocation EndLoc) {
6076   if (!AStmt)
6077     return StmtError();
6078 
6079   auto *CS = cast<CapturedStmt>(AStmt);
6080   // 1.2.2 OpenMP Language Terminology
6081   // Structured block - An executable statement with a single entry at the
6082   // top and a single exit at the bottom.
6083   // The point of exit cannot be a branch out of the structured block.
6084   // longjmp() and throw() must not violate the entry/exit criteria.
6085   OpenMPClauseKind AtomicKind = OMPC_unknown;
6086   SourceLocation AtomicKindLoc;
6087   for (auto *C : Clauses) {
6088     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
6089         C->getClauseKind() == OMPC_update ||
6090         C->getClauseKind() == OMPC_capture) {
6091       if (AtomicKind != OMPC_unknown) {
6092         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6093             << SourceRange(C->getLocStart(), C->getLocEnd());
6094         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6095             << getOpenMPClauseName(AtomicKind);
6096       } else {
6097         AtomicKind = C->getClauseKind();
6098         AtomicKindLoc = C->getLocStart();
6099       }
6100     }
6101   }
6102 
6103   auto Body = CS->getCapturedStmt();
6104   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6105     Body = EWC->getSubExpr();
6106 
6107   Expr *X = nullptr;
6108   Expr *V = nullptr;
6109   Expr *E = nullptr;
6110   Expr *UE = nullptr;
6111   bool IsXLHSInRHSPart = false;
6112   bool IsPostfixUpdate = false;
6113   // OpenMP [2.12.6, atomic Construct]
6114   // In the next expressions:
6115   // * x and v (as applicable) are both l-value expressions with scalar type.
6116   // * During the execution of an atomic region, multiple syntactic
6117   // occurrences of x must designate the same storage location.
6118   // * Neither of v and expr (as applicable) may access the storage location
6119   // designated by x.
6120   // * Neither of x and expr (as applicable) may access the storage location
6121   // designated by v.
6122   // * expr is an expression with scalar type.
6123   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6124   // * binop, binop=, ++, and -- are not overloaded operators.
6125   // * The expression x binop expr must be numerically equivalent to x binop
6126   // (expr). This requirement is satisfied if the operators in expr have
6127   // precedence greater than binop, or by using parentheses around expr or
6128   // subexpressions of expr.
6129   // * The expression expr binop x must be numerically equivalent to (expr)
6130   // binop x. This requirement is satisfied if the operators in expr have
6131   // precedence equal to or greater than binop, or by using parentheses around
6132   // expr or subexpressions of expr.
6133   // * For forms that allow multiple occurrences of x, the number of times
6134   // that x is evaluated is unspecified.
6135   if (AtomicKind == OMPC_read) {
6136     enum {
6137       NotAnExpression,
6138       NotAnAssignmentOp,
6139       NotAScalarType,
6140       NotAnLValue,
6141       NoError
6142     } ErrorFound = NoError;
6143     SourceLocation ErrorLoc, NoteLoc;
6144     SourceRange ErrorRange, NoteRange;
6145     // If clause is read:
6146     //  v = x;
6147     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6148       auto *AtomicBinOp =
6149           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6150       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6151         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6152         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6153         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6154             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6155           if (!X->isLValue() || !V->isLValue()) {
6156             auto NotLValueExpr = X->isLValue() ? V : X;
6157             ErrorFound = NotAnLValue;
6158             ErrorLoc = AtomicBinOp->getExprLoc();
6159             ErrorRange = AtomicBinOp->getSourceRange();
6160             NoteLoc = NotLValueExpr->getExprLoc();
6161             NoteRange = NotLValueExpr->getSourceRange();
6162           }
6163         } else if (!X->isInstantiationDependent() ||
6164                    !V->isInstantiationDependent()) {
6165           auto NotScalarExpr =
6166               (X->isInstantiationDependent() || X->getType()->isScalarType())
6167                   ? V
6168                   : X;
6169           ErrorFound = NotAScalarType;
6170           ErrorLoc = AtomicBinOp->getExprLoc();
6171           ErrorRange = AtomicBinOp->getSourceRange();
6172           NoteLoc = NotScalarExpr->getExprLoc();
6173           NoteRange = NotScalarExpr->getSourceRange();
6174         }
6175       } else if (!AtomicBody->isInstantiationDependent()) {
6176         ErrorFound = NotAnAssignmentOp;
6177         ErrorLoc = AtomicBody->getExprLoc();
6178         ErrorRange = AtomicBody->getSourceRange();
6179         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6180                               : AtomicBody->getExprLoc();
6181         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6182                                 : AtomicBody->getSourceRange();
6183       }
6184     } else {
6185       ErrorFound = NotAnExpression;
6186       NoteLoc = ErrorLoc = Body->getLocStart();
6187       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6188     }
6189     if (ErrorFound != NoError) {
6190       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6191           << ErrorRange;
6192       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6193                                                       << NoteRange;
6194       return StmtError();
6195     } else if (CurContext->isDependentContext())
6196       V = X = nullptr;
6197   } else if (AtomicKind == OMPC_write) {
6198     enum {
6199       NotAnExpression,
6200       NotAnAssignmentOp,
6201       NotAScalarType,
6202       NotAnLValue,
6203       NoError
6204     } ErrorFound = NoError;
6205     SourceLocation ErrorLoc, NoteLoc;
6206     SourceRange ErrorRange, NoteRange;
6207     // If clause is write:
6208     //  x = expr;
6209     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6210       auto *AtomicBinOp =
6211           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6212       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6213         X = AtomicBinOp->getLHS();
6214         E = AtomicBinOp->getRHS();
6215         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6216             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6217           if (!X->isLValue()) {
6218             ErrorFound = NotAnLValue;
6219             ErrorLoc = AtomicBinOp->getExprLoc();
6220             ErrorRange = AtomicBinOp->getSourceRange();
6221             NoteLoc = X->getExprLoc();
6222             NoteRange = X->getSourceRange();
6223           }
6224         } else if (!X->isInstantiationDependent() ||
6225                    !E->isInstantiationDependent()) {
6226           auto NotScalarExpr =
6227               (X->isInstantiationDependent() || X->getType()->isScalarType())
6228                   ? E
6229                   : X;
6230           ErrorFound = NotAScalarType;
6231           ErrorLoc = AtomicBinOp->getExprLoc();
6232           ErrorRange = AtomicBinOp->getSourceRange();
6233           NoteLoc = NotScalarExpr->getExprLoc();
6234           NoteRange = NotScalarExpr->getSourceRange();
6235         }
6236       } else if (!AtomicBody->isInstantiationDependent()) {
6237         ErrorFound = NotAnAssignmentOp;
6238         ErrorLoc = AtomicBody->getExprLoc();
6239         ErrorRange = AtomicBody->getSourceRange();
6240         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6241                               : AtomicBody->getExprLoc();
6242         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6243                                 : AtomicBody->getSourceRange();
6244       }
6245     } else {
6246       ErrorFound = NotAnExpression;
6247       NoteLoc = ErrorLoc = Body->getLocStart();
6248       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6249     }
6250     if (ErrorFound != NoError) {
6251       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6252           << ErrorRange;
6253       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6254                                                       << NoteRange;
6255       return StmtError();
6256     } else if (CurContext->isDependentContext())
6257       E = X = nullptr;
6258   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
6259     // If clause is update:
6260     //  x++;
6261     //  x--;
6262     //  ++x;
6263     //  --x;
6264     //  x binop= expr;
6265     //  x = x binop expr;
6266     //  x = expr binop x;
6267     OpenMPAtomicUpdateChecker Checker(*this);
6268     if (Checker.checkStatement(
6269             Body, (AtomicKind == OMPC_update)
6270                       ? diag::err_omp_atomic_update_not_expression_statement
6271                       : diag::err_omp_atomic_not_expression_statement,
6272             diag::note_omp_atomic_update))
6273       return StmtError();
6274     if (!CurContext->isDependentContext()) {
6275       E = Checker.getExpr();
6276       X = Checker.getX();
6277       UE = Checker.getUpdateExpr();
6278       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6279     }
6280   } else if (AtomicKind == OMPC_capture) {
6281     enum {
6282       NotAnAssignmentOp,
6283       NotACompoundStatement,
6284       NotTwoSubstatements,
6285       NotASpecificExpression,
6286       NoError
6287     } ErrorFound = NoError;
6288     SourceLocation ErrorLoc, NoteLoc;
6289     SourceRange ErrorRange, NoteRange;
6290     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6291       // If clause is a capture:
6292       //  v = x++;
6293       //  v = x--;
6294       //  v = ++x;
6295       //  v = --x;
6296       //  v = x binop= expr;
6297       //  v = x = x binop expr;
6298       //  v = x = expr binop x;
6299       auto *AtomicBinOp =
6300           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6301       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6302         V = AtomicBinOp->getLHS();
6303         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6304         OpenMPAtomicUpdateChecker Checker(*this);
6305         if (Checker.checkStatement(
6306                 Body, diag::err_omp_atomic_capture_not_expression_statement,
6307                 diag::note_omp_atomic_update))
6308           return StmtError();
6309         E = Checker.getExpr();
6310         X = Checker.getX();
6311         UE = Checker.getUpdateExpr();
6312         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6313         IsPostfixUpdate = Checker.isPostfixUpdate();
6314       } else if (!AtomicBody->isInstantiationDependent()) {
6315         ErrorLoc = AtomicBody->getExprLoc();
6316         ErrorRange = AtomicBody->getSourceRange();
6317         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6318                               : AtomicBody->getExprLoc();
6319         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6320                                 : AtomicBody->getSourceRange();
6321         ErrorFound = NotAnAssignmentOp;
6322       }
6323       if (ErrorFound != NoError) {
6324         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6325             << ErrorRange;
6326         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6327         return StmtError();
6328       } else if (CurContext->isDependentContext()) {
6329         UE = V = E = X = nullptr;
6330       }
6331     } else {
6332       // If clause is a capture:
6333       //  { v = x; x = expr; }
6334       //  { v = x; x++; }
6335       //  { v = x; x--; }
6336       //  { v = x; ++x; }
6337       //  { v = x; --x; }
6338       //  { v = x; x binop= expr; }
6339       //  { v = x; x = x binop expr; }
6340       //  { v = x; x = expr binop x; }
6341       //  { x++; v = x; }
6342       //  { x--; v = x; }
6343       //  { ++x; v = x; }
6344       //  { --x; v = x; }
6345       //  { x binop= expr; v = x; }
6346       //  { x = x binop expr; v = x; }
6347       //  { x = expr binop x; v = x; }
6348       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6349         // Check that this is { expr1; expr2; }
6350         if (CS->size() == 2) {
6351           auto *First = CS->body_front();
6352           auto *Second = CS->body_back();
6353           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6354             First = EWC->getSubExpr()->IgnoreParenImpCasts();
6355           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6356             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6357           // Need to find what subexpression is 'v' and what is 'x'.
6358           OpenMPAtomicUpdateChecker Checker(*this);
6359           bool IsUpdateExprFound = !Checker.checkStatement(Second);
6360           BinaryOperator *BinOp = nullptr;
6361           if (IsUpdateExprFound) {
6362             BinOp = dyn_cast<BinaryOperator>(First);
6363             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6364           }
6365           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6366             //  { v = x; x++; }
6367             //  { v = x; x--; }
6368             //  { v = x; ++x; }
6369             //  { v = x; --x; }
6370             //  { v = x; x binop= expr; }
6371             //  { v = x; x = x binop expr; }
6372             //  { v = x; x = expr binop x; }
6373             // Check that the first expression has form v = x.
6374             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6375             llvm::FoldingSetNodeID XId, PossibleXId;
6376             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6377             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6378             IsUpdateExprFound = XId == PossibleXId;
6379             if (IsUpdateExprFound) {
6380               V = BinOp->getLHS();
6381               X = Checker.getX();
6382               E = Checker.getExpr();
6383               UE = Checker.getUpdateExpr();
6384               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6385               IsPostfixUpdate = true;
6386             }
6387           }
6388           if (!IsUpdateExprFound) {
6389             IsUpdateExprFound = !Checker.checkStatement(First);
6390             BinOp = nullptr;
6391             if (IsUpdateExprFound) {
6392               BinOp = dyn_cast<BinaryOperator>(Second);
6393               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6394             }
6395             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6396               //  { x++; v = x; }
6397               //  { x--; v = x; }
6398               //  { ++x; v = x; }
6399               //  { --x; v = x; }
6400               //  { x binop= expr; v = x; }
6401               //  { x = x binop expr; v = x; }
6402               //  { x = expr binop x; v = x; }
6403               // Check that the second expression has form v = x.
6404               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6405               llvm::FoldingSetNodeID XId, PossibleXId;
6406               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6407               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6408               IsUpdateExprFound = XId == PossibleXId;
6409               if (IsUpdateExprFound) {
6410                 V = BinOp->getLHS();
6411                 X = Checker.getX();
6412                 E = Checker.getExpr();
6413                 UE = Checker.getUpdateExpr();
6414                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6415                 IsPostfixUpdate = false;
6416               }
6417             }
6418           }
6419           if (!IsUpdateExprFound) {
6420             //  { v = x; x = expr; }
6421             auto *FirstExpr = dyn_cast<Expr>(First);
6422             auto *SecondExpr = dyn_cast<Expr>(Second);
6423             if (!FirstExpr || !SecondExpr ||
6424                 !(FirstExpr->isInstantiationDependent() ||
6425                   SecondExpr->isInstantiationDependent())) {
6426               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6427               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
6428                 ErrorFound = NotAnAssignmentOp;
6429                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6430                                                 : First->getLocStart();
6431                 NoteRange = ErrorRange = FirstBinOp
6432                                              ? FirstBinOp->getSourceRange()
6433                                              : SourceRange(ErrorLoc, ErrorLoc);
6434               } else {
6435                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6436                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6437                   ErrorFound = NotAnAssignmentOp;
6438                   NoteLoc = ErrorLoc = SecondBinOp
6439                                            ? SecondBinOp->getOperatorLoc()
6440                                            : Second->getLocStart();
6441                   NoteRange = ErrorRange =
6442                       SecondBinOp ? SecondBinOp->getSourceRange()
6443                                   : SourceRange(ErrorLoc, ErrorLoc);
6444                 } else {
6445                   auto *PossibleXRHSInFirst =
6446                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
6447                   auto *PossibleXLHSInSecond =
6448                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
6449                   llvm::FoldingSetNodeID X1Id, X2Id;
6450                   PossibleXRHSInFirst->Profile(X1Id, Context,
6451                                                /*Canonical=*/true);
6452                   PossibleXLHSInSecond->Profile(X2Id, Context,
6453                                                 /*Canonical=*/true);
6454                   IsUpdateExprFound = X1Id == X2Id;
6455                   if (IsUpdateExprFound) {
6456                     V = FirstBinOp->getLHS();
6457                     X = SecondBinOp->getLHS();
6458                     E = SecondBinOp->getRHS();
6459                     UE = nullptr;
6460                     IsXLHSInRHSPart = false;
6461                     IsPostfixUpdate = true;
6462                   } else {
6463                     ErrorFound = NotASpecificExpression;
6464                     ErrorLoc = FirstBinOp->getExprLoc();
6465                     ErrorRange = FirstBinOp->getSourceRange();
6466                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6467                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
6468                   }
6469                 }
6470               }
6471             }
6472           }
6473         } else {
6474           NoteLoc = ErrorLoc = Body->getLocStart();
6475           NoteRange = ErrorRange =
6476               SourceRange(Body->getLocStart(), Body->getLocStart());
6477           ErrorFound = NotTwoSubstatements;
6478         }
6479       } else {
6480         NoteLoc = ErrorLoc = Body->getLocStart();
6481         NoteRange = ErrorRange =
6482             SourceRange(Body->getLocStart(), Body->getLocStart());
6483         ErrorFound = NotACompoundStatement;
6484       }
6485       if (ErrorFound != NoError) {
6486         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6487             << ErrorRange;
6488         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6489         return StmtError();
6490       } else if (CurContext->isDependentContext()) {
6491         UE = V = E = X = nullptr;
6492       }
6493     }
6494   }
6495 
6496   setFunctionHasBranchProtectedScope();
6497 
6498   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6499                                     X, V, E, UE, IsXLHSInRHSPart,
6500                                     IsPostfixUpdate);
6501 }
6502 
6503 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6504                                             Stmt *AStmt,
6505                                             SourceLocation StartLoc,
6506                                             SourceLocation EndLoc) {
6507   if (!AStmt)
6508     return StmtError();
6509 
6510   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6511   // 1.2.2 OpenMP Language Terminology
6512   // Structured block - An executable statement with a single entry at the
6513   // top and a single exit at the bottom.
6514   // The point of exit cannot be a branch out of the structured block.
6515   // longjmp() and throw() must not violate the entry/exit criteria.
6516   CS->getCapturedDecl()->setNothrow();
6517   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6518        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6519     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6520     // 1.2.2 OpenMP Language Terminology
6521     // Structured block - An executable statement with a single entry at the
6522     // top and a single exit at the bottom.
6523     // The point of exit cannot be a branch out of the structured block.
6524     // longjmp() and throw() must not violate the entry/exit criteria.
6525     CS->getCapturedDecl()->setNothrow();
6526   }
6527 
6528   // OpenMP [2.16, Nesting of Regions]
6529   // If specified, a teams construct must be contained within a target
6530   // construct. That target construct must contain no statements or directives
6531   // outside of the teams construct.
6532   if (DSAStack->hasInnerTeamsRegion()) {
6533     Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
6534     bool OMPTeamsFound = true;
6535     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6536       auto I = CS->body_begin();
6537       while (I != CS->body_end()) {
6538         auto *OED = dyn_cast<OMPExecutableDirective>(*I);
6539         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6540           OMPTeamsFound = false;
6541           break;
6542         }
6543         ++I;
6544       }
6545       assert(I != CS->body_end() && "Not found statement");
6546       S = *I;
6547     } else {
6548       auto *OED = dyn_cast<OMPExecutableDirective>(S);
6549       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
6550     }
6551     if (!OMPTeamsFound) {
6552       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6553       Diag(DSAStack->getInnerTeamsRegionLoc(),
6554            diag::note_omp_nested_teams_construct_here);
6555       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6556           << isa<OMPExecutableDirective>(S);
6557       return StmtError();
6558     }
6559   }
6560 
6561   setFunctionHasBranchProtectedScope();
6562 
6563   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6564 }
6565 
6566 StmtResult
6567 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6568                                          Stmt *AStmt, SourceLocation StartLoc,
6569                                          SourceLocation EndLoc) {
6570   if (!AStmt)
6571     return StmtError();
6572 
6573   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6574   // 1.2.2 OpenMP Language Terminology
6575   // Structured block - An executable statement with a single entry at the
6576   // top and a single exit at the bottom.
6577   // The point of exit cannot be a branch out of the structured block.
6578   // longjmp() and throw() must not violate the entry/exit criteria.
6579   CS->getCapturedDecl()->setNothrow();
6580   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6581        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6582     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6583     // 1.2.2 OpenMP Language Terminology
6584     // Structured block - An executable statement with a single entry at the
6585     // top and a single exit at the bottom.
6586     // The point of exit cannot be a branch out of the structured block.
6587     // longjmp() and throw() must not violate the entry/exit criteria.
6588     CS->getCapturedDecl()->setNothrow();
6589   }
6590 
6591   setFunctionHasBranchProtectedScope();
6592 
6593   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6594                                             AStmt);
6595 }
6596 
6597 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6598     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6599     SourceLocation EndLoc,
6600     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6601   if (!AStmt)
6602     return StmtError();
6603 
6604   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6605   // 1.2.2 OpenMP Language Terminology
6606   // Structured block - An executable statement with a single entry at the
6607   // top and a single exit at the bottom.
6608   // The point of exit cannot be a branch out of the structured block.
6609   // longjmp() and throw() must not violate the entry/exit criteria.
6610   CS->getCapturedDecl()->setNothrow();
6611   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6612        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6613     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6614     // 1.2.2 OpenMP Language Terminology
6615     // Structured block - An executable statement with a single entry at the
6616     // top and a single exit at the bottom.
6617     // The point of exit cannot be a branch out of the structured block.
6618     // longjmp() and throw() must not violate the entry/exit criteria.
6619     CS->getCapturedDecl()->setNothrow();
6620   }
6621 
6622   OMPLoopDirective::HelperExprs B;
6623   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6624   // define the nested loops number.
6625   unsigned NestedLoopCount =
6626       CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6627                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
6628                       VarsWithImplicitDSA, B);
6629   if (NestedLoopCount == 0)
6630     return StmtError();
6631 
6632   assert((CurContext->isDependentContext() || B.builtAll()) &&
6633          "omp target parallel for loop exprs were not built");
6634 
6635   if (!CurContext->isDependentContext()) {
6636     // Finalize the clauses that need pre-built expressions for CodeGen.
6637     for (auto C : Clauses) {
6638       if (auto *LC = dyn_cast<OMPLinearClause>(C))
6639         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6640                                      B.NumIterations, *this, CurScope,
6641                                      DSAStack))
6642           return StmtError();
6643     }
6644   }
6645 
6646   setFunctionHasBranchProtectedScope();
6647   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6648                                                NestedLoopCount, Clauses, AStmt,
6649                                                B, DSAStack->isCancelRegion());
6650 }
6651 
6652 /// Check for existence of a map clause in the list of clauses.
6653 static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6654                        const OpenMPClauseKind K) {
6655   return llvm::any_of(
6656       Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6657 }
6658 
6659 template <typename... Params>
6660 static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6661                        const Params... ClauseTypes) {
6662   return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
6663 }
6664 
6665 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6666                                                 Stmt *AStmt,
6667                                                 SourceLocation StartLoc,
6668                                                 SourceLocation EndLoc) {
6669   if (!AStmt)
6670     return StmtError();
6671 
6672   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6673 
6674   // OpenMP [2.10.1, Restrictions, p. 97]
6675   // At least one map clause must appear on the directive.
6676   if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6677     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6678         << "'map' or 'use_device_ptr'"
6679         << getOpenMPDirectiveName(OMPD_target_data);
6680     return StmtError();
6681   }
6682 
6683   setFunctionHasBranchProtectedScope();
6684 
6685   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6686                                         AStmt);
6687 }
6688 
6689 StmtResult
6690 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6691                                           SourceLocation StartLoc,
6692                                           SourceLocation EndLoc, Stmt *AStmt) {
6693   if (!AStmt)
6694     return StmtError();
6695 
6696   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6697   // 1.2.2 OpenMP Language Terminology
6698   // Structured block - An executable statement with a single entry at the
6699   // top and a single exit at the bottom.
6700   // The point of exit cannot be a branch out of the structured block.
6701   // longjmp() and throw() must not violate the entry/exit criteria.
6702   CS->getCapturedDecl()->setNothrow();
6703   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6704        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6705     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6706     // 1.2.2 OpenMP Language Terminology
6707     // Structured block - An executable statement with a single entry at the
6708     // top and a single exit at the bottom.
6709     // The point of exit cannot be a branch out of the structured block.
6710     // longjmp() and throw() must not violate the entry/exit criteria.
6711     CS->getCapturedDecl()->setNothrow();
6712   }
6713 
6714   // OpenMP [2.10.2, Restrictions, p. 99]
6715   // At least one map clause must appear on the directive.
6716   if (!hasClauses(Clauses, OMPC_map)) {
6717     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6718         << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
6719     return StmtError();
6720   }
6721 
6722   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6723                                              AStmt);
6724 }
6725 
6726 StmtResult
6727 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6728                                          SourceLocation StartLoc,
6729                                          SourceLocation EndLoc, Stmt *AStmt) {
6730   if (!AStmt)
6731     return StmtError();
6732 
6733   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6734   // 1.2.2 OpenMP Language Terminology
6735   // Structured block - An executable statement with a single entry at the
6736   // top and a single exit at the bottom.
6737   // The point of exit cannot be a branch out of the structured block.
6738   // longjmp() and throw() must not violate the entry/exit criteria.
6739   CS->getCapturedDecl()->setNothrow();
6740   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6741        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6742     CS = cast<CapturedStmt>(CS->getCapturedStmt());
6743     // 1.2.2 OpenMP Language Terminology
6744     // Structured block - An executable statement with a single entry at the
6745     // top and a single exit at the bottom.
6746     // The point of exit cannot be a branch out of the structured block.
6747     // longjmp() and throw() must not violate the entry/exit criteria.
6748     CS->getCapturedDecl()->setNothrow();
6749   }
6750 
6751   // OpenMP [2.10.3, Restrictions, p. 102]
6752   // At least one map clause must appear on the directive.
6753   if (!hasClauses(Clauses, OMPC_map)) {
6754     Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6755         << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
6756     return StmtError();
6757   }
6758 
6759   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6760                                             AStmt);
6761 }
6762 
6763 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6764                                                   SourceLocation StartLoc,
6765                                                   SourceLocation EndLoc,
6766                                                   Stmt *AStmt) {
6767   if (!AStmt)
6768     return StmtError();
6769 
6770   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6771   // 1.2.2 OpenMP Language Terminology
6772   // Structured block - An executable statement with a single entry at the
6773   // top and a single exit at the bottom.
6774   // The point of exit cannot be a branch out of the structured block.
6775   // longjmp() and throw() must not violate the entry/exit criteria.
6776   CS->getCapturedDecl()->setNothrow();
6777   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6778        ThisCaptureLevel > 1; --ThisCaptureLevel) {
6779     CS = cast<CapturedStmt>(CS->getCapturedStmt());
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 
6788   if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
6789     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6790     return StmtError();
6791   }
6792   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
6793                                           AStmt);
6794 }
6795 
6796 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6797                                            Stmt *AStmt, SourceLocation StartLoc,
6798                                            SourceLocation EndLoc) {
6799   if (!AStmt)
6800     return StmtError();
6801 
6802   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6803   // 1.2.2 OpenMP Language Terminology
6804   // Structured block - An executable statement with a single entry at the
6805   // top and a single exit at the bottom.
6806   // The point of exit cannot be a branch out of the structured block.
6807   // longjmp() and throw() must not violate the entry/exit criteria.
6808   CS->getCapturedDecl()->setNothrow();
6809 
6810   setFunctionHasBranchProtectedScope();
6811 
6812   DSAStack->setParentTeamsRegionLoc(StartLoc);
6813 
6814   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6815 }
6816 
6817 StmtResult
6818 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6819                                             SourceLocation EndLoc,
6820                                             OpenMPDirectiveKind CancelRegion) {
6821   if (DSAStack->isParentNowaitRegion()) {
6822     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6823     return StmtError();
6824   }
6825   if (DSAStack->isParentOrderedRegion()) {
6826     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6827     return StmtError();
6828   }
6829   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6830                                                CancelRegion);
6831 }
6832 
6833 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6834                                             SourceLocation StartLoc,
6835                                             SourceLocation EndLoc,
6836                                             OpenMPDirectiveKind CancelRegion) {
6837   if (DSAStack->isParentNowaitRegion()) {
6838     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6839     return StmtError();
6840   }
6841   if (DSAStack->isParentOrderedRegion()) {
6842     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6843     return StmtError();
6844   }
6845   DSAStack->setParentCancelRegion(/*Cancel=*/true);
6846   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6847                                     CancelRegion);
6848 }
6849 
6850 static bool checkGrainsizeNumTasksClauses(Sema &S,
6851                                           ArrayRef<OMPClause *> Clauses) {
6852   OMPClause *PrevClause = nullptr;
6853   bool ErrorFound = false;
6854   for (auto *C : Clauses) {
6855     if (C->getClauseKind() == OMPC_grainsize ||
6856         C->getClauseKind() == OMPC_num_tasks) {
6857       if (!PrevClause)
6858         PrevClause = C;
6859       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6860         S.Diag(C->getLocStart(),
6861                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6862             << getOpenMPClauseName(C->getClauseKind())
6863             << getOpenMPClauseName(PrevClause->getClauseKind());
6864         S.Diag(PrevClause->getLocStart(),
6865                diag::note_omp_previous_grainsize_num_tasks)
6866             << getOpenMPClauseName(PrevClause->getClauseKind());
6867         ErrorFound = true;
6868       }
6869     }
6870   }
6871   return ErrorFound;
6872 }
6873 
6874 static bool checkReductionClauseWithNogroup(Sema &S,
6875                                             ArrayRef<OMPClause *> Clauses) {
6876   OMPClause *ReductionClause = nullptr;
6877   OMPClause *NogroupClause = nullptr;
6878   for (auto *C : Clauses) {
6879     if (C->getClauseKind() == OMPC_reduction) {
6880       ReductionClause = C;
6881       if (NogroupClause)
6882         break;
6883       continue;
6884     }
6885     if (C->getClauseKind() == OMPC_nogroup) {
6886       NogroupClause = C;
6887       if (ReductionClause)
6888         break;
6889       continue;
6890     }
6891   }
6892   if (ReductionClause && NogroupClause) {
6893     S.Diag(ReductionClause->getLocStart(), diag::err_omp_reduction_with_nogroup)
6894         << SourceRange(NogroupClause->getLocStart(),
6895                        NogroupClause->getLocEnd());
6896     return true;
6897   }
6898   return false;
6899 }
6900 
6901 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6902     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6903     SourceLocation EndLoc,
6904     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6905   if (!AStmt)
6906     return StmtError();
6907 
6908   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6909   OMPLoopDirective::HelperExprs B;
6910   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6911   // define the nested loops number.
6912   unsigned NestedLoopCount =
6913       CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
6914                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6915                       VarsWithImplicitDSA, B);
6916   if (NestedLoopCount == 0)
6917     return StmtError();
6918 
6919   assert((CurContext->isDependentContext() || B.builtAll()) &&
6920          "omp for loop exprs were not built");
6921 
6922   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6923   // The grainsize clause and num_tasks clause are mutually exclusive and may
6924   // not appear on the same taskloop directive.
6925   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6926     return StmtError();
6927   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6928   // If a reduction clause is present on the taskloop directive, the nogroup
6929   // clause must not be specified.
6930   if (checkReductionClauseWithNogroup(*this, Clauses))
6931     return StmtError();
6932 
6933   setFunctionHasBranchProtectedScope();
6934   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6935                                       NestedLoopCount, Clauses, AStmt, B);
6936 }
6937 
6938 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6939     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6940     SourceLocation EndLoc,
6941     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6942   if (!AStmt)
6943     return StmtError();
6944 
6945   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6946   OMPLoopDirective::HelperExprs B;
6947   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6948   // define the nested loops number.
6949   unsigned NestedLoopCount =
6950       CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6951                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6952                       VarsWithImplicitDSA, B);
6953   if (NestedLoopCount == 0)
6954     return StmtError();
6955 
6956   assert((CurContext->isDependentContext() || B.builtAll()) &&
6957          "omp for loop exprs were not built");
6958 
6959   if (!CurContext->isDependentContext()) {
6960     // Finalize the clauses that need pre-built expressions for CodeGen.
6961     for (auto C : Clauses) {
6962       if (auto *LC = dyn_cast<OMPLinearClause>(C))
6963         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6964                                      B.NumIterations, *this, CurScope,
6965                                      DSAStack))
6966           return StmtError();
6967     }
6968   }
6969 
6970   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6971   // The grainsize clause and num_tasks clause are mutually exclusive and may
6972   // not appear on the same taskloop directive.
6973   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6974     return StmtError();
6975   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6976   // If a reduction clause is present on the taskloop directive, the nogroup
6977   // clause must not be specified.
6978   if (checkReductionClauseWithNogroup(*this, Clauses))
6979     return StmtError();
6980   if (checkSimdlenSafelenSpecified(*this, Clauses))
6981     return StmtError();
6982 
6983   setFunctionHasBranchProtectedScope();
6984   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6985                                           NestedLoopCount, Clauses, AStmt, B);
6986 }
6987 
6988 StmtResult Sema::ActOnOpenMPDistributeDirective(
6989     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6990     SourceLocation EndLoc,
6991     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6992   if (!AStmt)
6993     return StmtError();
6994 
6995   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6996   OMPLoopDirective::HelperExprs B;
6997   // In presence of clause 'collapse' with number of loops, it will
6998   // define the nested loops number.
6999   unsigned NestedLoopCount =
7000       CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7001                       nullptr /*ordered not a clause on distribute*/, AStmt,
7002                       *this, *DSAStack, VarsWithImplicitDSA, B);
7003   if (NestedLoopCount == 0)
7004     return StmtError();
7005 
7006   assert((CurContext->isDependentContext() || B.builtAll()) &&
7007          "omp for loop exprs were not built");
7008 
7009   setFunctionHasBranchProtectedScope();
7010   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7011                                         NestedLoopCount, Clauses, AStmt, B);
7012 }
7013 
7014 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7015     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7016     SourceLocation EndLoc,
7017     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7018   if (!AStmt)
7019     return StmtError();
7020 
7021   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7022   // 1.2.2 OpenMP Language Terminology
7023   // Structured block - An executable statement with a single entry at the
7024   // top and a single exit at the bottom.
7025   // The point of exit cannot be a branch out of the structured block.
7026   // longjmp() and throw() must not violate the entry/exit criteria.
7027   CS->getCapturedDecl()->setNothrow();
7028   for (int ThisCaptureLevel =
7029            getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7030        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7031     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7032     // 1.2.2 OpenMP Language Terminology
7033     // Structured block - An executable statement with a single entry at the
7034     // top and a single exit at the bottom.
7035     // The point of exit cannot be a branch out of the structured block.
7036     // longjmp() and throw() must not violate the entry/exit criteria.
7037     CS->getCapturedDecl()->setNothrow();
7038   }
7039 
7040   OMPLoopDirective::HelperExprs B;
7041   // In presence of clause 'collapse' with number of loops, it will
7042   // define the nested loops number.
7043   unsigned NestedLoopCount = CheckOpenMPLoop(
7044       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7045       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7046       VarsWithImplicitDSA, B);
7047   if (NestedLoopCount == 0)
7048     return StmtError();
7049 
7050   assert((CurContext->isDependentContext() || B.builtAll()) &&
7051          "omp for loop exprs were not built");
7052 
7053   setFunctionHasBranchProtectedScope();
7054   return OMPDistributeParallelForDirective::Create(
7055       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7056       DSAStack->isCancelRegion());
7057 }
7058 
7059 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7060     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7061     SourceLocation EndLoc,
7062     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7063   if (!AStmt)
7064     return StmtError();
7065 
7066   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7067   // 1.2.2 OpenMP Language Terminology
7068   // Structured block - An executable statement with a single entry at the
7069   // top and a single exit at the bottom.
7070   // The point of exit cannot be a branch out of the structured block.
7071   // longjmp() and throw() must not violate the entry/exit criteria.
7072   CS->getCapturedDecl()->setNothrow();
7073   for (int ThisCaptureLevel =
7074            getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7075        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7076     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7077     // 1.2.2 OpenMP Language Terminology
7078     // Structured block - An executable statement with a single entry at the
7079     // top and a single exit at the bottom.
7080     // The point of exit cannot be a branch out of the structured block.
7081     // longjmp() and throw() must not violate the entry/exit criteria.
7082     CS->getCapturedDecl()->setNothrow();
7083   }
7084 
7085   OMPLoopDirective::HelperExprs B;
7086   // In presence of clause 'collapse' with number of loops, it will
7087   // define the nested loops number.
7088   unsigned NestedLoopCount = CheckOpenMPLoop(
7089       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7090       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7091       VarsWithImplicitDSA, B);
7092   if (NestedLoopCount == 0)
7093     return StmtError();
7094 
7095   assert((CurContext->isDependentContext() || B.builtAll()) &&
7096          "omp for loop exprs were not built");
7097 
7098   if (!CurContext->isDependentContext()) {
7099     // Finalize the clauses that need pre-built expressions for CodeGen.
7100     for (auto C : Clauses) {
7101       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7102         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7103                                      B.NumIterations, *this, CurScope,
7104                                      DSAStack))
7105           return StmtError();
7106     }
7107   }
7108 
7109   if (checkSimdlenSafelenSpecified(*this, Clauses))
7110     return StmtError();
7111 
7112   setFunctionHasBranchProtectedScope();
7113   return OMPDistributeParallelForSimdDirective::Create(
7114       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7115 }
7116 
7117 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7118     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7119     SourceLocation EndLoc,
7120     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7121   if (!AStmt)
7122     return StmtError();
7123 
7124   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7125   // 1.2.2 OpenMP Language Terminology
7126   // Structured block - An executable statement with a single entry at the
7127   // top and a single exit at the bottom.
7128   // The point of exit cannot be a branch out of the structured block.
7129   // longjmp() and throw() must not violate the entry/exit criteria.
7130   CS->getCapturedDecl()->setNothrow();
7131   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7132        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7133     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7134     // 1.2.2 OpenMP Language Terminology
7135     // Structured block - An executable statement with a single entry at the
7136     // top and a single exit at the bottom.
7137     // The point of exit cannot be a branch out of the structured block.
7138     // longjmp() and throw() must not violate the entry/exit criteria.
7139     CS->getCapturedDecl()->setNothrow();
7140   }
7141 
7142   OMPLoopDirective::HelperExprs B;
7143   // In presence of clause 'collapse' with number of loops, it will
7144   // define the nested loops number.
7145   unsigned NestedLoopCount =
7146       CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7147                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7148                       *DSAStack, VarsWithImplicitDSA, B);
7149   if (NestedLoopCount == 0)
7150     return StmtError();
7151 
7152   assert((CurContext->isDependentContext() || B.builtAll()) &&
7153          "omp for loop exprs were not built");
7154 
7155   if (!CurContext->isDependentContext()) {
7156     // Finalize the clauses that need pre-built expressions for CodeGen.
7157     for (auto C : Clauses) {
7158       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7159         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7160                                      B.NumIterations, *this, CurScope,
7161                                      DSAStack))
7162           return StmtError();
7163     }
7164   }
7165 
7166   if (checkSimdlenSafelenSpecified(*this, Clauses))
7167     return StmtError();
7168 
7169   setFunctionHasBranchProtectedScope();
7170   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7171                                             NestedLoopCount, Clauses, AStmt, B);
7172 }
7173 
7174 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7175     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7176     SourceLocation EndLoc,
7177     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7178   if (!AStmt)
7179     return StmtError();
7180 
7181   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7182   // 1.2.2 OpenMP Language Terminology
7183   // Structured block - An executable statement with a single entry at the
7184   // top and a single exit at the bottom.
7185   // The point of exit cannot be a branch out of the structured block.
7186   // longjmp() and throw() must not violate the entry/exit criteria.
7187   CS->getCapturedDecl()->setNothrow();
7188   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7189        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7190     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7191     // 1.2.2 OpenMP Language Terminology
7192     // Structured block - An executable statement with a single entry at the
7193     // top and a single exit at the bottom.
7194     // The point of exit cannot be a branch out of the structured block.
7195     // longjmp() and throw() must not violate the entry/exit criteria.
7196     CS->getCapturedDecl()->setNothrow();
7197   }
7198 
7199   OMPLoopDirective::HelperExprs B;
7200   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7201   // define the nested loops number.
7202   unsigned NestedLoopCount = CheckOpenMPLoop(
7203       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7204       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
7205       VarsWithImplicitDSA, B);
7206   if (NestedLoopCount == 0)
7207     return StmtError();
7208 
7209   assert((CurContext->isDependentContext() || B.builtAll()) &&
7210          "omp target parallel for simd loop exprs were not built");
7211 
7212   if (!CurContext->isDependentContext()) {
7213     // Finalize the clauses that need pre-built expressions for CodeGen.
7214     for (auto C : Clauses) {
7215       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7216         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7217                                      B.NumIterations, *this, CurScope,
7218                                      DSAStack))
7219           return StmtError();
7220     }
7221   }
7222   if (checkSimdlenSafelenSpecified(*this, Clauses))
7223     return StmtError();
7224 
7225   setFunctionHasBranchProtectedScope();
7226   return OMPTargetParallelForSimdDirective::Create(
7227       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7228 }
7229 
7230 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7231     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7232     SourceLocation EndLoc,
7233     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7234   if (!AStmt)
7235     return StmtError();
7236 
7237   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7238   // 1.2.2 OpenMP Language Terminology
7239   // Structured block - An executable statement with a single entry at the
7240   // top and a single exit at the bottom.
7241   // The point of exit cannot be a branch out of the structured block.
7242   // longjmp() and throw() must not violate the entry/exit criteria.
7243   CS->getCapturedDecl()->setNothrow();
7244   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7245        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7246     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7247     // 1.2.2 OpenMP Language Terminology
7248     // Structured block - An executable statement with a single entry at the
7249     // top and a single exit at the bottom.
7250     // The point of exit cannot be a branch out of the structured block.
7251     // longjmp() and throw() must not violate the entry/exit criteria.
7252     CS->getCapturedDecl()->setNothrow();
7253   }
7254 
7255   OMPLoopDirective::HelperExprs B;
7256   // In presence of clause 'collapse' with number of loops, it will define the
7257   // nested loops number.
7258   unsigned NestedLoopCount =
7259       CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7260                       getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
7261                       VarsWithImplicitDSA, B);
7262   if (NestedLoopCount == 0)
7263     return StmtError();
7264 
7265   assert((CurContext->isDependentContext() || B.builtAll()) &&
7266          "omp target simd loop exprs were not built");
7267 
7268   if (!CurContext->isDependentContext()) {
7269     // Finalize the clauses that need pre-built expressions for CodeGen.
7270     for (auto C : Clauses) {
7271       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7272         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7273                                      B.NumIterations, *this, CurScope,
7274                                      DSAStack))
7275           return StmtError();
7276     }
7277   }
7278 
7279   if (checkSimdlenSafelenSpecified(*this, Clauses))
7280     return StmtError();
7281 
7282   setFunctionHasBranchProtectedScope();
7283   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7284                                         NestedLoopCount, Clauses, AStmt, B);
7285 }
7286 
7287 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7288     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7289     SourceLocation EndLoc,
7290     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7291   if (!AStmt)
7292     return StmtError();
7293 
7294   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7295   // 1.2.2 OpenMP Language Terminology
7296   // Structured block - An executable statement with a single entry at the
7297   // top and a single exit at the bottom.
7298   // The point of exit cannot be a branch out of the structured block.
7299   // longjmp() and throw() must not violate the entry/exit criteria.
7300   CS->getCapturedDecl()->setNothrow();
7301   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7302        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7303     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7304     // 1.2.2 OpenMP Language Terminology
7305     // Structured block - An executable statement with a single entry at the
7306     // top and a single exit at the bottom.
7307     // The point of exit cannot be a branch out of the structured block.
7308     // longjmp() and throw() must not violate the entry/exit criteria.
7309     CS->getCapturedDecl()->setNothrow();
7310   }
7311 
7312   OMPLoopDirective::HelperExprs B;
7313   // In presence of clause 'collapse' with number of loops, it will
7314   // define the nested loops number.
7315   unsigned NestedLoopCount =
7316       CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
7317                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7318                       *DSAStack, VarsWithImplicitDSA, B);
7319   if (NestedLoopCount == 0)
7320     return StmtError();
7321 
7322   assert((CurContext->isDependentContext() || B.builtAll()) &&
7323          "omp teams distribute loop exprs were not built");
7324 
7325   setFunctionHasBranchProtectedScope();
7326 
7327   DSAStack->setParentTeamsRegionLoc(StartLoc);
7328 
7329   return OMPTeamsDistributeDirective::Create(
7330       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7331 }
7332 
7333 StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7334     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7335     SourceLocation EndLoc,
7336     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7337   if (!AStmt)
7338     return StmtError();
7339 
7340   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7341   // 1.2.2 OpenMP Language Terminology
7342   // Structured block - An executable statement with a single entry at the
7343   // top and a single exit at the bottom.
7344   // The point of exit cannot be a branch out of the structured block.
7345   // longjmp() and throw() must not violate the entry/exit criteria.
7346   CS->getCapturedDecl()->setNothrow();
7347   for (int ThisCaptureLevel =
7348            getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7349        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7350     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7351     // 1.2.2 OpenMP Language Terminology
7352     // Structured block - An executable statement with a single entry at the
7353     // top and a single exit at the bottom.
7354     // The point of exit cannot be a branch out of the structured block.
7355     // longjmp() and throw() must not violate the entry/exit criteria.
7356     CS->getCapturedDecl()->setNothrow();
7357   }
7358 
7359 
7360   OMPLoopDirective::HelperExprs B;
7361   // In presence of clause 'collapse' with number of loops, it will
7362   // define the nested loops number.
7363   unsigned NestedLoopCount = CheckOpenMPLoop(
7364       OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
7365       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7366       VarsWithImplicitDSA, B);
7367 
7368   if (NestedLoopCount == 0)
7369     return StmtError();
7370 
7371   assert((CurContext->isDependentContext() || B.builtAll()) &&
7372          "omp teams distribute simd loop exprs were not built");
7373 
7374   if (!CurContext->isDependentContext()) {
7375     // Finalize the clauses that need pre-built expressions for CodeGen.
7376     for (auto C : Clauses) {
7377       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7378         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7379                                      B.NumIterations, *this, CurScope,
7380                                      DSAStack))
7381           return StmtError();
7382     }
7383   }
7384 
7385   if (checkSimdlenSafelenSpecified(*this, Clauses))
7386     return StmtError();
7387 
7388   setFunctionHasBranchProtectedScope();
7389 
7390   DSAStack->setParentTeamsRegionLoc(StartLoc);
7391 
7392   return OMPTeamsDistributeSimdDirective::Create(
7393       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7394 }
7395 
7396 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7397     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7398     SourceLocation EndLoc,
7399     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7400   if (!AStmt)
7401     return StmtError();
7402 
7403   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7404   // 1.2.2 OpenMP Language Terminology
7405   // Structured block - An executable statement with a single entry at the
7406   // top and a single exit at the bottom.
7407   // The point of exit cannot be a branch out of the structured block.
7408   // longjmp() and throw() must not violate the entry/exit criteria.
7409   CS->getCapturedDecl()->setNothrow();
7410 
7411   for (int ThisCaptureLevel =
7412            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7413        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7414     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7415     // 1.2.2 OpenMP Language Terminology
7416     // Structured block - An executable statement with a single entry at the
7417     // top and a single exit at the bottom.
7418     // The point of exit cannot be a branch out of the structured block.
7419     // longjmp() and throw() must not violate the entry/exit criteria.
7420     CS->getCapturedDecl()->setNothrow();
7421   }
7422 
7423   OMPLoopDirective::HelperExprs B;
7424   // In presence of clause 'collapse' with number of loops, it will
7425   // define the nested loops number.
7426   auto NestedLoopCount = CheckOpenMPLoop(
7427       OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7428       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7429       VarsWithImplicitDSA, B);
7430 
7431   if (NestedLoopCount == 0)
7432     return StmtError();
7433 
7434   assert((CurContext->isDependentContext() || B.builtAll()) &&
7435          "omp for loop exprs were not built");
7436 
7437   if (!CurContext->isDependentContext()) {
7438     // Finalize the clauses that need pre-built expressions for CodeGen.
7439     for (auto C : Clauses) {
7440       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7441         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7442                                      B.NumIterations, *this, CurScope,
7443                                      DSAStack))
7444           return StmtError();
7445     }
7446   }
7447 
7448   if (checkSimdlenSafelenSpecified(*this, Clauses))
7449     return StmtError();
7450 
7451   setFunctionHasBranchProtectedScope();
7452 
7453   DSAStack->setParentTeamsRegionLoc(StartLoc);
7454 
7455   return OMPTeamsDistributeParallelForSimdDirective::Create(
7456       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7457 }
7458 
7459 StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7460     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7461     SourceLocation EndLoc,
7462     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7463   if (!AStmt)
7464     return StmtError();
7465 
7466   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7467   // 1.2.2 OpenMP Language Terminology
7468   // Structured block - An executable statement with a single entry at the
7469   // top and a single exit at the bottom.
7470   // The point of exit cannot be a branch out of the structured block.
7471   // longjmp() and throw() must not violate the entry/exit criteria.
7472   CS->getCapturedDecl()->setNothrow();
7473 
7474   for (int ThisCaptureLevel =
7475            getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7476        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7477     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7478     // 1.2.2 OpenMP Language Terminology
7479     // Structured block - An executable statement with a single entry at the
7480     // top and a single exit at the bottom.
7481     // The point of exit cannot be a branch out of the structured block.
7482     // longjmp() and throw() must not violate the entry/exit criteria.
7483     CS->getCapturedDecl()->setNothrow();
7484   }
7485 
7486   OMPLoopDirective::HelperExprs B;
7487   // In presence of clause 'collapse' with number of loops, it will
7488   // define the nested loops number.
7489   unsigned NestedLoopCount = CheckOpenMPLoop(
7490       OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7491       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7492       VarsWithImplicitDSA, B);
7493 
7494   if (NestedLoopCount == 0)
7495     return StmtError();
7496 
7497   assert((CurContext->isDependentContext() || B.builtAll()) &&
7498          "omp for loop exprs were not built");
7499 
7500   setFunctionHasBranchProtectedScope();
7501 
7502   DSAStack->setParentTeamsRegionLoc(StartLoc);
7503 
7504   return OMPTeamsDistributeParallelForDirective::Create(
7505       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7506       DSAStack->isCancelRegion());
7507 }
7508 
7509 StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7510                                                  Stmt *AStmt,
7511                                                  SourceLocation StartLoc,
7512                                                  SourceLocation EndLoc) {
7513   if (!AStmt)
7514     return StmtError();
7515 
7516   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7517   // 1.2.2 OpenMP Language Terminology
7518   // Structured block - An executable statement with a single entry at the
7519   // top and a single exit at the bottom.
7520   // The point of exit cannot be a branch out of the structured block.
7521   // longjmp() and throw() must not violate the entry/exit criteria.
7522   CS->getCapturedDecl()->setNothrow();
7523 
7524   for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7525        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7526     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7527     // 1.2.2 OpenMP Language Terminology
7528     // Structured block - An executable statement with a single entry at the
7529     // top and a single exit at the bottom.
7530     // The point of exit cannot be a branch out of the structured block.
7531     // longjmp() and throw() must not violate the entry/exit criteria.
7532     CS->getCapturedDecl()->setNothrow();
7533   }
7534   setFunctionHasBranchProtectedScope();
7535 
7536   return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7537                                          AStmt);
7538 }
7539 
7540 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7541     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7542     SourceLocation EndLoc,
7543     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7544   if (!AStmt)
7545     return StmtError();
7546 
7547   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7548   // 1.2.2 OpenMP Language Terminology
7549   // Structured block - An executable statement with a single entry at the
7550   // top and a single exit at the bottom.
7551   // The point of exit cannot be a branch out of the structured block.
7552   // longjmp() and throw() must not violate the entry/exit criteria.
7553   CS->getCapturedDecl()->setNothrow();
7554   for (int ThisCaptureLevel =
7555            getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7556        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7557     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7558     // 1.2.2 OpenMP Language Terminology
7559     // Structured block - An executable statement with a single entry at the
7560     // top and a single exit at the bottom.
7561     // The point of exit cannot be a branch out of the structured block.
7562     // longjmp() and throw() must not violate the entry/exit criteria.
7563     CS->getCapturedDecl()->setNothrow();
7564   }
7565 
7566   OMPLoopDirective::HelperExprs B;
7567   // In presence of clause 'collapse' with number of loops, it will
7568   // define the nested loops number.
7569   auto NestedLoopCount = CheckOpenMPLoop(
7570       OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7571       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7572       VarsWithImplicitDSA, B);
7573   if (NestedLoopCount == 0)
7574     return StmtError();
7575 
7576   assert((CurContext->isDependentContext() || B.builtAll()) &&
7577          "omp target teams distribute loop exprs were not built");
7578 
7579   setFunctionHasBranchProtectedScope();
7580   return OMPTargetTeamsDistributeDirective::Create(
7581       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7582 }
7583 
7584 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7585     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7586     SourceLocation EndLoc,
7587     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7588   if (!AStmt)
7589     return StmtError();
7590 
7591   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7592   // 1.2.2 OpenMP Language Terminology
7593   // Structured block - An executable statement with a single entry at the
7594   // top and a single exit at the bottom.
7595   // The point of exit cannot be a branch out of the structured block.
7596   // longjmp() and throw() must not violate the entry/exit criteria.
7597   CS->getCapturedDecl()->setNothrow();
7598   for (int ThisCaptureLevel =
7599            getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7600        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7601     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7602     // 1.2.2 OpenMP Language Terminology
7603     // Structured block - An executable statement with a single entry at the
7604     // top and a single exit at the bottom.
7605     // The point of exit cannot be a branch out of the structured block.
7606     // longjmp() and throw() must not violate the entry/exit criteria.
7607     CS->getCapturedDecl()->setNothrow();
7608   }
7609 
7610   OMPLoopDirective::HelperExprs B;
7611   // In presence of clause 'collapse' with number of loops, it will
7612   // define the nested loops number.
7613   auto NestedLoopCount = CheckOpenMPLoop(
7614       OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7615       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7616       VarsWithImplicitDSA, B);
7617   if (NestedLoopCount == 0)
7618     return StmtError();
7619 
7620   assert((CurContext->isDependentContext() || B.builtAll()) &&
7621          "omp target teams distribute parallel for loop exprs were not built");
7622 
7623   if (!CurContext->isDependentContext()) {
7624     // Finalize the clauses that need pre-built expressions for CodeGen.
7625     for (auto C : Clauses) {
7626       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7627         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7628                                      B.NumIterations, *this, CurScope,
7629                                      DSAStack))
7630           return StmtError();
7631     }
7632   }
7633 
7634   setFunctionHasBranchProtectedScope();
7635   return OMPTargetTeamsDistributeParallelForDirective::Create(
7636       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7637       DSAStack->isCancelRegion());
7638 }
7639 
7640 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7641     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7642     SourceLocation EndLoc,
7643     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7644   if (!AStmt)
7645     return StmtError();
7646 
7647   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7648   // 1.2.2 OpenMP Language Terminology
7649   // Structured block - An executable statement with a single entry at the
7650   // top and a single exit at the bottom.
7651   // The point of exit cannot be a branch out of the structured block.
7652   // longjmp() and throw() must not violate the entry/exit criteria.
7653   CS->getCapturedDecl()->setNothrow();
7654   for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7655            OMPD_target_teams_distribute_parallel_for_simd);
7656        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7657     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7658     // 1.2.2 OpenMP Language Terminology
7659     // Structured block - An executable statement with a single entry at the
7660     // top and a single exit at the bottom.
7661     // The point of exit cannot be a branch out of the structured block.
7662     // longjmp() and throw() must not violate the entry/exit criteria.
7663     CS->getCapturedDecl()->setNothrow();
7664   }
7665 
7666   OMPLoopDirective::HelperExprs B;
7667   // In presence of clause 'collapse' with number of loops, it will
7668   // define the nested loops number.
7669   auto NestedLoopCount =
7670       CheckOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
7671                       getCollapseNumberExpr(Clauses),
7672                       nullptr /*ordered not a clause on distribute*/, CS, *this,
7673                       *DSAStack, VarsWithImplicitDSA, B);
7674   if (NestedLoopCount == 0)
7675     return StmtError();
7676 
7677   assert((CurContext->isDependentContext() || B.builtAll()) &&
7678          "omp target teams distribute parallel for simd loop exprs were not "
7679          "built");
7680 
7681   if (!CurContext->isDependentContext()) {
7682     // Finalize the clauses that need pre-built expressions for CodeGen.
7683     for (auto C : Clauses) {
7684       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7685         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7686                                      B.NumIterations, *this, CurScope,
7687                                      DSAStack))
7688           return StmtError();
7689     }
7690   }
7691 
7692   if (checkSimdlenSafelenSpecified(*this, Clauses))
7693     return StmtError();
7694 
7695   setFunctionHasBranchProtectedScope();
7696   return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7697       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7698 }
7699 
7700 StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7701     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7702     SourceLocation EndLoc,
7703     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7704   if (!AStmt)
7705     return StmtError();
7706 
7707   auto *CS = cast<CapturedStmt>(AStmt);
7708   // 1.2.2 OpenMP Language Terminology
7709   // Structured block - An executable statement with a single entry at the
7710   // top and a single exit at the bottom.
7711   // The point of exit cannot be a branch out of the structured block.
7712   // longjmp() and throw() must not violate the entry/exit criteria.
7713   CS->getCapturedDecl()->setNothrow();
7714   for (int ThisCaptureLevel =
7715            getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7716        ThisCaptureLevel > 1; --ThisCaptureLevel) {
7717     CS = cast<CapturedStmt>(CS->getCapturedStmt());
7718     // 1.2.2 OpenMP Language Terminology
7719     // Structured block - An executable statement with a single entry at the
7720     // top and a single exit at the bottom.
7721     // The point of exit cannot be a branch out of the structured block.
7722     // longjmp() and throw() must not violate the entry/exit criteria.
7723     CS->getCapturedDecl()->setNothrow();
7724   }
7725 
7726   OMPLoopDirective::HelperExprs B;
7727   // In presence of clause 'collapse' with number of loops, it will
7728   // define the nested loops number.
7729   auto NestedLoopCount = CheckOpenMPLoop(
7730       OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
7731       nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
7732       VarsWithImplicitDSA, B);
7733   if (NestedLoopCount == 0)
7734     return StmtError();
7735 
7736   assert((CurContext->isDependentContext() || B.builtAll()) &&
7737          "omp target teams distribute simd loop exprs were not built");
7738 
7739   if (!CurContext->isDependentContext()) {
7740     // Finalize the clauses that need pre-built expressions for CodeGen.
7741     for (auto C : Clauses) {
7742       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7743         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7744                                      B.NumIterations, *this, CurScope,
7745                                      DSAStack))
7746           return StmtError();
7747     }
7748   }
7749 
7750   if (checkSimdlenSafelenSpecified(*this, Clauses))
7751     return StmtError();
7752 
7753   setFunctionHasBranchProtectedScope();
7754   return OMPTargetTeamsDistributeSimdDirective::Create(
7755       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7756 }
7757 
7758 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
7759                                              SourceLocation StartLoc,
7760                                              SourceLocation LParenLoc,
7761                                              SourceLocation EndLoc) {
7762   OMPClause *Res = nullptr;
7763   switch (Kind) {
7764   case OMPC_final:
7765     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7766     break;
7767   case OMPC_num_threads:
7768     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7769     break;
7770   case OMPC_safelen:
7771     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7772     break;
7773   case OMPC_simdlen:
7774     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7775     break;
7776   case OMPC_collapse:
7777     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7778     break;
7779   case OMPC_ordered:
7780     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7781     break;
7782   case OMPC_device:
7783     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7784     break;
7785   case OMPC_num_teams:
7786     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7787     break;
7788   case OMPC_thread_limit:
7789     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7790     break;
7791   case OMPC_priority:
7792     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7793     break;
7794   case OMPC_grainsize:
7795     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7796     break;
7797   case OMPC_num_tasks:
7798     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7799     break;
7800   case OMPC_hint:
7801     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7802     break;
7803   case OMPC_if:
7804   case OMPC_default:
7805   case OMPC_proc_bind:
7806   case OMPC_schedule:
7807   case OMPC_private:
7808   case OMPC_firstprivate:
7809   case OMPC_lastprivate:
7810   case OMPC_shared:
7811   case OMPC_reduction:
7812   case OMPC_task_reduction:
7813   case OMPC_in_reduction:
7814   case OMPC_linear:
7815   case OMPC_aligned:
7816   case OMPC_copyin:
7817   case OMPC_copyprivate:
7818   case OMPC_nowait:
7819   case OMPC_untied:
7820   case OMPC_mergeable:
7821   case OMPC_threadprivate:
7822   case OMPC_flush:
7823   case OMPC_read:
7824   case OMPC_write:
7825   case OMPC_update:
7826   case OMPC_capture:
7827   case OMPC_seq_cst:
7828   case OMPC_depend:
7829   case OMPC_threads:
7830   case OMPC_simd:
7831   case OMPC_map:
7832   case OMPC_nogroup:
7833   case OMPC_dist_schedule:
7834   case OMPC_defaultmap:
7835   case OMPC_unknown:
7836   case OMPC_uniform:
7837   case OMPC_to:
7838   case OMPC_from:
7839   case OMPC_use_device_ptr:
7840   case OMPC_is_device_ptr:
7841     llvm_unreachable("Clause is not allowed.");
7842   }
7843   return Res;
7844 }
7845 
7846 // An OpenMP directive such as 'target parallel' has two captured regions:
7847 // for the 'target' and 'parallel' respectively.  This function returns
7848 // the region in which to capture expressions associated with a clause.
7849 // A return value of OMPD_unknown signifies that the expression should not
7850 // be captured.
7851 static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
7852     OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
7853     OpenMPDirectiveKind NameModifier = OMPD_unknown) {
7854   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
7855   switch (CKind) {
7856   case OMPC_if:
7857     switch (DKind) {
7858     case OMPD_target_parallel:
7859     case OMPD_target_parallel_for:
7860     case OMPD_target_parallel_for_simd:
7861       // If this clause applies to the nested 'parallel' region, capture within
7862       // the 'target' region, otherwise do not capture.
7863       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7864         CaptureRegion = OMPD_target;
7865       break;
7866     case OMPD_target_teams_distribute_parallel_for:
7867     case OMPD_target_teams_distribute_parallel_for_simd:
7868       // If this clause applies to the nested 'parallel' region, capture within
7869       // the 'teams' region, otherwise do not capture.
7870       if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
7871         CaptureRegion = OMPD_teams;
7872       break;
7873     case OMPD_teams_distribute_parallel_for:
7874     case OMPD_teams_distribute_parallel_for_simd:
7875       CaptureRegion = OMPD_teams;
7876       break;
7877     case OMPD_target_update:
7878     case OMPD_target_enter_data:
7879     case OMPD_target_exit_data:
7880       CaptureRegion = OMPD_task;
7881       break;
7882     case OMPD_cancel:
7883     case OMPD_parallel:
7884     case OMPD_parallel_sections:
7885     case OMPD_parallel_for:
7886     case OMPD_parallel_for_simd:
7887     case OMPD_target:
7888     case OMPD_target_simd:
7889     case OMPD_target_teams:
7890     case OMPD_target_teams_distribute:
7891     case OMPD_target_teams_distribute_simd:
7892     case OMPD_distribute_parallel_for:
7893     case OMPD_distribute_parallel_for_simd:
7894     case OMPD_task:
7895     case OMPD_taskloop:
7896     case OMPD_taskloop_simd:
7897     case OMPD_target_data:
7898       // Do not capture if-clause expressions.
7899       break;
7900     case OMPD_threadprivate:
7901     case OMPD_taskyield:
7902     case OMPD_barrier:
7903     case OMPD_taskwait:
7904     case OMPD_cancellation_point:
7905     case OMPD_flush:
7906     case OMPD_declare_reduction:
7907     case OMPD_declare_simd:
7908     case OMPD_declare_target:
7909     case OMPD_end_declare_target:
7910     case OMPD_teams:
7911     case OMPD_simd:
7912     case OMPD_for:
7913     case OMPD_for_simd:
7914     case OMPD_sections:
7915     case OMPD_section:
7916     case OMPD_single:
7917     case OMPD_master:
7918     case OMPD_critical:
7919     case OMPD_taskgroup:
7920     case OMPD_distribute:
7921     case OMPD_ordered:
7922     case OMPD_atomic:
7923     case OMPD_distribute_simd:
7924     case OMPD_teams_distribute:
7925     case OMPD_teams_distribute_simd:
7926       llvm_unreachable("Unexpected OpenMP directive with if-clause");
7927     case OMPD_unknown:
7928       llvm_unreachable("Unknown OpenMP directive");
7929     }
7930     break;
7931   case OMPC_num_threads:
7932     switch (DKind) {
7933     case OMPD_target_parallel:
7934     case OMPD_target_parallel_for:
7935     case OMPD_target_parallel_for_simd:
7936       CaptureRegion = OMPD_target;
7937       break;
7938     case OMPD_teams_distribute_parallel_for:
7939     case OMPD_teams_distribute_parallel_for_simd:
7940     case OMPD_target_teams_distribute_parallel_for:
7941     case OMPD_target_teams_distribute_parallel_for_simd:
7942       CaptureRegion = OMPD_teams;
7943       break;
7944     case OMPD_parallel:
7945     case OMPD_parallel_sections:
7946     case OMPD_parallel_for:
7947     case OMPD_parallel_for_simd:
7948     case OMPD_distribute_parallel_for:
7949     case OMPD_distribute_parallel_for_simd:
7950       // Do not capture num_threads-clause expressions.
7951       break;
7952     case OMPD_target_data:
7953     case OMPD_target_enter_data:
7954     case OMPD_target_exit_data:
7955     case OMPD_target_update:
7956     case OMPD_target:
7957     case OMPD_target_simd:
7958     case OMPD_target_teams:
7959     case OMPD_target_teams_distribute:
7960     case OMPD_target_teams_distribute_simd:
7961     case OMPD_cancel:
7962     case OMPD_task:
7963     case OMPD_taskloop:
7964     case OMPD_taskloop_simd:
7965     case OMPD_threadprivate:
7966     case OMPD_taskyield:
7967     case OMPD_barrier:
7968     case OMPD_taskwait:
7969     case OMPD_cancellation_point:
7970     case OMPD_flush:
7971     case OMPD_declare_reduction:
7972     case OMPD_declare_simd:
7973     case OMPD_declare_target:
7974     case OMPD_end_declare_target:
7975     case OMPD_teams:
7976     case OMPD_simd:
7977     case OMPD_for:
7978     case OMPD_for_simd:
7979     case OMPD_sections:
7980     case OMPD_section:
7981     case OMPD_single:
7982     case OMPD_master:
7983     case OMPD_critical:
7984     case OMPD_taskgroup:
7985     case OMPD_distribute:
7986     case OMPD_ordered:
7987     case OMPD_atomic:
7988     case OMPD_distribute_simd:
7989     case OMPD_teams_distribute:
7990     case OMPD_teams_distribute_simd:
7991       llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
7992     case OMPD_unknown:
7993       llvm_unreachable("Unknown OpenMP directive");
7994     }
7995     break;
7996   case OMPC_num_teams:
7997     switch (DKind) {
7998     case OMPD_target_teams:
7999     case OMPD_target_teams_distribute:
8000     case OMPD_target_teams_distribute_simd:
8001     case OMPD_target_teams_distribute_parallel_for:
8002     case OMPD_target_teams_distribute_parallel_for_simd:
8003       CaptureRegion = OMPD_target;
8004       break;
8005     case OMPD_teams_distribute_parallel_for:
8006     case OMPD_teams_distribute_parallel_for_simd:
8007     case OMPD_teams:
8008     case OMPD_teams_distribute:
8009     case OMPD_teams_distribute_simd:
8010       // Do not capture num_teams-clause expressions.
8011       break;
8012     case OMPD_distribute_parallel_for:
8013     case OMPD_distribute_parallel_for_simd:
8014     case OMPD_task:
8015     case OMPD_taskloop:
8016     case OMPD_taskloop_simd:
8017     case OMPD_target_data:
8018     case OMPD_target_enter_data:
8019     case OMPD_target_exit_data:
8020     case OMPD_target_update:
8021     case OMPD_cancel:
8022     case OMPD_parallel:
8023     case OMPD_parallel_sections:
8024     case OMPD_parallel_for:
8025     case OMPD_parallel_for_simd:
8026     case OMPD_target:
8027     case OMPD_target_simd:
8028     case OMPD_target_parallel:
8029     case OMPD_target_parallel_for:
8030     case OMPD_target_parallel_for_simd:
8031     case OMPD_threadprivate:
8032     case OMPD_taskyield:
8033     case OMPD_barrier:
8034     case OMPD_taskwait:
8035     case OMPD_cancellation_point:
8036     case OMPD_flush:
8037     case OMPD_declare_reduction:
8038     case OMPD_declare_simd:
8039     case OMPD_declare_target:
8040     case OMPD_end_declare_target:
8041     case OMPD_simd:
8042     case OMPD_for:
8043     case OMPD_for_simd:
8044     case OMPD_sections:
8045     case OMPD_section:
8046     case OMPD_single:
8047     case OMPD_master:
8048     case OMPD_critical:
8049     case OMPD_taskgroup:
8050     case OMPD_distribute:
8051     case OMPD_ordered:
8052     case OMPD_atomic:
8053     case OMPD_distribute_simd:
8054       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8055     case OMPD_unknown:
8056       llvm_unreachable("Unknown OpenMP directive");
8057     }
8058     break;
8059   case OMPC_thread_limit:
8060     switch (DKind) {
8061     case OMPD_target_teams:
8062     case OMPD_target_teams_distribute:
8063     case OMPD_target_teams_distribute_simd:
8064     case OMPD_target_teams_distribute_parallel_for:
8065     case OMPD_target_teams_distribute_parallel_for_simd:
8066       CaptureRegion = OMPD_target;
8067       break;
8068     case OMPD_teams_distribute_parallel_for:
8069     case OMPD_teams_distribute_parallel_for_simd:
8070     case OMPD_teams:
8071     case OMPD_teams_distribute:
8072     case OMPD_teams_distribute_simd:
8073       // Do not capture thread_limit-clause expressions.
8074       break;
8075     case OMPD_distribute_parallel_for:
8076     case OMPD_distribute_parallel_for_simd:
8077     case OMPD_task:
8078     case OMPD_taskloop:
8079     case OMPD_taskloop_simd:
8080     case OMPD_target_data:
8081     case OMPD_target_enter_data:
8082     case OMPD_target_exit_data:
8083     case OMPD_target_update:
8084     case OMPD_cancel:
8085     case OMPD_parallel:
8086     case OMPD_parallel_sections:
8087     case OMPD_parallel_for:
8088     case OMPD_parallel_for_simd:
8089     case OMPD_target:
8090     case OMPD_target_simd:
8091     case OMPD_target_parallel:
8092     case OMPD_target_parallel_for:
8093     case OMPD_target_parallel_for_simd:
8094     case OMPD_threadprivate:
8095     case OMPD_taskyield:
8096     case OMPD_barrier:
8097     case OMPD_taskwait:
8098     case OMPD_cancellation_point:
8099     case OMPD_flush:
8100     case OMPD_declare_reduction:
8101     case OMPD_declare_simd:
8102     case OMPD_declare_target:
8103     case OMPD_end_declare_target:
8104     case OMPD_simd:
8105     case OMPD_for:
8106     case OMPD_for_simd:
8107     case OMPD_sections:
8108     case OMPD_section:
8109     case OMPD_single:
8110     case OMPD_master:
8111     case OMPD_critical:
8112     case OMPD_taskgroup:
8113     case OMPD_distribute:
8114     case OMPD_ordered:
8115     case OMPD_atomic:
8116     case OMPD_distribute_simd:
8117       llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8118     case OMPD_unknown:
8119       llvm_unreachable("Unknown OpenMP directive");
8120     }
8121     break;
8122   case OMPC_schedule:
8123     switch (DKind) {
8124     case OMPD_parallel_for:
8125     case OMPD_parallel_for_simd:
8126     case OMPD_distribute_parallel_for:
8127     case OMPD_distribute_parallel_for_simd:
8128     case OMPD_teams_distribute_parallel_for:
8129     case OMPD_teams_distribute_parallel_for_simd:
8130     case OMPD_target_parallel_for:
8131     case OMPD_target_parallel_for_simd:
8132     case OMPD_target_teams_distribute_parallel_for:
8133     case OMPD_target_teams_distribute_parallel_for_simd:
8134       CaptureRegion = OMPD_parallel;
8135       break;
8136     case OMPD_for:
8137     case OMPD_for_simd:
8138       // Do not capture schedule-clause expressions.
8139       break;
8140     case OMPD_task:
8141     case OMPD_taskloop:
8142     case OMPD_taskloop_simd:
8143     case OMPD_target_data:
8144     case OMPD_target_enter_data:
8145     case OMPD_target_exit_data:
8146     case OMPD_target_update:
8147     case OMPD_teams:
8148     case OMPD_teams_distribute:
8149     case OMPD_teams_distribute_simd:
8150     case OMPD_target_teams_distribute:
8151     case OMPD_target_teams_distribute_simd:
8152     case OMPD_target:
8153     case OMPD_target_simd:
8154     case OMPD_target_parallel:
8155     case OMPD_cancel:
8156     case OMPD_parallel:
8157     case OMPD_parallel_sections:
8158     case OMPD_threadprivate:
8159     case OMPD_taskyield:
8160     case OMPD_barrier:
8161     case OMPD_taskwait:
8162     case OMPD_cancellation_point:
8163     case OMPD_flush:
8164     case OMPD_declare_reduction:
8165     case OMPD_declare_simd:
8166     case OMPD_declare_target:
8167     case OMPD_end_declare_target:
8168     case OMPD_simd:
8169     case OMPD_sections:
8170     case OMPD_section:
8171     case OMPD_single:
8172     case OMPD_master:
8173     case OMPD_critical:
8174     case OMPD_taskgroup:
8175     case OMPD_distribute:
8176     case OMPD_ordered:
8177     case OMPD_atomic:
8178     case OMPD_distribute_simd:
8179     case OMPD_target_teams:
8180       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8181     case OMPD_unknown:
8182       llvm_unreachable("Unknown OpenMP directive");
8183     }
8184     break;
8185   case OMPC_dist_schedule:
8186     switch (DKind) {
8187     case OMPD_teams_distribute_parallel_for:
8188     case OMPD_teams_distribute_parallel_for_simd:
8189     case OMPD_teams_distribute:
8190     case OMPD_teams_distribute_simd:
8191     case OMPD_target_teams_distribute_parallel_for:
8192     case OMPD_target_teams_distribute_parallel_for_simd:
8193     case OMPD_target_teams_distribute:
8194     case OMPD_target_teams_distribute_simd:
8195       CaptureRegion = OMPD_teams;
8196       break;
8197     case OMPD_distribute_parallel_for:
8198     case OMPD_distribute_parallel_for_simd:
8199     case OMPD_distribute:
8200     case OMPD_distribute_simd:
8201       // Do not capture thread_limit-clause expressions.
8202       break;
8203     case OMPD_parallel_for:
8204     case OMPD_parallel_for_simd:
8205     case OMPD_target_parallel_for_simd:
8206     case OMPD_target_parallel_for:
8207     case OMPD_task:
8208     case OMPD_taskloop:
8209     case OMPD_taskloop_simd:
8210     case OMPD_target_data:
8211     case OMPD_target_enter_data:
8212     case OMPD_target_exit_data:
8213     case OMPD_target_update:
8214     case OMPD_teams:
8215     case OMPD_target:
8216     case OMPD_target_simd:
8217     case OMPD_target_parallel:
8218     case OMPD_cancel:
8219     case OMPD_parallel:
8220     case OMPD_parallel_sections:
8221     case OMPD_threadprivate:
8222     case OMPD_taskyield:
8223     case OMPD_barrier:
8224     case OMPD_taskwait:
8225     case OMPD_cancellation_point:
8226     case OMPD_flush:
8227     case OMPD_declare_reduction:
8228     case OMPD_declare_simd:
8229     case OMPD_declare_target:
8230     case OMPD_end_declare_target:
8231     case OMPD_simd:
8232     case OMPD_for:
8233     case OMPD_for_simd:
8234     case OMPD_sections:
8235     case OMPD_section:
8236     case OMPD_single:
8237     case OMPD_master:
8238     case OMPD_critical:
8239     case OMPD_taskgroup:
8240     case OMPD_ordered:
8241     case OMPD_atomic:
8242     case OMPD_target_teams:
8243       llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8244     case OMPD_unknown:
8245       llvm_unreachable("Unknown OpenMP directive");
8246     }
8247     break;
8248   case OMPC_device:
8249     switch (DKind) {
8250     case OMPD_target_update:
8251     case OMPD_target_enter_data:
8252     case OMPD_target_exit_data:
8253     case OMPD_target:
8254     case OMPD_target_simd:
8255     case OMPD_target_teams:
8256     case OMPD_target_parallel:
8257     case OMPD_target_teams_distribute:
8258     case OMPD_target_teams_distribute_simd:
8259     case OMPD_target_parallel_for:
8260     case OMPD_target_parallel_for_simd:
8261     case OMPD_target_teams_distribute_parallel_for:
8262     case OMPD_target_teams_distribute_parallel_for_simd:
8263       CaptureRegion = OMPD_task;
8264       break;
8265     case OMPD_target_data:
8266       // Do not capture device-clause expressions.
8267       break;
8268     case OMPD_teams_distribute_parallel_for:
8269     case OMPD_teams_distribute_parallel_for_simd:
8270     case OMPD_teams:
8271     case OMPD_teams_distribute:
8272     case OMPD_teams_distribute_simd:
8273     case OMPD_distribute_parallel_for:
8274     case OMPD_distribute_parallel_for_simd:
8275     case OMPD_task:
8276     case OMPD_taskloop:
8277     case OMPD_taskloop_simd:
8278     case OMPD_cancel:
8279     case OMPD_parallel:
8280     case OMPD_parallel_sections:
8281     case OMPD_parallel_for:
8282     case OMPD_parallel_for_simd:
8283     case OMPD_threadprivate:
8284     case OMPD_taskyield:
8285     case OMPD_barrier:
8286     case OMPD_taskwait:
8287     case OMPD_cancellation_point:
8288     case OMPD_flush:
8289     case OMPD_declare_reduction:
8290     case OMPD_declare_simd:
8291     case OMPD_declare_target:
8292     case OMPD_end_declare_target:
8293     case OMPD_simd:
8294     case OMPD_for:
8295     case OMPD_for_simd:
8296     case OMPD_sections:
8297     case OMPD_section:
8298     case OMPD_single:
8299     case OMPD_master:
8300     case OMPD_critical:
8301     case OMPD_taskgroup:
8302     case OMPD_distribute:
8303     case OMPD_ordered:
8304     case OMPD_atomic:
8305     case OMPD_distribute_simd:
8306       llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8307     case OMPD_unknown:
8308       llvm_unreachable("Unknown OpenMP directive");
8309     }
8310     break;
8311   case OMPC_firstprivate:
8312   case OMPC_lastprivate:
8313   case OMPC_reduction:
8314   case OMPC_task_reduction:
8315   case OMPC_in_reduction:
8316   case OMPC_linear:
8317   case OMPC_default:
8318   case OMPC_proc_bind:
8319   case OMPC_final:
8320   case OMPC_safelen:
8321   case OMPC_simdlen:
8322   case OMPC_collapse:
8323   case OMPC_private:
8324   case OMPC_shared:
8325   case OMPC_aligned:
8326   case OMPC_copyin:
8327   case OMPC_copyprivate:
8328   case OMPC_ordered:
8329   case OMPC_nowait:
8330   case OMPC_untied:
8331   case OMPC_mergeable:
8332   case OMPC_threadprivate:
8333   case OMPC_flush:
8334   case OMPC_read:
8335   case OMPC_write:
8336   case OMPC_update:
8337   case OMPC_capture:
8338   case OMPC_seq_cst:
8339   case OMPC_depend:
8340   case OMPC_threads:
8341   case OMPC_simd:
8342   case OMPC_map:
8343   case OMPC_priority:
8344   case OMPC_grainsize:
8345   case OMPC_nogroup:
8346   case OMPC_num_tasks:
8347   case OMPC_hint:
8348   case OMPC_defaultmap:
8349   case OMPC_unknown:
8350   case OMPC_uniform:
8351   case OMPC_to:
8352   case OMPC_from:
8353   case OMPC_use_device_ptr:
8354   case OMPC_is_device_ptr:
8355     llvm_unreachable("Unexpected OpenMP clause.");
8356   }
8357   return CaptureRegion;
8358 }
8359 
8360 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8361                                      Expr *Condition, SourceLocation StartLoc,
8362                                      SourceLocation LParenLoc,
8363                                      SourceLocation NameModifierLoc,
8364                                      SourceLocation ColonLoc,
8365                                      SourceLocation EndLoc) {
8366   Expr *ValExpr = Condition;
8367   Stmt *HelperValStmt = nullptr;
8368   OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
8369   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8370       !Condition->isInstantiationDependent() &&
8371       !Condition->containsUnexpandedParameterPack()) {
8372     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
8373     if (Val.isInvalid())
8374       return nullptr;
8375 
8376     ValExpr = Val.get();
8377 
8378     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8379     CaptureRegion =
8380         getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
8381     if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
8382       ValExpr = MakeFullExpr(ValExpr).get();
8383       llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8384       ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8385       HelperValStmt = buildPreInits(Context, Captures);
8386     }
8387   }
8388 
8389   return new (Context)
8390       OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8391                   LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
8392 }
8393 
8394 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8395                                         SourceLocation StartLoc,
8396                                         SourceLocation LParenLoc,
8397                                         SourceLocation EndLoc) {
8398   Expr *ValExpr = Condition;
8399   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8400       !Condition->isInstantiationDependent() &&
8401       !Condition->containsUnexpandedParameterPack()) {
8402     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
8403     if (Val.isInvalid())
8404       return nullptr;
8405 
8406     ValExpr = MakeFullExpr(Val.get()).get();
8407   }
8408 
8409   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8410 }
8411 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8412                                                         Expr *Op) {
8413   if (!Op)
8414     return ExprError();
8415 
8416   class IntConvertDiagnoser : public ICEConvertDiagnoser {
8417   public:
8418     IntConvertDiagnoser()
8419         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
8420     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8421                                          QualType T) override {
8422       return S.Diag(Loc, diag::err_omp_not_integral) << T;
8423     }
8424     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8425                                              QualType T) override {
8426       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8427     }
8428     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8429                                                QualType T,
8430                                                QualType ConvTy) override {
8431       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8432     }
8433     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8434                                            QualType ConvTy) override {
8435       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
8436              << ConvTy->isEnumeralType() << ConvTy;
8437     }
8438     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8439                                             QualType T) override {
8440       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8441     }
8442     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8443                                         QualType ConvTy) override {
8444       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
8445              << ConvTy->isEnumeralType() << ConvTy;
8446     }
8447     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8448                                              QualType) override {
8449       llvm_unreachable("conversion functions are permitted");
8450     }
8451   } ConvertDiagnoser;
8452   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8453 }
8454 
8455 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
8456                                       OpenMPClauseKind CKind,
8457                                       bool StrictlyPositive) {
8458   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8459       !ValExpr->isInstantiationDependent()) {
8460     SourceLocation Loc = ValExpr->getExprLoc();
8461     ExprResult Value =
8462         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8463     if (Value.isInvalid())
8464       return false;
8465 
8466     ValExpr = Value.get();
8467     // The expression must evaluate to a non-negative integer value.
8468     llvm::APSInt Result;
8469     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
8470         Result.isSigned() &&
8471         !((!StrictlyPositive && Result.isNonNegative()) ||
8472           (StrictlyPositive && Result.isStrictlyPositive()))) {
8473       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
8474           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8475           << ValExpr->getSourceRange();
8476       return false;
8477     }
8478   }
8479   return true;
8480 }
8481 
8482 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8483                                              SourceLocation StartLoc,
8484                                              SourceLocation LParenLoc,
8485                                              SourceLocation EndLoc) {
8486   Expr *ValExpr = NumThreads;
8487   Stmt *HelperValStmt = nullptr;
8488 
8489   // OpenMP [2.5, Restrictions]
8490   //  The num_threads expression must evaluate to a positive integer value.
8491   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
8492                                  /*StrictlyPositive=*/true))
8493     return nullptr;
8494 
8495   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8496   OpenMPDirectiveKind CaptureRegion =
8497       getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8498   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
8499     ValExpr = MakeFullExpr(ValExpr).get();
8500     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8501     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8502     HelperValStmt = buildPreInits(Context, Captures);
8503   }
8504 
8505   return new (Context) OMPNumThreadsClause(
8506       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
8507 }
8508 
8509 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
8510                                                        OpenMPClauseKind CKind,
8511                                                        bool StrictlyPositive) {
8512   if (!E)
8513     return ExprError();
8514   if (E->isValueDependent() || E->isTypeDependent() ||
8515       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
8516     return E;
8517   llvm::APSInt Result;
8518   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8519   if (ICE.isInvalid())
8520     return ExprError();
8521   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8522       (!StrictlyPositive && !Result.isNonNegative())) {
8523     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
8524         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8525         << E->getSourceRange();
8526     return ExprError();
8527   }
8528   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8529     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8530         << E->getSourceRange();
8531     return ExprError();
8532   }
8533   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8534     DSAStack->setAssociatedLoops(Result.getExtValue());
8535   else if (CKind == OMPC_ordered)
8536     DSAStack->setAssociatedLoops(Result.getExtValue());
8537   return ICE;
8538 }
8539 
8540 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8541                                           SourceLocation LParenLoc,
8542                                           SourceLocation EndLoc) {
8543   // OpenMP [2.8.1, simd construct, Description]
8544   // The parameter of the safelen clause must be a constant
8545   // positive integer expression.
8546   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8547   if (Safelen.isInvalid())
8548     return nullptr;
8549   return new (Context)
8550       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
8551 }
8552 
8553 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8554                                           SourceLocation LParenLoc,
8555                                           SourceLocation EndLoc) {
8556   // OpenMP [2.8.1, simd construct, Description]
8557   // The parameter of the simdlen clause must be a constant
8558   // positive integer expression.
8559   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8560   if (Simdlen.isInvalid())
8561     return nullptr;
8562   return new (Context)
8563       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8564 }
8565 
8566 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8567                                            SourceLocation StartLoc,
8568                                            SourceLocation LParenLoc,
8569                                            SourceLocation EndLoc) {
8570   // OpenMP [2.7.1, loop construct, Description]
8571   // OpenMP [2.8.1, simd construct, Description]
8572   // OpenMP [2.9.6, distribute construct, Description]
8573   // The parameter of the collapse clause must be a constant
8574   // positive integer expression.
8575   ExprResult NumForLoopsResult =
8576       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8577   if (NumForLoopsResult.isInvalid())
8578     return nullptr;
8579   return new (Context)
8580       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
8581 }
8582 
8583 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8584                                           SourceLocation EndLoc,
8585                                           SourceLocation LParenLoc,
8586                                           Expr *NumForLoops) {
8587   // OpenMP [2.7.1, loop construct, Description]
8588   // OpenMP [2.8.1, simd construct, Description]
8589   // OpenMP [2.9.6, distribute construct, Description]
8590   // The parameter of the ordered clause must be a constant
8591   // positive integer expression if any.
8592   if (NumForLoops && LParenLoc.isValid()) {
8593     ExprResult NumForLoopsResult =
8594         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8595     if (NumForLoopsResult.isInvalid())
8596       return nullptr;
8597     NumForLoops = NumForLoopsResult.get();
8598   } else
8599     NumForLoops = nullptr;
8600   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
8601   return new (Context)
8602       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
8603 }
8604 
8605 OMPClause *Sema::ActOnOpenMPSimpleClause(
8606     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8607     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8608   OMPClause *Res = nullptr;
8609   switch (Kind) {
8610   case OMPC_default:
8611     Res =
8612         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8613                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
8614     break;
8615   case OMPC_proc_bind:
8616     Res = ActOnOpenMPProcBindClause(
8617         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8618         LParenLoc, EndLoc);
8619     break;
8620   case OMPC_if:
8621   case OMPC_final:
8622   case OMPC_num_threads:
8623   case OMPC_safelen:
8624   case OMPC_simdlen:
8625   case OMPC_collapse:
8626   case OMPC_schedule:
8627   case OMPC_private:
8628   case OMPC_firstprivate:
8629   case OMPC_lastprivate:
8630   case OMPC_shared:
8631   case OMPC_reduction:
8632   case OMPC_task_reduction:
8633   case OMPC_in_reduction:
8634   case OMPC_linear:
8635   case OMPC_aligned:
8636   case OMPC_copyin:
8637   case OMPC_copyprivate:
8638   case OMPC_ordered:
8639   case OMPC_nowait:
8640   case OMPC_untied:
8641   case OMPC_mergeable:
8642   case OMPC_threadprivate:
8643   case OMPC_flush:
8644   case OMPC_read:
8645   case OMPC_write:
8646   case OMPC_update:
8647   case OMPC_capture:
8648   case OMPC_seq_cst:
8649   case OMPC_depend:
8650   case OMPC_device:
8651   case OMPC_threads:
8652   case OMPC_simd:
8653   case OMPC_map:
8654   case OMPC_num_teams:
8655   case OMPC_thread_limit:
8656   case OMPC_priority:
8657   case OMPC_grainsize:
8658   case OMPC_nogroup:
8659   case OMPC_num_tasks:
8660   case OMPC_hint:
8661   case OMPC_dist_schedule:
8662   case OMPC_defaultmap:
8663   case OMPC_unknown:
8664   case OMPC_uniform:
8665   case OMPC_to:
8666   case OMPC_from:
8667   case OMPC_use_device_ptr:
8668   case OMPC_is_device_ptr:
8669     llvm_unreachable("Clause is not allowed.");
8670   }
8671   return Res;
8672 }
8673 
8674 static std::string
8675 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8676                         ArrayRef<unsigned> Exclude = llvm::None) {
8677   std::string Values;
8678   unsigned Bound = Last >= 2 ? Last - 2 : 0;
8679   unsigned Skipped = Exclude.size();
8680   auto S = Exclude.begin(), E = Exclude.end();
8681   for (unsigned i = First; i < Last; ++i) {
8682     if (std::find(S, E, i) != E) {
8683       --Skipped;
8684       continue;
8685     }
8686     Values += "'";
8687     Values += getOpenMPSimpleClauseTypeName(K, i);
8688     Values += "'";
8689     if (i == Bound - Skipped)
8690       Values += " or ";
8691     else if (i != Bound + 1 - Skipped)
8692       Values += ", ";
8693   }
8694   return Values;
8695 }
8696 
8697 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8698                                           SourceLocation KindKwLoc,
8699                                           SourceLocation StartLoc,
8700                                           SourceLocation LParenLoc,
8701                                           SourceLocation EndLoc) {
8702   if (Kind == OMPC_DEFAULT_unknown) {
8703     static_assert(OMPC_DEFAULT_unknown > 0,
8704                   "OMPC_DEFAULT_unknown not greater than 0");
8705     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
8706         << getListOfPossibleValues(OMPC_default, /*First=*/0,
8707                                    /*Last=*/OMPC_DEFAULT_unknown)
8708         << getOpenMPClauseName(OMPC_default);
8709     return nullptr;
8710   }
8711   switch (Kind) {
8712   case OMPC_DEFAULT_none:
8713     DSAStack->setDefaultDSANone(KindKwLoc);
8714     break;
8715   case OMPC_DEFAULT_shared:
8716     DSAStack->setDefaultDSAShared(KindKwLoc);
8717     break;
8718   case OMPC_DEFAULT_unknown:
8719     llvm_unreachable("Clause kind is not allowed.");
8720     break;
8721   }
8722   return new (Context)
8723       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
8724 }
8725 
8726 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8727                                            SourceLocation KindKwLoc,
8728                                            SourceLocation StartLoc,
8729                                            SourceLocation LParenLoc,
8730                                            SourceLocation EndLoc) {
8731   if (Kind == OMPC_PROC_BIND_unknown) {
8732     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
8733         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8734                                    /*Last=*/OMPC_PROC_BIND_unknown)
8735         << getOpenMPClauseName(OMPC_proc_bind);
8736     return nullptr;
8737   }
8738   return new (Context)
8739       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
8740 }
8741 
8742 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
8743     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
8744     SourceLocation StartLoc, SourceLocation LParenLoc,
8745     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
8746     SourceLocation EndLoc) {
8747   OMPClause *Res = nullptr;
8748   switch (Kind) {
8749   case OMPC_schedule:
8750     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8751     assert(Argument.size() == NumberOfElements &&
8752            ArgumentLoc.size() == NumberOfElements);
8753     Res = ActOnOpenMPScheduleClause(
8754         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8755         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8756         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8757         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8758         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
8759     break;
8760   case OMPC_if:
8761     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8762     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8763                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8764                               DelimLoc, EndLoc);
8765     break;
8766   case OMPC_dist_schedule:
8767     Res = ActOnOpenMPDistScheduleClause(
8768         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8769         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8770     break;
8771   case OMPC_defaultmap:
8772     enum { Modifier, DefaultmapKind };
8773     Res = ActOnOpenMPDefaultmapClause(
8774         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8775         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
8776         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8777         EndLoc);
8778     break;
8779   case OMPC_final:
8780   case OMPC_num_threads:
8781   case OMPC_safelen:
8782   case OMPC_simdlen:
8783   case OMPC_collapse:
8784   case OMPC_default:
8785   case OMPC_proc_bind:
8786   case OMPC_private:
8787   case OMPC_firstprivate:
8788   case OMPC_lastprivate:
8789   case OMPC_shared:
8790   case OMPC_reduction:
8791   case OMPC_task_reduction:
8792   case OMPC_in_reduction:
8793   case OMPC_linear:
8794   case OMPC_aligned:
8795   case OMPC_copyin:
8796   case OMPC_copyprivate:
8797   case OMPC_ordered:
8798   case OMPC_nowait:
8799   case OMPC_untied:
8800   case OMPC_mergeable:
8801   case OMPC_threadprivate:
8802   case OMPC_flush:
8803   case OMPC_read:
8804   case OMPC_write:
8805   case OMPC_update:
8806   case OMPC_capture:
8807   case OMPC_seq_cst:
8808   case OMPC_depend:
8809   case OMPC_device:
8810   case OMPC_threads:
8811   case OMPC_simd:
8812   case OMPC_map:
8813   case OMPC_num_teams:
8814   case OMPC_thread_limit:
8815   case OMPC_priority:
8816   case OMPC_grainsize:
8817   case OMPC_nogroup:
8818   case OMPC_num_tasks:
8819   case OMPC_hint:
8820   case OMPC_unknown:
8821   case OMPC_uniform:
8822   case OMPC_to:
8823   case OMPC_from:
8824   case OMPC_use_device_ptr:
8825   case OMPC_is_device_ptr:
8826     llvm_unreachable("Clause is not allowed.");
8827   }
8828   return Res;
8829 }
8830 
8831 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
8832                                    OpenMPScheduleClauseModifier M2,
8833                                    SourceLocation M1Loc, SourceLocation M2Loc) {
8834   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8835     SmallVector<unsigned, 2> Excluded;
8836     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8837       Excluded.push_back(M2);
8838     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8839       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8840     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8841       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8842     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8843         << getListOfPossibleValues(OMPC_schedule,
8844                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8845                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8846                                    Excluded)
8847         << getOpenMPClauseName(OMPC_schedule);
8848     return true;
8849   }
8850   return false;
8851 }
8852 
8853 OMPClause *Sema::ActOnOpenMPScheduleClause(
8854     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
8855     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
8856     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8857     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8858   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8859       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8860     return nullptr;
8861   // OpenMP, 2.7.1, Loop Construct, Restrictions
8862   // Either the monotonic modifier or the nonmonotonic modifier can be specified
8863   // but not both.
8864   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8865       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8866        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8867       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8868        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8869     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8870         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8871         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8872     return nullptr;
8873   }
8874   if (Kind == OMPC_SCHEDULE_unknown) {
8875     std::string Values;
8876     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8877       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8878       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8879                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8880                                        Exclude);
8881     } else {
8882       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8883                                        /*Last=*/OMPC_SCHEDULE_unknown);
8884     }
8885     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8886         << Values << getOpenMPClauseName(OMPC_schedule);
8887     return nullptr;
8888   }
8889   // OpenMP, 2.7.1, Loop Construct, Restrictions
8890   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8891   // schedule(guided).
8892   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8893        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8894       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8895     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8896          diag::err_omp_schedule_nonmonotonic_static);
8897     return nullptr;
8898   }
8899   Expr *ValExpr = ChunkSize;
8900   Stmt *HelperValStmt = nullptr;
8901   if (ChunkSize) {
8902     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8903         !ChunkSize->isInstantiationDependent() &&
8904         !ChunkSize->containsUnexpandedParameterPack()) {
8905       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8906       ExprResult Val =
8907           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8908       if (Val.isInvalid())
8909         return nullptr;
8910 
8911       ValExpr = Val.get();
8912 
8913       // OpenMP [2.7.1, Restrictions]
8914       //  chunk_size must be a loop invariant integer expression with a positive
8915       //  value.
8916       llvm::APSInt Result;
8917       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8918         if (Result.isSigned() && !Result.isStrictlyPositive()) {
8919           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
8920               << "schedule" << 1 << ChunkSize->getSourceRange();
8921           return nullptr;
8922         }
8923       } else if (getOpenMPCaptureRegionForClause(
8924                      DSAStack->getCurrentDirective(), OMPC_schedule) !=
8925                      OMPD_unknown &&
8926                  !CurContext->isDependentContext()) {
8927         ValExpr = MakeFullExpr(ValExpr).get();
8928         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8929         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8930         HelperValStmt = buildPreInits(Context, Captures);
8931       }
8932     }
8933   }
8934 
8935   return new (Context)
8936       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
8937                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
8938 }
8939 
8940 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8941                                    SourceLocation StartLoc,
8942                                    SourceLocation EndLoc) {
8943   OMPClause *Res = nullptr;
8944   switch (Kind) {
8945   case OMPC_ordered:
8946     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8947     break;
8948   case OMPC_nowait:
8949     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8950     break;
8951   case OMPC_untied:
8952     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8953     break;
8954   case OMPC_mergeable:
8955     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8956     break;
8957   case OMPC_read:
8958     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8959     break;
8960   case OMPC_write:
8961     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8962     break;
8963   case OMPC_update:
8964     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8965     break;
8966   case OMPC_capture:
8967     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8968     break;
8969   case OMPC_seq_cst:
8970     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8971     break;
8972   case OMPC_threads:
8973     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8974     break;
8975   case OMPC_simd:
8976     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8977     break;
8978   case OMPC_nogroup:
8979     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8980     break;
8981   case OMPC_if:
8982   case OMPC_final:
8983   case OMPC_num_threads:
8984   case OMPC_safelen:
8985   case OMPC_simdlen:
8986   case OMPC_collapse:
8987   case OMPC_schedule:
8988   case OMPC_private:
8989   case OMPC_firstprivate:
8990   case OMPC_lastprivate:
8991   case OMPC_shared:
8992   case OMPC_reduction:
8993   case OMPC_task_reduction:
8994   case OMPC_in_reduction:
8995   case OMPC_linear:
8996   case OMPC_aligned:
8997   case OMPC_copyin:
8998   case OMPC_copyprivate:
8999   case OMPC_default:
9000   case OMPC_proc_bind:
9001   case OMPC_threadprivate:
9002   case OMPC_flush:
9003   case OMPC_depend:
9004   case OMPC_device:
9005   case OMPC_map:
9006   case OMPC_num_teams:
9007   case OMPC_thread_limit:
9008   case OMPC_priority:
9009   case OMPC_grainsize:
9010   case OMPC_num_tasks:
9011   case OMPC_hint:
9012   case OMPC_dist_schedule:
9013   case OMPC_defaultmap:
9014   case OMPC_unknown:
9015   case OMPC_uniform:
9016   case OMPC_to:
9017   case OMPC_from:
9018   case OMPC_use_device_ptr:
9019   case OMPC_is_device_ptr:
9020     llvm_unreachable("Clause is not allowed.");
9021   }
9022   return Res;
9023 }
9024 
9025 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9026                                          SourceLocation EndLoc) {
9027   DSAStack->setNowaitRegion();
9028   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9029 }
9030 
9031 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9032                                          SourceLocation EndLoc) {
9033   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9034 }
9035 
9036 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9037                                             SourceLocation EndLoc) {
9038   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9039 }
9040 
9041 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9042                                        SourceLocation EndLoc) {
9043   return new (Context) OMPReadClause(StartLoc, EndLoc);
9044 }
9045 
9046 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9047                                         SourceLocation EndLoc) {
9048   return new (Context) OMPWriteClause(StartLoc, EndLoc);
9049 }
9050 
9051 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9052                                          SourceLocation EndLoc) {
9053   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9054 }
9055 
9056 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9057                                           SourceLocation EndLoc) {
9058   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9059 }
9060 
9061 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9062                                          SourceLocation EndLoc) {
9063   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9064 }
9065 
9066 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9067                                           SourceLocation EndLoc) {
9068   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9069 }
9070 
9071 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9072                                        SourceLocation EndLoc) {
9073   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9074 }
9075 
9076 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9077                                           SourceLocation EndLoc) {
9078   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9079 }
9080 
9081 OMPClause *Sema::ActOnOpenMPVarListClause(
9082     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9083     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9084     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
9085     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
9086     OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9087     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9088     SourceLocation DepLinMapLoc) {
9089   OMPClause *Res = nullptr;
9090   switch (Kind) {
9091   case OMPC_private:
9092     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9093     break;
9094   case OMPC_firstprivate:
9095     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9096     break;
9097   case OMPC_lastprivate:
9098     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9099     break;
9100   case OMPC_shared:
9101     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9102     break;
9103   case OMPC_reduction:
9104     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9105                                      EndLoc, ReductionIdScopeSpec, ReductionId);
9106     break;
9107   case OMPC_task_reduction:
9108     Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9109                                          EndLoc, ReductionIdScopeSpec,
9110                                          ReductionId);
9111     break;
9112   case OMPC_in_reduction:
9113     Res =
9114         ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9115                                      EndLoc, ReductionIdScopeSpec, ReductionId);
9116     break;
9117   case OMPC_linear:
9118     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
9119                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
9120     break;
9121   case OMPC_aligned:
9122     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9123                                    ColonLoc, EndLoc);
9124     break;
9125   case OMPC_copyin:
9126     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9127     break;
9128   case OMPC_copyprivate:
9129     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9130     break;
9131   case OMPC_flush:
9132     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9133     break;
9134   case OMPC_depend:
9135     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
9136                                   StartLoc, LParenLoc, EndLoc);
9137     break;
9138   case OMPC_map:
9139     Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9140                                DepLinMapLoc, ColonLoc, VarList, StartLoc,
9141                                LParenLoc, EndLoc);
9142     break;
9143   case OMPC_to:
9144     Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9145     break;
9146   case OMPC_from:
9147     Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9148     break;
9149   case OMPC_use_device_ptr:
9150     Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9151     break;
9152   case OMPC_is_device_ptr:
9153     Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9154     break;
9155   case OMPC_if:
9156   case OMPC_final:
9157   case OMPC_num_threads:
9158   case OMPC_safelen:
9159   case OMPC_simdlen:
9160   case OMPC_collapse:
9161   case OMPC_default:
9162   case OMPC_proc_bind:
9163   case OMPC_schedule:
9164   case OMPC_ordered:
9165   case OMPC_nowait:
9166   case OMPC_untied:
9167   case OMPC_mergeable:
9168   case OMPC_threadprivate:
9169   case OMPC_read:
9170   case OMPC_write:
9171   case OMPC_update:
9172   case OMPC_capture:
9173   case OMPC_seq_cst:
9174   case OMPC_device:
9175   case OMPC_threads:
9176   case OMPC_simd:
9177   case OMPC_num_teams:
9178   case OMPC_thread_limit:
9179   case OMPC_priority:
9180   case OMPC_grainsize:
9181   case OMPC_nogroup:
9182   case OMPC_num_tasks:
9183   case OMPC_hint:
9184   case OMPC_dist_schedule:
9185   case OMPC_defaultmap:
9186   case OMPC_unknown:
9187   case OMPC_uniform:
9188     llvm_unreachable("Clause is not allowed.");
9189   }
9190   return Res;
9191 }
9192 
9193 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
9194                                        ExprObjectKind OK, SourceLocation Loc) {
9195   ExprResult Res = BuildDeclRefExpr(
9196       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9197   if (!Res.isUsable())
9198     return ExprError();
9199   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9200     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9201     if (!Res.isUsable())
9202       return ExprError();
9203   }
9204   if (VK != VK_LValue && Res.get()->isGLValue()) {
9205     Res = DefaultLvalueConversion(Res.get());
9206     if (!Res.isUsable())
9207       return ExprError();
9208   }
9209   return Res;
9210 }
9211 
9212 static std::pair<ValueDecl *, bool>
9213 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9214                SourceRange &ERange, bool AllowArraySection = false) {
9215   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9216       RefExpr->containsUnexpandedParameterPack())
9217     return std::make_pair(nullptr, true);
9218 
9219   // OpenMP [3.1, C/C++]
9220   //  A list item is a variable name.
9221   // OpenMP  [2.9.3.3, Restrictions, p.1]
9222   //  A variable that is part of another variable (as an array or
9223   //  structure element) cannot appear in a private clause.
9224   RefExpr = RefExpr->IgnoreParens();
9225   enum {
9226     NoArrayExpr = -1,
9227     ArraySubscript = 0,
9228     OMPArraySection = 1
9229   } IsArrayExpr = NoArrayExpr;
9230   if (AllowArraySection) {
9231     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
9232       auto *Base = ASE->getBase()->IgnoreParenImpCasts();
9233       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9234         Base = TempASE->getBase()->IgnoreParenImpCasts();
9235       RefExpr = Base;
9236       IsArrayExpr = ArraySubscript;
9237     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
9238       auto *Base = OASE->getBase()->IgnoreParenImpCasts();
9239       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9240         Base = TempOASE->getBase()->IgnoreParenImpCasts();
9241       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9242         Base = TempASE->getBase()->IgnoreParenImpCasts();
9243       RefExpr = Base;
9244       IsArrayExpr = OMPArraySection;
9245     }
9246   }
9247   ELoc = RefExpr->getExprLoc();
9248   ERange = RefExpr->getSourceRange();
9249   RefExpr = RefExpr->IgnoreParenImpCasts();
9250   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9251   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9252   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9253       (S.getCurrentThisType().isNull() || !ME ||
9254        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9255        !isa<FieldDecl>(ME->getMemberDecl()))) {
9256     if (IsArrayExpr != NoArrayExpr)
9257       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9258                                                          << ERange;
9259     else {
9260       S.Diag(ELoc,
9261              AllowArraySection
9262                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
9263                  : diag::err_omp_expected_var_name_member_expr)
9264           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9265     }
9266     return std::make_pair(nullptr, false);
9267   }
9268   return std::make_pair(
9269       getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
9270 }
9271 
9272 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9273                                           SourceLocation StartLoc,
9274                                           SourceLocation LParenLoc,
9275                                           SourceLocation EndLoc) {
9276   SmallVector<Expr *, 8> Vars;
9277   SmallVector<Expr *, 8> PrivateCopies;
9278   for (auto &RefExpr : VarList) {
9279     assert(RefExpr && "NULL expr in OpenMP private clause.");
9280     SourceLocation ELoc;
9281     SourceRange ERange;
9282     Expr *SimpleRefExpr = RefExpr;
9283     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9284     if (Res.second) {
9285       // It will be analyzed later.
9286       Vars.push_back(RefExpr);
9287       PrivateCopies.push_back(nullptr);
9288     }
9289     ValueDecl *D = Res.first;
9290     if (!D)
9291       continue;
9292 
9293     QualType Type = D->getType();
9294     auto *VD = dyn_cast<VarDecl>(D);
9295 
9296     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9297     //  A variable that appears in a private clause must not have an incomplete
9298     //  type or a reference type.
9299     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
9300       continue;
9301     Type = Type.getNonReferenceType();
9302 
9303     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9304     // in a Construct]
9305     //  Variables with the predetermined data-sharing attributes may not be
9306     //  listed in data-sharing attributes clauses, except for the cases
9307     //  listed below. For these exceptions only, listing a predetermined
9308     //  variable in a data-sharing attribute clause is allowed and overrides
9309     //  the variable's predetermined data-sharing attributes.
9310     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9311     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
9312       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9313                                           << getOpenMPClauseName(OMPC_private);
9314       ReportOriginalDSA(*this, DSAStack, D, DVar);
9315       continue;
9316     }
9317 
9318     auto CurrDir = DSAStack->getCurrentDirective();
9319     // Variably modified types are not supported for tasks.
9320     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
9321         isOpenMPTaskingDirective(CurrDir)) {
9322       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9323           << getOpenMPClauseName(OMPC_private) << Type
9324           << getOpenMPDirectiveName(CurrDir);
9325       bool IsDecl =
9326           !VD ||
9327           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9328       Diag(D->getLocation(),
9329            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9330           << D;
9331       continue;
9332     }
9333 
9334     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9335     // A list item cannot appear in both a map clause and a data-sharing
9336     // attribute clause on the same construct
9337     if (CurrDir == OMPD_target || CurrDir == OMPD_target_parallel ||
9338         CurrDir == OMPD_target_teams ||
9339         CurrDir == OMPD_target_teams_distribute ||
9340         CurrDir == OMPD_target_teams_distribute_parallel_for ||
9341         CurrDir == OMPD_target_teams_distribute_parallel_for_simd ||
9342         CurrDir == OMPD_target_teams_distribute_simd ||
9343         CurrDir == OMPD_target_parallel_for_simd ||
9344         CurrDir == OMPD_target_parallel_for) {
9345       OpenMPClauseKind ConflictKind;
9346       if (DSAStack->checkMappableExprComponentListsForDecl(
9347               VD, /*CurrentRegionOnly=*/true,
9348               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9349                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
9350                 ConflictKind = WhereFoundClauseKind;
9351                 return true;
9352               })) {
9353         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
9354             << getOpenMPClauseName(OMPC_private)
9355             << getOpenMPClauseName(ConflictKind)
9356             << getOpenMPDirectiveName(CurrDir);
9357         ReportOriginalDSA(*this, DSAStack, D, DVar);
9358         continue;
9359       }
9360     }
9361 
9362     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9363     //  A variable of class type (or array thereof) that appears in a private
9364     //  clause requires an accessible, unambiguous default constructor for the
9365     //  class type.
9366     // Generate helper private variable and initialize it with the default
9367     // value. The address of the original variable is replaced by the address of
9368     // the new private variable in CodeGen. This new variable is not added to
9369     // IdResolver, so the code in the OpenMP region uses original variable for
9370     // proper diagnostics.
9371     Type = Type.getUnqualifiedType();
9372     auto VDPrivate =
9373         buildVarDecl(*this, ELoc, Type, D->getName(),
9374                      D->hasAttrs() ? &D->getAttrs() : nullptr,
9375                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
9376     ActOnUninitializedDecl(VDPrivate);
9377     if (VDPrivate->isInvalidDecl())
9378       continue;
9379     auto VDPrivateRefExpr = buildDeclRefExpr(
9380         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
9381 
9382     DeclRefExpr *Ref = nullptr;
9383     if (!VD && !CurContext->isDependentContext())
9384       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9385     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
9386     Vars.push_back((VD || CurContext->isDependentContext())
9387                        ? RefExpr->IgnoreParens()
9388                        : Ref);
9389     PrivateCopies.push_back(VDPrivateRefExpr);
9390   }
9391 
9392   if (Vars.empty())
9393     return nullptr;
9394 
9395   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9396                                   PrivateCopies);
9397 }
9398 
9399 namespace {
9400 class DiagsUninitializedSeveretyRAII {
9401 private:
9402   DiagnosticsEngine &Diags;
9403   SourceLocation SavedLoc;
9404   bool IsIgnored;
9405 
9406 public:
9407   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9408                                  bool IsIgnored)
9409       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9410     if (!IsIgnored) {
9411       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9412                         /*Map*/ diag::Severity::Ignored, Loc);
9413     }
9414   }
9415   ~DiagsUninitializedSeveretyRAII() {
9416     if (!IsIgnored)
9417       Diags.popMappings(SavedLoc);
9418   }
9419 };
9420 }
9421 
9422 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9423                                                SourceLocation StartLoc,
9424                                                SourceLocation LParenLoc,
9425                                                SourceLocation EndLoc) {
9426   SmallVector<Expr *, 8> Vars;
9427   SmallVector<Expr *, 8> PrivateCopies;
9428   SmallVector<Expr *, 8> Inits;
9429   SmallVector<Decl *, 4> ExprCaptures;
9430   bool IsImplicitClause =
9431       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
9432   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
9433 
9434   for (auto &RefExpr : VarList) {
9435     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
9436     SourceLocation ELoc;
9437     SourceRange ERange;
9438     Expr *SimpleRefExpr = RefExpr;
9439     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9440     if (Res.second) {
9441       // It will be analyzed later.
9442       Vars.push_back(RefExpr);
9443       PrivateCopies.push_back(nullptr);
9444       Inits.push_back(nullptr);
9445     }
9446     ValueDecl *D = Res.first;
9447     if (!D)
9448       continue;
9449 
9450     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
9451     QualType Type = D->getType();
9452     auto *VD = dyn_cast<VarDecl>(D);
9453 
9454     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9455     //  A variable that appears in a private clause must not have an incomplete
9456     //  type or a reference type.
9457     if (RequireCompleteType(ELoc, Type,
9458                             diag::err_omp_firstprivate_incomplete_type))
9459       continue;
9460     Type = Type.getNonReferenceType();
9461 
9462     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9463     //  A variable of class type (or array thereof) that appears in a private
9464     //  clause requires an accessible, unambiguous copy constructor for the
9465     //  class type.
9466     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
9467 
9468     // If an implicit firstprivate variable found it was checked already.
9469     DSAStackTy::DSAVarData TopDVar;
9470     if (!IsImplicitClause) {
9471       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9472       TopDVar = DVar;
9473       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9474       bool IsConstant = ElemType.isConstant(Context);
9475       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9476       //  A list item that specifies a given variable may not appear in more
9477       // than one clause on the same directive, except that a variable may be
9478       //  specified in both firstprivate and lastprivate clauses.
9479       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9480       // A list item may appear in a firstprivate or lastprivate clause but not
9481       // both.
9482       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
9483           (isOpenMPDistributeDirective(CurrDir) ||
9484            DVar.CKind != OMPC_lastprivate) &&
9485           DVar.RefExpr) {
9486         Diag(ELoc, diag::err_omp_wrong_dsa)
9487             << getOpenMPClauseName(DVar.CKind)
9488             << getOpenMPClauseName(OMPC_firstprivate);
9489         ReportOriginalDSA(*this, DSAStack, D, DVar);
9490         continue;
9491       }
9492 
9493       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9494       // in a Construct]
9495       //  Variables with the predetermined data-sharing attributes may not be
9496       //  listed in data-sharing attributes clauses, except for the cases
9497       //  listed below. For these exceptions only, listing a predetermined
9498       //  variable in a data-sharing attribute clause is allowed and overrides
9499       //  the variable's predetermined data-sharing attributes.
9500       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9501       // in a Construct, C/C++, p.2]
9502       //  Variables with const-qualified type having no mutable member may be
9503       //  listed in a firstprivate clause, even if they are static data members.
9504       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
9505           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9506         Diag(ELoc, diag::err_omp_wrong_dsa)
9507             << getOpenMPClauseName(DVar.CKind)
9508             << getOpenMPClauseName(OMPC_firstprivate);
9509         ReportOriginalDSA(*this, DSAStack, D, DVar);
9510         continue;
9511       }
9512 
9513       // OpenMP [2.9.3.4, Restrictions, p.2]
9514       //  A list item that is private within a parallel region must not appear
9515       //  in a firstprivate clause on a worksharing construct if any of the
9516       //  worksharing regions arising from the worksharing construct ever bind
9517       //  to any of the parallel regions arising from the parallel construct.
9518       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9519       // A list item that is private within a teams region must not appear in a
9520       // firstprivate clause on a distribute construct if any of the distribute
9521       // regions arising from the distribute construct ever bind to any of the
9522       // teams regions arising from the teams construct.
9523       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9524       // A list item that appears in a reduction clause of a teams construct
9525       // must not appear in a firstprivate clause on a distribute construct if
9526       // any of the distribute regions arising from the distribute construct
9527       // ever bind to any of the teams regions arising from the teams construct.
9528       if ((isOpenMPWorksharingDirective(CurrDir) ||
9529            isOpenMPDistributeDirective(CurrDir)) &&
9530           !isOpenMPParallelDirective(CurrDir) &&
9531           !isOpenMPTeamsDirective(CurrDir)) {
9532         DVar = DSAStack->getImplicitDSA(D, true);
9533         if (DVar.CKind != OMPC_shared &&
9534             (isOpenMPParallelDirective(DVar.DKind) ||
9535              isOpenMPTeamsDirective(DVar.DKind) ||
9536              DVar.DKind == OMPD_unknown)) {
9537           Diag(ELoc, diag::err_omp_required_access)
9538               << getOpenMPClauseName(OMPC_firstprivate)
9539               << getOpenMPClauseName(OMPC_shared);
9540           ReportOriginalDSA(*this, DSAStack, D, DVar);
9541           continue;
9542         }
9543       }
9544       // OpenMP [2.9.3.4, Restrictions, p.3]
9545       //  A list item that appears in a reduction clause of a parallel construct
9546       //  must not appear in a firstprivate clause on a worksharing or task
9547       //  construct if any of the worksharing or task regions arising from the
9548       //  worksharing or task construct ever bind to any of the parallel regions
9549       //  arising from the parallel construct.
9550       // OpenMP [2.9.3.4, Restrictions, p.4]
9551       //  A list item that appears in a reduction clause in worksharing
9552       //  construct must not appear in a firstprivate clause in a task construct
9553       //  encountered during execution of any of the worksharing regions arising
9554       //  from the worksharing construct.
9555       if (isOpenMPTaskingDirective(CurrDir)) {
9556         DVar = DSAStack->hasInnermostDSA(
9557             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
9558             [](OpenMPDirectiveKind K) -> bool {
9559               return isOpenMPParallelDirective(K) ||
9560                      isOpenMPWorksharingDirective(K) ||
9561                      isOpenMPTeamsDirective(K);
9562             },
9563             /*FromParent=*/true);
9564         if (DVar.CKind == OMPC_reduction &&
9565             (isOpenMPParallelDirective(DVar.DKind) ||
9566              isOpenMPWorksharingDirective(DVar.DKind) ||
9567              isOpenMPTeamsDirective(DVar.DKind))) {
9568           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9569               << getOpenMPDirectiveName(DVar.DKind);
9570           ReportOriginalDSA(*this, DSAStack, D, DVar);
9571           continue;
9572         }
9573       }
9574 
9575       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9576       // A list item cannot appear in both a map clause and a data-sharing
9577       // attribute clause on the same construct
9578       if (isOpenMPTargetExecutionDirective(CurrDir)) {
9579         OpenMPClauseKind ConflictKind;
9580         if (DSAStack->checkMappableExprComponentListsForDecl(
9581                 VD, /*CurrentRegionOnly=*/true,
9582                 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9583                     OpenMPClauseKind WhereFoundClauseKind) -> bool {
9584                   ConflictKind = WhereFoundClauseKind;
9585                   return true;
9586                 })) {
9587           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
9588               << getOpenMPClauseName(OMPC_firstprivate)
9589               << getOpenMPClauseName(ConflictKind)
9590               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9591           ReportOriginalDSA(*this, DSAStack, D, DVar);
9592           continue;
9593         }
9594       }
9595     }
9596 
9597     // Variably modified types are not supported for tasks.
9598     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
9599         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
9600       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9601           << getOpenMPClauseName(OMPC_firstprivate) << Type
9602           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9603       bool IsDecl =
9604           !VD ||
9605           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9606       Diag(D->getLocation(),
9607            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9608           << D;
9609       continue;
9610     }
9611 
9612     Type = Type.getUnqualifiedType();
9613     auto VDPrivate =
9614         buildVarDecl(*this, ELoc, Type, D->getName(),
9615                      D->hasAttrs() ? &D->getAttrs() : nullptr,
9616                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
9617     // Generate helper private variable and initialize it with the value of the
9618     // original variable. The address of the original variable is replaced by
9619     // the address of the new private variable in the CodeGen. This new variable
9620     // is not added to IdResolver, so the code in the OpenMP region uses
9621     // original variable for proper diagnostics and variable capturing.
9622     Expr *VDInitRefExpr = nullptr;
9623     // For arrays generate initializer for single element and replace it by the
9624     // original array element in CodeGen.
9625     if (Type->isArrayType()) {
9626       auto VDInit =
9627           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
9628       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
9629       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
9630       ElemType = ElemType.getUnqualifiedType();
9631       auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
9632                                       ".firstprivate.temp");
9633       InitializedEntity Entity =
9634           InitializedEntity::InitializeVariable(VDInitTemp);
9635       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9636 
9637       InitializationSequence InitSeq(*this, Entity, Kind, Init);
9638       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9639       if (Result.isInvalid())
9640         VDPrivate->setInvalidDecl();
9641       else
9642         VDPrivate->setInit(Result.getAs<Expr>());
9643       // Remove temp variable declaration.
9644       Context.Deallocate(VDInitTemp);
9645     } else {
9646       auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9647                                   ".firstprivate.temp");
9648       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9649                                        RefExpr->getExprLoc());
9650       AddInitializerToDecl(VDPrivate,
9651                            DefaultLvalueConversion(VDInitRefExpr).get(),
9652                            /*DirectInit=*/false);
9653     }
9654     if (VDPrivate->isInvalidDecl()) {
9655       if (IsImplicitClause) {
9656         Diag(RefExpr->getExprLoc(),
9657              diag::note_omp_task_predetermined_firstprivate_here);
9658       }
9659       continue;
9660     }
9661     CurContext->addDecl(VDPrivate);
9662     auto VDPrivateRefExpr = buildDeclRefExpr(
9663         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9664         RefExpr->getExprLoc());
9665     DeclRefExpr *Ref = nullptr;
9666     if (!VD && !CurContext->isDependentContext()) {
9667       if (TopDVar.CKind == OMPC_lastprivate)
9668         Ref = TopDVar.PrivateCopy;
9669       else {
9670         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9671         if (!IsOpenMPCapturedDecl(D))
9672           ExprCaptures.push_back(Ref->getDecl());
9673       }
9674     }
9675     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
9676     Vars.push_back((VD || CurContext->isDependentContext())
9677                        ? RefExpr->IgnoreParens()
9678                        : Ref);
9679     PrivateCopies.push_back(VDPrivateRefExpr);
9680     Inits.push_back(VDInitRefExpr);
9681   }
9682 
9683   if (Vars.empty())
9684     return nullptr;
9685 
9686   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9687                                        Vars, PrivateCopies, Inits,
9688                                        buildPreInits(Context, ExprCaptures));
9689 }
9690 
9691 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9692                                               SourceLocation StartLoc,
9693                                               SourceLocation LParenLoc,
9694                                               SourceLocation EndLoc) {
9695   SmallVector<Expr *, 8> Vars;
9696   SmallVector<Expr *, 8> SrcExprs;
9697   SmallVector<Expr *, 8> DstExprs;
9698   SmallVector<Expr *, 8> AssignmentOps;
9699   SmallVector<Decl *, 4> ExprCaptures;
9700   SmallVector<Expr *, 4> ExprPostUpdates;
9701   for (auto &RefExpr : VarList) {
9702     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
9703     SourceLocation ELoc;
9704     SourceRange ERange;
9705     Expr *SimpleRefExpr = RefExpr;
9706     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9707     if (Res.second) {
9708       // It will be analyzed later.
9709       Vars.push_back(RefExpr);
9710       SrcExprs.push_back(nullptr);
9711       DstExprs.push_back(nullptr);
9712       AssignmentOps.push_back(nullptr);
9713     }
9714     ValueDecl *D = Res.first;
9715     if (!D)
9716       continue;
9717 
9718     QualType Type = D->getType();
9719     auto *VD = dyn_cast<VarDecl>(D);
9720 
9721     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9722     //  A variable that appears in a lastprivate clause must not have an
9723     //  incomplete type or a reference type.
9724     if (RequireCompleteType(ELoc, Type,
9725                             diag::err_omp_lastprivate_incomplete_type))
9726       continue;
9727     Type = Type.getNonReferenceType();
9728 
9729     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9730     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9731     // in a Construct]
9732     //  Variables with the predetermined data-sharing attributes may not be
9733     //  listed in data-sharing attributes clauses, except for the cases
9734     //  listed below.
9735     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9736     // A list item may appear in a firstprivate or lastprivate clause but not
9737     // both.
9738     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9739     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
9740         (isOpenMPDistributeDirective(CurrDir) ||
9741          DVar.CKind != OMPC_firstprivate) &&
9742         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
9743       Diag(ELoc, diag::err_omp_wrong_dsa)
9744           << getOpenMPClauseName(DVar.CKind)
9745           << getOpenMPClauseName(OMPC_lastprivate);
9746       ReportOriginalDSA(*this, DSAStack, D, DVar);
9747       continue;
9748     }
9749 
9750     // OpenMP [2.14.3.5, Restrictions, p.2]
9751     // A list item that is private within a parallel region, or that appears in
9752     // the reduction clause of a parallel construct, must not appear in a
9753     // lastprivate clause on a worksharing construct if any of the corresponding
9754     // worksharing regions ever binds to any of the corresponding parallel
9755     // regions.
9756     DSAStackTy::DSAVarData TopDVar = DVar;
9757     if (isOpenMPWorksharingDirective(CurrDir) &&
9758         !isOpenMPParallelDirective(CurrDir) &&
9759         !isOpenMPTeamsDirective(CurrDir)) {
9760       DVar = DSAStack->getImplicitDSA(D, true);
9761       if (DVar.CKind != OMPC_shared) {
9762         Diag(ELoc, diag::err_omp_required_access)
9763             << getOpenMPClauseName(OMPC_lastprivate)
9764             << getOpenMPClauseName(OMPC_shared);
9765         ReportOriginalDSA(*this, DSAStack, D, DVar);
9766         continue;
9767       }
9768     }
9769 
9770     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
9771     //  A variable of class type (or array thereof) that appears in a
9772     //  lastprivate clause requires an accessible, unambiguous default
9773     //  constructor for the class type, unless the list item is also specified
9774     //  in a firstprivate clause.
9775     //  A variable of class type (or array thereof) that appears in a
9776     //  lastprivate clause requires an accessible, unambiguous copy assignment
9777     //  operator for the class type.
9778     Type = Context.getBaseElementType(Type).getNonReferenceType();
9779     auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
9780                                Type.getUnqualifiedType(), ".lastprivate.src",
9781                                D->hasAttrs() ? &D->getAttrs() : nullptr);
9782     auto *PseudoSrcExpr =
9783         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
9784     auto *DstVD =
9785         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
9786                      D->hasAttrs() ? &D->getAttrs() : nullptr);
9787     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
9788     // For arrays generate assignment operation for single element and replace
9789     // it by the original array element in CodeGen.
9790     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
9791                                    PseudoDstExpr, PseudoSrcExpr);
9792     if (AssignmentOp.isInvalid())
9793       continue;
9794     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
9795                                        /*DiscardedValue=*/true);
9796     if (AssignmentOp.isInvalid())
9797       continue;
9798 
9799     DeclRefExpr *Ref = nullptr;
9800     if (!VD && !CurContext->isDependentContext()) {
9801       if (TopDVar.CKind == OMPC_firstprivate)
9802         Ref = TopDVar.PrivateCopy;
9803       else {
9804         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9805         if (!IsOpenMPCapturedDecl(D))
9806           ExprCaptures.push_back(Ref->getDecl());
9807       }
9808       if (TopDVar.CKind == OMPC_firstprivate ||
9809           (!IsOpenMPCapturedDecl(D) &&
9810            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
9811         ExprResult RefRes = DefaultLvalueConversion(Ref);
9812         if (!RefRes.isUsable())
9813           continue;
9814         ExprResult PostUpdateRes =
9815             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
9816                        RefRes.get());
9817         if (!PostUpdateRes.isUsable())
9818           continue;
9819         ExprPostUpdates.push_back(
9820             IgnoredValueConversions(PostUpdateRes.get()).get());
9821       }
9822     }
9823     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
9824     Vars.push_back((VD || CurContext->isDependentContext())
9825                        ? RefExpr->IgnoreParens()
9826                        : Ref);
9827     SrcExprs.push_back(PseudoSrcExpr);
9828     DstExprs.push_back(PseudoDstExpr);
9829     AssignmentOps.push_back(AssignmentOp.get());
9830   }
9831 
9832   if (Vars.empty())
9833     return nullptr;
9834 
9835   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9836                                       Vars, SrcExprs, DstExprs, AssignmentOps,
9837                                       buildPreInits(Context, ExprCaptures),
9838                                       buildPostUpdate(*this, ExprPostUpdates));
9839 }
9840 
9841 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9842                                          SourceLocation StartLoc,
9843                                          SourceLocation LParenLoc,
9844                                          SourceLocation EndLoc) {
9845   SmallVector<Expr *, 8> Vars;
9846   for (auto &RefExpr : VarList) {
9847     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
9848     SourceLocation ELoc;
9849     SourceRange ERange;
9850     Expr *SimpleRefExpr = RefExpr;
9851     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9852     if (Res.second) {
9853       // It will be analyzed later.
9854       Vars.push_back(RefExpr);
9855     }
9856     ValueDecl *D = Res.first;
9857     if (!D)
9858       continue;
9859 
9860     auto *VD = dyn_cast<VarDecl>(D);
9861     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9862     // in a Construct]
9863     //  Variables with the predetermined data-sharing attributes may not be
9864     //  listed in data-sharing attributes clauses, except for the cases
9865     //  listed below. For these exceptions only, listing a predetermined
9866     //  variable in a data-sharing attribute clause is allowed and overrides
9867     //  the variable's predetermined data-sharing attributes.
9868     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9869     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9870         DVar.RefExpr) {
9871       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9872                                           << getOpenMPClauseName(OMPC_shared);
9873       ReportOriginalDSA(*this, DSAStack, D, DVar);
9874       continue;
9875     }
9876 
9877     DeclRefExpr *Ref = nullptr;
9878     if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
9879       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9880     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
9881     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9882                        ? RefExpr->IgnoreParens()
9883                        : Ref);
9884   }
9885 
9886   if (Vars.empty())
9887     return nullptr;
9888 
9889   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9890 }
9891 
9892 namespace {
9893 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9894   DSAStackTy *Stack;
9895 
9896 public:
9897   bool VisitDeclRefExpr(DeclRefExpr *E) {
9898     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
9899       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
9900       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9901         return false;
9902       if (DVar.CKind != OMPC_unknown)
9903         return true;
9904       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9905           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
9906           /*FromParent=*/true);
9907       if (DVarPrivate.CKind != OMPC_unknown)
9908         return true;
9909       return false;
9910     }
9911     return false;
9912   }
9913   bool VisitStmt(Stmt *S) {
9914     for (auto Child : S->children()) {
9915       if (Child && Visit(Child))
9916         return true;
9917     }
9918     return false;
9919   }
9920   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
9921 };
9922 } // namespace
9923 
9924 namespace {
9925 // Transform MemberExpression for specified FieldDecl of current class to
9926 // DeclRefExpr to specified OMPCapturedExprDecl.
9927 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9928   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9929   ValueDecl *Field;
9930   DeclRefExpr *CapturedExpr;
9931 
9932 public:
9933   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9934       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9935 
9936   ExprResult TransformMemberExpr(MemberExpr *E) {
9937     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9938         E->getMemberDecl() == Field) {
9939       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
9940       return CapturedExpr;
9941     }
9942     return BaseTransform::TransformMemberExpr(E);
9943   }
9944   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9945 };
9946 } // namespace
9947 
9948 template <typename T, typename U>
9949 static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
9950                             const llvm::function_ref<T(ValueDecl *)> Gen) {
9951   for (auto &Set : Lookups) {
9952     for (auto *D : Set) {
9953       if (auto Res = Gen(cast<ValueDecl>(D)))
9954         return Res;
9955     }
9956   }
9957   return T();
9958 }
9959 
9960 static ExprResult
9961 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9962                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9963                          const DeclarationNameInfo &ReductionId, QualType Ty,
9964                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9965   if (ReductionIdScopeSpec.isInvalid())
9966     return ExprError();
9967   SmallVector<UnresolvedSet<8>, 4> Lookups;
9968   if (S) {
9969     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9970     Lookup.suppressDiagnostics();
9971     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9972       auto *D = Lookup.getRepresentativeDecl();
9973       do {
9974         S = S->getParent();
9975       } while (S && !S->isDeclScope(D));
9976       if (S)
9977         S = S->getParent();
9978       Lookups.push_back(UnresolvedSet<8>());
9979       Lookups.back().append(Lookup.begin(), Lookup.end());
9980       Lookup.clear();
9981     }
9982   } else if (auto *ULE =
9983                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9984     Lookups.push_back(UnresolvedSet<8>());
9985     Decl *PrevD = nullptr;
9986     for (auto *D : ULE->decls()) {
9987       if (D == PrevD)
9988         Lookups.push_back(UnresolvedSet<8>());
9989       else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9990         Lookups.back().addDecl(DRD);
9991       PrevD = D;
9992     }
9993   }
9994   if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
9995       Ty->isInstantiationDependentType() ||
9996       Ty->containsUnexpandedParameterPack() ||
9997       filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9998         return !D->isInvalidDecl() &&
9999                (D->getType()->isDependentType() ||
10000                 D->getType()->isInstantiationDependentType() ||
10001                 D->getType()->containsUnexpandedParameterPack());
10002       })) {
10003     UnresolvedSet<8> ResSet;
10004     for (auto &Set : Lookups) {
10005       ResSet.append(Set.begin(), Set.end());
10006       // The last item marks the end of all declarations at the specified scope.
10007       ResSet.addDecl(Set[Set.size() - 1]);
10008     }
10009     return UnresolvedLookupExpr::Create(
10010         SemaRef.Context, /*NamingClass=*/nullptr,
10011         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10012         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10013   }
10014   if (auto *VD = filterLookupForUDR<ValueDecl *>(
10015           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10016             if (!D->isInvalidDecl() &&
10017                 SemaRef.Context.hasSameType(D->getType(), Ty))
10018               return D;
10019             return nullptr;
10020           }))
10021     return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10022   if (auto *VD = filterLookupForUDR<ValueDecl *>(
10023           Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10024             if (!D->isInvalidDecl() &&
10025                 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10026                 !Ty.isMoreQualifiedThan(D->getType()))
10027               return D;
10028             return nullptr;
10029           })) {
10030     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10031                        /*DetectVirtual=*/false);
10032     if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10033       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10034               VD->getType().getUnqualifiedType()))) {
10035         if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10036                                          /*DiagID=*/0) !=
10037             Sema::AR_inaccessible) {
10038           SemaRef.BuildBasePathArray(Paths, BasePath);
10039           return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10040         }
10041       }
10042     }
10043   }
10044   if (ReductionIdScopeSpec.isSet()) {
10045     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10046     return ExprError();
10047   }
10048   return ExprEmpty();
10049 }
10050 
10051 namespace {
10052 /// Data for the reduction-based clauses.
10053 struct ReductionData {
10054   /// List of original reduction items.
10055   SmallVector<Expr *, 8> Vars;
10056   /// List of private copies of the reduction items.
10057   SmallVector<Expr *, 8> Privates;
10058   /// LHS expressions for the reduction_op expressions.
10059   SmallVector<Expr *, 8> LHSs;
10060   /// RHS expressions for the reduction_op expressions.
10061   SmallVector<Expr *, 8> RHSs;
10062   /// Reduction operation expression.
10063   SmallVector<Expr *, 8> ReductionOps;
10064   /// Taskgroup descriptors for the corresponding reduction items in
10065   /// in_reduction clauses.
10066   SmallVector<Expr *, 8> TaskgroupDescriptors;
10067   /// List of captures for clause.
10068   SmallVector<Decl *, 4> ExprCaptures;
10069   /// List of postupdate expressions.
10070   SmallVector<Expr *, 4> ExprPostUpdates;
10071   ReductionData() = delete;
10072   /// Reserves required memory for the reduction data.
10073   ReductionData(unsigned Size) {
10074     Vars.reserve(Size);
10075     Privates.reserve(Size);
10076     LHSs.reserve(Size);
10077     RHSs.reserve(Size);
10078     ReductionOps.reserve(Size);
10079     TaskgroupDescriptors.reserve(Size);
10080     ExprCaptures.reserve(Size);
10081     ExprPostUpdates.reserve(Size);
10082   }
10083   /// Stores reduction item and reduction operation only (required for dependent
10084   /// reduction item).
10085   void push(Expr *Item, Expr *ReductionOp) {
10086     Vars.emplace_back(Item);
10087     Privates.emplace_back(nullptr);
10088     LHSs.emplace_back(nullptr);
10089     RHSs.emplace_back(nullptr);
10090     ReductionOps.emplace_back(ReductionOp);
10091     TaskgroupDescriptors.emplace_back(nullptr);
10092   }
10093   /// Stores reduction data.
10094   void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10095             Expr *TaskgroupDescriptor) {
10096     Vars.emplace_back(Item);
10097     Privates.emplace_back(Private);
10098     LHSs.emplace_back(LHS);
10099     RHSs.emplace_back(RHS);
10100     ReductionOps.emplace_back(ReductionOp);
10101     TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
10102   }
10103 };
10104 } // namespace
10105 
10106 static bool CheckOMPArraySectionConstantForReduction(
10107     ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10108     SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10109   const Expr *Length = OASE->getLength();
10110   if (Length == nullptr) {
10111     // For array sections of the form [1:] or [:], we would need to analyze
10112     // the lower bound...
10113     if (OASE->getColonLoc().isValid())
10114       return false;
10115 
10116     // This is an array subscript which has implicit length 1!
10117     SingleElement = true;
10118     ArraySizes.push_back(llvm::APSInt::get(1));
10119   } else {
10120     llvm::APSInt ConstantLengthValue;
10121     if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10122       return false;
10123 
10124     SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10125     ArraySizes.push_back(ConstantLengthValue);
10126   }
10127 
10128   // Get the base of this array section and walk up from there.
10129   const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10130 
10131   // We require length = 1 for all array sections except the right-most to
10132   // guarantee that the memory region is contiguous and has no holes in it.
10133   while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10134     Length = TempOASE->getLength();
10135     if (Length == nullptr) {
10136       // For array sections of the form [1:] or [:], we would need to analyze
10137       // the lower bound...
10138       if (OASE->getColonLoc().isValid())
10139         return false;
10140 
10141       // This is an array subscript which has implicit length 1!
10142       ArraySizes.push_back(llvm::APSInt::get(1));
10143     } else {
10144       llvm::APSInt ConstantLengthValue;
10145       if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10146           ConstantLengthValue.getSExtValue() != 1)
10147         return false;
10148 
10149       ArraySizes.push_back(ConstantLengthValue);
10150     }
10151     Base = TempOASE->getBase()->IgnoreParenImpCasts();
10152   }
10153 
10154   // If we have a single element, we don't need to add the implicit lengths.
10155   if (!SingleElement) {
10156     while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10157       // Has implicit length 1!
10158       ArraySizes.push_back(llvm::APSInt::get(1));
10159       Base = TempASE->getBase()->IgnoreParenImpCasts();
10160     }
10161   }
10162 
10163   // This array section can be privatized as a single value or as a constant
10164   // sized array.
10165   return true;
10166 }
10167 
10168 static bool ActOnOMPReductionKindClause(
10169     Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10170     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10171     SourceLocation ColonLoc, SourceLocation EndLoc,
10172     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10173     ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
10174   auto DN = ReductionId.getName();
10175   auto OOK = DN.getCXXOverloadedOperator();
10176   BinaryOperatorKind BOK = BO_Comma;
10177 
10178   ASTContext &Context = S.Context;
10179   // OpenMP [2.14.3.6, reduction clause]
10180   // C
10181   // reduction-identifier is either an identifier or one of the following
10182   // operators: +, -, *,  &, |, ^, && and ||
10183   // C++
10184   // reduction-identifier is either an id-expression or one of the following
10185   // operators: +, -, *, &, |, ^, && and ||
10186   switch (OOK) {
10187   case OO_Plus:
10188   case OO_Minus:
10189     BOK = BO_Add;
10190     break;
10191   case OO_Star:
10192     BOK = BO_Mul;
10193     break;
10194   case OO_Amp:
10195     BOK = BO_And;
10196     break;
10197   case OO_Pipe:
10198     BOK = BO_Or;
10199     break;
10200   case OO_Caret:
10201     BOK = BO_Xor;
10202     break;
10203   case OO_AmpAmp:
10204     BOK = BO_LAnd;
10205     break;
10206   case OO_PipePipe:
10207     BOK = BO_LOr;
10208     break;
10209   case OO_New:
10210   case OO_Delete:
10211   case OO_Array_New:
10212   case OO_Array_Delete:
10213   case OO_Slash:
10214   case OO_Percent:
10215   case OO_Tilde:
10216   case OO_Exclaim:
10217   case OO_Equal:
10218   case OO_Less:
10219   case OO_Greater:
10220   case OO_LessEqual:
10221   case OO_GreaterEqual:
10222   case OO_PlusEqual:
10223   case OO_MinusEqual:
10224   case OO_StarEqual:
10225   case OO_SlashEqual:
10226   case OO_PercentEqual:
10227   case OO_CaretEqual:
10228   case OO_AmpEqual:
10229   case OO_PipeEqual:
10230   case OO_LessLess:
10231   case OO_GreaterGreater:
10232   case OO_LessLessEqual:
10233   case OO_GreaterGreaterEqual:
10234   case OO_EqualEqual:
10235   case OO_ExclaimEqual:
10236   case OO_Spaceship:
10237   case OO_PlusPlus:
10238   case OO_MinusMinus:
10239   case OO_Comma:
10240   case OO_ArrowStar:
10241   case OO_Arrow:
10242   case OO_Call:
10243   case OO_Subscript:
10244   case OO_Conditional:
10245   case OO_Coawait:
10246   case NUM_OVERLOADED_OPERATORS:
10247     llvm_unreachable("Unexpected reduction identifier");
10248   case OO_None:
10249     if (auto *II = DN.getAsIdentifierInfo()) {
10250       if (II->isStr("max"))
10251         BOK = BO_GT;
10252       else if (II->isStr("min"))
10253         BOK = BO_LT;
10254     }
10255     break;
10256   }
10257   SourceRange ReductionIdRange;
10258   if (ReductionIdScopeSpec.isValid())
10259     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
10260   else
10261     ReductionIdRange.setBegin(ReductionId.getBeginLoc());
10262   ReductionIdRange.setEnd(ReductionId.getEndLoc());
10263 
10264   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10265   bool FirstIter = true;
10266   for (auto RefExpr : VarList) {
10267     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
10268     // OpenMP [2.1, C/C++]
10269     //  A list item is a variable or array section, subject to the restrictions
10270     //  specified in Section 2.4 on page 42 and in each of the sections
10271     // describing clauses and directives for which a list appears.
10272     // OpenMP  [2.14.3.3, Restrictions, p.1]
10273     //  A variable that is part of another variable (as an array or
10274     //  structure element) cannot appear in a private clause.
10275     if (!FirstIter && IR != ER)
10276       ++IR;
10277     FirstIter = false;
10278     SourceLocation ELoc;
10279     SourceRange ERange;
10280     Expr *SimpleRefExpr = RefExpr;
10281     auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
10282                               /*AllowArraySection=*/true);
10283     if (Res.second) {
10284       // Try to find 'declare reduction' corresponding construct before using
10285       // builtin/overloaded operators.
10286       QualType Type = Context.DependentTy;
10287       CXXCastPath BasePath;
10288       ExprResult DeclareReductionRef = buildDeclareReductionRef(
10289           S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
10290           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10291       Expr *ReductionOp = nullptr;
10292       if (S.CurContext->isDependentContext() &&
10293           (DeclareReductionRef.isUnset() ||
10294            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
10295         ReductionOp = DeclareReductionRef.get();
10296       // It will be analyzed later.
10297       RD.push(RefExpr, ReductionOp);
10298     }
10299     ValueDecl *D = Res.first;
10300     if (!D)
10301       continue;
10302 
10303     Expr *TaskgroupDescriptor = nullptr;
10304     QualType Type;
10305     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10306     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
10307     if (ASE)
10308       Type = ASE->getType().getNonReferenceType();
10309     else if (OASE) {
10310       auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10311       if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
10312         Type = ATy->getElementType();
10313       else
10314         Type = BaseType->getPointeeType();
10315       Type = Type.getNonReferenceType();
10316     } else
10317       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
10318     auto *VD = dyn_cast<VarDecl>(D);
10319 
10320     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10321     //  A variable that appears in a private clause must not have an incomplete
10322     //  type or a reference type.
10323     if (S.RequireCompleteType(ELoc, Type,
10324                               diag::err_omp_reduction_incomplete_type))
10325       continue;
10326     // OpenMP [2.14.3.6, reduction clause, Restrictions]
10327     // A list item that appears in a reduction clause must not be
10328     // const-qualified.
10329     if (Type.getNonReferenceType().isConstant(Context)) {
10330       S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
10331       if (!ASE && !OASE) {
10332         bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10333                                  VarDecl::DeclarationOnly;
10334         S.Diag(D->getLocation(),
10335                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10336             << D;
10337       }
10338       continue;
10339     }
10340     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10341     //  If a list-item is a reference type then it must bind to the same object
10342     //  for all threads of the team.
10343     if (!ASE && !OASE && VD) {
10344       VarDecl *VDDef = VD->getDefinition();
10345       if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10346         DSARefChecker Check(Stack);
10347         if (Check.Visit(VDDef->getInit())) {
10348           S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10349               << getOpenMPClauseName(ClauseKind) << ERange;
10350           S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10351           continue;
10352         }
10353       }
10354     }
10355 
10356     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10357     // in a Construct]
10358     //  Variables with the predetermined data-sharing attributes may not be
10359     //  listed in data-sharing attributes clauses, except for the cases
10360     //  listed below. For these exceptions only, listing a predetermined
10361     //  variable in a data-sharing attribute clause is allowed and overrides
10362     //  the variable's predetermined data-sharing attributes.
10363     // OpenMP [2.14.3.6, Restrictions, p.3]
10364     //  Any number of reduction clauses can be specified on the directive,
10365     //  but a list item can appear only once in the reduction clauses for that
10366     //  directive.
10367     DSAStackTy::DSAVarData DVar;
10368     DVar = Stack->getTopDSA(D, false);
10369     if (DVar.CKind == OMPC_reduction) {
10370       S.Diag(ELoc, diag::err_omp_once_referenced)
10371           << getOpenMPClauseName(ClauseKind);
10372       if (DVar.RefExpr)
10373         S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10374       continue;
10375     } else if (DVar.CKind != OMPC_unknown) {
10376       S.Diag(ELoc, diag::err_omp_wrong_dsa)
10377           << getOpenMPClauseName(DVar.CKind)
10378           << getOpenMPClauseName(OMPC_reduction);
10379       ReportOriginalDSA(S, Stack, D, DVar);
10380       continue;
10381     }
10382 
10383     // OpenMP [2.14.3.6, Restrictions, p.1]
10384     //  A list item that appears in a reduction clause of a worksharing
10385     //  construct must be shared in the parallel regions to which any of the
10386     //  worksharing regions arising from the worksharing construct bind.
10387     OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
10388     if (isOpenMPWorksharingDirective(CurrDir) &&
10389         !isOpenMPParallelDirective(CurrDir) &&
10390         !isOpenMPTeamsDirective(CurrDir)) {
10391       DVar = Stack->getImplicitDSA(D, true);
10392       if (DVar.CKind != OMPC_shared) {
10393         S.Diag(ELoc, diag::err_omp_required_access)
10394             << getOpenMPClauseName(OMPC_reduction)
10395             << getOpenMPClauseName(OMPC_shared);
10396         ReportOriginalDSA(S, Stack, D, DVar);
10397         continue;
10398       }
10399     }
10400 
10401     // Try to find 'declare reduction' corresponding construct before using
10402     // builtin/overloaded operators.
10403     CXXCastPath BasePath;
10404     ExprResult DeclareReductionRef = buildDeclareReductionRef(
10405         S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
10406         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10407     if (DeclareReductionRef.isInvalid())
10408       continue;
10409     if (S.CurContext->isDependentContext() &&
10410         (DeclareReductionRef.isUnset() ||
10411          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
10412       RD.push(RefExpr, DeclareReductionRef.get());
10413       continue;
10414     }
10415     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10416       // Not allowed reduction identifier is found.
10417       S.Diag(ReductionId.getLocStart(),
10418              diag::err_omp_unknown_reduction_identifier)
10419           << Type << ReductionIdRange;
10420       continue;
10421     }
10422 
10423     // OpenMP [2.14.3.6, reduction clause, Restrictions]
10424     // The type of a list item that appears in a reduction clause must be valid
10425     // for the reduction-identifier. For a max or min reduction in C, the type
10426     // of the list item must be an allowed arithmetic data type: char, int,
10427     // float, double, or _Bool, possibly modified with long, short, signed, or
10428     // unsigned. For a max or min reduction in C++, the type of the list item
10429     // must be an allowed arithmetic data type: char, wchar_t, int, float,
10430     // double, or bool, possibly modified with long, short, signed, or unsigned.
10431     if (DeclareReductionRef.isUnset()) {
10432       if ((BOK == BO_GT || BOK == BO_LT) &&
10433           !(Type->isScalarType() ||
10434             (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10435         S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
10436             << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
10437         if (!ASE && !OASE) {
10438           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10439                                    VarDecl::DeclarationOnly;
10440           S.Diag(D->getLocation(),
10441                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10442               << D;
10443         }
10444         continue;
10445       }
10446       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
10447           !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
10448         S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10449             << getOpenMPClauseName(ClauseKind);
10450         if (!ASE && !OASE) {
10451           bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10452                                    VarDecl::DeclarationOnly;
10453           S.Diag(D->getLocation(),
10454                  IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10455               << D;
10456         }
10457         continue;
10458       }
10459     }
10460 
10461     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
10462     auto *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10463                                D->hasAttrs() ? &D->getAttrs() : nullptr);
10464     auto *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10465                                D->hasAttrs() ? &D->getAttrs() : nullptr);
10466     auto PrivateTy = Type;
10467 
10468     // Try if we can determine constant lengths for all array sections and avoid
10469     // the VLA.
10470     bool ConstantLengthOASE = false;
10471     if (OASE) {
10472       bool SingleElement;
10473       llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
10474       ConstantLengthOASE = CheckOMPArraySectionConstantForReduction(
10475           Context, OASE, SingleElement, ArraySizes);
10476 
10477       // If we don't have a single element, we must emit a constant array type.
10478       if (ConstantLengthOASE && !SingleElement) {
10479         for (auto &Size : ArraySizes) {
10480           PrivateTy = Context.getConstantArrayType(
10481               PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
10482         }
10483       }
10484     }
10485 
10486     if ((OASE && !ConstantLengthOASE) ||
10487         (!OASE && !ASE &&
10488          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
10489       if (!Context.getTargetInfo().isVLASupported() &&
10490           S.shouldDiagnoseTargetSupportFromOpenMP()) {
10491         S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10492         S.Diag(ELoc, diag::note_vla_unsupported);
10493         continue;
10494       }
10495       // For arrays/array sections only:
10496       // Create pseudo array type for private copy. The size for this array will
10497       // be generated during codegen.
10498       // For array subscripts or single variables Private Ty is the same as Type
10499       // (type of the variable or single array element).
10500       PrivateTy = Context.getVariableArrayType(
10501           Type,
10502           new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
10503           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
10504     } else if (!ASE && !OASE &&
10505                Context.getAsArrayType(D->getType().getNonReferenceType()))
10506       PrivateTy = D->getType().getNonReferenceType();
10507     // Private copy.
10508     auto *PrivateVD =
10509         buildVarDecl(S, ELoc, PrivateTy, D->getName(),
10510                      D->hasAttrs() ? &D->getAttrs() : nullptr,
10511                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
10512     // Add initializer for private variable.
10513     Expr *Init = nullptr;
10514     auto *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10515     auto *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
10516     if (DeclareReductionRef.isUsable()) {
10517       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10518       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10519       if (DRD->getInitializer()) {
10520         Init = DRDRef;
10521         RHSVD->setInit(DRDRef);
10522         RHSVD->setInitStyle(VarDecl::CallInit);
10523       }
10524     } else {
10525       switch (BOK) {
10526       case BO_Add:
10527       case BO_Xor:
10528       case BO_Or:
10529       case BO_LOr:
10530         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10531         if (Type->isScalarType() || Type->isAnyComplexType())
10532           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
10533         break;
10534       case BO_Mul:
10535       case BO_LAnd:
10536         if (Type->isScalarType() || Type->isAnyComplexType()) {
10537           // '*' and '&&' reduction ops - initializer is '1'.
10538           Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
10539         }
10540         break;
10541       case BO_And: {
10542         // '&' reduction op - initializer is '~0'.
10543         QualType OrigType = Type;
10544         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10545           Type = ComplexTy->getElementType();
10546         if (Type->isRealFloatingType()) {
10547           llvm::APFloat InitValue =
10548               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10549                                              /*isIEEE=*/true);
10550           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10551                                          Type, ELoc);
10552         } else if (Type->isScalarType()) {
10553           auto Size = Context.getTypeSize(Type);
10554           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10555           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10556           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10557         }
10558         if (Init && OrigType->isAnyComplexType()) {
10559           // Init = 0xFFFF + 0xFFFFi;
10560           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
10561           Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
10562         }
10563         Type = OrigType;
10564         break;
10565       }
10566       case BO_LT:
10567       case BO_GT: {
10568         // 'min' reduction op - initializer is 'Largest representable number in
10569         // the reduction list item type'.
10570         // 'max' reduction op - initializer is 'Least representable number in
10571         // the reduction list item type'.
10572         if (Type->isIntegerType() || Type->isPointerType()) {
10573           bool IsSigned = Type->hasSignedIntegerRepresentation();
10574           auto Size = Context.getTypeSize(Type);
10575           QualType IntTy =
10576               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10577           llvm::APInt InitValue =
10578               (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10579                                         : llvm::APInt::getMinValue(Size)
10580                              : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10581                                         : llvm::APInt::getMaxValue(Size);
10582           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10583           if (Type->isPointerType()) {
10584             // Cast to pointer type.
10585             auto CastExpr = S.BuildCStyleCastExpr(
10586                 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
10587             if (CastExpr.isInvalid())
10588               continue;
10589             Init = CastExpr.get();
10590           }
10591         } else if (Type->isRealFloatingType()) {
10592           llvm::APFloat InitValue = llvm::APFloat::getLargest(
10593               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10594           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10595                                          Type, ELoc);
10596         }
10597         break;
10598       }
10599       case BO_PtrMemD:
10600       case BO_PtrMemI:
10601       case BO_MulAssign:
10602       case BO_Div:
10603       case BO_Rem:
10604       case BO_Sub:
10605       case BO_Shl:
10606       case BO_Shr:
10607       case BO_LE:
10608       case BO_GE:
10609       case BO_EQ:
10610       case BO_NE:
10611       case BO_Cmp:
10612       case BO_AndAssign:
10613       case BO_XorAssign:
10614       case BO_OrAssign:
10615       case BO_Assign:
10616       case BO_AddAssign:
10617       case BO_SubAssign:
10618       case BO_DivAssign:
10619       case BO_RemAssign:
10620       case BO_ShlAssign:
10621       case BO_ShrAssign:
10622       case BO_Comma:
10623         llvm_unreachable("Unexpected reduction operation");
10624       }
10625     }
10626     if (Init && DeclareReductionRef.isUnset())
10627       S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10628     else if (!Init)
10629       S.ActOnUninitializedDecl(RHSVD);
10630     if (RHSVD->isInvalidDecl())
10631       continue;
10632     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
10633       S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10634           << Type << ReductionIdRange;
10635       bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10636                                VarDecl::DeclarationOnly;
10637       S.Diag(D->getLocation(),
10638              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10639           << D;
10640       continue;
10641     }
10642     // Store initializer for single element in private copy. Will be used during
10643     // codegen.
10644     PrivateVD->setInit(RHSVD->getInit());
10645     PrivateVD->setInitStyle(RHSVD->getInitStyle());
10646     auto *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
10647     ExprResult ReductionOp;
10648     if (DeclareReductionRef.isUsable()) {
10649       QualType RedTy = DeclareReductionRef.get()->getType();
10650       QualType PtrRedTy = Context.getPointerType(RedTy);
10651       ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
10652       ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
10653       if (!BasePath.empty()) {
10654         LHS = S.DefaultLvalueConversion(LHS.get());
10655         RHS = S.DefaultLvalueConversion(RHS.get());
10656         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10657                                        CK_UncheckedDerivedToBase, LHS.get(),
10658                                        &BasePath, LHS.get()->getValueKind());
10659         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
10660                                        CK_UncheckedDerivedToBase, RHS.get(),
10661                                        &BasePath, RHS.get()->getValueKind());
10662       }
10663       FunctionProtoType::ExtProtoInfo EPI;
10664       QualType Params[] = {PtrRedTy, PtrRedTy};
10665       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
10666       auto *OVE = new (Context) OpaqueValueExpr(
10667           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
10668           S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
10669       Expr *Args[] = {LHS.get(), RHS.get()};
10670       ReductionOp = new (Context)
10671           CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
10672     } else {
10673       ReductionOp = S.BuildBinOp(
10674           Stack->getCurScope(), ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
10675       if (ReductionOp.isUsable()) {
10676         if (BOK != BO_LT && BOK != BO_GT) {
10677           ReductionOp =
10678               S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10679                            BO_Assign, LHSDRE, ReductionOp.get());
10680         } else {
10681           auto *ConditionalOp = new (Context)
10682               ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
10683                                   Type, VK_LValue, OK_Ordinary);
10684           ReductionOp =
10685               S.BuildBinOp(Stack->getCurScope(), ReductionId.getLocStart(),
10686                            BO_Assign, LHSDRE, ConditionalOp);
10687         }
10688         if (ReductionOp.isUsable())
10689           ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
10690       }
10691       if (!ReductionOp.isUsable())
10692         continue;
10693     }
10694 
10695     // OpenMP [2.15.4.6, Restrictions, p.2]
10696     // A list item that appears in an in_reduction clause of a task construct
10697     // must appear in a task_reduction clause of a construct associated with a
10698     // taskgroup region that includes the participating task in its taskgroup
10699     // set. The construct associated with the innermost region that meets this
10700     // condition must specify the same reduction-identifier as the in_reduction
10701     // clause.
10702     if (ClauseKind == OMPC_in_reduction) {
10703       SourceRange ParentSR;
10704       BinaryOperatorKind ParentBOK;
10705       const Expr *ParentReductionOp;
10706       Expr *ParentBOKTD, *ParentReductionOpTD;
10707       DSAStackTy::DSAVarData ParentBOKDSA =
10708           Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
10709                                                   ParentBOKTD);
10710       DSAStackTy::DSAVarData ParentReductionOpDSA =
10711           Stack->getTopMostTaskgroupReductionData(
10712               D, ParentSR, ParentReductionOp, ParentReductionOpTD);
10713       bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
10714       bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
10715       if (!IsParentBOK && !IsParentReductionOp) {
10716         S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
10717         continue;
10718       }
10719       if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
10720           (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
10721           IsParentReductionOp) {
10722         bool EmitError = true;
10723         if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
10724           llvm::FoldingSetNodeID RedId, ParentRedId;
10725           ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
10726           DeclareReductionRef.get()->Profile(RedId, Context,
10727                                              /*Canonical=*/true);
10728           EmitError = RedId != ParentRedId;
10729         }
10730         if (EmitError) {
10731           S.Diag(ReductionId.getLocStart(),
10732                  diag::err_omp_reduction_identifier_mismatch)
10733               << ReductionIdRange << RefExpr->getSourceRange();
10734           S.Diag(ParentSR.getBegin(),
10735                  diag::note_omp_previous_reduction_identifier)
10736               << ParentSR
10737               << (IsParentBOK ? ParentBOKDSA.RefExpr
10738                               : ParentReductionOpDSA.RefExpr)
10739                      ->getSourceRange();
10740           continue;
10741         }
10742       }
10743       TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
10744       assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
10745     }
10746 
10747     DeclRefExpr *Ref = nullptr;
10748     Expr *VarsExpr = RefExpr->IgnoreParens();
10749     if (!VD && !S.CurContext->isDependentContext()) {
10750       if (ASE || OASE) {
10751         TransformExprToCaptures RebuildToCapture(S, D);
10752         VarsExpr =
10753             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
10754         Ref = RebuildToCapture.getCapturedExpr();
10755       } else {
10756         VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
10757       }
10758       if (!S.IsOpenMPCapturedDecl(D)) {
10759         RD.ExprCaptures.emplace_back(Ref->getDecl());
10760         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10761           ExprResult RefRes = S.DefaultLvalueConversion(Ref);
10762           if (!RefRes.isUsable())
10763             continue;
10764           ExprResult PostUpdateRes =
10765               S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10766                            RefRes.get());
10767           if (!PostUpdateRes.isUsable())
10768             continue;
10769           if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
10770               Stack->getCurrentDirective() == OMPD_taskgroup) {
10771             S.Diag(RefExpr->getExprLoc(),
10772                    diag::err_omp_reduction_non_addressable_expression)
10773                 << RefExpr->getSourceRange();
10774             continue;
10775           }
10776           RD.ExprPostUpdates.emplace_back(
10777               S.IgnoredValueConversions(PostUpdateRes.get()).get());
10778         }
10779       }
10780     }
10781     // All reduction items are still marked as reduction (to do not increase
10782     // code base size).
10783     Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
10784     if (CurrDir == OMPD_taskgroup) {
10785       if (DeclareReductionRef.isUsable())
10786         Stack->addTaskgroupReductionData(D, ReductionIdRange,
10787                                          DeclareReductionRef.get());
10788       else
10789         Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
10790     }
10791     RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
10792             TaskgroupDescriptor);
10793   }
10794   return RD.Vars.empty();
10795 }
10796 
10797 OMPClause *Sema::ActOnOpenMPReductionClause(
10798     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10799     SourceLocation ColonLoc, SourceLocation EndLoc,
10800     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10801     ArrayRef<Expr *> UnresolvedReductions) {
10802   ReductionData RD(VarList.size());
10803 
10804   if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
10805                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
10806                                   ReductionIdScopeSpec, ReductionId,
10807                                   UnresolvedReductions, RD))
10808     return nullptr;
10809 
10810   return OMPReductionClause::Create(
10811       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10812       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10813       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10814       buildPreInits(Context, RD.ExprCaptures),
10815       buildPostUpdate(*this, RD.ExprPostUpdates));
10816 }
10817 
10818 OMPClause *Sema::ActOnOpenMPTaskReductionClause(
10819     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10820     SourceLocation ColonLoc, SourceLocation EndLoc,
10821     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10822     ArrayRef<Expr *> UnresolvedReductions) {
10823   ReductionData RD(VarList.size());
10824 
10825   if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction,
10826                                   VarList, StartLoc, LParenLoc, ColonLoc,
10827                                   EndLoc, ReductionIdScopeSpec, ReductionId,
10828                                   UnresolvedReductions, RD))
10829     return nullptr;
10830 
10831   return OMPTaskReductionClause::Create(
10832       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10833       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10834       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
10835       buildPreInits(Context, RD.ExprCaptures),
10836       buildPostUpdate(*this, RD.ExprPostUpdates));
10837 }
10838 
10839 OMPClause *Sema::ActOnOpenMPInReductionClause(
10840     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10841     SourceLocation ColonLoc, SourceLocation EndLoc,
10842     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
10843     ArrayRef<Expr *> UnresolvedReductions) {
10844   ReductionData RD(VarList.size());
10845 
10846   if (ActOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
10847                                   StartLoc, LParenLoc, ColonLoc, EndLoc,
10848                                   ReductionIdScopeSpec, ReductionId,
10849                                   UnresolvedReductions, RD))
10850     return nullptr;
10851 
10852   return OMPInReductionClause::Create(
10853       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
10854       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
10855       RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
10856       buildPreInits(Context, RD.ExprCaptures),
10857       buildPostUpdate(*this, RD.ExprPostUpdates));
10858 }
10859 
10860 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
10861                                      SourceLocation LinLoc) {
10862   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
10863       LinKind == OMPC_LINEAR_unknown) {
10864     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
10865     return true;
10866   }
10867   return false;
10868 }
10869 
10870 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
10871                                  OpenMPLinearClauseKind LinKind,
10872                                  QualType Type) {
10873   auto *VD = dyn_cast_or_null<VarDecl>(D);
10874   // A variable must not have an incomplete type or a reference type.
10875   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
10876     return true;
10877   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
10878       !Type->isReferenceType()) {
10879     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
10880         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
10881     return true;
10882   }
10883   Type = Type.getNonReferenceType();
10884 
10885   // A list item must not be const-qualified.
10886   if (Type.isConstant(Context)) {
10887     Diag(ELoc, diag::err_omp_const_variable)
10888         << getOpenMPClauseName(OMPC_linear);
10889     if (D) {
10890       bool IsDecl =
10891           !VD ||
10892           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10893       Diag(D->getLocation(),
10894            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10895           << D;
10896     }
10897     return true;
10898   }
10899 
10900   // A list item must be of integral or pointer type.
10901   Type = Type.getUnqualifiedType().getCanonicalType();
10902   const auto *Ty = Type.getTypePtrOrNull();
10903   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
10904               !Ty->isPointerType())) {
10905     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
10906     if (D) {
10907       bool IsDecl =
10908           !VD ||
10909           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10910       Diag(D->getLocation(),
10911            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10912           << D;
10913     }
10914     return true;
10915   }
10916   return false;
10917 }
10918 
10919 OMPClause *Sema::ActOnOpenMPLinearClause(
10920     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
10921     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
10922     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10923   SmallVector<Expr *, 8> Vars;
10924   SmallVector<Expr *, 8> Privates;
10925   SmallVector<Expr *, 8> Inits;
10926   SmallVector<Decl *, 4> ExprCaptures;
10927   SmallVector<Expr *, 4> ExprPostUpdates;
10928   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
10929     LinKind = OMPC_LINEAR_val;
10930   for (auto &RefExpr : VarList) {
10931     assert(RefExpr && "NULL expr in OpenMP linear clause.");
10932     SourceLocation ELoc;
10933     SourceRange ERange;
10934     Expr *SimpleRefExpr = RefExpr;
10935     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10936                               /*AllowArraySection=*/false);
10937     if (Res.second) {
10938       // It will be analyzed later.
10939       Vars.push_back(RefExpr);
10940       Privates.push_back(nullptr);
10941       Inits.push_back(nullptr);
10942     }
10943     ValueDecl *D = Res.first;
10944     if (!D)
10945       continue;
10946 
10947     QualType Type = D->getType();
10948     auto *VD = dyn_cast<VarDecl>(D);
10949 
10950     // OpenMP [2.14.3.7, linear clause]
10951     //  A list-item cannot appear in more than one linear clause.
10952     //  A list-item that appears in a linear clause cannot appear in any
10953     //  other data-sharing attribute clause.
10954     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
10955     if (DVar.RefExpr) {
10956       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10957                                           << getOpenMPClauseName(OMPC_linear);
10958       ReportOriginalDSA(*this, DSAStack, D, DVar);
10959       continue;
10960     }
10961 
10962     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
10963       continue;
10964     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
10965 
10966     // Build private copy of original var.
10967     auto *Private =
10968         buildVarDecl(*this, ELoc, Type, D->getName(),
10969                      D->hasAttrs() ? &D->getAttrs() : nullptr,
10970                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
10971     auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
10972     // Build var to save initial value.
10973     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
10974     Expr *InitExpr;
10975     DeclRefExpr *Ref = nullptr;
10976     if (!VD && !CurContext->isDependentContext()) {
10977       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
10978       if (!IsOpenMPCapturedDecl(D)) {
10979         ExprCaptures.push_back(Ref->getDecl());
10980         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
10981           ExprResult RefRes = DefaultLvalueConversion(Ref);
10982           if (!RefRes.isUsable())
10983             continue;
10984           ExprResult PostUpdateRes =
10985               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10986                          SimpleRefExpr, RefRes.get());
10987           if (!PostUpdateRes.isUsable())
10988             continue;
10989           ExprPostUpdates.push_back(
10990               IgnoredValueConversions(PostUpdateRes.get()).get());
10991         }
10992       }
10993     }
10994     if (LinKind == OMPC_LINEAR_uval)
10995       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
10996     else
10997       InitExpr = VD ? SimpleRefExpr : Ref;
10998     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
10999                          /*DirectInit=*/false);
11000     auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
11001 
11002     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
11003     Vars.push_back((VD || CurContext->isDependentContext())
11004                        ? RefExpr->IgnoreParens()
11005                        : Ref);
11006     Privates.push_back(PrivateRef);
11007     Inits.push_back(InitRef);
11008   }
11009 
11010   if (Vars.empty())
11011     return nullptr;
11012 
11013   Expr *StepExpr = Step;
11014   Expr *CalcStepExpr = nullptr;
11015   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11016       !Step->isInstantiationDependent() &&
11017       !Step->containsUnexpandedParameterPack()) {
11018     SourceLocation StepLoc = Step->getLocStart();
11019     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
11020     if (Val.isInvalid())
11021       return nullptr;
11022     StepExpr = Val.get();
11023 
11024     // Build var to save the step value.
11025     VarDecl *SaveVar =
11026         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
11027     ExprResult SaveRef =
11028         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
11029     ExprResult CalcStep =
11030         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
11031     CalcStep = ActOnFinishFullExpr(CalcStep.get());
11032 
11033     // Warn about zero linear step (it would be probably better specified as
11034     // making corresponding variables 'const').
11035     llvm::APSInt Result;
11036     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11037     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
11038       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11039                                                      << (Vars.size() > 1);
11040     if (!IsConstant && CalcStep.isUsable()) {
11041       // Calculate the step beforehand instead of doing this on each iteration.
11042       // (This is not used if the number of iterations may be kfold-ed).
11043       CalcStepExpr = CalcStep.get();
11044     }
11045   }
11046 
11047   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11048                                  ColonLoc, EndLoc, Vars, Privates, Inits,
11049                                  StepExpr, CalcStepExpr,
11050                                  buildPreInits(Context, ExprCaptures),
11051                                  buildPostUpdate(*this, ExprPostUpdates));
11052 }
11053 
11054 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11055                                      Expr *NumIterations, Sema &SemaRef,
11056                                      Scope *S, DSAStackTy *Stack) {
11057   // Walk the vars and build update/final expressions for the CodeGen.
11058   SmallVector<Expr *, 8> Updates;
11059   SmallVector<Expr *, 8> Finals;
11060   Expr *Step = Clause.getStep();
11061   Expr *CalcStep = Clause.getCalcStep();
11062   // OpenMP [2.14.3.7, linear clause]
11063   // If linear-step is not specified it is assumed to be 1.
11064   if (Step == nullptr)
11065     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
11066   else if (CalcStep) {
11067     Step = cast<BinaryOperator>(CalcStep)->getLHS();
11068   }
11069   bool HasErrors = false;
11070   auto CurInit = Clause.inits().begin();
11071   auto CurPrivate = Clause.privates().begin();
11072   auto LinKind = Clause.getModifier();
11073   for (auto &RefExpr : Clause.varlists()) {
11074     SourceLocation ELoc;
11075     SourceRange ERange;
11076     Expr *SimpleRefExpr = RefExpr;
11077     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
11078                               /*AllowArraySection=*/false);
11079     ValueDecl *D = Res.first;
11080     if (Res.second || !D) {
11081       Updates.push_back(nullptr);
11082       Finals.push_back(nullptr);
11083       HasErrors = true;
11084       continue;
11085     }
11086     auto &&Info = Stack->isLoopControlVariable(D);
11087     // OpenMP [2.15.11, distribute simd Construct]
11088     // A list item may not appear in a linear clause, unless it is the loop
11089     // iteration variable.
11090     if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11091         isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11092       SemaRef.Diag(ELoc,
11093                    diag::err_omp_linear_distribute_var_non_loop_iteration);
11094       Updates.push_back(nullptr);
11095       Finals.push_back(nullptr);
11096       HasErrors = true;
11097       continue;
11098     }
11099     Expr *InitExpr = *CurInit;
11100 
11101     // Build privatized reference to the current linear var.
11102     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
11103     Expr *CapturedRef;
11104     if (LinKind == OMPC_LINEAR_uval)
11105       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11106     else
11107       CapturedRef =
11108           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11109                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11110                            /*RefersToCapture=*/true);
11111 
11112     // Build update: Var = InitExpr + IV * Step
11113     ExprResult Update;
11114     if (!Info.first) {
11115       Update =
11116           BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
11117                              InitExpr, IV, Step, /* Subtract */ false);
11118     } else
11119       Update = *CurPrivate;
11120     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
11121                                          /*DiscardedValue=*/true);
11122 
11123     // Build final: Var = InitExpr + NumIterations * Step
11124     ExprResult Final;
11125     if (!Info.first) {
11126       Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11127                                  InitExpr, NumIterations, Step,
11128                                  /* Subtract */ false);
11129     } else
11130       Final = *CurPrivate;
11131     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
11132                                         /*DiscardedValue=*/true);
11133 
11134     if (!Update.isUsable() || !Final.isUsable()) {
11135       Updates.push_back(nullptr);
11136       Finals.push_back(nullptr);
11137       HasErrors = true;
11138     } else {
11139       Updates.push_back(Update.get());
11140       Finals.push_back(Final.get());
11141     }
11142     ++CurInit;
11143     ++CurPrivate;
11144   }
11145   Clause.setUpdates(Updates);
11146   Clause.setFinals(Finals);
11147   return HasErrors;
11148 }
11149 
11150 OMPClause *Sema::ActOnOpenMPAlignedClause(
11151     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11152     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
11153 
11154   SmallVector<Expr *, 8> Vars;
11155   for (auto &RefExpr : VarList) {
11156     assert(RefExpr && "NULL expr in OpenMP linear clause.");
11157     SourceLocation ELoc;
11158     SourceRange ERange;
11159     Expr *SimpleRefExpr = RefExpr;
11160     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11161                               /*AllowArraySection=*/false);
11162     if (Res.second) {
11163       // It will be analyzed later.
11164       Vars.push_back(RefExpr);
11165     }
11166     ValueDecl *D = Res.first;
11167     if (!D)
11168       continue;
11169 
11170     QualType QType = D->getType();
11171     auto *VD = dyn_cast<VarDecl>(D);
11172 
11173     // OpenMP  [2.8.1, simd construct, Restrictions]
11174     // The type of list items appearing in the aligned clause must be
11175     // array, pointer, reference to array, or reference to pointer.
11176     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
11177     const Type *Ty = QType.getTypePtrOrNull();
11178     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
11179       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
11180           << QType << getLangOpts().CPlusPlus << ERange;
11181       bool IsDecl =
11182           !VD ||
11183           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11184       Diag(D->getLocation(),
11185            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11186           << D;
11187       continue;
11188     }
11189 
11190     // OpenMP  [2.8.1, simd construct, Restrictions]
11191     // A list-item cannot appear in more than one aligned clause.
11192     if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
11193       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
11194       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11195           << getOpenMPClauseName(OMPC_aligned);
11196       continue;
11197     }
11198 
11199     DeclRefExpr *Ref = nullptr;
11200     if (!VD && IsOpenMPCapturedDecl(D))
11201       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11202     Vars.push_back(DefaultFunctionArrayConversion(
11203                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11204                        .get());
11205   }
11206 
11207   // OpenMP [2.8.1, simd construct, Description]
11208   // The parameter of the aligned clause, alignment, must be a constant
11209   // positive integer expression.
11210   // If no optional parameter is specified, implementation-defined default
11211   // alignments for SIMD instructions on the target platforms are assumed.
11212   if (Alignment != nullptr) {
11213     ExprResult AlignResult =
11214         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11215     if (AlignResult.isInvalid())
11216       return nullptr;
11217     Alignment = AlignResult.get();
11218   }
11219   if (Vars.empty())
11220     return nullptr;
11221 
11222   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11223                                   EndLoc, Vars, Alignment);
11224 }
11225 
11226 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11227                                          SourceLocation StartLoc,
11228                                          SourceLocation LParenLoc,
11229                                          SourceLocation EndLoc) {
11230   SmallVector<Expr *, 8> Vars;
11231   SmallVector<Expr *, 8> SrcExprs;
11232   SmallVector<Expr *, 8> DstExprs;
11233   SmallVector<Expr *, 8> AssignmentOps;
11234   for (auto &RefExpr : VarList) {
11235     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11236     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11237       // It will be analyzed later.
11238       Vars.push_back(RefExpr);
11239       SrcExprs.push_back(nullptr);
11240       DstExprs.push_back(nullptr);
11241       AssignmentOps.push_back(nullptr);
11242       continue;
11243     }
11244 
11245     SourceLocation ELoc = RefExpr->getExprLoc();
11246     // OpenMP [2.1, C/C++]
11247     //  A list item is a variable name.
11248     // OpenMP  [2.14.4.1, Restrictions, p.1]
11249     //  A list item that appears in a copyin clause must be threadprivate.
11250     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
11251     if (!DE || !isa<VarDecl>(DE->getDecl())) {
11252       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11253           << 0 << RefExpr->getSourceRange();
11254       continue;
11255     }
11256 
11257     Decl *D = DE->getDecl();
11258     VarDecl *VD = cast<VarDecl>(D);
11259 
11260     QualType Type = VD->getType();
11261     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11262       // It will be analyzed later.
11263       Vars.push_back(DE);
11264       SrcExprs.push_back(nullptr);
11265       DstExprs.push_back(nullptr);
11266       AssignmentOps.push_back(nullptr);
11267       continue;
11268     }
11269 
11270     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11271     //  A list item that appears in a copyin clause must be threadprivate.
11272     if (!DSAStack->isThreadPrivate(VD)) {
11273       Diag(ELoc, diag::err_omp_required_access)
11274           << getOpenMPClauseName(OMPC_copyin)
11275           << getOpenMPDirectiveName(OMPD_threadprivate);
11276       continue;
11277     }
11278 
11279     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11280     //  A variable of class type (or array thereof) that appears in a
11281     //  copyin clause requires an accessible, unambiguous copy assignment
11282     //  operator for the class type.
11283     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11284     auto *SrcVD =
11285         buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
11286                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
11287     auto *PseudoSrcExpr = buildDeclRefExpr(
11288         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
11289     auto *DstVD =
11290         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
11291                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
11292     auto *PseudoDstExpr =
11293         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
11294     // For arrays generate assignment operation for single element and replace
11295     // it by the original array element in CodeGen.
11296     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
11297                                    PseudoDstExpr, PseudoSrcExpr);
11298     if (AssignmentOp.isInvalid())
11299       continue;
11300     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11301                                        /*DiscardedValue=*/true);
11302     if (AssignmentOp.isInvalid())
11303       continue;
11304 
11305     DSAStack->addDSA(VD, DE, OMPC_copyin);
11306     Vars.push_back(DE);
11307     SrcExprs.push_back(PseudoSrcExpr);
11308     DstExprs.push_back(PseudoDstExpr);
11309     AssignmentOps.push_back(AssignmentOp.get());
11310   }
11311 
11312   if (Vars.empty())
11313     return nullptr;
11314 
11315   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11316                                  SrcExprs, DstExprs, AssignmentOps);
11317 }
11318 
11319 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11320                                               SourceLocation StartLoc,
11321                                               SourceLocation LParenLoc,
11322                                               SourceLocation EndLoc) {
11323   SmallVector<Expr *, 8> Vars;
11324   SmallVector<Expr *, 8> SrcExprs;
11325   SmallVector<Expr *, 8> DstExprs;
11326   SmallVector<Expr *, 8> AssignmentOps;
11327   for (auto &RefExpr : VarList) {
11328     assert(RefExpr && "NULL expr in OpenMP linear clause.");
11329     SourceLocation ELoc;
11330     SourceRange ERange;
11331     Expr *SimpleRefExpr = RefExpr;
11332     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
11333                               /*AllowArraySection=*/false);
11334     if (Res.second) {
11335       // It will be analyzed later.
11336       Vars.push_back(RefExpr);
11337       SrcExprs.push_back(nullptr);
11338       DstExprs.push_back(nullptr);
11339       AssignmentOps.push_back(nullptr);
11340     }
11341     ValueDecl *D = Res.first;
11342     if (!D)
11343       continue;
11344 
11345     QualType Type = D->getType();
11346     auto *VD = dyn_cast<VarDecl>(D);
11347 
11348     // OpenMP [2.14.4.2, Restrictions, p.2]
11349     //  A list item that appears in a copyprivate clause may not appear in a
11350     //  private or firstprivate clause on the single construct.
11351     if (!VD || !DSAStack->isThreadPrivate(VD)) {
11352       auto DVar = DSAStack->getTopDSA(D, false);
11353       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11354           DVar.RefExpr) {
11355         Diag(ELoc, diag::err_omp_wrong_dsa)
11356             << getOpenMPClauseName(DVar.CKind)
11357             << getOpenMPClauseName(OMPC_copyprivate);
11358         ReportOriginalDSA(*this, DSAStack, D, DVar);
11359         continue;
11360       }
11361 
11362       // OpenMP [2.11.4.2, Restrictions, p.1]
11363       //  All list items that appear in a copyprivate clause must be either
11364       //  threadprivate or private in the enclosing context.
11365       if (DVar.CKind == OMPC_unknown) {
11366         DVar = DSAStack->getImplicitDSA(D, false);
11367         if (DVar.CKind == OMPC_shared) {
11368           Diag(ELoc, diag::err_omp_required_access)
11369               << getOpenMPClauseName(OMPC_copyprivate)
11370               << "threadprivate or private in the enclosing context";
11371           ReportOriginalDSA(*this, DSAStack, D, DVar);
11372           continue;
11373         }
11374       }
11375     }
11376 
11377     // Variably modified types are not supported.
11378     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
11379       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
11380           << getOpenMPClauseName(OMPC_copyprivate) << Type
11381           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
11382       bool IsDecl =
11383           !VD ||
11384           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11385       Diag(D->getLocation(),
11386            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11387           << D;
11388       continue;
11389     }
11390 
11391     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11392     //  A variable of class type (or array thereof) that appears in a
11393     //  copyin clause requires an accessible, unambiguous copy assignment
11394     //  operator for the class type.
11395     Type = Context.getBaseElementType(Type.getNonReferenceType())
11396                .getUnqualifiedType();
11397     auto *SrcVD =
11398         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
11399                      D->hasAttrs() ? &D->getAttrs() : nullptr);
11400     auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11401     auto *DstVD =
11402         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
11403                      D->hasAttrs() ? &D->getAttrs() : nullptr);
11404     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11405     auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11406                                    PseudoDstExpr, PseudoSrcExpr);
11407     if (AssignmentOp.isInvalid())
11408       continue;
11409     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
11410                                        /*DiscardedValue=*/true);
11411     if (AssignmentOp.isInvalid())
11412       continue;
11413 
11414     // No need to mark vars as copyprivate, they are already threadprivate or
11415     // implicitly private.
11416     assert(VD || IsOpenMPCapturedDecl(D));
11417     Vars.push_back(
11418         VD ? RefExpr->IgnoreParens()
11419            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
11420     SrcExprs.push_back(PseudoSrcExpr);
11421     DstExprs.push_back(PseudoDstExpr);
11422     AssignmentOps.push_back(AssignmentOp.get());
11423   }
11424 
11425   if (Vars.empty())
11426     return nullptr;
11427 
11428   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11429                                       Vars, SrcExprs, DstExprs, AssignmentOps);
11430 }
11431 
11432 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11433                                         SourceLocation StartLoc,
11434                                         SourceLocation LParenLoc,
11435                                         SourceLocation EndLoc) {
11436   if (VarList.empty())
11437     return nullptr;
11438 
11439   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11440 }
11441 
11442 OMPClause *
11443 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11444                               SourceLocation DepLoc, SourceLocation ColonLoc,
11445                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11446                               SourceLocation LParenLoc, SourceLocation EndLoc) {
11447   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
11448       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
11449     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
11450         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
11451     return nullptr;
11452   }
11453   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
11454       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11455        DepKind == OMPC_DEPEND_sink)) {
11456     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
11457     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
11458         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11459                                    /*Last=*/OMPC_DEPEND_unknown, Except)
11460         << getOpenMPClauseName(OMPC_depend);
11461     return nullptr;
11462   }
11463   SmallVector<Expr *, 8> Vars;
11464   DSAStackTy::OperatorOffsetTy OpsOffs;
11465   llvm::APSInt DepCounter(/*BitWidth=*/32);
11466   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
11467   if (DepKind == OMPC_DEPEND_sink) {
11468     if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
11469       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11470       TotalDepCount.setIsUnsigned(/*Val=*/true);
11471     }
11472   }
11473   for (auto &RefExpr : VarList) {
11474     assert(RefExpr && "NULL expr in OpenMP shared clause.");
11475     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11476       // It will be analyzed later.
11477       Vars.push_back(RefExpr);
11478       continue;
11479     }
11480 
11481     SourceLocation ELoc = RefExpr->getExprLoc();
11482     auto *SimpleExpr = RefExpr->IgnoreParenCasts();
11483     if (DepKind == OMPC_DEPEND_sink) {
11484       if (DSAStack->getParentOrderedRegionParam() &&
11485           DepCounter >= TotalDepCount) {
11486         Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11487         continue;
11488       }
11489       ++DepCounter;
11490       // OpenMP  [2.13.9, Summary]
11491       // depend(dependence-type : vec), where dependence-type is:
11492       // 'sink' and where vec is the iteration vector, which has the form:
11493       //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11494       // where n is the value specified by the ordered clause in the loop
11495       // directive, xi denotes the loop iteration variable of the i-th nested
11496       // loop associated with the loop directive, and di is a constant
11497       // non-negative integer.
11498       if (CurContext->isDependentContext()) {
11499         // It will be analyzed later.
11500         Vars.push_back(RefExpr);
11501         continue;
11502       }
11503       SimpleExpr = SimpleExpr->IgnoreImplicit();
11504       OverloadedOperatorKind OOK = OO_None;
11505       SourceLocation OOLoc;
11506       Expr *LHS = SimpleExpr;
11507       Expr *RHS = nullptr;
11508       if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11509         OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11510         OOLoc = BO->getOperatorLoc();
11511         LHS = BO->getLHS()->IgnoreParenImpCasts();
11512         RHS = BO->getRHS()->IgnoreParenImpCasts();
11513       } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11514         OOK = OCE->getOperator();
11515         OOLoc = OCE->getOperatorLoc();
11516         LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11517         RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11518       } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11519         OOK = MCE->getMethodDecl()
11520                   ->getNameInfo()
11521                   .getName()
11522                   .getCXXOverloadedOperator();
11523         OOLoc = MCE->getCallee()->getExprLoc();
11524         LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11525         RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11526       }
11527       SourceLocation ELoc;
11528       SourceRange ERange;
11529       auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
11530                                 /*AllowArraySection=*/false);
11531       if (Res.second) {
11532         // It will be analyzed later.
11533         Vars.push_back(RefExpr);
11534       }
11535       ValueDecl *D = Res.first;
11536       if (!D)
11537         continue;
11538 
11539       if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11540         Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11541         continue;
11542       }
11543       if (RHS) {
11544         ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11545             RHS, OMPC_depend, /*StrictlyPositive=*/false);
11546         if (RHSRes.isInvalid())
11547           continue;
11548       }
11549       if (!CurContext->isDependentContext() &&
11550           DSAStack->getParentOrderedRegionParam() &&
11551           DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
11552         ValueDecl *VD =
11553             DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
11554         if (VD) {
11555           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11556               << 1 << VD;
11557         } else {
11558           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
11559         }
11560         continue;
11561       }
11562       OpsOffs.push_back({RHS, OOK});
11563     } else {
11564       auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
11565       if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
11566           (ASE &&
11567            !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
11568            !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
11569         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11570             << RefExpr->getSourceRange();
11571         continue;
11572       }
11573       bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11574       getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
11575       ExprResult Res =
11576           CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
11577       getDiagnostics().setSuppressAllDiagnostics(Suppress);
11578       if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11579         Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11580             << RefExpr->getSourceRange();
11581         continue;
11582       }
11583     }
11584     Vars.push_back(RefExpr->IgnoreParenImpCasts());
11585   }
11586 
11587   if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11588       TotalDepCount > VarList.size() &&
11589       DSAStack->getParentOrderedRegionParam() &&
11590       DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11591     Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
11592         << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11593   }
11594   if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11595       Vars.empty())
11596     return nullptr;
11597 
11598   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11599                                     DepKind, DepLoc, ColonLoc, Vars);
11600   if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
11601       DSAStack->isParentOrderedRegion())
11602     DSAStack->addDoacrossDependClause(C, OpsOffs);
11603   return C;
11604 }
11605 
11606 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11607                                          SourceLocation LParenLoc,
11608                                          SourceLocation EndLoc) {
11609   Expr *ValExpr = Device;
11610   Stmt *HelperValStmt = nullptr;
11611 
11612   // OpenMP [2.9.1, Restrictions]
11613   // The device expression must evaluate to a non-negative integer value.
11614   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
11615                                  /*StrictlyPositive=*/false))
11616     return nullptr;
11617 
11618   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
11619   OpenMPDirectiveKind CaptureRegion =
11620       getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11621   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
11622     ValExpr = MakeFullExpr(ValExpr).get();
11623     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11624     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11625     HelperValStmt = buildPreInits(Context, Captures);
11626   }
11627 
11628   return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
11629                                        StartLoc, LParenLoc, EndLoc);
11630 }
11631 
11632 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
11633                               DSAStackTy *Stack, QualType QTy,
11634                               bool FullCheck = true) {
11635   NamedDecl *ND;
11636   if (QTy->isIncompleteType(&ND)) {
11637     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11638     return false;
11639   }
11640   if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
11641       !QTy.isTrivialType(SemaRef.Context))
11642     SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
11643   return true;
11644 }
11645 
11646 /// \brief Return true if it can be proven that the provided array expression
11647 /// (array section or array subscript) does NOT specify the whole size of the
11648 /// array whose base type is \a BaseQTy.
11649 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
11650                                                         const Expr *E,
11651                                                         QualType BaseQTy) {
11652   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11653 
11654   // If this is an array subscript, it refers to the whole size if the size of
11655   // the dimension is constant and equals 1. Also, an array section assumes the
11656   // format of an array subscript if no colon is used.
11657   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
11658     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11659       return ATy->getSize().getSExtValue() != 1;
11660     // Size can't be evaluated statically.
11661     return false;
11662   }
11663 
11664   assert(OASE && "Expecting array section if not an array subscript.");
11665   auto *LowerBound = OASE->getLowerBound();
11666   auto *Length = OASE->getLength();
11667 
11668   // If there is a lower bound that does not evaluates to zero, we are not
11669   // covering the whole dimension.
11670   if (LowerBound) {
11671     llvm::APSInt ConstLowerBound;
11672     if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
11673       return false; // Can't get the integer value as a constant.
11674     if (ConstLowerBound.getSExtValue())
11675       return true;
11676   }
11677 
11678   // If we don't have a length we covering the whole dimension.
11679   if (!Length)
11680     return false;
11681 
11682   // If the base is a pointer, we don't have a way to get the size of the
11683   // pointee.
11684   if (BaseQTy->isPointerType())
11685     return false;
11686 
11687   // We can only check if the length is the same as the size of the dimension
11688   // if we have a constant array.
11689   auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
11690   if (!CATy)
11691     return false;
11692 
11693   llvm::APSInt ConstLength;
11694   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11695     return false; // Can't get the integer value as a constant.
11696 
11697   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
11698 }
11699 
11700 // Return true if it can be proven that the provided array expression (array
11701 // section or array subscript) does NOT specify a single element of the array
11702 // whose base type is \a BaseQTy.
11703 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
11704                                                         const Expr *E,
11705                                                         QualType BaseQTy) {
11706   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
11707 
11708   // An array subscript always refer to a single element. Also, an array section
11709   // assumes the format of an array subscript if no colon is used.
11710   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
11711     return false;
11712 
11713   assert(OASE && "Expecting array section if not an array subscript.");
11714   auto *Length = OASE->getLength();
11715 
11716   // If we don't have a length we have to check if the array has unitary size
11717   // for this dimension. Also, we should always expect a length if the base type
11718   // is pointer.
11719   if (!Length) {
11720     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
11721       return ATy->getSize().getSExtValue() != 1;
11722     // We cannot assume anything.
11723     return false;
11724   }
11725 
11726   // Check if the length evaluates to 1.
11727   llvm::APSInt ConstLength;
11728   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
11729     return false; // Can't get the integer value as a constant.
11730 
11731   return ConstLength.getSExtValue() != 1;
11732 }
11733 
11734 // Return the expression of the base of the mappable expression or null if it
11735 // cannot be determined and do all the necessary checks to see if the expression
11736 // is valid as a standalone mappable expression. In the process, record all the
11737 // components of the expression.
11738 static Expr *CheckMapClauseExpressionBase(
11739     Sema &SemaRef, Expr *E,
11740     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
11741     OpenMPClauseKind CKind, bool NoDiagnose) {
11742   SourceLocation ELoc = E->getExprLoc();
11743   SourceRange ERange = E->getSourceRange();
11744 
11745   // The base of elements of list in a map clause have to be either:
11746   //  - a reference to variable or field.
11747   //  - a member expression.
11748   //  - an array expression.
11749   //
11750   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
11751   // reference to 'r'.
11752   //
11753   // If we have:
11754   //
11755   // struct SS {
11756   //   Bla S;
11757   //   foo() {
11758   //     #pragma omp target map (S.Arr[:12]);
11759   //   }
11760   // }
11761   //
11762   // We want to retrieve the member expression 'this->S';
11763 
11764   Expr *RelevantExpr = nullptr;
11765 
11766   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
11767   //  If a list item is an array section, it must specify contiguous storage.
11768   //
11769   // For this restriction it is sufficient that we make sure only references
11770   // to variables or fields and array expressions, and that no array sections
11771   // exist except in the rightmost expression (unless they cover the whole
11772   // dimension of the array). E.g. these would be invalid:
11773   //
11774   //   r.ArrS[3:5].Arr[6:7]
11775   //
11776   //   r.ArrS[3:5].x
11777   //
11778   // but these would be valid:
11779   //   r.ArrS[3].Arr[6:7]
11780   //
11781   //   r.ArrS[3].x
11782 
11783   bool AllowUnitySizeArraySection = true;
11784   bool AllowWholeSizeArraySection = true;
11785 
11786   while (!RelevantExpr) {
11787     E = E->IgnoreParenImpCasts();
11788 
11789     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
11790       if (!isa<VarDecl>(CurE->getDecl()))
11791         return nullptr;
11792 
11793       RelevantExpr = CurE;
11794 
11795       // If we got a reference to a declaration, we should not expect any array
11796       // section before that.
11797       AllowUnitySizeArraySection = false;
11798       AllowWholeSizeArraySection = false;
11799 
11800       // Record the component.
11801       CurComponents.emplace_back(CurE, CurE->getDecl());
11802     } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
11803       auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
11804 
11805       if (isa<CXXThisExpr>(BaseE))
11806         // We found a base expression: this->Val.
11807         RelevantExpr = CurE;
11808       else
11809         E = BaseE;
11810 
11811       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
11812         if (!NoDiagnose) {
11813           SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
11814               << CurE->getSourceRange();
11815           return nullptr;
11816         }
11817         if (RelevantExpr)
11818           return nullptr;
11819         continue;
11820       }
11821 
11822       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
11823 
11824       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
11825       //  A bit-field cannot appear in a map clause.
11826       //
11827       if (FD->isBitField()) {
11828         if (!NoDiagnose) {
11829           SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
11830               << CurE->getSourceRange() << getOpenMPClauseName(CKind);
11831           return nullptr;
11832         }
11833         if (RelevantExpr)
11834           return nullptr;
11835         continue;
11836       }
11837 
11838       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11839       //  If the type of a list item is a reference to a type T then the type
11840       //  will be considered to be T for all purposes of this clause.
11841       QualType CurType = BaseE->getType().getNonReferenceType();
11842 
11843       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
11844       //  A list item cannot be a variable that is a member of a structure with
11845       //  a union type.
11846       //
11847       if (auto *RT = CurType->getAs<RecordType>()) {
11848         if (RT->isUnionType()) {
11849           if (!NoDiagnose) {
11850             SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
11851                 << CurE->getSourceRange();
11852             return nullptr;
11853           }
11854           continue;
11855         }
11856       }
11857 
11858       // If we got a member expression, we should not expect any array section
11859       // before that:
11860       //
11861       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
11862       //  If a list item is an element of a structure, only the rightmost symbol
11863       //  of the variable reference can be an array section.
11864       //
11865       AllowUnitySizeArraySection = false;
11866       AllowWholeSizeArraySection = false;
11867 
11868       // Record the component.
11869       CurComponents.emplace_back(CurE, FD);
11870     } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
11871       E = CurE->getBase()->IgnoreParenImpCasts();
11872 
11873       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
11874         if (!NoDiagnose) {
11875           SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11876               << 0 << CurE->getSourceRange();
11877           return nullptr;
11878         }
11879         continue;
11880       }
11881 
11882       // If we got an array subscript that express the whole dimension we
11883       // can have any array expressions before. If it only expressing part of
11884       // the dimension, we can only have unitary-size array expressions.
11885       if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
11886                                                       E->getType()))
11887         AllowWholeSizeArraySection = false;
11888 
11889       // Record the component - we don't have any declaration associated.
11890       CurComponents.emplace_back(CurE, nullptr);
11891     } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
11892       assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
11893       E = CurE->getBase()->IgnoreParenImpCasts();
11894 
11895       QualType CurType =
11896           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
11897 
11898       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11899       //  If the type of a list item is a reference to a type T then the type
11900       //  will be considered to be T for all purposes of this clause.
11901       if (CurType->isReferenceType())
11902         CurType = CurType->getPointeeType();
11903 
11904       bool IsPointer = CurType->isAnyPointerType();
11905 
11906       if (!IsPointer && !CurType->isArrayType()) {
11907         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
11908             << 0 << CurE->getSourceRange();
11909         return nullptr;
11910       }
11911 
11912       bool NotWhole =
11913           CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
11914       bool NotUnity =
11915           CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
11916 
11917       if (AllowWholeSizeArraySection) {
11918         // Any array section is currently allowed. Allowing a whole size array
11919         // section implies allowing a unity array section as well.
11920         //
11921         // If this array section refers to the whole dimension we can still
11922         // accept other array sections before this one, except if the base is a
11923         // pointer. Otherwise, only unitary sections are accepted.
11924         if (NotWhole || IsPointer)
11925           AllowWholeSizeArraySection = false;
11926       } else if (AllowUnitySizeArraySection && NotUnity) {
11927         // A unity or whole array section is not allowed and that is not
11928         // compatible with the properties of the current array section.
11929         SemaRef.Diag(
11930             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
11931             << CurE->getSourceRange();
11932         return nullptr;
11933       }
11934 
11935       // Record the component - we don't have any declaration associated.
11936       CurComponents.emplace_back(CurE, nullptr);
11937     } else {
11938       if (!NoDiagnose) {
11939         // If nothing else worked, this is not a valid map clause expression.
11940         SemaRef.Diag(
11941             ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
11942             << ERange;
11943       }
11944       return nullptr;
11945     }
11946   }
11947 
11948   return RelevantExpr;
11949 }
11950 
11951 // Return true if expression E associated with value VD has conflicts with other
11952 // map information.
11953 static bool CheckMapConflicts(
11954     Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
11955     bool CurrentRegionOnly,
11956     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
11957     OpenMPClauseKind CKind) {
11958   assert(VD && E);
11959   SourceLocation ELoc = E->getExprLoc();
11960   SourceRange ERange = E->getSourceRange();
11961 
11962   // In order to easily check the conflicts we need to match each component of
11963   // the expression under test with the components of the expressions that are
11964   // already in the stack.
11965 
11966   assert(!CurComponents.empty() && "Map clause expression with no components!");
11967   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
11968          "Map clause expression with unexpected base!");
11969 
11970   // Variables to help detecting enclosing problems in data environment nests.
11971   bool IsEnclosedByDataEnvironmentExpr = false;
11972   const Expr *EnclosingExpr = nullptr;
11973 
11974   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
11975       VD, CurrentRegionOnly,
11976       [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
11977               StackComponents,
11978           OpenMPClauseKind) -> bool {
11979 
11980         assert(!StackComponents.empty() &&
11981                "Map clause expression with no components!");
11982         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
11983                "Map clause expression with unexpected base!");
11984 
11985         // The whole expression in the stack.
11986         auto *RE = StackComponents.front().getAssociatedExpression();
11987 
11988         // Expressions must start from the same base. Here we detect at which
11989         // point both expressions diverge from each other and see if we can
11990         // detect if the memory referred to both expressions is contiguous and
11991         // do not overlap.
11992         auto CI = CurComponents.rbegin();
11993         auto CE = CurComponents.rend();
11994         auto SI = StackComponents.rbegin();
11995         auto SE = StackComponents.rend();
11996         for (; CI != CE && SI != SE; ++CI, ++SI) {
11997 
11998           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
11999           //  At most one list item can be an array item derived from a given
12000           //  variable in map clauses of the same construct.
12001           if (CurrentRegionOnly &&
12002               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12003                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12004               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12005                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12006             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
12007                          diag::err_omp_multiple_array_items_in_map_clause)
12008                 << CI->getAssociatedExpression()->getSourceRange();
12009             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12010                          diag::note_used_here)
12011                 << SI->getAssociatedExpression()->getSourceRange();
12012             return true;
12013           }
12014 
12015           // Do both expressions have the same kind?
12016           if (CI->getAssociatedExpression()->getStmtClass() !=
12017               SI->getAssociatedExpression()->getStmtClass())
12018             break;
12019 
12020           // Are we dealing with different variables/fields?
12021           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
12022             break;
12023         }
12024         // Check if the extra components of the expressions in the enclosing
12025         // data environment are redundant for the current base declaration.
12026         // If they are, the maps completely overlap, which is legal.
12027         for (; SI != SE; ++SI) {
12028           QualType Type;
12029           if (auto *ASE =
12030                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
12031             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
12032           } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
12033                          SI->getAssociatedExpression())) {
12034             auto *E = OASE->getBase()->IgnoreParenImpCasts();
12035             Type =
12036                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12037           }
12038           if (Type.isNull() || Type->isAnyPointerType() ||
12039               CheckArrayExpressionDoesNotReferToWholeSize(
12040                   SemaRef, SI->getAssociatedExpression(), Type))
12041             break;
12042         }
12043 
12044         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12045         //  List items of map clauses in the same construct must not share
12046         //  original storage.
12047         //
12048         // If the expressions are exactly the same or one is a subset of the
12049         // other, it means they are sharing storage.
12050         if (CI == CE && SI == SE) {
12051           if (CurrentRegionOnly) {
12052             if (CKind == OMPC_map)
12053               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
12054             else {
12055               assert(CKind == OMPC_to || CKind == OMPC_from);
12056               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12057                   << ERange;
12058             }
12059             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12060                 << RE->getSourceRange();
12061             return true;
12062           } else {
12063             // If we find the same expression in the enclosing data environment,
12064             // that is legal.
12065             IsEnclosedByDataEnvironmentExpr = true;
12066             return false;
12067           }
12068         }
12069 
12070         QualType DerivedType =
12071             std::prev(CI)->getAssociatedDeclaration()->getType();
12072         SourceLocation DerivedLoc =
12073             std::prev(CI)->getAssociatedExpression()->getExprLoc();
12074 
12075         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12076         //  If the type of a list item is a reference to a type T then the type
12077         //  will be considered to be T for all purposes of this clause.
12078         DerivedType = DerivedType.getNonReferenceType();
12079 
12080         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12081         //  A variable for which the type is pointer and an array section
12082         //  derived from that variable must not appear as list items of map
12083         //  clauses of the same construct.
12084         //
12085         // Also, cover one of the cases in:
12086         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12087         //  If any part of the original storage of a list item has corresponding
12088         //  storage in the device data environment, all of the original storage
12089         //  must have corresponding storage in the device data environment.
12090         //
12091         if (DerivedType->isAnyPointerType()) {
12092           if (CI == CE || SI == SE) {
12093             SemaRef.Diag(
12094                 DerivedLoc,
12095                 diag::err_omp_pointer_mapped_along_with_derived_section)
12096                 << DerivedLoc;
12097             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12098                 << RE->getSourceRange();
12099             return true;
12100           } else if (CI->getAssociatedExpression()->getStmtClass() !=
12101                          SI->getAssociatedExpression()->getStmtClass() ||
12102                      CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12103                          SI->getAssociatedDeclaration()->getCanonicalDecl()) {
12104             assert(CI != CE && SI != SE);
12105             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
12106                 << DerivedLoc;
12107             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12108                 << RE->getSourceRange();
12109             return true;
12110           }
12111         }
12112 
12113         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12114         //  List items of map clauses in the same construct must not share
12115         //  original storage.
12116         //
12117         // An expression is a subset of the other.
12118         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
12119           if (CKind == OMPC_map)
12120             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
12121           else {
12122             assert(CKind == OMPC_to || CKind == OMPC_from);
12123             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12124                 << ERange;
12125           }
12126           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12127               << RE->getSourceRange();
12128           return true;
12129         }
12130 
12131         // The current expression uses the same base as other expression in the
12132         // data environment but does not contain it completely.
12133         if (!CurrentRegionOnly && SI != SE)
12134           EnclosingExpr = RE;
12135 
12136         // The current expression is a subset of the expression in the data
12137         // environment.
12138         IsEnclosedByDataEnvironmentExpr |=
12139             (!CurrentRegionOnly && CI != CE && SI == SE);
12140 
12141         return false;
12142       });
12143 
12144   if (CurrentRegionOnly)
12145     return FoundError;
12146 
12147   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12148   //  If any part of the original storage of a list item has corresponding
12149   //  storage in the device data environment, all of the original storage must
12150   //  have corresponding storage in the device data environment.
12151   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12152   //  If a list item is an element of a structure, and a different element of
12153   //  the structure has a corresponding list item in the device data environment
12154   //  prior to a task encountering the construct associated with the map clause,
12155   //  then the list item must also have a corresponding list item in the device
12156   //  data environment prior to the task encountering the construct.
12157   //
12158   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12159     SemaRef.Diag(ELoc,
12160                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
12161         << ERange;
12162     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12163         << EnclosingExpr->getSourceRange();
12164     return true;
12165   }
12166 
12167   return FoundError;
12168 }
12169 
12170 namespace {
12171 // Utility struct that gathers all the related lists associated with a mappable
12172 // expression.
12173 struct MappableVarListInfo final {
12174   // The list of expressions.
12175   ArrayRef<Expr *> VarList;
12176   // The list of processed expressions.
12177   SmallVector<Expr *, 16> ProcessedVarList;
12178   // The mappble components for each expression.
12179   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12180   // The base declaration of the variable.
12181   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12182 
12183   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12184     // We have a list of components and base declarations for each entry in the
12185     // variable list.
12186     VarComponents.reserve(VarList.size());
12187     VarBaseDeclarations.reserve(VarList.size());
12188   }
12189 };
12190 }
12191 
12192 // Check the validity of the provided variable list for the provided clause kind
12193 // \a CKind. In the check process the valid expressions, and mappable expression
12194 // components and variables are extracted and used to fill \a Vars,
12195 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12196 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12197 static void
12198 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12199                             OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12200                             SourceLocation StartLoc,
12201                             OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12202                             bool IsMapTypeImplicit = false) {
12203   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12204   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
12205          "Unexpected clause kind with mappable expressions!");
12206 
12207   // Keep track of the mappable components and base declarations in this clause.
12208   // Each entry in the list is going to have a list of components associated. We
12209   // record each set of the components so that we can build the clause later on.
12210   // In the end we should have the same amount of declarations and component
12211   // lists.
12212 
12213   for (auto &RE : MVLI.VarList) {
12214     assert(RE && "Null expr in omp to/from/map clause");
12215     SourceLocation ELoc = RE->getExprLoc();
12216 
12217     auto *VE = RE->IgnoreParenLValueCasts();
12218 
12219     if (VE->isValueDependent() || VE->isTypeDependent() ||
12220         VE->isInstantiationDependent() ||
12221         VE->containsUnexpandedParameterPack()) {
12222       // We can only analyze this information once the missing information is
12223       // resolved.
12224       MVLI.ProcessedVarList.push_back(RE);
12225       continue;
12226     }
12227 
12228     auto *SimpleExpr = RE->IgnoreParenCasts();
12229 
12230     if (!RE->IgnoreParenImpCasts()->isLValue()) {
12231       SemaRef.Diag(ELoc,
12232                    diag::err_omp_expected_named_var_member_or_array_expression)
12233           << RE->getSourceRange();
12234       continue;
12235     }
12236 
12237     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12238     ValueDecl *CurDeclaration = nullptr;
12239 
12240     // Obtain the array or member expression bases if required. Also, fill the
12241     // components array with all the components identified in the process.
12242     auto *BE = CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents,
12243                                             CKind, /*NoDiagnose=*/false);
12244     if (!BE)
12245       continue;
12246 
12247     assert(!CurComponents.empty() &&
12248            "Invalid mappable expression information.");
12249 
12250     // For the following checks, we rely on the base declaration which is
12251     // expected to be associated with the last component. The declaration is
12252     // expected to be a variable or a field (if 'this' is being mapped).
12253     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12254     assert(CurDeclaration && "Null decl on map clause.");
12255     assert(
12256         CurDeclaration->isCanonicalDecl() &&
12257         "Expecting components to have associated only canonical declarations.");
12258 
12259     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
12260     auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
12261 
12262     assert((VD || FD) && "Only variables or fields are expected here!");
12263     (void)FD;
12264 
12265     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
12266     // threadprivate variables cannot appear in a map clause.
12267     // OpenMP 4.5 [2.10.5, target update Construct]
12268     // threadprivate variables cannot appear in a from clause.
12269     if (VD && DSAS->isThreadPrivate(VD)) {
12270       auto DVar = DSAS->getTopDSA(VD, false);
12271       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12272           << getOpenMPClauseName(CKind);
12273       ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
12274       continue;
12275     }
12276 
12277     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12278     //  A list item cannot appear in both a map clause and a data-sharing
12279     //  attribute clause on the same construct.
12280 
12281     // Check conflicts with other map clause expressions. We check the conflicts
12282     // with the current construct separately from the enclosing data
12283     // environment, because the restrictions are different. We only have to
12284     // check conflicts across regions for the map clauses.
12285     if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12286                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
12287       break;
12288     if (CKind == OMPC_map &&
12289         CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
12290                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
12291       break;
12292 
12293     // OpenMP 4.5 [2.10.5, target update Construct]
12294     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12295     //  If the type of a list item is a reference to a type T then the type will
12296     //  be considered to be T for all purposes of this clause.
12297     QualType Type = CurDeclaration->getType().getNonReferenceType();
12298 
12299     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12300     // A list item in a to or from clause must have a mappable type.
12301     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12302     //  A list item must have a mappable type.
12303     if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
12304                            DSAS, Type))
12305       continue;
12306 
12307     if (CKind == OMPC_map) {
12308       // target enter data
12309       // OpenMP [2.10.2, Restrictions, p. 99]
12310       // A map-type must be specified in all map clauses and must be either
12311       // to or alloc.
12312       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12313       if (DKind == OMPD_target_enter_data &&
12314           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12315         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12316             << (IsMapTypeImplicit ? 1 : 0)
12317             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12318             << getOpenMPDirectiveName(DKind);
12319         continue;
12320       }
12321 
12322       // target exit_data
12323       // OpenMP [2.10.3, Restrictions, p. 102]
12324       // A map-type must be specified in all map clauses and must be either
12325       // from, release, or delete.
12326       if (DKind == OMPD_target_exit_data &&
12327           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12328             MapType == OMPC_MAP_delete)) {
12329         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12330             << (IsMapTypeImplicit ? 1 : 0)
12331             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12332             << getOpenMPDirectiveName(DKind);
12333         continue;
12334       }
12335 
12336       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12337       // A list item cannot appear in both a map clause and a data-sharing
12338       // attribute clause on the same construct
12339       if ((DKind == OMPD_target || DKind == OMPD_target_teams ||
12340            DKind == OMPD_target_teams_distribute ||
12341            DKind == OMPD_target_teams_distribute_parallel_for ||
12342            DKind == OMPD_target_teams_distribute_parallel_for_simd ||
12343            DKind == OMPD_target_teams_distribute_simd) && VD) {
12344         auto DVar = DSAS->getTopDSA(VD, false);
12345         if (isOpenMPPrivate(DVar.CKind)) {
12346           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12347               << getOpenMPClauseName(DVar.CKind)
12348               << getOpenMPClauseName(OMPC_map)
12349               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
12350           ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
12351           continue;
12352         }
12353       }
12354     }
12355 
12356     // Save the current expression.
12357     MVLI.ProcessedVarList.push_back(RE);
12358 
12359     // Store the components in the stack so that they can be used to check
12360     // against other clauses later on.
12361     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12362                                           /*WhereFoundClauseKind=*/OMPC_map);
12363 
12364     // Save the components and declaration to create the clause. For purposes of
12365     // the clause creation, any component list that has has base 'this' uses
12366     // null as base declaration.
12367     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12368     MVLI.VarComponents.back().append(CurComponents.begin(),
12369                                      CurComponents.end());
12370     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12371                                                            : CurDeclaration);
12372   }
12373 }
12374 
12375 OMPClause *
12376 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12377                            OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12378                            SourceLocation MapLoc, SourceLocation ColonLoc,
12379                            ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12380                            SourceLocation LParenLoc, SourceLocation EndLoc) {
12381   MappableVarListInfo MVLI(VarList);
12382   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12383                               MapType, IsMapTypeImplicit);
12384 
12385   // We need to produce a map clause even if we don't have variables so that
12386   // other diagnostics related with non-existing map clauses are accurate.
12387   return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12388                               MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12389                               MVLI.VarComponents, MapTypeModifier, MapType,
12390                               IsMapTypeImplicit, MapLoc);
12391 }
12392 
12393 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12394                                                TypeResult ParsedType) {
12395   assert(ParsedType.isUsable());
12396 
12397   QualType ReductionType = GetTypeFromParser(ParsedType.get());
12398   if (ReductionType.isNull())
12399     return QualType();
12400 
12401   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12402   // A type name in a declare reduction directive cannot be a function type, an
12403   // array type, a reference type, or a type qualified with const, volatile or
12404   // restrict.
12405   if (ReductionType.hasQualifiers()) {
12406     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12407     return QualType();
12408   }
12409 
12410   if (ReductionType->isFunctionType()) {
12411     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12412     return QualType();
12413   }
12414   if (ReductionType->isReferenceType()) {
12415     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12416     return QualType();
12417   }
12418   if (ReductionType->isArrayType()) {
12419     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12420     return QualType();
12421   }
12422   return ReductionType;
12423 }
12424 
12425 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12426     Scope *S, DeclContext *DC, DeclarationName Name,
12427     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12428     AccessSpecifier AS, Decl *PrevDeclInScope) {
12429   SmallVector<Decl *, 8> Decls;
12430   Decls.reserve(ReductionTypes.size());
12431 
12432   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
12433                       forRedeclarationInCurContext());
12434   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12435   // A reduction-identifier may not be re-declared in the current scope for the
12436   // same type or for a type that is compatible according to the base language
12437   // rules.
12438   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12439   OMPDeclareReductionDecl *PrevDRD = nullptr;
12440   bool InCompoundScope = true;
12441   if (S != nullptr) {
12442     // Find previous declaration with the same name not referenced in other
12443     // declarations.
12444     FunctionScopeInfo *ParentFn = getEnclosingFunction();
12445     InCompoundScope =
12446         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12447     LookupName(Lookup, S);
12448     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12449                          /*AllowInlineNamespace=*/false);
12450     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
12451     auto Filter = Lookup.makeFilter();
12452     while (Filter.hasNext()) {
12453       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12454       if (InCompoundScope) {
12455         auto I = UsedAsPrevious.find(PrevDecl);
12456         if (I == UsedAsPrevious.end())
12457           UsedAsPrevious[PrevDecl] = false;
12458         if (auto *D = PrevDecl->getPrevDeclInScope())
12459           UsedAsPrevious[D] = true;
12460       }
12461       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12462           PrevDecl->getLocation();
12463     }
12464     Filter.done();
12465     if (InCompoundScope) {
12466       for (auto &PrevData : UsedAsPrevious) {
12467         if (!PrevData.second) {
12468           PrevDRD = PrevData.first;
12469           break;
12470         }
12471       }
12472     }
12473   } else if (PrevDeclInScope != nullptr) {
12474     auto *PrevDRDInScope = PrevDRD =
12475         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12476     do {
12477       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12478           PrevDRDInScope->getLocation();
12479       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12480     } while (PrevDRDInScope != nullptr);
12481   }
12482   for (auto &TyData : ReductionTypes) {
12483     auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
12484     bool Invalid = false;
12485     if (I != PreviousRedeclTypes.end()) {
12486       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12487           << TyData.first;
12488       Diag(I->second, diag::note_previous_definition);
12489       Invalid = true;
12490     }
12491     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12492     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12493                                                 Name, TyData.first, PrevDRD);
12494     DC->addDecl(DRD);
12495     DRD->setAccess(AS);
12496     Decls.push_back(DRD);
12497     if (Invalid)
12498       DRD->setInvalidDecl();
12499     else
12500       PrevDRD = DRD;
12501   }
12502 
12503   return DeclGroupPtrTy::make(
12504       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12505 }
12506 
12507 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12508   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12509 
12510   // Enter new function scope.
12511   PushFunctionScope();
12512   setFunctionHasBranchProtectedScope();
12513   getCurFunction()->setHasOMPDeclareReductionCombiner();
12514 
12515   if (S != nullptr)
12516     PushDeclContext(S, DRD);
12517   else
12518     CurContext = DRD;
12519 
12520   PushExpressionEvaluationContext(
12521       ExpressionEvaluationContext::PotentiallyEvaluated);
12522 
12523   QualType ReductionType = DRD->getType();
12524   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12525   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12526   // uses semantics of argument handles by value, but it should be passed by
12527   // reference. C lang does not support references, so pass all parameters as
12528   // pointers.
12529   // Create 'T omp_in;' variable.
12530   auto *OmpInParm =
12531       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
12532   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12533   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12534   // uses semantics of argument handles by value, but it should be passed by
12535   // reference. C lang does not support references, so pass all parameters as
12536   // pointers.
12537   // Create 'T omp_out;' variable.
12538   auto *OmpOutParm =
12539       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12540   if (S != nullptr) {
12541     PushOnScopeChains(OmpInParm, S);
12542     PushOnScopeChains(OmpOutParm, S);
12543   } else {
12544     DRD->addDecl(OmpInParm);
12545     DRD->addDecl(OmpOutParm);
12546   }
12547 }
12548 
12549 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12550   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12551   DiscardCleanupsInEvaluationContext();
12552   PopExpressionEvaluationContext();
12553 
12554   PopDeclContext();
12555   PopFunctionScopeInfo();
12556 
12557   if (Combiner != nullptr)
12558     DRD->setCombiner(Combiner);
12559   else
12560     DRD->setInvalidDecl();
12561 }
12562 
12563 VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
12564   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12565 
12566   // Enter new function scope.
12567   PushFunctionScope();
12568   setFunctionHasBranchProtectedScope();
12569 
12570   if (S != nullptr)
12571     PushDeclContext(S, DRD);
12572   else
12573     CurContext = DRD;
12574 
12575   PushExpressionEvaluationContext(
12576       ExpressionEvaluationContext::PotentiallyEvaluated);
12577 
12578   QualType ReductionType = DRD->getType();
12579   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12580   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12581   // uses semantics of argument handles by value, but it should be passed by
12582   // reference. C lang does not support references, so pass all parameters as
12583   // pointers.
12584   // Create 'T omp_priv;' variable.
12585   auto *OmpPrivParm =
12586       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
12587   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12588   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12589   // uses semantics of argument handles by value, but it should be passed by
12590   // reference. C lang does not support references, so pass all parameters as
12591   // pointers.
12592   // Create 'T omp_orig;' variable.
12593   auto *OmpOrigParm =
12594       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
12595   if (S != nullptr) {
12596     PushOnScopeChains(OmpPrivParm, S);
12597     PushOnScopeChains(OmpOrigParm, S);
12598   } else {
12599     DRD->addDecl(OmpPrivParm);
12600     DRD->addDecl(OmpOrigParm);
12601   }
12602   return OmpPrivParm;
12603 }
12604 
12605 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12606                                                      VarDecl *OmpPrivParm) {
12607   auto *DRD = cast<OMPDeclareReductionDecl>(D);
12608   DiscardCleanupsInEvaluationContext();
12609   PopExpressionEvaluationContext();
12610 
12611   PopDeclContext();
12612   PopFunctionScopeInfo();
12613 
12614   if (Initializer != nullptr) {
12615     DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
12616   } else if (OmpPrivParm->hasInit()) {
12617     DRD->setInitializer(OmpPrivParm->getInit(),
12618                         OmpPrivParm->isDirectInit()
12619                             ? OMPDeclareReductionDecl::DirectInit
12620                             : OMPDeclareReductionDecl::CopyInit);
12621   } else {
12622     DRD->setInvalidDecl();
12623   }
12624 }
12625 
12626 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
12627     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
12628   for (auto *D : DeclReductions.get()) {
12629     if (IsValid) {
12630       auto *DRD = cast<OMPDeclareReductionDecl>(D);
12631       if (S != nullptr)
12632         PushOnScopeChains(DRD, S, /*AddToContext=*/false);
12633     } else
12634       D->setInvalidDecl();
12635   }
12636   return DeclReductions;
12637 }
12638 
12639 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
12640                                            SourceLocation StartLoc,
12641                                            SourceLocation LParenLoc,
12642                                            SourceLocation EndLoc) {
12643   Expr *ValExpr = NumTeams;
12644   Stmt *HelperValStmt = nullptr;
12645 
12646   // OpenMP [teams Constrcut, Restrictions]
12647   // The num_teams expression must evaluate to a positive integer value.
12648   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
12649                                  /*StrictlyPositive=*/true))
12650     return nullptr;
12651 
12652   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12653   OpenMPDirectiveKind CaptureRegion =
12654       getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
12655   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12656     ValExpr = MakeFullExpr(ValExpr).get();
12657     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12658     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12659     HelperValStmt = buildPreInits(Context, Captures);
12660   }
12661 
12662   return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
12663                                          StartLoc, LParenLoc, EndLoc);
12664 }
12665 
12666 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
12667                                               SourceLocation StartLoc,
12668                                               SourceLocation LParenLoc,
12669                                               SourceLocation EndLoc) {
12670   Expr *ValExpr = ThreadLimit;
12671   Stmt *HelperValStmt = nullptr;
12672 
12673   // OpenMP [teams Constrcut, Restrictions]
12674   // The thread_limit expression must evaluate to a positive integer value.
12675   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
12676                                  /*StrictlyPositive=*/true))
12677     return nullptr;
12678 
12679   OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
12680   OpenMPDirectiveKind CaptureRegion =
12681       getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
12682   if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
12683     ValExpr = MakeFullExpr(ValExpr).get();
12684     llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12685     ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12686     HelperValStmt = buildPreInits(Context, Captures);
12687   }
12688 
12689   return new (Context) OMPThreadLimitClause(
12690       ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
12691 }
12692 
12693 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
12694                                            SourceLocation StartLoc,
12695                                            SourceLocation LParenLoc,
12696                                            SourceLocation EndLoc) {
12697   Expr *ValExpr = Priority;
12698 
12699   // OpenMP [2.9.1, task Constrcut]
12700   // The priority-value is a non-negative numerical scalar expression.
12701   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
12702                                  /*StrictlyPositive=*/false))
12703     return nullptr;
12704 
12705   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12706 }
12707 
12708 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
12709                                             SourceLocation StartLoc,
12710                                             SourceLocation LParenLoc,
12711                                             SourceLocation EndLoc) {
12712   Expr *ValExpr = Grainsize;
12713 
12714   // OpenMP [2.9.2, taskloop Constrcut]
12715   // The parameter of the grainsize clause must be a positive integer
12716   // expression.
12717   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
12718                                  /*StrictlyPositive=*/true))
12719     return nullptr;
12720 
12721   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12722 }
12723 
12724 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
12725                                            SourceLocation StartLoc,
12726                                            SourceLocation LParenLoc,
12727                                            SourceLocation EndLoc) {
12728   Expr *ValExpr = NumTasks;
12729 
12730   // OpenMP [2.9.2, taskloop Constrcut]
12731   // The parameter of the num_tasks clause must be a positive integer
12732   // expression.
12733   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
12734                                  /*StrictlyPositive=*/true))
12735     return nullptr;
12736 
12737   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
12738 }
12739 
12740 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
12741                                        SourceLocation LParenLoc,
12742                                        SourceLocation EndLoc) {
12743   // OpenMP [2.13.2, critical construct, Description]
12744   // ... where hint-expression is an integer constant expression that evaluates
12745   // to a valid lock hint.
12746   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
12747   if (HintExpr.isInvalid())
12748     return nullptr;
12749   return new (Context)
12750       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
12751 }
12752 
12753 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
12754     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
12755     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
12756     SourceLocation EndLoc) {
12757   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
12758     std::string Values;
12759     Values += "'";
12760     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
12761     Values += "'";
12762     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
12763         << Values << getOpenMPClauseName(OMPC_dist_schedule);
12764     return nullptr;
12765   }
12766   Expr *ValExpr = ChunkSize;
12767   Stmt *HelperValStmt = nullptr;
12768   if (ChunkSize) {
12769     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
12770         !ChunkSize->isInstantiationDependent() &&
12771         !ChunkSize->containsUnexpandedParameterPack()) {
12772       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
12773       ExprResult Val =
12774           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
12775       if (Val.isInvalid())
12776         return nullptr;
12777 
12778       ValExpr = Val.get();
12779 
12780       // OpenMP [2.7.1, Restrictions]
12781       //  chunk_size must be a loop invariant integer expression with a positive
12782       //  value.
12783       llvm::APSInt Result;
12784       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
12785         if (Result.isSigned() && !Result.isStrictlyPositive()) {
12786           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
12787               << "dist_schedule" << ChunkSize->getSourceRange();
12788           return nullptr;
12789         }
12790       } else if (getOpenMPCaptureRegionForClause(
12791                      DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
12792                      OMPD_unknown &&
12793                  !CurContext->isDependentContext()) {
12794         ValExpr = MakeFullExpr(ValExpr).get();
12795         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
12796         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12797         HelperValStmt = buildPreInits(Context, Captures);
12798       }
12799     }
12800   }
12801 
12802   return new (Context)
12803       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
12804                             Kind, ValExpr, HelperValStmt);
12805 }
12806 
12807 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
12808     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
12809     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
12810     SourceLocation KindLoc, SourceLocation EndLoc) {
12811   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
12812   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
12813     std::string Value;
12814     SourceLocation Loc;
12815     Value += "'";
12816     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
12817       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
12818                                              OMPC_DEFAULTMAP_MODIFIER_tofrom);
12819       Loc = MLoc;
12820     } else {
12821       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
12822                                              OMPC_DEFAULTMAP_scalar);
12823       Loc = KindLoc;
12824     }
12825     Value += "'";
12826     Diag(Loc, diag::err_omp_unexpected_clause_value)
12827         << Value << getOpenMPClauseName(OMPC_defaultmap);
12828     return nullptr;
12829   }
12830   DSAStack->setDefaultDMAToFromScalar(StartLoc);
12831 
12832   return new (Context)
12833       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
12834 }
12835 
12836 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
12837   DeclContext *CurLexicalContext = getCurLexicalContext();
12838   if (!CurLexicalContext->isFileContext() &&
12839       !CurLexicalContext->isExternCContext() &&
12840       !CurLexicalContext->isExternCXXContext() &&
12841       !isa<CXXRecordDecl>(CurLexicalContext) &&
12842       !isa<ClassTemplateDecl>(CurLexicalContext) &&
12843       !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
12844       !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
12845     Diag(Loc, diag::err_omp_region_not_file_context);
12846     return false;
12847   }
12848   if (IsInOpenMPDeclareTargetContext) {
12849     Diag(Loc, diag::err_omp_enclosed_declare_target);
12850     return false;
12851   }
12852 
12853   IsInOpenMPDeclareTargetContext = true;
12854   return true;
12855 }
12856 
12857 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
12858   assert(IsInOpenMPDeclareTargetContext &&
12859          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
12860 
12861   IsInOpenMPDeclareTargetContext = false;
12862 }
12863 
12864 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
12865                                         CXXScopeSpec &ScopeSpec,
12866                                         const DeclarationNameInfo &Id,
12867                                         OMPDeclareTargetDeclAttr::MapTypeTy MT,
12868                                         NamedDeclSetType &SameDirectiveDecls) {
12869   LookupResult Lookup(*this, Id, LookupOrdinaryName);
12870   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
12871 
12872   if (Lookup.isAmbiguous())
12873     return;
12874   Lookup.suppressDiagnostics();
12875 
12876   if (!Lookup.isSingleResult()) {
12877     if (TypoCorrection Corrected =
12878             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
12879                         llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
12880                         CTK_ErrorRecovery)) {
12881       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
12882                                   << Id.getName());
12883       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
12884       return;
12885     }
12886 
12887     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
12888     return;
12889   }
12890 
12891   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
12892   if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
12893     if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
12894       Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
12895 
12896     if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
12897       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
12898       ND->addAttr(A);
12899       if (ASTMutationListener *ML = Context.getASTMutationListener())
12900         ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
12901       checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
12902     } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
12903       Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
12904           << Id.getName();
12905     }
12906   } else
12907     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
12908 }
12909 
12910 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
12911                                      Sema &SemaRef, Decl *D) {
12912   if (!D)
12913     return;
12914   const Decl *LD = nullptr;
12915   if (isa<TagDecl>(D)) {
12916     LD = cast<TagDecl>(D)->getDefinition();
12917   } else if (isa<VarDecl>(D)) {
12918     LD = cast<VarDecl>(D)->getDefinition();
12919 
12920     // If this is an implicit variable that is legal and we do not need to do
12921     // anything.
12922     if (cast<VarDecl>(D)->isImplicit()) {
12923       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12924           SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12925       D->addAttr(A);
12926       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
12927         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
12928       return;
12929     }
12930   } else if (auto *F = dyn_cast<FunctionDecl>(D)) {
12931     const FunctionDecl *FD = nullptr;
12932     if (cast<FunctionDecl>(D)->hasBody(FD)) {
12933       LD = FD;
12934       // If the definition is associated with the current declaration in the
12935       // target region (it can be e.g. a lambda) that is legal and we do not
12936       // need to do anything else.
12937       if (LD == D) {
12938         Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12939             SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12940         D->addAttr(A);
12941         if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
12942           ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
12943         return;
12944       }
12945     } else if (F->isFunctionTemplateSpecialization() &&
12946                F->getTemplateSpecializationKind() ==
12947                    TSK_ImplicitInstantiation) {
12948       // Check if the function is implicitly instantiated from the template
12949       // defined in the declare target region.
12950       const FunctionTemplateDecl *FTD = F->getPrimaryTemplate();
12951       if (FTD && FTD->hasAttr<OMPDeclareTargetDeclAttr>())
12952         return;
12953     }
12954   }
12955   if (!LD)
12956     LD = D;
12957   if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
12958       (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
12959     // Outlined declaration is not declared target.
12960     if (LD->isOutOfLine()) {
12961       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12962       SemaRef.Diag(SL, diag::note_used_here) << SR;
12963     } else {
12964       const DeclContext *DC = LD->getDeclContext();
12965       while (DC) {
12966         if (isa<FunctionDecl>(DC) &&
12967             cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
12968           break;
12969         DC = DC->getParent();
12970       }
12971       if (DC)
12972         return;
12973 
12974       // Is not declared in target context.
12975       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
12976       SemaRef.Diag(SL, diag::note_used_here) << SR;
12977     }
12978     // Mark decl as declared target to prevent further diagnostic.
12979     Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
12980         SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
12981     D->addAttr(A);
12982     if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
12983       ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
12984   }
12985 }
12986 
12987 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
12988                                    Sema &SemaRef, DSAStackTy *Stack,
12989                                    ValueDecl *VD) {
12990   if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
12991     return true;
12992   if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
12993                          /*FullCheck=*/false))
12994     return false;
12995   return true;
12996 }
12997 
12998 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
12999                                             SourceLocation IdLoc) {
13000   if (!D || D->isInvalidDecl())
13001     return;
13002   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
13003   SourceLocation SL = E ? E->getLocStart() : D->getLocation();
13004   // 2.10.6: threadprivate variable cannot appear in a declare target directive.
13005   if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
13006     if (DSAStack->isThreadPrivate(VD)) {
13007       Diag(SL, diag::err_omp_threadprivate_in_target);
13008       ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
13009       return;
13010     }
13011   }
13012   if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
13013     // Problem if any with var declared with incomplete type will be reported
13014     // as normal, so no need to check it here.
13015     if ((E || !VD->getType()->isIncompleteType()) &&
13016         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
13017       // Mark decl as declared target to prevent further diagnostic.
13018       if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD) ||
13019           isa<FunctionTemplateDecl>(VD)) {
13020         Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13021             Context, OMPDeclareTargetDeclAttr::MT_To);
13022         VD->addAttr(A);
13023         if (ASTMutationListener *ML = Context.getASTMutationListener())
13024           ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
13025       }
13026       return;
13027     }
13028   }
13029   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
13030     if (FD->hasAttr<OMPDeclareTargetDeclAttr>() &&
13031         (FD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
13032          OMPDeclareTargetDeclAttr::MT_Link)) {
13033       assert(IdLoc.isValid() && "Source location is expected");
13034       Diag(IdLoc, diag::err_omp_function_in_link_clause);
13035       Diag(FD->getLocation(), diag::note_defined_here) << FD;
13036       return;
13037     }
13038   }
13039   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(D)) {
13040     if (FTD->hasAttr<OMPDeclareTargetDeclAttr>() &&
13041         (FTD->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() ==
13042          OMPDeclareTargetDeclAttr::MT_Link)) {
13043       assert(IdLoc.isValid() && "Source location is expected");
13044       Diag(IdLoc, diag::err_omp_function_in_link_clause);
13045       Diag(FTD->getLocation(), diag::note_defined_here) << FTD;
13046       return;
13047     }
13048   }
13049   if (!E) {
13050     // Checking declaration inside declare target region.
13051     if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
13052         (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13053          isa<FunctionTemplateDecl>(D))) {
13054       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13055           Context, OMPDeclareTargetDeclAttr::MT_To);
13056       D->addAttr(A);
13057       if (ASTMutationListener *ML = Context.getASTMutationListener())
13058         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13059     }
13060     return;
13061   }
13062   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13063 }
13064 
13065 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13066                                      SourceLocation StartLoc,
13067                                      SourceLocation LParenLoc,
13068                                      SourceLocation EndLoc) {
13069   MappableVarListInfo MVLI(VarList);
13070   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13071   if (MVLI.ProcessedVarList.empty())
13072     return nullptr;
13073 
13074   return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13075                              MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13076                              MVLI.VarComponents);
13077 }
13078 
13079 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13080                                        SourceLocation StartLoc,
13081                                        SourceLocation LParenLoc,
13082                                        SourceLocation EndLoc) {
13083   MappableVarListInfo MVLI(VarList);
13084   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13085   if (MVLI.ProcessedVarList.empty())
13086     return nullptr;
13087 
13088   return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13089                                MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13090                                MVLI.VarComponents);
13091 }
13092 
13093 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13094                                                SourceLocation StartLoc,
13095                                                SourceLocation LParenLoc,
13096                                                SourceLocation EndLoc) {
13097   MappableVarListInfo MVLI(VarList);
13098   SmallVector<Expr *, 8> PrivateCopies;
13099   SmallVector<Expr *, 8> Inits;
13100 
13101   for (auto &RefExpr : VarList) {
13102     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13103     SourceLocation ELoc;
13104     SourceRange ERange;
13105     Expr *SimpleRefExpr = RefExpr;
13106     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13107     if (Res.second) {
13108       // It will be analyzed later.
13109       MVLI.ProcessedVarList.push_back(RefExpr);
13110       PrivateCopies.push_back(nullptr);
13111       Inits.push_back(nullptr);
13112     }
13113     ValueDecl *D = Res.first;
13114     if (!D)
13115       continue;
13116 
13117     QualType Type = D->getType();
13118     Type = Type.getNonReferenceType().getUnqualifiedType();
13119 
13120     auto *VD = dyn_cast<VarDecl>(D);
13121 
13122     // Item should be a pointer or reference to pointer.
13123     if (!Type->isPointerType()) {
13124       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13125           << 0 << RefExpr->getSourceRange();
13126       continue;
13127     }
13128 
13129     // Build the private variable and the expression that refers to it.
13130     auto VDPrivate =
13131         buildVarDecl(*this, ELoc, Type, D->getName(),
13132                      D->hasAttrs() ? &D->getAttrs() : nullptr,
13133                      VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
13134     if (VDPrivate->isInvalidDecl())
13135       continue;
13136 
13137     CurContext->addDecl(VDPrivate);
13138     auto VDPrivateRefExpr = buildDeclRefExpr(
13139         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13140 
13141     // Add temporary variable to initialize the private copy of the pointer.
13142     auto *VDInit =
13143         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
13144     auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
13145                                            RefExpr->getExprLoc());
13146     AddInitializerToDecl(VDPrivate,
13147                          DefaultLvalueConversion(VDInitRefExpr).get(),
13148                          /*DirectInit=*/false);
13149 
13150     // If required, build a capture to implement the privatization initialized
13151     // with the current list item value.
13152     DeclRefExpr *Ref = nullptr;
13153     if (!VD)
13154       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13155     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13156     PrivateCopies.push_back(VDPrivateRefExpr);
13157     Inits.push_back(VDInitRefExpr);
13158 
13159     // We need to add a data sharing attribute for this variable to make sure it
13160     // is correctly captured. A variable that shows up in a use_device_ptr has
13161     // similar properties of a first private variable.
13162     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13163 
13164     // Create a mappable component for the list item. List items in this clause
13165     // only need a component.
13166     MVLI.VarBaseDeclarations.push_back(D);
13167     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13168     MVLI.VarComponents.back().push_back(
13169         OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
13170   }
13171 
13172   if (MVLI.ProcessedVarList.empty())
13173     return nullptr;
13174 
13175   return OMPUseDevicePtrClause::Create(
13176       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13177       PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
13178 }
13179 
13180 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13181                                               SourceLocation StartLoc,
13182                                               SourceLocation LParenLoc,
13183                                               SourceLocation EndLoc) {
13184   MappableVarListInfo MVLI(VarList);
13185   for (auto &RefExpr : VarList) {
13186     assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
13187     SourceLocation ELoc;
13188     SourceRange ERange;
13189     Expr *SimpleRefExpr = RefExpr;
13190     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13191     if (Res.second) {
13192       // It will be analyzed later.
13193       MVLI.ProcessedVarList.push_back(RefExpr);
13194     }
13195     ValueDecl *D = Res.first;
13196     if (!D)
13197       continue;
13198 
13199     QualType Type = D->getType();
13200     // item should be a pointer or array or reference to pointer or array
13201     if (!Type.getNonReferenceType()->isPointerType() &&
13202         !Type.getNonReferenceType()->isArrayType()) {
13203       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13204           << 0 << RefExpr->getSourceRange();
13205       continue;
13206     }
13207 
13208     // Check if the declaration in the clause does not show up in any data
13209     // sharing attribute.
13210     auto DVar = DSAStack->getTopDSA(D, false);
13211     if (isOpenMPPrivate(DVar.CKind)) {
13212       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13213           << getOpenMPClauseName(DVar.CKind)
13214           << getOpenMPClauseName(OMPC_is_device_ptr)
13215           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
13216       ReportOriginalDSA(*this, DSAStack, D, DVar);
13217       continue;
13218     }
13219 
13220     Expr *ConflictExpr;
13221     if (DSAStack->checkMappableExprComponentListsForDecl(
13222             D, /*CurrentRegionOnly=*/true,
13223             [&ConflictExpr](
13224                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13225                 OpenMPClauseKind) -> bool {
13226               ConflictExpr = R.front().getAssociatedExpression();
13227               return true;
13228             })) {
13229       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13230       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13231           << ConflictExpr->getSourceRange();
13232       continue;
13233     }
13234 
13235     // Store the components in the stack so that they can be used to check
13236     // against other clauses later on.
13237     OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13238     DSAStack->addMappableExpressionComponents(
13239         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13240 
13241     // Record the expression we've just processed.
13242     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13243 
13244     // Create a mappable component for the list item. List items in this clause
13245     // only need a component. We use a null declaration to signal fields in
13246     // 'this'.
13247     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13248             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13249            "Unexpected device pointer expression!");
13250     MVLI.VarBaseDeclarations.push_back(
13251         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13252     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13253     MVLI.VarComponents.back().push_back(MC);
13254   }
13255 
13256   if (MVLI.ProcessedVarList.empty())
13257     return nullptr;
13258 
13259   return OMPIsDevicePtrClause::Create(
13260       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13261       MVLI.VarBaseDeclarations, MVLI.VarComponents);
13262 }
13263