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/AST/TypeOrdering.h"
26 #include "clang/Basic/OpenMPKinds.h"
27 #include "clang/Basic/TargetInfo.h"
28 #include "clang/Lex/Preprocessor.h"
29 #include "clang/Sema/Initialization.h"
30 #include "clang/Sema/Lookup.h"
31 #include "clang/Sema/Scope.h"
32 #include "clang/Sema/ScopeInfo.h"
33 #include "clang/Sema/SemaInternal.h"
34 using namespace clang;
35 
36 //===----------------------------------------------------------------------===//
37 // Stack of data-sharing attributes for variables
38 //===----------------------------------------------------------------------===//
39 
40 namespace {
41 /// \brief Default data sharing attributes, which can be applied to directive.
42 enum DefaultDataSharingAttributes {
43   DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44   DSA_none = 1 << 0,   /// \brief Default data sharing attribute 'none'.
45   DSA_shared = 1 << 1  /// \brief Default data sharing attribute 'shared'.
46 };
47 
48 /// \brief Stack for tracking declarations used in OpenMP directives and
49 /// clauses and their data-sharing attributes.
50 class DSAStackTy final {
51 public:
52   struct DSAVarData final {
53     OpenMPDirectiveKind DKind = OMPD_unknown;
54     OpenMPClauseKind CKind = OMPC_unknown;
55     Expr *RefExpr = nullptr;
56     DeclRefExpr *PrivateCopy = nullptr;
57     SourceLocation ImplicitDSALoc;
58     DSAVarData() {}
59   };
60   typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61       OperatorOffsetTy;
62 
63 private:
64   struct DSAInfo final {
65     OpenMPClauseKind Attributes = OMPC_unknown;
66     /// Pointer to a reference expression and a flag which shows that the
67     /// variable is marked as lastprivate(true) or not (false).
68     llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69     DeclRefExpr *PrivateCopy = nullptr;
70   };
71   typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72   typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
73   typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74   typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
75   /// Struct that associates a component with the clause kind where they are
76   /// found.
77   struct MappedExprComponentTy {
78     OMPClauseMappableExprCommon::MappableExprComponentLists Components;
79     OpenMPClauseKind Kind = OMPC_unknown;
80   };
81   typedef llvm::DenseMap<ValueDecl *, MappedExprComponentTy>
82       MappedExprComponentsTy;
83   typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
84       CriticalsWithHintsTy;
85   typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
86       DoacrossDependMapTy;
87 
88   struct SharingMapTy final {
89     DeclSAMapTy SharingMap;
90     AlignedMapTy AlignedMap;
91     MappedExprComponentsTy MappedExprComponents;
92     LoopControlVariablesMapTy LCVMap;
93     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
94     SourceLocation DefaultAttrLoc;
95     OpenMPDirectiveKind Directive = OMPD_unknown;
96     DeclarationNameInfo DirectiveName;
97     Scope *CurScope = nullptr;
98     SourceLocation ConstructLoc;
99     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
100     /// get the data (loop counters etc.) about enclosing loop-based construct.
101     /// This data is required during codegen.
102     DoacrossDependMapTy DoacrossDepends;
103     /// \brief first argument (Expr *) contains optional argument of the
104     /// 'ordered' clause, the second one is true if the regions has 'ordered'
105     /// clause, false otherwise.
106     llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
107     bool NowaitRegion = false;
108     bool CancelRegion = false;
109     unsigned AssociatedLoops = 1;
110     SourceLocation InnerTeamsRegionLoc;
111     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
112                  Scope *CurScope, SourceLocation Loc)
113         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
114           ConstructLoc(Loc) {}
115     SharingMapTy() {}
116   };
117 
118   typedef SmallVector<SharingMapTy, 4> StackTy;
119 
120   /// \brief Stack of used declaration and their data-sharing attributes.
121   StackTy Stack;
122   /// \brief true, if check for DSA must be from parent directive, false, if
123   /// from current directive.
124   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
125   Sema &SemaRef;
126   bool ForceCapturing = false;
127   CriticalsWithHintsTy Criticals;
128 
129   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
130 
131   DSAVarData getDSA(StackTy::reverse_iterator &Iter, ValueDecl *D);
132 
133   /// \brief Checks if the variable is a local for OpenMP region.
134   bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
135 
136 public:
137   explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
138 
139   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
140   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
141 
142   bool isForceVarCapturing() const { return ForceCapturing; }
143   void setForceVarCapturing(bool V) { ForceCapturing = V; }
144 
145   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
146             Scope *CurScope, SourceLocation Loc) {
147     Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
148     Stack.back().DefaultAttrLoc = Loc;
149   }
150 
151   void pop() {
152     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
153     Stack.pop_back();
154   }
155 
156   void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
157     Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
158   }
159   const std::pair<OMPCriticalDirective *, llvm::APSInt>
160   getCriticalWithHint(const DeclarationNameInfo &Name) const {
161     auto I = Criticals.find(Name.getAsString());
162     if (I != Criticals.end())
163       return I->second;
164     return std::make_pair(nullptr, llvm::APSInt());
165   }
166   /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
167   /// add it and return NULL; otherwise return previous occurrence's expression
168   /// for diagnostics.
169   Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
170 
171   /// \brief Register specified variable as loop control variable.
172   void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
173   /// \brief Check if the specified variable is a loop control variable for
174   /// current region.
175   /// \return The index of the loop control variable in the list of associated
176   /// for-loops (from outer to inner).
177   LCDeclInfo isLoopControlVariable(ValueDecl *D);
178   /// \brief Check if the specified variable is a loop control variable for
179   /// parent region.
180   /// \return The index of the loop control variable in the list of associated
181   /// for-loops (from outer to inner).
182   LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
183   /// \brief Get the loop control variable for the I-th loop (or nullptr) in
184   /// parent directive.
185   ValueDecl *getParentLoopControlVariable(unsigned I);
186 
187   /// \brief Adds explicit data sharing attribute to the specified declaration.
188   void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
189               DeclRefExpr *PrivateCopy = nullptr);
190 
191   /// \brief Returns data sharing attributes from top of the stack for the
192   /// specified declaration.
193   DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
194   /// \brief Returns data-sharing attributes for the specified declaration.
195   DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
196   /// \brief Checks if the specified variables has data-sharing attributes which
197   /// match specified \a CPred predicate in any directive which matches \a DPred
198   /// predicate.
199   DSAVarData hasDSA(ValueDecl *D,
200                     const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
201                     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
202                     bool FromParent);
203   /// \brief Checks if the specified variables has data-sharing attributes which
204   /// match specified \a CPred predicate in any innermost directive which
205   /// matches \a DPred predicate.
206   DSAVarData
207   hasInnermostDSA(ValueDecl *D,
208                   const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
209                   const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
210                   bool FromParent);
211   /// \brief Checks if the specified variables has explicit data-sharing
212   /// attributes which match specified \a CPred predicate at the specified
213   /// OpenMP region.
214   bool hasExplicitDSA(ValueDecl *D,
215                       const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
216                       unsigned Level, bool NotLastprivate = false);
217 
218   /// \brief Returns true if the directive at level \Level matches in the
219   /// specified \a DPred predicate.
220   bool hasExplicitDirective(
221       const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
222       unsigned Level);
223 
224   /// \brief Finds a directive which matches specified \a DPred predicate.
225   bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
226                                                   const DeclarationNameInfo &,
227                                                   SourceLocation)> &DPred,
228                     bool FromParent);
229 
230   /// \brief Returns currently analyzed directive.
231   OpenMPDirectiveKind getCurrentDirective() const {
232     return Stack.back().Directive;
233   }
234   /// \brief Returns parent directive.
235   OpenMPDirectiveKind getParentDirective() const {
236     if (Stack.size() > 2)
237       return Stack[Stack.size() - 2].Directive;
238     return OMPD_unknown;
239   }
240 
241   /// \brief Set default data sharing attribute to none.
242   void setDefaultDSANone(SourceLocation Loc) {
243     Stack.back().DefaultAttr = DSA_none;
244     Stack.back().DefaultAttrLoc = Loc;
245   }
246   /// \brief Set default data sharing attribute to shared.
247   void setDefaultDSAShared(SourceLocation Loc) {
248     Stack.back().DefaultAttr = DSA_shared;
249     Stack.back().DefaultAttrLoc = Loc;
250   }
251 
252   DefaultDataSharingAttributes getDefaultDSA() const {
253     return Stack.back().DefaultAttr;
254   }
255   SourceLocation getDefaultDSALocation() const {
256     return Stack.back().DefaultAttrLoc;
257   }
258 
259   /// \brief Checks if the specified variable is a threadprivate.
260   bool isThreadPrivate(VarDecl *D) {
261     DSAVarData DVar = getTopDSA(D, false);
262     return isOpenMPThreadPrivate(DVar.CKind);
263   }
264 
265   /// \brief Marks current region as ordered (it has an 'ordered' clause).
266   void setOrderedRegion(bool IsOrdered, Expr *Param) {
267     Stack.back().OrderedRegion.setInt(IsOrdered);
268     Stack.back().OrderedRegion.setPointer(Param);
269   }
270   /// \brief Returns true, if parent region is ordered (has associated
271   /// 'ordered' clause), false - otherwise.
272   bool isParentOrderedRegion() const {
273     if (Stack.size() > 2)
274       return Stack[Stack.size() - 2].OrderedRegion.getInt();
275     return false;
276   }
277   /// \brief Returns optional parameter for the ordered region.
278   Expr *getParentOrderedRegionParam() const {
279     if (Stack.size() > 2)
280       return Stack[Stack.size() - 2].OrderedRegion.getPointer();
281     return nullptr;
282   }
283   /// \brief Marks current region as nowait (it has a 'nowait' clause).
284   void setNowaitRegion(bool IsNowait = true) {
285     Stack.back().NowaitRegion = IsNowait;
286   }
287   /// \brief Returns true, if parent region is nowait (has associated
288   /// 'nowait' clause), false - otherwise.
289   bool isParentNowaitRegion() const {
290     if (Stack.size() > 2)
291       return Stack[Stack.size() - 2].NowaitRegion;
292     return false;
293   }
294   /// \brief Marks parent region as cancel region.
295   void setParentCancelRegion(bool Cancel = true) {
296     if (Stack.size() > 2)
297       Stack[Stack.size() - 2].CancelRegion =
298           Stack[Stack.size() - 2].CancelRegion || Cancel;
299   }
300   /// \brief Return true if current region has inner cancel construct.
301   bool isCancelRegion() const { return Stack.back().CancelRegion; }
302 
303   /// \brief Set collapse value for the region.
304   void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
305   /// \brief Return collapse value for region.
306   unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
307 
308   /// \brief Marks current target region as one with closely nested teams
309   /// region.
310   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
311     if (Stack.size() > 2)
312       Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
313   }
314   /// \brief Returns true, if current region has closely nested teams region.
315   bool hasInnerTeamsRegion() const {
316     return getInnerTeamsRegionLoc().isValid();
317   }
318   /// \brief Returns location of the nested teams region (if any).
319   SourceLocation getInnerTeamsRegionLoc() const {
320     if (Stack.size() > 1)
321       return Stack.back().InnerTeamsRegionLoc;
322     return SourceLocation();
323   }
324 
325   Scope *getCurScope() const { return Stack.back().CurScope; }
326   Scope *getCurScope() { return Stack.back().CurScope; }
327   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
328 
329   // Do the check specified in \a Check to all component lists and return true
330   // if any issue is found.
331   bool checkMappableExprComponentListsForDecl(
332       ValueDecl *VD, bool CurrentRegionOnly,
333       const llvm::function_ref<
334           bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
335                OpenMPClauseKind)> &Check) {
336     auto SI = Stack.rbegin();
337     auto SE = Stack.rend();
338 
339     if (SI == SE)
340       return false;
341 
342     if (CurrentRegionOnly) {
343       SE = std::next(SI);
344     } else {
345       ++SI;
346     }
347 
348     for (; SI != SE; ++SI) {
349       auto MI = SI->MappedExprComponents.find(VD);
350       if (MI != SI->MappedExprComponents.end())
351         for (auto &L : MI->second.Components)
352           if (Check(L, MI->second.Kind))
353             return true;
354     }
355     return false;
356   }
357 
358   // Create a new mappable expression component list associated with a given
359   // declaration and initialize it with the provided list of components.
360   void addMappableExpressionComponents(
361       ValueDecl *VD,
362       OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
363       OpenMPClauseKind WhereFoundClauseKind) {
364     assert(Stack.size() > 1 &&
365            "Not expecting to retrieve components from a empty stack!");
366     auto &MEC = Stack.back().MappedExprComponents[VD];
367     // Create new entry and append the new components there.
368     MEC.Components.resize(MEC.Components.size() + 1);
369     MEC.Components.back().append(Components.begin(), Components.end());
370     MEC.Kind = WhereFoundClauseKind;
371   }
372 
373   unsigned getNestingLevel() const {
374     assert(Stack.size() > 1);
375     return Stack.size() - 2;
376   }
377   void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
378     assert(Stack.size() > 2);
379     assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
380     Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
381   }
382   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
383   getDoacrossDependClauses() const {
384     assert(Stack.size() > 1);
385     if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
386       auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
387       return llvm::make_range(Ref.begin(), Ref.end());
388     }
389     return llvm::make_range(Stack[0].DoacrossDepends.end(),
390                             Stack[0].DoacrossDepends.end());
391   }
392 };
393 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
394   return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
395          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
396 }
397 } // namespace
398 
399 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
400   auto *VD = dyn_cast<VarDecl>(D);
401   auto *FD = dyn_cast<FieldDecl>(D);
402   if (VD != nullptr) {
403     VD = VD->getCanonicalDecl();
404     D = VD;
405   } else {
406     assert(FD);
407     FD = FD->getCanonicalDecl();
408     D = FD;
409   }
410   return D;
411 }
412 
413 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator &Iter,
414                                           ValueDecl *D) {
415   D = getCanonicalDecl(D);
416   auto *VD = dyn_cast<VarDecl>(D);
417   auto *FD = dyn_cast<FieldDecl>(D);
418   DSAVarData DVar;
419   if (Iter == std::prev(Stack.rend())) {
420     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
421     // in a region but not in construct]
422     //  File-scope or namespace-scope variables referenced in called routines
423     //  in the region are shared unless they appear in a threadprivate
424     //  directive.
425     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
426       DVar.CKind = OMPC_shared;
427 
428     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
429     // in a region but not in construct]
430     //  Variables with static storage duration that are declared in called
431     //  routines in the region are shared.
432     if (VD && VD->hasGlobalStorage())
433       DVar.CKind = OMPC_shared;
434 
435     // Non-static data members are shared by default.
436     if (FD)
437       DVar.CKind = OMPC_shared;
438 
439     return DVar;
440   }
441 
442   DVar.DKind = Iter->Directive;
443   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
444   // in a Construct, C/C++, predetermined, p.1]
445   // Variables with automatic storage duration that are declared in a scope
446   // inside the construct are private.
447   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
448       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
449     DVar.CKind = OMPC_private;
450     return DVar;
451   }
452 
453   // Explicitly specified attributes and local variables with predetermined
454   // attributes.
455   if (Iter->SharingMap.count(D)) {
456     DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
457     DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
458     DVar.CKind = Iter->SharingMap[D].Attributes;
459     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
460     return DVar;
461   }
462 
463   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
464   // in a Construct, C/C++, implicitly determined, p.1]
465   //  In a parallel or task construct, the data-sharing attributes of these
466   //  variables are determined by the default clause, if present.
467   switch (Iter->DefaultAttr) {
468   case DSA_shared:
469     DVar.CKind = OMPC_shared;
470     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
471     return DVar;
472   case DSA_none:
473     return DVar;
474   case DSA_unspecified:
475     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
476     // in a Construct, implicitly determined, p.2]
477     //  In a parallel construct, if no default clause is present, these
478     //  variables are shared.
479     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
480     if (isOpenMPParallelDirective(DVar.DKind) ||
481         isOpenMPTeamsDirective(DVar.DKind)) {
482       DVar.CKind = OMPC_shared;
483       return DVar;
484     }
485 
486     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
487     // in a Construct, implicitly determined, p.4]
488     //  In a task construct, if no default clause is present, a variable that in
489     //  the enclosing context is determined to be shared by all implicit tasks
490     //  bound to the current team is shared.
491     if (isOpenMPTaskingDirective(DVar.DKind)) {
492       DSAVarData DVarTemp;
493       for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
494            I != EE; ++I) {
495         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
496         // Referenced in a Construct, implicitly determined, p.6]
497         //  In a task construct, if no default clause is present, a variable
498         //  whose data-sharing attribute is not determined by the rules above is
499         //  firstprivate.
500         DVarTemp = getDSA(I, D);
501         if (DVarTemp.CKind != OMPC_shared) {
502           DVar.RefExpr = nullptr;
503           DVar.CKind = OMPC_firstprivate;
504           return DVar;
505         }
506         if (isParallelOrTaskRegion(I->Directive))
507           break;
508       }
509       DVar.CKind =
510           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
511       return DVar;
512     }
513   }
514   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
515   // in a Construct, implicitly determined, p.3]
516   //  For constructs other than task, if no default clause is present, these
517   //  variables inherit their data-sharing attributes from the enclosing
518   //  context.
519   return getDSA(++Iter, D);
520 }
521 
522 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
523   assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
524   D = getCanonicalDecl(D);
525   auto It = Stack.back().AlignedMap.find(D);
526   if (It == Stack.back().AlignedMap.end()) {
527     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
528     Stack.back().AlignedMap[D] = NewDE;
529     return nullptr;
530   } else {
531     assert(It->second && "Unexpected nullptr expr in the aligned map");
532     return It->second;
533   }
534   return nullptr;
535 }
536 
537 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
538   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
539   D = getCanonicalDecl(D);
540   Stack.back().LCVMap.insert(
541       std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
542 }
543 
544 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
545   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
546   D = getCanonicalDecl(D);
547   return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
548                                           : LCDeclInfo(0, nullptr);
549 }
550 
551 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
552   assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
553   D = getCanonicalDecl(D);
554   return Stack[Stack.size() - 2].LCVMap.count(D) > 0
555              ? Stack[Stack.size() - 2].LCVMap[D]
556              : LCDeclInfo(0, nullptr);
557 }
558 
559 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
560   assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
561   if (Stack[Stack.size() - 2].LCVMap.size() < I)
562     return nullptr;
563   for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
564     if (Pair.second.first == I)
565       return Pair.first;
566   }
567   return nullptr;
568 }
569 
570 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
571                         DeclRefExpr *PrivateCopy) {
572   D = getCanonicalDecl(D);
573   if (A == OMPC_threadprivate) {
574     auto &Data = Stack[0].SharingMap[D];
575     Data.Attributes = A;
576     Data.RefExpr.setPointer(E);
577     Data.PrivateCopy = nullptr;
578   } else {
579     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
580     auto &Data = Stack.back().SharingMap[D];
581     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
582            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
583            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
584            (isLoopControlVariable(D).first && A == OMPC_private));
585     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
586       Data.RefExpr.setInt(/*IntVal=*/true);
587       return;
588     }
589     const bool IsLastprivate =
590         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
591     Data.Attributes = A;
592     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
593     Data.PrivateCopy = PrivateCopy;
594     if (PrivateCopy) {
595       auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
596       Data.Attributes = A;
597       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
598       Data.PrivateCopy = nullptr;
599     }
600   }
601 }
602 
603 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
604   D = D->getCanonicalDecl();
605   if (Stack.size() > 2) {
606     reverse_iterator I = Iter, E = std::prev(Stack.rend());
607     Scope *TopScope = nullptr;
608     while (I != E && !isParallelOrTaskRegion(I->Directive)) {
609       ++I;
610     }
611     if (I == E)
612       return false;
613     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
614     Scope *CurScope = getCurScope();
615     while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
616       CurScope = CurScope->getParent();
617     }
618     return CurScope != TopScope;
619   }
620   return false;
621 }
622 
623 /// \brief Build a variable declaration for OpenMP loop iteration variable.
624 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
625                              StringRef Name, const AttrVec *Attrs = nullptr) {
626   DeclContext *DC = SemaRef.CurContext;
627   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
628   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
629   VarDecl *Decl =
630       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
631   if (Attrs) {
632     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
633          I != E; ++I)
634       Decl->addAttr(*I);
635   }
636   Decl->setImplicit();
637   return Decl;
638 }
639 
640 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
641                                      SourceLocation Loc,
642                                      bool RefersToCapture = false) {
643   D->setReferenced();
644   D->markUsed(S.Context);
645   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
646                              SourceLocation(), D, RefersToCapture, Loc, Ty,
647                              VK_LValue);
648 }
649 
650 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
651   D = getCanonicalDecl(D);
652   DSAVarData DVar;
653 
654   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
655   // in a Construct, C/C++, predetermined, p.1]
656   //  Variables appearing in threadprivate directives are threadprivate.
657   auto *VD = dyn_cast<VarDecl>(D);
658   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
659        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
660          SemaRef.getLangOpts().OpenMPUseTLS &&
661          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
662       (VD && VD->getStorageClass() == SC_Register &&
663        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
664     addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
665                                D->getLocation()),
666            OMPC_threadprivate);
667   }
668   if (Stack[0].SharingMap.count(D)) {
669     DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
670     DVar.CKind = OMPC_threadprivate;
671     return DVar;
672   }
673 
674   if (Stack.size() == 1) {
675     // Not in OpenMP execution region and top scope was already checked.
676     return DVar;
677   }
678 
679   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
680   // in a Construct, C/C++, predetermined, p.4]
681   //  Static data members are shared.
682   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
683   // in a Construct, C/C++, predetermined, p.7]
684   //  Variables with static storage duration that are declared in a scope
685   //  inside the construct are shared.
686   auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
687   if (VD && VD->isStaticDataMember()) {
688     DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
689     if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
690       return DVar;
691 
692     DVar.CKind = OMPC_shared;
693     return DVar;
694   }
695 
696   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
697   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
698   Type = SemaRef.getASTContext().getBaseElementType(Type);
699   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
700   // in a Construct, C/C++, predetermined, p.6]
701   //  Variables with const qualified type having no mutable member are
702   //  shared.
703   CXXRecordDecl *RD =
704       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
705   if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
706     if (auto *CTD = CTSD->getSpecializedTemplate())
707       RD = CTD->getTemplatedDecl();
708   if (IsConstant &&
709       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
710         RD->hasMutableFields())) {
711     // Variables with const-qualified type having no mutable member may be
712     // listed in a firstprivate clause, even if they are static data members.
713     DSAVarData DVarTemp = hasDSA(
714         D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
715         MatchesAlways, FromParent);
716     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
717       return DVar;
718 
719     DVar.CKind = OMPC_shared;
720     return DVar;
721   }
722 
723   // Explicitly specified attributes and local variables with predetermined
724   // attributes.
725   auto StartI = std::next(Stack.rbegin());
726   auto EndI = std::prev(Stack.rend());
727   if (FromParent && StartI != EndI) {
728     StartI = std::next(StartI);
729   }
730   auto I = std::prev(StartI);
731   if (I->SharingMap.count(D)) {
732     DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
733     DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
734     DVar.CKind = I->SharingMap[D].Attributes;
735     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
736   }
737 
738   return DVar;
739 }
740 
741 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
742                                                   bool FromParent) {
743   D = getCanonicalDecl(D);
744   auto StartI = Stack.rbegin();
745   auto EndI = std::prev(Stack.rend());
746   if (FromParent && StartI != EndI) {
747     StartI = std::next(StartI);
748   }
749   return getDSA(StartI, D);
750 }
751 
752 DSAStackTy::DSAVarData
753 DSAStackTy::hasDSA(ValueDecl *D,
754                    const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
755                    const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
756                    bool FromParent) {
757   D = getCanonicalDecl(D);
758   auto StartI = std::next(Stack.rbegin());
759   auto EndI = Stack.rend();
760   if (FromParent && StartI != EndI) {
761     StartI = std::next(StartI);
762   }
763   for (auto I = StartI, EE = EndI; I != EE; ++I) {
764     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
765       continue;
766     DSAVarData DVar = getDSA(I, D);
767     if (CPred(DVar.CKind))
768       return DVar;
769   }
770   return DSAVarData();
771 }
772 
773 DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
774     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
775     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
776     bool FromParent) {
777   D = getCanonicalDecl(D);
778   auto StartI = std::next(Stack.rbegin());
779   auto EndI = Stack.rend();
780   if (FromParent && StartI != EndI)
781     StartI = std::next(StartI);
782   if (StartI == EndI || !DPred(StartI->Directive))
783     return DSAVarData();
784   DSAVarData DVar = getDSA(StartI, D);
785   return CPred(DVar.CKind) ? DVar : DSAVarData();
786 }
787 
788 bool DSAStackTy::hasExplicitDSA(
789     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
790     unsigned Level, bool NotLastprivate) {
791   if (CPred(ClauseKindMode))
792     return true;
793   D = getCanonicalDecl(D);
794   auto StartI = std::next(Stack.begin());
795   auto EndI = Stack.end();
796   if (std::distance(StartI, EndI) <= (int)Level)
797     return false;
798   std::advance(StartI, Level);
799   return (StartI->SharingMap.count(D) > 0) &&
800          StartI->SharingMap[D].RefExpr.getPointer() &&
801          CPred(StartI->SharingMap[D].Attributes) &&
802          (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
803 }
804 
805 bool DSAStackTy::hasExplicitDirective(
806     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807     unsigned Level) {
808   auto StartI = std::next(Stack.begin());
809   auto EndI = Stack.end();
810   if (std::distance(StartI, EndI) <= (int)Level)
811     return false;
812   std::advance(StartI, Level);
813   return DPred(StartI->Directive);
814 }
815 
816 bool DSAStackTy::hasDirective(
817     const llvm::function_ref<bool(OpenMPDirectiveKind,
818                                   const DeclarationNameInfo &, SourceLocation)>
819         &DPred,
820     bool FromParent) {
821   // We look only in the enclosing region.
822   if (Stack.size() < 2)
823     return false;
824   auto StartI = std::next(Stack.rbegin());
825   auto EndI = std::prev(Stack.rend());
826   if (FromParent && StartI != EndI) {
827     StartI = std::next(StartI);
828   }
829   for (auto I = StartI, EE = EndI; I != EE; ++I) {
830     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831       return true;
832   }
833   return false;
834 }
835 
836 void Sema::InitDataSharingAttributesStack() {
837   VarDataSharingAttributesStack = new DSAStackTy(*this);
838 }
839 
840 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841 
842 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
843   assert(LangOpts.OpenMP && "OpenMP is not allowed");
844 
845   auto &Ctx = getASTContext();
846   bool IsByRef = true;
847 
848   // Find the directive that is associated with the provided scope.
849   auto Ty = D->getType();
850 
851   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
852     // This table summarizes how a given variable should be passed to the device
853     // given its type and the clauses where it appears. This table is based on
854     // the description in OpenMP 4.5 [2.10.4, target Construct] and
855     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856     //
857     // =========================================================================
858     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
859     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
860     // =========================================================================
861     // | scl  |               |     |       |       -       |          | bycopy|
862     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
863     // | scl  |               |  x  |   -   |       -       |     -    | null  |
864     // | scl  |       x       |     |       |       -       |          | byref |
865     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
866     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
867     // | scl  |               |  -  |   -   |       -       |     x    | byref |
868     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
869     //
870     // | agg  |      n.a.     |     |       |       -       |          | byref |
871     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
872     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
873     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
874     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
875     //
876     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
877     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
878     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
879     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
880     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
881     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
882     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
883     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
884     // =========================================================================
885     // Legend:
886     //  scl - scalar
887     //  ptr - pointer
888     //  agg - aggregate
889     //  x - applies
890     //  - - invalid in this combination
891     //  [] - mapped with an array section
892     //  byref - should be mapped by reference
893     //  byval - should be mapped by value
894     //  null - initialize a local variable to null on the device
895     //
896     // Observations:
897     //  - All scalar declarations that show up in a map clause have to be passed
898     //    by reference, because they may have been mapped in the enclosing data
899     //    environment.
900     //  - If the scalar value does not fit the size of uintptr, it has to be
901     //    passed by reference, regardless the result in the table above.
902     //  - For pointers mapped by value that have either an implicit map or an
903     //    array section, the runtime library may pass the NULL value to the
904     //    device instead of the value passed to it by the compiler.
905 
906     if (Ty->isReferenceType())
907       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
908 
909     // Locate map clauses and see if the variable being captured is referred to
910     // in any of those clauses. Here we only care about variables, not fields,
911     // because fields are part of aggregates.
912     bool IsVariableUsedInMapClause = false;
913     bool IsVariableAssociatedWithSection = false;
914 
915     DSAStack->checkMappableExprComponentListsForDecl(
916         D, /*CurrentRegionOnly=*/true,
917         [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
918                 MapExprComponents,
919             OpenMPClauseKind WhereFoundClauseKind) {
920           // Only the map clause information influences how a variable is
921           // captured. E.g. is_device_ptr does not require changing the default
922           // behaviour.
923           if (WhereFoundClauseKind != OMPC_map)
924             return false;
925 
926           auto EI = MapExprComponents.rbegin();
927           auto EE = MapExprComponents.rend();
928 
929           assert(EI != EE && "Invalid map expression!");
930 
931           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
932             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
933 
934           ++EI;
935           if (EI == EE)
936             return false;
937 
938           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
939               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
940               isa<MemberExpr>(EI->getAssociatedExpression())) {
941             IsVariableAssociatedWithSection = true;
942             // There is nothing more we need to know about this variable.
943             return true;
944           }
945 
946           // Keep looking for more map info.
947           return false;
948         });
949 
950     if (IsVariableUsedInMapClause) {
951       // If variable is identified in a map clause it is always captured by
952       // reference except if it is a pointer that is dereferenced somehow.
953       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
954     } else {
955       // By default, all the data that has a scalar type is mapped by copy.
956       IsByRef = !Ty->isScalarType();
957     }
958   }
959 
960   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
961     IsByRef = !DSAStack->hasExplicitDSA(
962         D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
963         Level, /*NotLastprivate=*/true);
964   }
965 
966   // When passing data by copy, we need to make sure it fits the uintptr size
967   // and alignment, because the runtime library only deals with uintptr types.
968   // If it does not fit the uintptr size, we need to pass the data by reference
969   // instead.
970   if (!IsByRef &&
971       (Ctx.getTypeSizeInChars(Ty) >
972            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
973        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
974     IsByRef = true;
975   }
976 
977   return IsByRef;
978 }
979 
980 unsigned Sema::getOpenMPNestingLevel() const {
981   assert(getLangOpts().OpenMP);
982   return DSAStack->getNestingLevel();
983 }
984 
985 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
986   assert(LangOpts.OpenMP && "OpenMP is not allowed");
987   D = getCanonicalDecl(D);
988 
989   // If we are attempting to capture a global variable in a directive with
990   // 'target' we return true so that this global is also mapped to the device.
991   //
992   // FIXME: If the declaration is enclosed in a 'declare target' directive,
993   // then it should not be captured. Therefore, an extra check has to be
994   // inserted here once support for 'declare target' is added.
995   //
996   auto *VD = dyn_cast<VarDecl>(D);
997   if (VD && !VD->hasLocalStorage()) {
998     if (DSAStack->getCurrentDirective() == OMPD_target &&
999         !DSAStack->isClauseParsingMode())
1000       return VD;
1001     if (DSAStack->hasDirective(
1002             [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1003                SourceLocation) -> bool {
1004               return isOpenMPTargetExecutionDirective(K);
1005             },
1006             false))
1007       return VD;
1008   }
1009 
1010   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1011       (!DSAStack->isClauseParsingMode() ||
1012        DSAStack->getParentDirective() != OMPD_unknown)) {
1013     auto &&Info = DSAStack->isLoopControlVariable(D);
1014     if (Info.first ||
1015         (VD && VD->hasLocalStorage() &&
1016          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
1017         (VD && DSAStack->isForceVarCapturing()))
1018       return VD ? VD : Info.second;
1019     auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1020     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1021       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1022     DVarPrivate = DSAStack->hasDSA(
1023         D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1024         DSAStack->isClauseParsingMode());
1025     if (DVarPrivate.CKind != OMPC_unknown)
1026       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1027   }
1028   return nullptr;
1029 }
1030 
1031 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
1032   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1033   return DSAStack->hasExplicitDSA(
1034       D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
1035 }
1036 
1037 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
1038   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1039   // Return true if the current level is no longer enclosed in a target region.
1040 
1041   auto *VD = dyn_cast<VarDecl>(D);
1042   return VD && !VD->hasLocalStorage() &&
1043          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1044                                         Level);
1045 }
1046 
1047 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1048 
1049 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1050                                const DeclarationNameInfo &DirName,
1051                                Scope *CurScope, SourceLocation Loc) {
1052   DSAStack->push(DKind, DirName, CurScope, Loc);
1053   PushExpressionEvaluationContext(PotentiallyEvaluated);
1054 }
1055 
1056 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1057   DSAStack->setClauseParsingMode(K);
1058 }
1059 
1060 void Sema::EndOpenMPClause() {
1061   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1062 }
1063 
1064 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1065   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1066   //  A variable of class type (or array thereof) that appears in a lastprivate
1067   //  clause requires an accessible, unambiguous default constructor for the
1068   //  class type, unless the list item is also specified in a firstprivate
1069   //  clause.
1070   if (auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1071     for (auto *C : D->clauses()) {
1072       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1073         SmallVector<Expr *, 8> PrivateCopies;
1074         for (auto *DE : Clause->varlists()) {
1075           if (DE->isValueDependent() || DE->isTypeDependent()) {
1076             PrivateCopies.push_back(nullptr);
1077             continue;
1078           }
1079           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1080           VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1081           QualType Type = VD->getType().getNonReferenceType();
1082           auto DVar = DSAStack->getTopDSA(VD, false);
1083           if (DVar.CKind == OMPC_lastprivate) {
1084             // Generate helper private variable and initialize it with the
1085             // default value. The address of the original variable is replaced
1086             // by the address of the new private variable in CodeGen. This new
1087             // variable is not added to IdResolver, so the code in the OpenMP
1088             // region uses original variable for proper diagnostics.
1089             auto *VDPrivate = buildVarDecl(
1090                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1091                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
1092             ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1093             if (VDPrivate->isInvalidDecl())
1094               continue;
1095             PrivateCopies.push_back(buildDeclRefExpr(
1096                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1097           } else {
1098             // The variable is also a firstprivate, so initialization sequence
1099             // for private copy is generated already.
1100             PrivateCopies.push_back(nullptr);
1101           }
1102         }
1103         // Set initializers to private copies if no errors were found.
1104         if (PrivateCopies.size() == Clause->varlist_size())
1105           Clause->setPrivateCopies(PrivateCopies);
1106       }
1107     }
1108   }
1109 
1110   DSAStack->pop();
1111   DiscardCleanupsInEvaluationContext();
1112   PopExpressionEvaluationContext();
1113 }
1114 
1115 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1116                                      Expr *NumIterations, Sema &SemaRef,
1117                                      Scope *S, DSAStackTy *Stack);
1118 
1119 namespace {
1120 
1121 class VarDeclFilterCCC : public CorrectionCandidateCallback {
1122 private:
1123   Sema &SemaRef;
1124 
1125 public:
1126   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1127   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1128     NamedDecl *ND = Candidate.getCorrectionDecl();
1129     if (auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
1130       return VD->hasGlobalStorage() &&
1131              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1132                                    SemaRef.getCurScope());
1133     }
1134     return false;
1135   }
1136 };
1137 
1138 class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1139 private:
1140   Sema &SemaRef;
1141 
1142 public:
1143   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1144   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1145     NamedDecl *ND = Candidate.getCorrectionDecl();
1146     if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1147       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1148                                    SemaRef.getCurScope());
1149     }
1150     return false;
1151   }
1152 };
1153 
1154 } // namespace
1155 
1156 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1157                                          CXXScopeSpec &ScopeSpec,
1158                                          const DeclarationNameInfo &Id) {
1159   LookupResult Lookup(*this, Id, LookupOrdinaryName);
1160   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1161 
1162   if (Lookup.isAmbiguous())
1163     return ExprError();
1164 
1165   VarDecl *VD;
1166   if (!Lookup.isSingleResult()) {
1167     if (TypoCorrection Corrected = CorrectTypo(
1168             Id, LookupOrdinaryName, CurScope, nullptr,
1169             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1170       diagnoseTypo(Corrected,
1171                    PDiag(Lookup.empty()
1172                              ? diag::err_undeclared_var_use_suggest
1173                              : diag::err_omp_expected_var_arg_suggest)
1174                        << Id.getName());
1175       VD = Corrected.getCorrectionDeclAs<VarDecl>();
1176     } else {
1177       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1178                                        : diag::err_omp_expected_var_arg)
1179           << Id.getName();
1180       return ExprError();
1181     }
1182   } else {
1183     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1184       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1185       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1186       return ExprError();
1187     }
1188   }
1189   Lookup.suppressDiagnostics();
1190 
1191   // OpenMP [2.9.2, Syntax, C/C++]
1192   //   Variables must be file-scope, namespace-scope, or static block-scope.
1193   if (!VD->hasGlobalStorage()) {
1194     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1195         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1196     bool IsDecl =
1197         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1198     Diag(VD->getLocation(),
1199          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1200         << VD;
1201     return ExprError();
1202   }
1203 
1204   VarDecl *CanonicalVD = VD->getCanonicalDecl();
1205   NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
1206   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1207   //   A threadprivate directive for file-scope variables must appear outside
1208   //   any definition or declaration.
1209   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1210       !getCurLexicalContext()->isTranslationUnit()) {
1211     Diag(Id.getLoc(), diag::err_omp_var_scope)
1212         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1213     bool IsDecl =
1214         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1215     Diag(VD->getLocation(),
1216          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1217         << VD;
1218     return ExprError();
1219   }
1220   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1221   //   A threadprivate directive for static class member variables must appear
1222   //   in the class definition, in the same scope in which the member
1223   //   variables are declared.
1224   if (CanonicalVD->isStaticDataMember() &&
1225       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1226     Diag(Id.getLoc(), diag::err_omp_var_scope)
1227         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1228     bool IsDecl =
1229         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1230     Diag(VD->getLocation(),
1231          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1232         << VD;
1233     return ExprError();
1234   }
1235   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1236   //   A threadprivate directive for namespace-scope variables must appear
1237   //   outside any definition or declaration other than the namespace
1238   //   definition itself.
1239   if (CanonicalVD->getDeclContext()->isNamespace() &&
1240       (!getCurLexicalContext()->isFileContext() ||
1241        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1242     Diag(Id.getLoc(), diag::err_omp_var_scope)
1243         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1244     bool IsDecl =
1245         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1246     Diag(VD->getLocation(),
1247          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1248         << VD;
1249     return ExprError();
1250   }
1251   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1252   //   A threadprivate directive for static block-scope variables must appear
1253   //   in the scope of the variable and not in a nested scope.
1254   if (CanonicalVD->isStaticLocal() && CurScope &&
1255       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1256     Diag(Id.getLoc(), diag::err_omp_var_scope)
1257         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1258     bool IsDecl =
1259         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1260     Diag(VD->getLocation(),
1261          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1262         << VD;
1263     return ExprError();
1264   }
1265 
1266   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1267   //   A threadprivate directive must lexically precede all references to any
1268   //   of the variables in its list.
1269   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1270     Diag(Id.getLoc(), diag::err_omp_var_used)
1271         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1272     return ExprError();
1273   }
1274 
1275   QualType ExprType = VD->getType().getNonReferenceType();
1276   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1277                              SourceLocation(), VD,
1278                              /*RefersToEnclosingVariableOrCapture=*/false,
1279                              Id.getLoc(), ExprType, VK_LValue);
1280 }
1281 
1282 Sema::DeclGroupPtrTy
1283 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1284                                         ArrayRef<Expr *> VarList) {
1285   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1286     CurContext->addDecl(D);
1287     return DeclGroupPtrTy::make(DeclGroupRef(D));
1288   }
1289   return nullptr;
1290 }
1291 
1292 namespace {
1293 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1294   Sema &SemaRef;
1295 
1296 public:
1297   bool VisitDeclRefExpr(const DeclRefExpr *E) {
1298     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1299       if (VD->hasLocalStorage()) {
1300         SemaRef.Diag(E->getLocStart(),
1301                      diag::err_omp_local_var_in_threadprivate_init)
1302             << E->getSourceRange();
1303         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1304             << VD << VD->getSourceRange();
1305         return true;
1306       }
1307     }
1308     return false;
1309   }
1310   bool VisitStmt(const Stmt *S) {
1311     for (auto Child : S->children()) {
1312       if (Child && Visit(Child))
1313         return true;
1314     }
1315     return false;
1316   }
1317   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1318 };
1319 } // namespace
1320 
1321 OMPThreadPrivateDecl *
1322 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1323   SmallVector<Expr *, 8> Vars;
1324   for (auto &RefExpr : VarList) {
1325     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1326     VarDecl *VD = cast<VarDecl>(DE->getDecl());
1327     SourceLocation ILoc = DE->getExprLoc();
1328 
1329     // Mark variable as used.
1330     VD->setReferenced();
1331     VD->markUsed(Context);
1332 
1333     QualType QType = VD->getType();
1334     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1335       // It will be analyzed later.
1336       Vars.push_back(DE);
1337       continue;
1338     }
1339 
1340     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1341     //   A threadprivate variable must not have an incomplete type.
1342     if (RequireCompleteType(ILoc, VD->getType(),
1343                             diag::err_omp_threadprivate_incomplete_type)) {
1344       continue;
1345     }
1346 
1347     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1348     //   A threadprivate variable must not have a reference type.
1349     if (VD->getType()->isReferenceType()) {
1350       Diag(ILoc, diag::err_omp_ref_type_arg)
1351           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1352       bool IsDecl =
1353           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1354       Diag(VD->getLocation(),
1355            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1356           << VD;
1357       continue;
1358     }
1359 
1360     // Check if this is a TLS variable. If TLS is not being supported, produce
1361     // the corresponding diagnostic.
1362     if ((VD->getTLSKind() != VarDecl::TLS_None &&
1363          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1364            getLangOpts().OpenMPUseTLS &&
1365            getASTContext().getTargetInfo().isTLSSupported())) ||
1366         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1367          !VD->isLocalVarDecl())) {
1368       Diag(ILoc, diag::err_omp_var_thread_local)
1369           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1370       bool IsDecl =
1371           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1372       Diag(VD->getLocation(),
1373            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1374           << VD;
1375       continue;
1376     }
1377 
1378     // Check if initial value of threadprivate variable reference variable with
1379     // local storage (it is not supported by runtime).
1380     if (auto Init = VD->getAnyInitializer()) {
1381       LocalVarRefChecker Checker(*this);
1382       if (Checker.Visit(Init))
1383         continue;
1384     }
1385 
1386     Vars.push_back(RefExpr);
1387     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1388     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1389         Context, SourceRange(Loc, Loc)));
1390     if (auto *ML = Context.getASTMutationListener())
1391       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1392   }
1393   OMPThreadPrivateDecl *D = nullptr;
1394   if (!Vars.empty()) {
1395     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1396                                      Vars);
1397     D->setAccess(AS_public);
1398   }
1399   return D;
1400 }
1401 
1402 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1403                               const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1404                               bool IsLoopIterVar = false) {
1405   if (DVar.RefExpr) {
1406     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1407         << getOpenMPClauseName(DVar.CKind);
1408     return;
1409   }
1410   enum {
1411     PDSA_StaticMemberShared,
1412     PDSA_StaticLocalVarShared,
1413     PDSA_LoopIterVarPrivate,
1414     PDSA_LoopIterVarLinear,
1415     PDSA_LoopIterVarLastprivate,
1416     PDSA_ConstVarShared,
1417     PDSA_GlobalVarShared,
1418     PDSA_TaskVarFirstprivate,
1419     PDSA_LocalVarPrivate,
1420     PDSA_Implicit
1421   } Reason = PDSA_Implicit;
1422   bool ReportHint = false;
1423   auto ReportLoc = D->getLocation();
1424   auto *VD = dyn_cast<VarDecl>(D);
1425   if (IsLoopIterVar) {
1426     if (DVar.CKind == OMPC_private)
1427       Reason = PDSA_LoopIterVarPrivate;
1428     else if (DVar.CKind == OMPC_lastprivate)
1429       Reason = PDSA_LoopIterVarLastprivate;
1430     else
1431       Reason = PDSA_LoopIterVarLinear;
1432   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1433              DVar.CKind == OMPC_firstprivate) {
1434     Reason = PDSA_TaskVarFirstprivate;
1435     ReportLoc = DVar.ImplicitDSALoc;
1436   } else if (VD && VD->isStaticLocal())
1437     Reason = PDSA_StaticLocalVarShared;
1438   else if (VD && VD->isStaticDataMember())
1439     Reason = PDSA_StaticMemberShared;
1440   else if (VD && VD->isFileVarDecl())
1441     Reason = PDSA_GlobalVarShared;
1442   else if (D->getType().isConstant(SemaRef.getASTContext()))
1443     Reason = PDSA_ConstVarShared;
1444   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1445     ReportHint = true;
1446     Reason = PDSA_LocalVarPrivate;
1447   }
1448   if (Reason != PDSA_Implicit) {
1449     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1450         << Reason << ReportHint
1451         << getOpenMPDirectiveName(Stack->getCurrentDirective());
1452   } else if (DVar.ImplicitDSALoc.isValid()) {
1453     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1454         << getOpenMPClauseName(DVar.CKind);
1455   }
1456 }
1457 
1458 namespace {
1459 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1460   DSAStackTy *Stack;
1461   Sema &SemaRef;
1462   bool ErrorFound;
1463   CapturedStmt *CS;
1464   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1465   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1466 
1467 public:
1468   void VisitDeclRefExpr(DeclRefExpr *E) {
1469     if (E->isTypeDependent() || E->isValueDependent() ||
1470         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1471       return;
1472     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1473       // Skip internally declared variables.
1474       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1475         return;
1476 
1477       auto DVar = Stack->getTopDSA(VD, false);
1478       // Check if the variable has explicit DSA set and stop analysis if it so.
1479       if (DVar.RefExpr)
1480         return;
1481 
1482       auto ELoc = E->getExprLoc();
1483       auto DKind = Stack->getCurrentDirective();
1484       // The default(none) clause requires that each variable that is referenced
1485       // in the construct, and does not have a predetermined data-sharing
1486       // attribute, must have its data-sharing attribute explicitly determined
1487       // by being listed in a data-sharing attribute clause.
1488       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1489           isParallelOrTaskRegion(DKind) &&
1490           VarsWithInheritedDSA.count(VD) == 0) {
1491         VarsWithInheritedDSA[VD] = E;
1492         return;
1493       }
1494 
1495       // OpenMP [2.9.3.6, Restrictions, p.2]
1496       //  A list item that appears in a reduction clause of the innermost
1497       //  enclosing worksharing or parallel construct may not be accessed in an
1498       //  explicit task.
1499       DVar = Stack->hasInnermostDSA(
1500           VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1501           [](OpenMPDirectiveKind K) -> bool {
1502             return isOpenMPParallelDirective(K) ||
1503                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1504           },
1505           false);
1506       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1507         ErrorFound = true;
1508         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1509         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1510         return;
1511       }
1512 
1513       // Define implicit data-sharing attributes for task.
1514       DVar = Stack->getImplicitDSA(VD, false);
1515       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1516           !Stack->isLoopControlVariable(VD).first)
1517         ImplicitFirstprivate.push_back(E);
1518     }
1519   }
1520   void VisitMemberExpr(MemberExpr *E) {
1521     if (E->isTypeDependent() || E->isValueDependent() ||
1522         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1523       return;
1524     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1525       if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1526         auto DVar = Stack->getTopDSA(FD, false);
1527         // Check if the variable has explicit DSA set and stop analysis if it
1528         // so.
1529         if (DVar.RefExpr)
1530           return;
1531 
1532         auto ELoc = E->getExprLoc();
1533         auto DKind = Stack->getCurrentDirective();
1534         // OpenMP [2.9.3.6, Restrictions, p.2]
1535         //  A list item that appears in a reduction clause of the innermost
1536         //  enclosing worksharing or parallel construct may not be accessed in
1537         //  an  explicit task.
1538         DVar = Stack->hasInnermostDSA(
1539             FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1540             [](OpenMPDirectiveKind K) -> bool {
1541               return isOpenMPParallelDirective(K) ||
1542                      isOpenMPWorksharingDirective(K) ||
1543                      isOpenMPTeamsDirective(K);
1544             },
1545             false);
1546         if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1547           ErrorFound = true;
1548           SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1549           ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1550           return;
1551         }
1552 
1553         // Define implicit data-sharing attributes for task.
1554         DVar = Stack->getImplicitDSA(FD, false);
1555         if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1556             !Stack->isLoopControlVariable(FD).first)
1557           ImplicitFirstprivate.push_back(E);
1558       }
1559     }
1560   }
1561   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1562     for (auto *C : S->clauses()) {
1563       // Skip analysis of arguments of implicitly defined firstprivate clause
1564       // for task directives.
1565       if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1566         for (auto *CC : C->children()) {
1567           if (CC)
1568             Visit(CC);
1569         }
1570     }
1571   }
1572   void VisitStmt(Stmt *S) {
1573     for (auto *C : S->children()) {
1574       if (C && !isa<OMPExecutableDirective>(C))
1575         Visit(C);
1576     }
1577   }
1578 
1579   bool isErrorFound() { return ErrorFound; }
1580   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
1581   llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
1582     return VarsWithInheritedDSA;
1583   }
1584 
1585   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1586       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1587 };
1588 } // namespace
1589 
1590 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1591   switch (DKind) {
1592   case OMPD_parallel:
1593   case OMPD_parallel_for:
1594   case OMPD_parallel_for_simd:
1595   case OMPD_parallel_sections:
1596   case OMPD_teams: {
1597     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1598     QualType KmpInt32PtrTy =
1599         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1600     Sema::CapturedParamNameType Params[] = {
1601         std::make_pair(".global_tid.", KmpInt32PtrTy),
1602         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1603         std::make_pair(StringRef(), QualType()) // __context with shared vars
1604     };
1605     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1606                              Params);
1607     break;
1608   }
1609   case OMPD_simd:
1610   case OMPD_for:
1611   case OMPD_for_simd:
1612   case OMPD_sections:
1613   case OMPD_section:
1614   case OMPD_single:
1615   case OMPD_master:
1616   case OMPD_critical:
1617   case OMPD_taskgroup:
1618   case OMPD_distribute:
1619   case OMPD_ordered:
1620   case OMPD_atomic:
1621   case OMPD_target_data:
1622   case OMPD_target:
1623   case OMPD_target_parallel:
1624   case OMPD_target_parallel_for:
1625   case OMPD_target_parallel_for_simd:
1626   case OMPD_target_simd: {
1627     Sema::CapturedParamNameType Params[] = {
1628         std::make_pair(StringRef(), QualType()) // __context with shared vars
1629     };
1630     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1631                              Params);
1632     break;
1633   }
1634   case OMPD_task: {
1635     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1636     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1637     FunctionProtoType::ExtProtoInfo EPI;
1638     EPI.Variadic = true;
1639     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1640     Sema::CapturedParamNameType Params[] = {
1641         std::make_pair(".global_tid.", KmpInt32Ty),
1642         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1643         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1644         std::make_pair(".copy_fn.",
1645                        Context.getPointerType(CopyFnType).withConst()),
1646         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1647         std::make_pair(StringRef(), QualType()) // __context with shared vars
1648     };
1649     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1650                              Params);
1651     // Mark this captured region as inlined, because we don't use outlined
1652     // function directly.
1653     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1654         AlwaysInlineAttr::CreateImplicit(
1655             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1656     break;
1657   }
1658   case OMPD_taskloop:
1659   case OMPD_taskloop_simd: {
1660     QualType KmpInt32Ty =
1661         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1662     QualType KmpUInt64Ty =
1663         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1664     QualType KmpInt64Ty =
1665         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1666     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1667     FunctionProtoType::ExtProtoInfo EPI;
1668     EPI.Variadic = true;
1669     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1670     Sema::CapturedParamNameType Params[] = {
1671         std::make_pair(".global_tid.", KmpInt32Ty),
1672         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1673         std::make_pair(".privates.",
1674                        Context.VoidPtrTy.withConst().withRestrict()),
1675         std::make_pair(
1676             ".copy_fn.",
1677             Context.getPointerType(CopyFnType).withConst().withRestrict()),
1678         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1679         std::make_pair(".lb.", KmpUInt64Ty),
1680         std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1681         std::make_pair(".liter.", KmpInt32Ty),
1682         std::make_pair(StringRef(), QualType()) // __context with shared vars
1683     };
1684     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1685                              Params);
1686     // Mark this captured region as inlined, because we don't use outlined
1687     // function directly.
1688     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1689         AlwaysInlineAttr::CreateImplicit(
1690             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1691     break;
1692   }
1693   case OMPD_distribute_parallel_for_simd:
1694   case OMPD_distribute_simd:
1695   case OMPD_distribute_parallel_for:
1696   case OMPD_teams_distribute: {
1697     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1698     QualType KmpInt32PtrTy =
1699         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1700     Sema::CapturedParamNameType Params[] = {
1701         std::make_pair(".global_tid.", KmpInt32PtrTy),
1702         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1703         std::make_pair(".previous.lb.", Context.getSizeType()),
1704         std::make_pair(".previous.ub.", Context.getSizeType()),
1705         std::make_pair(StringRef(), QualType()) // __context with shared vars
1706     };
1707     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1708                              Params);
1709     break;
1710   }
1711   case OMPD_threadprivate:
1712   case OMPD_taskyield:
1713   case OMPD_barrier:
1714   case OMPD_taskwait:
1715   case OMPD_cancellation_point:
1716   case OMPD_cancel:
1717   case OMPD_flush:
1718   case OMPD_target_enter_data:
1719   case OMPD_target_exit_data:
1720   case OMPD_declare_reduction:
1721   case OMPD_declare_simd:
1722   case OMPD_declare_target:
1723   case OMPD_end_declare_target:
1724   case OMPD_target_update:
1725     llvm_unreachable("OpenMP Directive is not allowed");
1726   case OMPD_unknown:
1727     llvm_unreachable("Unknown OpenMP directive");
1728   }
1729 }
1730 
1731 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1732                                              Expr *CaptureExpr, bool WithInit,
1733                                              bool AsExpression) {
1734   assert(CaptureExpr);
1735   ASTContext &C = S.getASTContext();
1736   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
1737   QualType Ty = Init->getType();
1738   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1739     if (S.getLangOpts().CPlusPlus)
1740       Ty = C.getLValueReferenceType(Ty);
1741     else {
1742       Ty = C.getPointerType(Ty);
1743       ExprResult Res =
1744           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1745       if (!Res.isUsable())
1746         return nullptr;
1747       Init = Res.get();
1748     }
1749     WithInit = true;
1750   }
1751   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1752   if (!WithInit)
1753     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
1754   S.CurContext->addHiddenDecl(CED);
1755   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1756                          /*TypeMayContainAuto=*/true);
1757   return CED;
1758 }
1759 
1760 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1761                                  bool WithInit) {
1762   OMPCapturedExprDecl *CD;
1763   if (auto *VD = S.IsOpenMPCapturedDecl(D))
1764     CD = cast<OMPCapturedExprDecl>(VD);
1765   else
1766     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1767                           /*AsExpression=*/false);
1768   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1769                           CaptureExpr->getExprLoc());
1770 }
1771 
1772 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1773   if (!Ref) {
1774     auto *CD =
1775         buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1776                          CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1777     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1778                            CaptureExpr->getExprLoc());
1779   }
1780   ExprResult Res = Ref;
1781   if (!S.getLangOpts().CPlusPlus &&
1782       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1783       Ref->getType()->isPointerType())
1784     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1785   if (!Res.isUsable())
1786     return ExprError();
1787   return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
1788 }
1789 
1790 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1791                                       ArrayRef<OMPClause *> Clauses) {
1792   if (!S.isUsable()) {
1793     ActOnCapturedRegionError();
1794     return StmtError();
1795   }
1796 
1797   OMPOrderedClause *OC = nullptr;
1798   OMPScheduleClause *SC = nullptr;
1799   SmallVector<OMPLinearClause *, 4> LCs;
1800   // This is required for proper codegen.
1801   for (auto *Clause : Clauses) {
1802     if (isOpenMPPrivate(Clause->getClauseKind()) ||
1803         Clause->getClauseKind() == OMPC_copyprivate ||
1804         (getLangOpts().OpenMPUseTLS &&
1805          getASTContext().getTargetInfo().isTLSSupported() &&
1806          Clause->getClauseKind() == OMPC_copyin)) {
1807       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
1808       // Mark all variables in private list clauses as used in inner region.
1809       for (auto *VarRef : Clause->children()) {
1810         if (auto *E = cast_or_null<Expr>(VarRef)) {
1811           MarkDeclarationsReferencedInExpr(E);
1812         }
1813       }
1814       DSAStack->setForceVarCapturing(/*V=*/false);
1815     } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
1816       // Mark all variables in private list clauses as used in inner region.
1817       // Required for proper codegen of combined directives.
1818       // TODO: add processing for other clauses.
1819       if (auto *C = OMPClauseWithPreInit::get(Clause)) {
1820         if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1821           for (auto *D : DS->decls())
1822             MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1823         }
1824       }
1825       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1826         if (auto *E = C->getPostUpdateExpr())
1827           MarkDeclarationsReferencedInExpr(E);
1828       }
1829     }
1830     if (Clause->getClauseKind() == OMPC_schedule)
1831       SC = cast<OMPScheduleClause>(Clause);
1832     else if (Clause->getClauseKind() == OMPC_ordered)
1833       OC = cast<OMPOrderedClause>(Clause);
1834     else if (Clause->getClauseKind() == OMPC_linear)
1835       LCs.push_back(cast<OMPLinearClause>(Clause));
1836   }
1837   bool ErrorFound = false;
1838   // OpenMP, 2.7.1 Loop Construct, Restrictions
1839   // The nonmonotonic modifier cannot be specified if an ordered clause is
1840   // specified.
1841   if (SC &&
1842       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1843        SC->getSecondScheduleModifier() ==
1844            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1845       OC) {
1846     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1847              ? SC->getFirstScheduleModifierLoc()
1848              : SC->getSecondScheduleModifierLoc(),
1849          diag::err_omp_schedule_nonmonotonic_ordered)
1850         << SourceRange(OC->getLocStart(), OC->getLocEnd());
1851     ErrorFound = true;
1852   }
1853   if (!LCs.empty() && OC && OC->getNumForLoops()) {
1854     for (auto *C : LCs) {
1855       Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1856           << SourceRange(OC->getLocStart(), OC->getLocEnd());
1857     }
1858     ErrorFound = true;
1859   }
1860   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1861       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1862       OC->getNumForLoops()) {
1863     Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1864         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1865     ErrorFound = true;
1866   }
1867   if (ErrorFound) {
1868     ActOnCapturedRegionError();
1869     return StmtError();
1870   }
1871   return ActOnCapturedRegionEnd(S.get());
1872 }
1873 
1874 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1875                                   OpenMPDirectiveKind CurrentRegion,
1876                                   const DeclarationNameInfo &CurrentName,
1877                                   OpenMPDirectiveKind CancelRegion,
1878                                   SourceLocation StartLoc) {
1879   // Allowed nesting of constructs
1880   // +------------------+-----------------+------------------------------------+
1881   // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1882   // +------------------+-----------------+------------------------------------+
1883   // | parallel         | parallel        | *                                  |
1884   // | parallel         | for             | *                                  |
1885   // | parallel         | for simd        | *                                  |
1886   // | parallel         | master          | *                                  |
1887   // | parallel         | critical        | *                                  |
1888   // | parallel         | simd            | *                                  |
1889   // | parallel         | sections        | *                                  |
1890   // | parallel         | section         | +                                  |
1891   // | parallel         | single          | *                                  |
1892   // | parallel         | parallel for    | *                                  |
1893   // | parallel         |parallel for simd| *                                  |
1894   // | parallel         |parallel sections| *                                  |
1895   // | parallel         | task            | *                                  |
1896   // | parallel         | taskyield       | *                                  |
1897   // | parallel         | barrier         | *                                  |
1898   // | parallel         | taskwait        | *                                  |
1899   // | parallel         | taskgroup       | *                                  |
1900   // | parallel         | flush           | *                                  |
1901   // | parallel         | ordered         | +                                  |
1902   // | parallel         | atomic          | *                                  |
1903   // | parallel         | target          | *                                  |
1904   // | parallel         | target parallel | *                                  |
1905   // | parallel         | target parallel | *                                  |
1906   // |                  | for             |                                    |
1907   // | parallel         | target enter    | *                                  |
1908   // |                  | data            |                                    |
1909   // | parallel         | target exit     | *                                  |
1910   // |                  | data            |                                    |
1911   // | parallel         | teams           | +                                  |
1912   // | parallel         | cancellation    |                                    |
1913   // |                  | point           | !                                  |
1914   // | parallel         | cancel          | !                                  |
1915   // | parallel         | taskloop        | *                                  |
1916   // | parallel         | taskloop simd   | *                                  |
1917   // | parallel         | distribute      | +                                  |
1918   // | parallel         | distribute      | +                                  |
1919   // |                  | parallel for    |                                    |
1920   // | parallel         | distribute      | +                                  |
1921   // |                  |parallel for simd|                                    |
1922   // | parallel         | distribute simd | +                                  |
1923   // | parallel         | target simd     | *                                  |
1924   // | parallel         | teams distribute| +                                  |
1925   // +------------------+-----------------+------------------------------------+
1926   // | for              | parallel        | *                                  |
1927   // | for              | for             | +                                  |
1928   // | for              | for simd        | +                                  |
1929   // | for              | master          | +                                  |
1930   // | for              | critical        | *                                  |
1931   // | for              | simd            | *                                  |
1932   // | for              | sections        | +                                  |
1933   // | for              | section         | +                                  |
1934   // | for              | single          | +                                  |
1935   // | for              | parallel for    | *                                  |
1936   // | for              |parallel for simd| *                                  |
1937   // | for              |parallel sections| *                                  |
1938   // | for              | task            | *                                  |
1939   // | for              | taskyield       | *                                  |
1940   // | for              | barrier         | +                                  |
1941   // | for              | taskwait        | *                                  |
1942   // | for              | taskgroup       | *                                  |
1943   // | for              | flush           | *                                  |
1944   // | for              | ordered         | * (if construct is ordered)        |
1945   // | for              | atomic          | *                                  |
1946   // | for              | target          | *                                  |
1947   // | for              | target parallel | *                                  |
1948   // | for              | target parallel | *                                  |
1949   // |                  | for             |                                    |
1950   // | for              | target enter    | *                                  |
1951   // |                  | data            |                                    |
1952   // | for              | target exit     | *                                  |
1953   // |                  | data            |                                    |
1954   // | for              | teams           | +                                  |
1955   // | for              | cancellation    |                                    |
1956   // |                  | point           | !                                  |
1957   // | for              | cancel          | !                                  |
1958   // | for              | taskloop        | *                                  |
1959   // | for              | taskloop simd   | *                                  |
1960   // | for              | distribute      | +                                  |
1961   // | for              | distribute      | +                                  |
1962   // |                  | parallel for    |                                    |
1963   // | for              | distribute      | +                                  |
1964   // |                  |parallel for simd|                                    |
1965   // | for              | distribute simd | +                                  |
1966   // | for              | target parallel | +                                  |
1967   // |                  | for simd        |                                    |
1968   // | for              | target simd     | *                                  |
1969   // | for              | teams distribute| +                                  |
1970   // +------------------+-----------------+------------------------------------+
1971   // | master           | parallel        | *                                  |
1972   // | master           | for             | +                                  |
1973   // | master           | for simd        | +                                  |
1974   // | master           | master          | *                                  |
1975   // | master           | critical        | *                                  |
1976   // | master           | simd            | *                                  |
1977   // | master           | sections        | +                                  |
1978   // | master           | section         | +                                  |
1979   // | master           | single          | +                                  |
1980   // | master           | parallel for    | *                                  |
1981   // | master           |parallel for simd| *                                  |
1982   // | master           |parallel sections| *                                  |
1983   // | master           | task            | *                                  |
1984   // | master           | taskyield       | *                                  |
1985   // | master           | barrier         | +                                  |
1986   // | master           | taskwait        | *                                  |
1987   // | master           | taskgroup       | *                                  |
1988   // | master           | flush           | *                                  |
1989   // | master           | ordered         | +                                  |
1990   // | master           | atomic          | *                                  |
1991   // | master           | target          | *                                  |
1992   // | master           | target parallel | *                                  |
1993   // | master           | target parallel | *                                  |
1994   // |                  | for             |                                    |
1995   // | master           | target enter    | *                                  |
1996   // |                  | data            |                                    |
1997   // | master           | target exit     | *                                  |
1998   // |                  | data            |                                    |
1999   // | master           | teams           | +                                  |
2000   // | master           | cancellation    |                                    |
2001   // |                  | point           |                                    |
2002   // | master           | cancel          |                                    |
2003   // | master           | taskloop        | *                                  |
2004   // | master           | taskloop simd   | *                                  |
2005   // | master           | distribute      | +                                  |
2006   // | master           | distribute      | +                                  |
2007   // |                  | parallel for    |                                    |
2008   // | master           | distribute      | +                                  |
2009   // |                  |parallel for simd|                                    |
2010   // | master           | distribute simd | +                                  |
2011   // | master           | target parallel | +                                  |
2012   // |                  | for simd        |                                    |
2013   // | master           | target simd     | *                                  |
2014   // | master           | teams distribute| +                                  |
2015   // +------------------+-----------------+------------------------------------+
2016   // | critical         | parallel        | *                                  |
2017   // | critical         | for             | +                                  |
2018   // | critical         | for simd        | +                                  |
2019   // | critical         | master          | *                                  |
2020   // | critical         | critical        | * (should have different names)    |
2021   // | critical         | simd            | *                                  |
2022   // | critical         | sections        | +                                  |
2023   // | critical         | section         | +                                  |
2024   // | critical         | single          | +                                  |
2025   // | critical         | parallel for    | *                                  |
2026   // | critical         |parallel for simd| *                                  |
2027   // | critical         |parallel sections| *                                  |
2028   // | critical         | task            | *                                  |
2029   // | critical         | taskyield       | *                                  |
2030   // | critical         | barrier         | +                                  |
2031   // | critical         | taskwait        | *                                  |
2032   // | critical         | taskgroup       | *                                  |
2033   // | critical         | ordered         | +                                  |
2034   // | critical         | atomic          | *                                  |
2035   // | critical         | target          | *                                  |
2036   // | critical         | target parallel | *                                  |
2037   // | critical         | target parallel | *                                  |
2038   // |                  | for             |                                    |
2039   // | critical         | target enter    | *                                  |
2040   // |                  | data            |                                    |
2041   // | critical         | target exit     | *                                  |
2042   // |                  | data            |                                    |
2043   // | critical         | teams           | +                                  |
2044   // | critical         | cancellation    |                                    |
2045   // |                  | point           |                                    |
2046   // | critical         | cancel          |                                    |
2047   // | critical         | taskloop        | *                                  |
2048   // | critical         | taskloop simd   | *                                  |
2049   // | critical         | distribute      | +                                  |
2050   // | critical         | distribute      | +                                  |
2051   // |                  | parallel for    |                                    |
2052   // | critical         | distribute      | +                                  |
2053   // |                  |parallel for simd|                                    |
2054   // | critical         | distribute simd | +                                  |
2055   // | critical         | target parallel | +                                  |
2056   // |                  | for simd        |                                    |
2057   // | critical         | target simd     | *                                  |
2058   // | critical         | teams distribute| +                                  |
2059   // +------------------+-----------------+------------------------------------+
2060   // | simd             | parallel        |                                    |
2061   // | simd             | for             |                                    |
2062   // | simd             | for simd        |                                    |
2063   // | simd             | master          |                                    |
2064   // | simd             | critical        |                                    |
2065   // | simd             | simd            | *                                  |
2066   // | simd             | sections        |                                    |
2067   // | simd             | section         |                                    |
2068   // | simd             | single          |                                    |
2069   // | simd             | parallel for    |                                    |
2070   // | simd             |parallel for simd|                                    |
2071   // | simd             |parallel sections|                                    |
2072   // | simd             | task            |                                    |
2073   // | simd             | taskyield       |                                    |
2074   // | simd             | barrier         |                                    |
2075   // | simd             | taskwait        |                                    |
2076   // | simd             | taskgroup       |                                    |
2077   // | simd             | flush           |                                    |
2078   // | simd             | ordered         | + (with simd clause)               |
2079   // | simd             | atomic          |                                    |
2080   // | simd             | target          |                                    |
2081   // | simd             | target parallel |                                    |
2082   // | simd             | target parallel |                                    |
2083   // |                  | for             |                                    |
2084   // | simd             | target enter    |                                    |
2085   // |                  | data            |                                    |
2086   // | simd             | target exit     |                                    |
2087   // |                  | data            |                                    |
2088   // | simd             | teams           |                                    |
2089   // | simd             | cancellation    |                                    |
2090   // |                  | point           |                                    |
2091   // | simd             | cancel          |                                    |
2092   // | simd             | taskloop        |                                    |
2093   // | simd             | taskloop simd   |                                    |
2094   // | simd             | distribute      |                                    |
2095   // | simd             | distribute      |                                    |
2096   // |                  | parallel for    |                                    |
2097   // | simd             | distribute      |                                    |
2098   // |                  |parallel for simd|                                    |
2099   // | simd             | distribute simd |                                    |
2100   // | simd             | target parallel |                                    |
2101   // |                  | for simd        |                                    |
2102   // | simd             | target simd     |                                    |
2103   // | simd             | teams distribute|                                    |
2104   // +------------------+-----------------+------------------------------------+
2105   // | for simd         | parallel        |                                    |
2106   // | for simd         | for             |                                    |
2107   // | for simd         | for simd        |                                    |
2108   // | for simd         | master          |                                    |
2109   // | for simd         | critical        |                                    |
2110   // | for simd         | simd            | *                                  |
2111   // | for simd         | sections        |                                    |
2112   // | for simd         | section         |                                    |
2113   // | for simd         | single          |                                    |
2114   // | for simd         | parallel for    |                                    |
2115   // | for simd         |parallel for simd|                                    |
2116   // | for simd         |parallel sections|                                    |
2117   // | for simd         | task            |                                    |
2118   // | for simd         | taskyield       |                                    |
2119   // | for simd         | barrier         |                                    |
2120   // | for simd         | taskwait        |                                    |
2121   // | for simd         | taskgroup       |                                    |
2122   // | for simd         | flush           |                                    |
2123   // | for simd         | ordered         | + (with simd clause)               |
2124   // | for simd         | atomic          |                                    |
2125   // | for simd         | target          |                                    |
2126   // | for simd         | target parallel |                                    |
2127   // | for simd         | target parallel |                                    |
2128   // |                  | for             |                                    |
2129   // | for simd         | target enter    |                                    |
2130   // |                  | data            |                                    |
2131   // | for simd         | target exit     |                                    |
2132   // |                  | data            |                                    |
2133   // | for simd         | teams           |                                    |
2134   // | for simd         | cancellation    |                                    |
2135   // |                  | point           |                                    |
2136   // | for simd         | cancel          |                                    |
2137   // | for simd         | taskloop        |                                    |
2138   // | for simd         | taskloop simd   |                                    |
2139   // | for simd         | distribute      |                                    |
2140   // | for simd         | distribute      |                                    |
2141   // |                  | parallel for    |                                    |
2142   // | for simd         | distribute      |                                    |
2143   // |                  |parallel for simd|                                    |
2144   // | for simd         | distribute simd |                                    |
2145   // | for simd         | target parallel |                                    |
2146   // |                  | for simd        |                                    |
2147   // | for simd         | target simd     |                                    |
2148   // | for simd         | teams distribute|                                    |
2149   // +------------------+-----------------+------------------------------------+
2150   // | parallel for simd| parallel        |                                    |
2151   // | parallel for simd| for             |                                    |
2152   // | parallel for simd| for simd        |                                    |
2153   // | parallel for simd| master          |                                    |
2154   // | parallel for simd| critical        |                                    |
2155   // | parallel for simd| simd            | *                                  |
2156   // | parallel for simd| sections        |                                    |
2157   // | parallel for simd| section         |                                    |
2158   // | parallel for simd| single          |                                    |
2159   // | parallel for simd| parallel for    |                                    |
2160   // | parallel for simd|parallel for simd|                                    |
2161   // | parallel for simd|parallel sections|                                    |
2162   // | parallel for simd| task            |                                    |
2163   // | parallel for simd| taskyield       |                                    |
2164   // | parallel for simd| barrier         |                                    |
2165   // | parallel for simd| taskwait        |                                    |
2166   // | parallel for simd| taskgroup       |                                    |
2167   // | parallel for simd| flush           |                                    |
2168   // | parallel for simd| ordered         | + (with simd clause)               |
2169   // | parallel for simd| atomic          |                                    |
2170   // | parallel for simd| target          |                                    |
2171   // | parallel for simd| target parallel |                                    |
2172   // | parallel for simd| target parallel |                                    |
2173   // |                  | for             |                                    |
2174   // | parallel for simd| target enter    |                                    |
2175   // |                  | data            |                                    |
2176   // | parallel for simd| target exit     |                                    |
2177   // |                  | data            |                                    |
2178   // | parallel for simd| teams           |                                    |
2179   // | parallel for simd| cancellation    |                                    |
2180   // |                  | point           |                                    |
2181   // | parallel for simd| cancel          |                                    |
2182   // | parallel for simd| taskloop        |                                    |
2183   // | parallel for simd| taskloop simd   |                                    |
2184   // | parallel for simd| distribute      |                                    |
2185   // | parallel for simd| distribute      |                                    |
2186   // |                  | parallel for    |                                    |
2187   // | parallel for simd| distribute      |                                    |
2188   // |                  |parallel for simd|                                    |
2189   // | parallel for simd| distribute simd |                                    |
2190   // |                  | for simd        |                                    |
2191   // | parallel for simd| target simd     |                                    |
2192   // | parallel for simd| teams distribute|                                    |
2193   // +------------------+-----------------+------------------------------------+
2194   // | sections         | parallel        | *                                  |
2195   // | sections         | for             | +                                  |
2196   // | sections         | for simd        | +                                  |
2197   // | sections         | master          | +                                  |
2198   // | sections         | critical        | *                                  |
2199   // | sections         | simd            | *                                  |
2200   // | sections         | sections        | +                                  |
2201   // | sections         | section         | *                                  |
2202   // | sections         | single          | +                                  |
2203   // | sections         | parallel for    | *                                  |
2204   // | sections         |parallel for simd| *                                  |
2205   // | sections         |parallel sections| *                                  |
2206   // | sections         | task            | *                                  |
2207   // | sections         | taskyield       | *                                  |
2208   // | sections         | barrier         | +                                  |
2209   // | sections         | taskwait        | *                                  |
2210   // | sections         | taskgroup       | *                                  |
2211   // | sections         | flush           | *                                  |
2212   // | sections         | ordered         | +                                  |
2213   // | sections         | atomic          | *                                  |
2214   // | sections         | target          | *                                  |
2215   // | sections         | target parallel | *                                  |
2216   // | sections         | target parallel | *                                  |
2217   // |                  | for             |                                    |
2218   // | sections         | target enter    | *                                  |
2219   // |                  | data            |                                    |
2220   // | sections         | target exit     | *                                  |
2221   // |                  | data            |                                    |
2222   // | sections         | teams           | +                                  |
2223   // | sections         | cancellation    |                                    |
2224   // |                  | point           | !                                  |
2225   // | sections         | cancel          | !                                  |
2226   // | sections         | taskloop        | *                                  |
2227   // | sections         | taskloop simd   | *                                  |
2228   // | sections         | distribute      | +                                  |
2229   // | sections         | distribute      | +                                  |
2230   // |                  | parallel for    |                                    |
2231   // | sections         | distribute      | +                                  |
2232   // |                  |parallel for simd|                                    |
2233   // | sections         | distribute simd | +                                  |
2234   // | sections         | target parallel | +                                  |
2235   // |                  | for simd        |                                    |
2236   // | sections         | target simd     | *                                  |
2237   // +------------------+-----------------+------------------------------------+
2238   // | section          | parallel        | *                                  |
2239   // | section          | for             | +                                  |
2240   // | section          | for simd        | +                                  |
2241   // | section          | master          | +                                  |
2242   // | section          | critical        | *                                  |
2243   // | section          | simd            | *                                  |
2244   // | section          | sections        | +                                  |
2245   // | section          | section         | +                                  |
2246   // | section          | single          | +                                  |
2247   // | section          | parallel for    | *                                  |
2248   // | section          |parallel for simd| *                                  |
2249   // | section          |parallel sections| *                                  |
2250   // | section          | task            | *                                  |
2251   // | section          | taskyield       | *                                  |
2252   // | section          | barrier         | +                                  |
2253   // | section          | taskwait        | *                                  |
2254   // | section          | taskgroup       | *                                  |
2255   // | section          | flush           | *                                  |
2256   // | section          | ordered         | +                                  |
2257   // | section          | atomic          | *                                  |
2258   // | section          | target          | *                                  |
2259   // | section          | target parallel | *                                  |
2260   // | section          | target parallel | *                                  |
2261   // |                  | for             |                                    |
2262   // | section          | target enter    | *                                  |
2263   // |                  | data            |                                    |
2264   // | section          | target exit     | *                                  |
2265   // |                  | data            |                                    |
2266   // | section          | teams           | +                                  |
2267   // | section          | cancellation    |                                    |
2268   // |                  | point           | !                                  |
2269   // | section          | cancel          | !                                  |
2270   // | section          | taskloop        | *                                  |
2271   // | section          | taskloop simd   | *                                  |
2272   // | section          | distribute      | +                                  |
2273   // | section          | distribute      | +                                  |
2274   // |                  | parallel for    |                                    |
2275   // | section          | distribute      | +                                  |
2276   // |                  |parallel for simd|                                    |
2277   // | section          | distribute simd | +                                  |
2278   // | section          | target parallel | +                                  |
2279   // |                  | for simd        |                                    |
2280   // | section          | target simd     | *                                  |
2281   // | section          | teams distrubte | +                                  |
2282   // +------------------+-----------------+------------------------------------+
2283   // | single           | parallel        | *                                  |
2284   // | single           | for             | +                                  |
2285   // | single           | for simd        | +                                  |
2286   // | single           | master          | +                                  |
2287   // | single           | critical        | *                                  |
2288   // | single           | simd            | *                                  |
2289   // | single           | sections        | +                                  |
2290   // | single           | section         | +                                  |
2291   // | single           | single          | +                                  |
2292   // | single           | parallel for    | *                                  |
2293   // | single           |parallel for simd| *                                  |
2294   // | single           |parallel sections| *                                  |
2295   // | single           | task            | *                                  |
2296   // | single           | taskyield       | *                                  |
2297   // | single           | barrier         | +                                  |
2298   // | single           | taskwait        | *                                  |
2299   // | single           | taskgroup       | *                                  |
2300   // | single           | flush           | *                                  |
2301   // | single           | ordered         | +                                  |
2302   // | single           | atomic          | *                                  |
2303   // | single           | target          | *                                  |
2304   // | single           | target parallel | *                                  |
2305   // | single           | target parallel | *                                  |
2306   // |                  | for             |                                    |
2307   // | single           | target enter    | *                                  |
2308   // |                  | data            |                                    |
2309   // | single           | target exit     | *                                  |
2310   // |                  | data            |                                    |
2311   // | single           | teams           | +                                  |
2312   // | single           | cancellation    |                                    |
2313   // |                  | point           |                                    |
2314   // | single           | cancel          |                                    |
2315   // | single           | taskloop        | *                                  |
2316   // | single           | taskloop simd   | *                                  |
2317   // | single           | distribute      | +                                  |
2318   // | single           | distribute      | +                                  |
2319   // |                  | parallel for    |                                    |
2320   // | single           | distribute      | +                                  |
2321   // |                  |parallel for simd|                                    |
2322   // | single           | distribute simd | +                                  |
2323   // | single           | target parallel | +                                  |
2324   // |                  | for simd        |                                    |
2325   // | single           | target simd     | *                                  |
2326   // | single           | teams distrubte | +                                  |
2327   // +------------------+-----------------+------------------------------------+
2328   // | parallel for     | parallel        | *                                  |
2329   // | parallel for     | for             | +                                  |
2330   // | parallel for     | for simd        | +                                  |
2331   // | parallel for     | master          | +                                  |
2332   // | parallel for     | critical        | *                                  |
2333   // | parallel for     | simd            | *                                  |
2334   // | parallel for     | sections        | +                                  |
2335   // | parallel for     | section         | +                                  |
2336   // | parallel for     | single          | +                                  |
2337   // | parallel for     | parallel for    | *                                  |
2338   // | parallel for     |parallel for simd| *                                  |
2339   // | parallel for     |parallel sections| *                                  |
2340   // | parallel for     | task            | *                                  |
2341   // | parallel for     | taskyield       | *                                  |
2342   // | parallel for     | barrier         | +                                  |
2343   // | parallel for     | taskwait        | *                                  |
2344   // | parallel for     | taskgroup       | *                                  |
2345   // | parallel for     | flush           | *                                  |
2346   // | parallel for     | ordered         | * (if construct is ordered)        |
2347   // | parallel for     | atomic          | *                                  |
2348   // | parallel for     | target          | *                                  |
2349   // | parallel for     | target parallel | *                                  |
2350   // | parallel for     | target parallel | *                                  |
2351   // |                  | for             |                                    |
2352   // | parallel for     | target enter    | *                                  |
2353   // |                  | data            |                                    |
2354   // | parallel for     | target exit     | *                                  |
2355   // |                  | data            |                                    |
2356   // | parallel for     | teams           | +                                  |
2357   // | parallel for     | cancellation    |                                    |
2358   // |                  | point           | !                                  |
2359   // | parallel for     | cancel          | !                                  |
2360   // | parallel for     | taskloop        | *                                  |
2361   // | parallel for     | taskloop simd   | *                                  |
2362   // | parallel for     | distribute      | +                                  |
2363   // | parallel for     | distribute      | +                                  |
2364   // |                  | parallel for    |                                    |
2365   // | parallel for     | distribute      | +                                  |
2366   // |                  |parallel for simd|                                    |
2367   // | parallel for     | distribute simd | +                                  |
2368   // | parallel for     | target parallel | +                                  |
2369   // |                  | for simd        |                                    |
2370   // | parallel for     | target simd     | *                                  |
2371   // | parallel for     | teams distribute| +                                  |
2372   // +------------------+-----------------+------------------------------------+
2373   // | parallel sections| parallel        | *                                  |
2374   // | parallel sections| for             | +                                  |
2375   // | parallel sections| for simd        | +                                  |
2376   // | parallel sections| master          | +                                  |
2377   // | parallel sections| critical        | +                                  |
2378   // | parallel sections| simd            | *                                  |
2379   // | parallel sections| sections        | +                                  |
2380   // | parallel sections| section         | *                                  |
2381   // | parallel sections| single          | +                                  |
2382   // | parallel sections| parallel for    | *                                  |
2383   // | parallel sections|parallel for simd| *                                  |
2384   // | parallel sections|parallel sections| *                                  |
2385   // | parallel sections| task            | *                                  |
2386   // | parallel sections| taskyield       | *                                  |
2387   // | parallel sections| barrier         | +                                  |
2388   // | parallel sections| taskwait        | *                                  |
2389   // | parallel sections| taskgroup       | *                                  |
2390   // | parallel sections| flush           | *                                  |
2391   // | parallel sections| ordered         | +                                  |
2392   // | parallel sections| atomic          | *                                  |
2393   // | parallel sections| target          | *                                  |
2394   // | parallel sections| target parallel | *                                  |
2395   // | parallel sections| target parallel | *                                  |
2396   // |                  | for             |                                    |
2397   // | parallel sections| target enter    | *                                  |
2398   // |                  | data            |                                    |
2399   // | parallel sections| target exit     | *                                  |
2400   // |                  | data            |                                    |
2401   // | parallel sections| teams           | +                                  |
2402   // | parallel sections| cancellation    |                                    |
2403   // |                  | point           | !                                  |
2404   // | parallel sections| cancel          | !                                  |
2405   // | parallel sections| taskloop        | *                                  |
2406   // | parallel sections| taskloop simd   | *                                  |
2407   // | parallel sections| distribute      | +                                  |
2408   // | parallel sections| distribute      | +                                  |
2409   // |                  | parallel for    |                                    |
2410   // | parallel sections| distribute      | +                                  |
2411   // |                  |parallel for simd|                                    |
2412   // | parallel sections| distribute simd | +                                  |
2413   // | parallel sections| target parallel | +                                  |
2414   // |                  | for simd        |                                    |
2415   // | parallel sections| target simd     | *                                  |
2416   // | parallel sections| teams distribute| +                                  |
2417   // +------------------+-----------------+------------------------------------+
2418   // | task             | parallel        | *                                  |
2419   // | task             | for             | +                                  |
2420   // | task             | for simd        | +                                  |
2421   // | task             | master          | +                                  |
2422   // | task             | critical        | *                                  |
2423   // | task             | simd            | *                                  |
2424   // | task             | sections        | +                                  |
2425   // | task             | section         | +                                  |
2426   // | task             | single          | +                                  |
2427   // | task             | parallel for    | *                                  |
2428   // | task             |parallel for simd| *                                  |
2429   // | task             |parallel sections| *                                  |
2430   // | task             | task            | *                                  |
2431   // | task             | taskyield       | *                                  |
2432   // | task             | barrier         | +                                  |
2433   // | task             | taskwait        | *                                  |
2434   // | task             | taskgroup       | *                                  |
2435   // | task             | flush           | *                                  |
2436   // | task             | ordered         | +                                  |
2437   // | task             | atomic          | *                                  |
2438   // | task             | target          | *                                  |
2439   // | task             | target parallel | *                                  |
2440   // | task             | target parallel | *                                  |
2441   // |                  | for             |                                    |
2442   // | task             | target enter    | *                                  |
2443   // |                  | data            |                                    |
2444   // | task             | target exit     | *                                  |
2445   // |                  | data            |                                    |
2446   // | task             | teams           | +                                  |
2447   // | task             | cancellation    |                                    |
2448   // |                  | point           | !                                  |
2449   // | task             | cancel          | !                                  |
2450   // | task             | taskloop        | *                                  |
2451   // | task             | taskloop simd   | *                                  |
2452   // | task             | distribute      | +                                  |
2453   // | task             | distribute      | +                                  |
2454   // |                  | parallel for    |                                    |
2455   // | task             | distribute      | +                                  |
2456   // |                  |parallel for simd|                                    |
2457   // | task             | distribute simd | +                                  |
2458   // | task             | target parallel | +                                  |
2459   // |                  | for simd        |                                    |
2460   // | task             | target simd     | *                                  |
2461   // | task             | teams distribute| +                                  |
2462   // +------------------+-----------------+------------------------------------+
2463   // | ordered          | parallel        | *                                  |
2464   // | ordered          | for             | +                                  |
2465   // | ordered          | for simd        | +                                  |
2466   // | ordered          | master          | *                                  |
2467   // | ordered          | critical        | *                                  |
2468   // | ordered          | simd            | *                                  |
2469   // | ordered          | sections        | +                                  |
2470   // | ordered          | section         | +                                  |
2471   // | ordered          | single          | +                                  |
2472   // | ordered          | parallel for    | *                                  |
2473   // | ordered          |parallel for simd| *                                  |
2474   // | ordered          |parallel sections| *                                  |
2475   // | ordered          | task            | *                                  |
2476   // | ordered          | taskyield       | *                                  |
2477   // | ordered          | barrier         | +                                  |
2478   // | ordered          | taskwait        | *                                  |
2479   // | ordered          | taskgroup       | *                                  |
2480   // | ordered          | flush           | *                                  |
2481   // | ordered          | ordered         | +                                  |
2482   // | ordered          | atomic          | *                                  |
2483   // | ordered          | target          | *                                  |
2484   // | ordered          | target parallel | *                                  |
2485   // | ordered          | target parallel | *                                  |
2486   // |                  | for             |                                    |
2487   // | ordered          | target enter    | *                                  |
2488   // |                  | data            |                                    |
2489   // | ordered          | target exit     | *                                  |
2490   // |                  | data            |                                    |
2491   // | ordered          | teams           | +                                  |
2492   // | ordered          | cancellation    |                                    |
2493   // |                  | point           |                                    |
2494   // | ordered          | cancel          |                                    |
2495   // | ordered          | taskloop        | *                                  |
2496   // | ordered          | taskloop simd   | *                                  |
2497   // | ordered          | distribute      | +                                  |
2498   // | ordered          | distribute      | +                                  |
2499   // |                  | parallel for    |                                    |
2500   // | ordered          | distribute      | +                                  |
2501   // |                  |parallel for simd|                                    |
2502   // | ordered          | distribute simd | +                                  |
2503   // | ordered          | target parallel | +                                  |
2504   // |                  | for simd        |                                    |
2505   // | ordered          | target simd     | *                                  |
2506   // | ordered          | teams distribute| +                                  |
2507   // +------------------+-----------------+------------------------------------+
2508   // | atomic           | parallel        |                                    |
2509   // | atomic           | for             |                                    |
2510   // | atomic           | for simd        |                                    |
2511   // | atomic           | master          |                                    |
2512   // | atomic           | critical        |                                    |
2513   // | atomic           | simd            |                                    |
2514   // | atomic           | sections        |                                    |
2515   // | atomic           | section         |                                    |
2516   // | atomic           | single          |                                    |
2517   // | atomic           | parallel for    |                                    |
2518   // | atomic           |parallel for simd|                                    |
2519   // | atomic           |parallel sections|                                    |
2520   // | atomic           | task            |                                    |
2521   // | atomic           | taskyield       |                                    |
2522   // | atomic           | barrier         |                                    |
2523   // | atomic           | taskwait        |                                    |
2524   // | atomic           | taskgroup       |                                    |
2525   // | atomic           | flush           |                                    |
2526   // | atomic           | ordered         |                                    |
2527   // | atomic           | atomic          |                                    |
2528   // | atomic           | target          |                                    |
2529   // | atomic           | target parallel |                                    |
2530   // | atomic           | target parallel |                                    |
2531   // |                  | for             |                                    |
2532   // | atomic           | target enter    |                                    |
2533   // |                  | data            |                                    |
2534   // | atomic           | target exit     |                                    |
2535   // |                  | data            |                                    |
2536   // | atomic           | teams           |                                    |
2537   // | atomic           | cancellation    |                                    |
2538   // |                  | point           |                                    |
2539   // | atomic           | cancel          |                                    |
2540   // | atomic           | taskloop        |                                    |
2541   // | atomic           | taskloop simd   |                                    |
2542   // | atomic           | distribute      |                                    |
2543   // | atomic           | distribute      |                                    |
2544   // |                  | parallel for    |                                    |
2545   // | atomic           | distribute      |                                    |
2546   // |                  |parallel for simd|                                    |
2547   // | atomic           | distribute simd |                                    |
2548   // | atomic           | target parallel |                                    |
2549   // |                  | for simd        |                                    |
2550   // | atomic           | target simd     |                                    |
2551   // | atomic           | teams distribute|                                    |
2552   // +------------------+-----------------+------------------------------------+
2553   // | target           | parallel        | *                                  |
2554   // | target           | for             | *                                  |
2555   // | target           | for simd        | *                                  |
2556   // | target           | master          | *                                  |
2557   // | target           | critical        | *                                  |
2558   // | target           | simd            | *                                  |
2559   // | target           | sections        | *                                  |
2560   // | target           | section         | *                                  |
2561   // | target           | single          | *                                  |
2562   // | target           | parallel for    | *                                  |
2563   // | target           |parallel for simd| *                                  |
2564   // | target           |parallel sections| *                                  |
2565   // | target           | task            | *                                  |
2566   // | target           | taskyield       | *                                  |
2567   // | target           | barrier         | *                                  |
2568   // | target           | taskwait        | *                                  |
2569   // | target           | taskgroup       | *                                  |
2570   // | target           | flush           | *                                  |
2571   // | target           | ordered         | *                                  |
2572   // | target           | atomic          | *                                  |
2573   // | target           | target          |                                    |
2574   // | target           | target parallel |                                    |
2575   // | target           | target parallel |                                    |
2576   // |                  | for             |                                    |
2577   // | target           | target enter    |                                    |
2578   // |                  | data            |                                    |
2579   // | target           | target exit     |                                    |
2580   // |                  | data            |                                    |
2581   // | target           | teams           | *                                  |
2582   // | target           | cancellation    |                                    |
2583   // |                  | point           |                                    |
2584   // | target           | cancel          |                                    |
2585   // | target           | taskloop        | *                                  |
2586   // | target           | taskloop simd   | *                                  |
2587   // | target           | distribute      | +                                  |
2588   // | target           | distribute      | +                                  |
2589   // |                  | parallel for    |                                    |
2590   // | target           | distribute      | +                                  |
2591   // |                  |parallel for simd|                                    |
2592   // | target           | distribute simd | +                                  |
2593   // | target           | target parallel |                                    |
2594   // |                  | for simd        |                                    |
2595   // | target           | target simd     |                                    |
2596   // | target           | teams distribute|                                    |
2597   // +------------------+-----------------+------------------------------------+
2598   // | target parallel  | parallel        | *                                  |
2599   // | target parallel  | for             | *                                  |
2600   // | target parallel  | for simd        | *                                  |
2601   // | target parallel  | master          | *                                  |
2602   // | target parallel  | critical        | *                                  |
2603   // | target parallel  | simd            | *                                  |
2604   // | target parallel  | sections        | *                                  |
2605   // | target parallel  | section         | *                                  |
2606   // | target parallel  | single          | *                                  |
2607   // | target parallel  | parallel for    | *                                  |
2608   // | target parallel  |parallel for simd| *                                  |
2609   // | target parallel  |parallel sections| *                                  |
2610   // | target parallel  | task            | *                                  |
2611   // | target parallel  | taskyield       | *                                  |
2612   // | target parallel  | barrier         | *                                  |
2613   // | target parallel  | taskwait        | *                                  |
2614   // | target parallel  | taskgroup       | *                                  |
2615   // | target parallel  | flush           | *                                  |
2616   // | target parallel  | ordered         | *                                  |
2617   // | target parallel  | atomic          | *                                  |
2618   // | target parallel  | target          |                                    |
2619   // | target parallel  | target parallel |                                    |
2620   // | target parallel  | target parallel |                                    |
2621   // |                  | for             |                                    |
2622   // | target parallel  | target enter    |                                    |
2623   // |                  | data            |                                    |
2624   // | target parallel  | target exit     |                                    |
2625   // |                  | data            |                                    |
2626   // | target parallel  | teams           |                                    |
2627   // | target parallel  | cancellation    |                                    |
2628   // |                  | point           | !                                  |
2629   // | target parallel  | cancel          | !                                  |
2630   // | target parallel  | taskloop        | *                                  |
2631   // | target parallel  | taskloop simd   | *                                  |
2632   // | target parallel  | distribute      |                                    |
2633   // | target parallel  | distribute      |                                    |
2634   // |                  | parallel for    |                                    |
2635   // | target parallel  | distribute      |                                    |
2636   // |                  |parallel for simd|                                    |
2637   // | target parallel  | distribute simd |                                    |
2638   // | target parallel  | target parallel |                                    |
2639   // |                  | for simd        |                                    |
2640   // | target parallel  | target simd     |                                    |
2641   // | target parallel  | teams distribute| +                                  |
2642   // +------------------+-----------------+------------------------------------+
2643   // | target parallel  | parallel        | *                                  |
2644   // | for              |                 |                                    |
2645   // | target parallel  | for             | *                                  |
2646   // | for              |                 |                                    |
2647   // | target parallel  | for simd        | *                                  |
2648   // | for              |                 |                                    |
2649   // | target parallel  | master          | *                                  |
2650   // | for              |                 |                                    |
2651   // | target parallel  | critical        | *                                  |
2652   // | for              |                 |                                    |
2653   // | target parallel  | simd            | *                                  |
2654   // | for              |                 |                                    |
2655   // | target parallel  | sections        | *                                  |
2656   // | for              |                 |                                    |
2657   // | target parallel  | section         | *                                  |
2658   // | for              |                 |                                    |
2659   // | target parallel  | single          | *                                  |
2660   // | for              |                 |                                    |
2661   // | target parallel  | parallel for    | *                                  |
2662   // | for              |                 |                                    |
2663   // | target parallel  |parallel for simd| *                                  |
2664   // | for              |                 |                                    |
2665   // | target parallel  |parallel sections| *                                  |
2666   // | for              |                 |                                    |
2667   // | target parallel  | task            | *                                  |
2668   // | for              |                 |                                    |
2669   // | target parallel  | taskyield       | *                                  |
2670   // | for              |                 |                                    |
2671   // | target parallel  | barrier         | *                                  |
2672   // | for              |                 |                                    |
2673   // | target parallel  | taskwait        | *                                  |
2674   // | for              |                 |                                    |
2675   // | target parallel  | taskgroup       | *                                  |
2676   // | for              |                 |                                    |
2677   // | target parallel  | flush           | *                                  |
2678   // | for              |                 |                                    |
2679   // | target parallel  | ordered         | *                                  |
2680   // | for              |                 |                                    |
2681   // | target parallel  | atomic          | *                                  |
2682   // | for              |                 |                                    |
2683   // | target parallel  | target          |                                    |
2684   // | for              |                 |                                    |
2685   // | target parallel  | target parallel |                                    |
2686   // | for              |                 |                                    |
2687   // | target parallel  | target parallel |                                    |
2688   // | for              | for             |                                    |
2689   // | target parallel  | target enter    |                                    |
2690   // | for              | data            |                                    |
2691   // | target parallel  | target exit     |                                    |
2692   // | for              | data            |                                    |
2693   // | target parallel  | teams           |                                    |
2694   // | for              |                 |                                    |
2695   // | target parallel  | cancellation    |                                    |
2696   // | for              | point           | !                                  |
2697   // | target parallel  | cancel          | !                                  |
2698   // | for              |                 |                                    |
2699   // | target parallel  | taskloop        | *                                  |
2700   // | for              |                 |                                    |
2701   // | target parallel  | taskloop simd   | *                                  |
2702   // | for              |                 |                                    |
2703   // | target parallel  | distribute      |                                    |
2704   // | for              |                 |                                    |
2705   // | target parallel  | distribute      |                                    |
2706   // | for              | parallel for    |                                    |
2707   // | target parallel  | distribute      |                                    |
2708   // | for              |parallel for simd|                                    |
2709   // | target parallel  | distribute simd |                                    |
2710   // | for              |                 |                                    |
2711   // | target parallel  | target parallel |                                    |
2712   // | for              | for simd        |                                    |
2713   // | target parallel  | target simd     |                                    |
2714   // | for              |                 |                                    |
2715   // | target parallel  | teams distribute|                                    |
2716   // | for              |                 |                                    |
2717   // +------------------+-----------------+------------------------------------+
2718   // | teams            | parallel        | *                                  |
2719   // | teams            | for             | +                                  |
2720   // | teams            | for simd        | +                                  |
2721   // | teams            | master          | +                                  |
2722   // | teams            | critical        | +                                  |
2723   // | teams            | simd            | +                                  |
2724   // | teams            | sections        | +                                  |
2725   // | teams            | section         | +                                  |
2726   // | teams            | single          | +                                  |
2727   // | teams            | parallel for    | *                                  |
2728   // | teams            |parallel for simd| *                                  |
2729   // | teams            |parallel sections| *                                  |
2730   // | teams            | task            | +                                  |
2731   // | teams            | taskyield       | +                                  |
2732   // | teams            | barrier         | +                                  |
2733   // | teams            | taskwait        | +                                  |
2734   // | teams            | taskgroup       | +                                  |
2735   // | teams            | flush           | +                                  |
2736   // | teams            | ordered         | +                                  |
2737   // | teams            | atomic          | +                                  |
2738   // | teams            | target          | +                                  |
2739   // | teams            | target parallel | +                                  |
2740   // | teams            | target parallel | +                                  |
2741   // |                  | for             |                                    |
2742   // | teams            | target enter    | +                                  |
2743   // |                  | data            |                                    |
2744   // | teams            | target exit     | +                                  |
2745   // |                  | data            |                                    |
2746   // | teams            | teams           | +                                  |
2747   // | teams            | cancellation    |                                    |
2748   // |                  | point           |                                    |
2749   // | teams            | cancel          |                                    |
2750   // | teams            | taskloop        | +                                  |
2751   // | teams            | taskloop simd   | +                                  |
2752   // | teams            | distribute      | !                                  |
2753   // | teams            | distribute      | !                                  |
2754   // |                  | parallel for    |                                    |
2755   // | teams            | distribute      | !                                  |
2756   // |                  |parallel for simd|                                    |
2757   // | teams            | distribute simd | !                                  |
2758   // | teams            | target parallel | +                                  |
2759   // |                  | for simd        |                                    |
2760   // | teams            | target simd     | +                                  |
2761   // | teams            | teams distribute| +                                  |
2762   // +------------------+-----------------+------------------------------------+
2763   // | taskloop         | parallel        | *                                  |
2764   // | taskloop         | for             | +                                  |
2765   // | taskloop         | for simd        | +                                  |
2766   // | taskloop         | master          | +                                  |
2767   // | taskloop         | critical        | *                                  |
2768   // | taskloop         | simd            | *                                  |
2769   // | taskloop         | sections        | +                                  |
2770   // | taskloop         | section         | +                                  |
2771   // | taskloop         | single          | +                                  |
2772   // | taskloop         | parallel for    | *                                  |
2773   // | taskloop         |parallel for simd| *                                  |
2774   // | taskloop         |parallel sections| *                                  |
2775   // | taskloop         | task            | *                                  |
2776   // | taskloop         | taskyield       | *                                  |
2777   // | taskloop         | barrier         | +                                  |
2778   // | taskloop         | taskwait        | *                                  |
2779   // | taskloop         | taskgroup       | *                                  |
2780   // | taskloop         | flush           | *                                  |
2781   // | taskloop         | ordered         | +                                  |
2782   // | taskloop         | atomic          | *                                  |
2783   // | taskloop         | target          | *                                  |
2784   // | taskloop         | target parallel | *                                  |
2785   // | taskloop         | target parallel | *                                  |
2786   // |                  | for             |                                    |
2787   // | taskloop         | target enter    | *                                  |
2788   // |                  | data            |                                    |
2789   // | taskloop         | target exit     | *                                  |
2790   // |                  | data            |                                    |
2791   // | taskloop         | teams           | +                                  |
2792   // | taskloop         | cancellation    |                                    |
2793   // |                  | point           |                                    |
2794   // | taskloop         | cancel          |                                    |
2795   // | taskloop         | taskloop        | *                                  |
2796   // | taskloop         | distribute      | +                                  |
2797   // | taskloop         | distribute      | +                                  |
2798   // |                  | parallel for    |                                    |
2799   // | taskloop         | distribute      | +                                  |
2800   // |                  |parallel for simd|                                    |
2801   // | taskloop         | distribute simd | +                                  |
2802   // | taskloop         | target parallel | *                                  |
2803   // |                  | for simd        |                                    |
2804   // | taskloop         | target simd     | *                                  |
2805   // | taskloop         | teams distribute| +                                  |
2806   // +------------------+-----------------+------------------------------------+
2807   // | taskloop simd    | parallel        |                                    |
2808   // | taskloop simd    | for             |                                    |
2809   // | taskloop simd    | for simd        |                                    |
2810   // | taskloop simd    | master          |                                    |
2811   // | taskloop simd    | critical        |                                    |
2812   // | taskloop simd    | simd            | *                                  |
2813   // | taskloop simd    | sections        |                                    |
2814   // | taskloop simd    | section         |                                    |
2815   // | taskloop simd    | single          |                                    |
2816   // | taskloop simd    | parallel for    |                                    |
2817   // | taskloop simd    |parallel for simd|                                    |
2818   // | taskloop simd    |parallel sections|                                    |
2819   // | taskloop simd    | task            |                                    |
2820   // | taskloop simd    | taskyield       |                                    |
2821   // | taskloop simd    | barrier         |                                    |
2822   // | taskloop simd    | taskwait        |                                    |
2823   // | taskloop simd    | taskgroup       |                                    |
2824   // | taskloop simd    | flush           |                                    |
2825   // | taskloop simd    | ordered         | + (with simd clause)               |
2826   // | taskloop simd    | atomic          |                                    |
2827   // | taskloop simd    | target          |                                    |
2828   // | taskloop simd    | target parallel |                                    |
2829   // | taskloop simd    | target parallel |                                    |
2830   // |                  | for             |                                    |
2831   // | taskloop simd    | target enter    |                                    |
2832   // |                  | data            |                                    |
2833   // | taskloop simd    | target exit     |                                    |
2834   // |                  | data            |                                    |
2835   // | taskloop simd    | teams           |                                    |
2836   // | taskloop simd    | cancellation    |                                    |
2837   // |                  | point           |                                    |
2838   // | taskloop simd    | cancel          |                                    |
2839   // | taskloop simd    | taskloop        |                                    |
2840   // | taskloop simd    | taskloop simd   |                                    |
2841   // | taskloop simd    | distribute      |                                    |
2842   // | taskloop simd    | distribute      |                                    |
2843   // |                  | parallel for    |                                    |
2844   // | taskloop simd    | distribute      |                                    |
2845   // |                  |parallel for simd|                                    |
2846   // | taskloop simd    | distribute simd |                                    |
2847   // | taskloop simd    | target parallel |                                    |
2848   // |                  | for simd        |                                    |
2849   // | taskloop simd    | target simd     |                                    |
2850   // | taskloop simd    | teams distribute|                                    |
2851   // +------------------+-----------------+------------------------------------+
2852   // | distribute       | parallel        | *                                  |
2853   // | distribute       | for             | *                                  |
2854   // | distribute       | for simd        | *                                  |
2855   // | distribute       | master          | *                                  |
2856   // | distribute       | critical        | *                                  |
2857   // | distribute       | simd            | *                                  |
2858   // | distribute       | sections        | *                                  |
2859   // | distribute       | section         | *                                  |
2860   // | distribute       | single          | *                                  |
2861   // | distribute       | parallel for    | *                                  |
2862   // | distribute       |parallel for simd| *                                  |
2863   // | distribute       |parallel sections| *                                  |
2864   // | distribute       | task            | *                                  |
2865   // | distribute       | taskyield       | *                                  |
2866   // | distribute       | barrier         | *                                  |
2867   // | distribute       | taskwait        | *                                  |
2868   // | distribute       | taskgroup       | *                                  |
2869   // | distribute       | flush           | *                                  |
2870   // | distribute       | ordered         | +                                  |
2871   // | distribute       | atomic          | *                                  |
2872   // | distribute       | target          |                                    |
2873   // | distribute       | target parallel |                                    |
2874   // | distribute       | target parallel |                                    |
2875   // |                  | for             |                                    |
2876   // | distribute       | target enter    |                                    |
2877   // |                  | data            |                                    |
2878   // | distribute       | target exit     |                                    |
2879   // |                  | data            |                                    |
2880   // | distribute       | teams           |                                    |
2881   // | distribute       | cancellation    | +                                  |
2882   // |                  | point           |                                    |
2883   // | distribute       | cancel          | +                                  |
2884   // | distribute       | taskloop        | *                                  |
2885   // | distribute       | taskloop simd   | *                                  |
2886   // | distribute       | distribute      |                                    |
2887   // | distribute       | distribute      |                                    |
2888   // |                  | parallel for    |                                    |
2889   // | distribute       | distribute      |                                    |
2890   // |                  |parallel for simd|                                    |
2891   // | distribute       | distribute simd |                                    |
2892   // | distribute       | target parallel |                                    |
2893   // |                  | for simd        |                                    |
2894   // | distribute       | target simd     |                                    |
2895   // | distribute       | teams distribute|                                    |
2896   // +------------------+-----------------+------------------------------------+
2897   // | distribute       | parallel        | *                                  |
2898   // | parallel for     |                 |                                    |
2899   // | distribute       | for             | *                                  |
2900   // | parallel for     |                 |                                    |
2901   // | distribute       | for simd        | *                                  |
2902   // | parallel for     |                 |                                    |
2903   // | distribute       | master          | *                                  |
2904   // | parallel for     |                 |                                    |
2905   // | distribute       | critical        | *                                  |
2906   // | parallel for     |                 |                                    |
2907   // | distribute       | simd            | *                                  |
2908   // | parallel for     |                 |                                    |
2909   // | distribute       | sections        | *                                  |
2910   // | parallel for     |                 |                                    |
2911   // | distribute       | section         | *                                  |
2912   // | parallel for     |                 |                                    |
2913   // | distribute       | single          | *                                  |
2914   // | parallel for     |                 |                                    |
2915   // | distribute       | parallel for    | *                                  |
2916   // | parallel for     |                 |                                    |
2917   // | distribute       |parallel for simd| *                                  |
2918   // | parallel for     |                 |                                    |
2919   // | distribute       |parallel sections| *                                  |
2920   // | parallel for     |                 |                                    |
2921   // | distribute       | task            | *                                  |
2922   // | parallel for     |                 |                                    |
2923   // | parallel for     |                 |                                    |
2924   // | distribute       | taskyield       | *                                  |
2925   // | parallel for     |                 |                                    |
2926   // | distribute       | barrier         | *                                  |
2927   // | parallel for     |                 |                                    |
2928   // | distribute       | taskwait        | *                                  |
2929   // | parallel for     |                 |                                    |
2930   // | distribute       | taskgroup       | *                                  |
2931   // | parallel for     |                 |                                    |
2932   // | distribute       | flush           | *                                  |
2933   // | parallel for     |                 |                                    |
2934   // | distribute       | ordered         | +                                  |
2935   // | parallel for     |                 |                                    |
2936   // | distribute       | atomic          | *                                  |
2937   // | parallel for     |                 |                                    |
2938   // | distribute       | target          |                                    |
2939   // | parallel for     |                 |                                    |
2940   // | distribute       | target parallel |                                    |
2941   // | parallel for     |                 |                                    |
2942   // | distribute       | target parallel |                                    |
2943   // | parallel for     | for             |                                    |
2944   // | distribute       | target enter    |                                    |
2945   // | parallel for     | data            |                                    |
2946   // | distribute       | target exit     |                                    |
2947   // | parallel for     | data            |                                    |
2948   // | distribute       | teams           |                                    |
2949   // | parallel for     |                 |                                    |
2950   // | distribute       | cancellation    | +                                  |
2951   // | parallel for     | point           |                                    |
2952   // | distribute       | cancel          | +                                  |
2953   // | parallel for     |                 |                                    |
2954   // | distribute       | taskloop        | *                                  |
2955   // | parallel for     |                 |                                    |
2956   // | distribute       | taskloop simd   | *                                  |
2957   // | parallel for     |                 |                                    |
2958   // | distribute       | distribute      |                                    |
2959   // | parallel for     |                 |                                    |
2960   // | distribute       | distribute      |                                    |
2961   // | parallel for     | parallel for    |                                    |
2962   // | distribute       | distribute      |                                    |
2963   // | parallel for     |parallel for simd|                                    |
2964   // | distribute       | distribute simd |                                    |
2965   // | parallel for     |                 |                                    |
2966   // | distribute       | target parallel |                                    |
2967   // | parallel for     | for simd        |                                    |
2968   // | distribute       | target simd     |                                    |
2969   // | parallel for     |                 |                                    |
2970   // | distribute       | teams distribute|                                    |
2971   // | parallel for     |                 |                                    |
2972   // +------------------+-----------------+------------------------------------+
2973   // | distribute       | parallel        | *                                  |
2974   // | parallel for simd|                 |                                    |
2975   // | distribute       | for             | *                                  |
2976   // | parallel for simd|                 |                                    |
2977   // | distribute       | for simd        | *                                  |
2978   // | parallel for simd|                 |                                    |
2979   // | distribute       | master          | *                                  |
2980   // | parallel for simd|                 |                                    |
2981   // | distribute       | critical        | *                                  |
2982   // | parallel for simd|                 |                                    |
2983   // | distribute       | simd            | *                                  |
2984   // | parallel for simd|                 |                                    |
2985   // | distribute       | sections        | *                                  |
2986   // | parallel for simd|                 |                                    |
2987   // | distribute       | section         | *                                  |
2988   // | parallel for simd|                 |                                    |
2989   // | distribute       | single          | *                                  |
2990   // | parallel for simd|                 |                                    |
2991   // | distribute       | parallel for    | *                                  |
2992   // | parallel for simd|                 |                                    |
2993   // | distribute       |parallel for simd| *                                  |
2994   // | parallel for simd|                 |                                    |
2995   // | distribute       |parallel sections| *                                  |
2996   // | parallel for simd|                 |                                    |
2997   // | distribute       | task            | *                                  |
2998   // | parallel for simd|                 |                                    |
2999   // | distribute       | taskyield       | *                                  |
3000   // | parallel for simd|                 |                                    |
3001   // | distribute       | barrier         | *                                  |
3002   // | parallel for simd|                 |                                    |
3003   // | distribute       | taskwait        | *                                  |
3004   // | parallel for simd|                 |                                    |
3005   // | distribute       | taskgroup       | *                                  |
3006   // | parallel for simd|                 |                                    |
3007   // | distribute       | flush           | *                                  |
3008   // | parallel for simd|                 |                                    |
3009   // | distribute       | ordered         | +                                  |
3010   // | parallel for simd|                 |                                    |
3011   // | distribute       | atomic          | *                                  |
3012   // | parallel for simd|                 |                                    |
3013   // | distribute       | target          |                                    |
3014   // | parallel for simd|                 |                                    |
3015   // | distribute       | target parallel |                                    |
3016   // | parallel for simd|                 |                                    |
3017   // | distribute       | target parallel |                                    |
3018   // | parallel for simd| for             |                                    |
3019   // | distribute       | target enter    |                                    |
3020   // | parallel for simd| data            |                                    |
3021   // | distribute       | target exit     |                                    |
3022   // | parallel for simd| data            |                                    |
3023   // | distribute       | teams           |                                    |
3024   // | parallel for simd|                 |                                    |
3025   // | distribute       | cancellation    | +                                  |
3026   // | parallel for simd| point           |                                    |
3027   // | distribute       | cancel          | +                                  |
3028   // | parallel for simd|                 |                                    |
3029   // | distribute       | taskloop        | *                                  |
3030   // | parallel for simd|                 |                                    |
3031   // | distribute       | taskloop simd   | *                                  |
3032   // | parallel for simd|                 |                                    |
3033   // | distribute       | distribute      |                                    |
3034   // | parallel for simd|                 |                                    |
3035   // | distribute       | distribute      | *                                  |
3036   // | parallel for simd| parallel for    |                                    |
3037   // | distribute       | distribute      | *                                  |
3038   // | parallel for simd|parallel for simd|                                    |
3039   // | distribute       | distribute simd | *                                  |
3040   // | parallel for simd|                 |                                    |
3041   // | distribute       | target parallel |                                    |
3042   // | parallel for simd| for simd        |                                    |
3043   // | distribute       | target simd     |                                    |
3044   // | parallel for simd|                 |                                    |
3045   // | distribute       | teams distribute|                                    |
3046   // | parallel for simd|                 |                                    |
3047   // +------------------+-----------------+------------------------------------+
3048   // | distribute simd  | parallel        | *                                  |
3049   // | distribute simd  | for             | *                                  |
3050   // | distribute simd  | for simd        | *                                  |
3051   // | distribute simd  | master          | *                                  |
3052   // | distribute simd  | critical        | *                                  |
3053   // | distribute simd  | simd            | *                                  |
3054   // | distribute simd  | sections        | *                                  |
3055   // | distribute simd  | section         | *                                  |
3056   // | distribute simd  | single          | *                                  |
3057   // | distribute simd  | parallel for    | *                                  |
3058   // | distribute simd  |parallel for simd| *                                  |
3059   // | distribute simd  |parallel sections| *                                  |
3060   // | distribute simd  | task            | *                                  |
3061   // | distribute simd  | taskyield       | *                                  |
3062   // | distribute simd  | barrier         | *                                  |
3063   // | distribute simd  | taskwait        | *                                  |
3064   // | distribute simd  | taskgroup       | *                                  |
3065   // | distribute simd  | flush           | *                                  |
3066   // | distribute simd  | ordered         | +                                  |
3067   // | distribute simd  | atomic          | *                                  |
3068   // | distribute simd  | target          | *                                  |
3069   // | distribute simd  | target parallel | *                                  |
3070   // | distribute simd  | target parallel | *                                  |
3071   // |                  | for             |                                    |
3072   // | distribute simd  | target enter    | *                                  |
3073   // |                  | data            |                                    |
3074   // | distribute simd  | target exit     | *                                  |
3075   // |                  | data            |                                    |
3076   // | distribute simd  | teams           | *                                  |
3077   // | distribute simd  | cancellation    | +                                  |
3078   // |                  | point           |                                    |
3079   // | distribute simd  | cancel          | +                                  |
3080   // | distribute simd  | taskloop        | *                                  |
3081   // | distribute simd  | taskloop simd   | *                                  |
3082   // | distribute simd  | distribute      |                                    |
3083   // | distribute simd  | distribute      | *                                  |
3084   // |                  | parallel for    |                                    |
3085   // | distribute simd  | distribute      | *                                  |
3086   // |                  |parallel for simd|                                    |
3087   // | distribute simd  | distribute simd | *                                  |
3088   // | distribute simd  | target parallel | *                                  |
3089   // |                  | for simd        |                                    |
3090   // | distribute simd  | target simd     | *                                  |
3091   // | distribute simd  | teams distribute| *                                  |
3092   // +------------------+-----------------+------------------------------------+
3093   // | target parallel  | parallel        | *                                  |
3094   // | for simd         |                 |                                    |
3095   // | target parallel  | for             | *                                  |
3096   // | for simd         |                 |                                    |
3097   // | target parallel  | for simd        | *                                  |
3098   // | for simd         |                 |                                    |
3099   // | target parallel  | master          | *                                  |
3100   // | for simd         |                 |                                    |
3101   // | target parallel  | critical        | *                                  |
3102   // | for simd         |                 |                                    |
3103   // | target parallel  | simd            | !                                  |
3104   // | for simd         |                 |                                    |
3105   // | target parallel  | sections        | *                                  |
3106   // | for simd         |                 |                                    |
3107   // | target parallel  | section         | *                                  |
3108   // | for simd         |                 |                                    |
3109   // | target parallel  | single          | *                                  |
3110   // | for simd         |                 |                                    |
3111   // | target parallel  | parallel for    | *                                  |
3112   // | for simd         |                 |                                    |
3113   // | target parallel  |parallel for simd| *                                  |
3114   // | for simd         |                 |                                    |
3115   // | target parallel  |parallel sections| *                                  |
3116   // | for simd         |                 |                                    |
3117   // | target parallel  | task            | *                                  |
3118   // | for simd         |                 |                                    |
3119   // | target parallel  | taskyield       | *                                  |
3120   // | for simd         |                 |                                    |
3121   // | target parallel  | barrier         | *                                  |
3122   // | for simd         |                 |                                    |
3123   // | target parallel  | taskwait        | *                                  |
3124   // | for simd         |                 |                                    |
3125   // | target parallel  | taskgroup       | *                                  |
3126   // | for simd         |                 |                                    |
3127   // | target parallel  | flush           | *                                  |
3128   // | for simd         |                 |                                    |
3129   // | target parallel  | ordered         | + (with simd clause)               |
3130   // | for simd         |                 |                                    |
3131   // | target parallel  | atomic          | *                                  |
3132   // | for simd         |                 |                                    |
3133   // | target parallel  | target          | *                                  |
3134   // | for simd         |                 |                                    |
3135   // | target parallel  | target parallel | *                                  |
3136   // | for simd         |                 |                                    |
3137   // | target parallel  | target parallel | *                                  |
3138   // | for simd         | for             |                                    |
3139   // | target parallel  | target enter    | *                                  |
3140   // | for simd         | data            |                                    |
3141   // | target parallel  | target exit     | *                                  |
3142   // | for simd         | data            |                                    |
3143   // | target parallel  | teams           | *                                  |
3144   // | for simd         |                 |                                    |
3145   // | target parallel  | cancellation    | *                                  |
3146   // | for simd         | point           |                                    |
3147   // | target parallel  | cancel          | *                                  |
3148   // | for simd         |                 |                                    |
3149   // | target parallel  | taskloop        | *                                  |
3150   // | for simd         |                 |                                    |
3151   // | target parallel  | taskloop simd   | *                                  |
3152   // | for simd         |                 |                                    |
3153   // | target parallel  | distribute      | *                                  |
3154   // | for simd         |                 |                                    |
3155   // | target parallel  | distribute      | *                                  |
3156   // | for simd         | parallel for    |                                    |
3157   // | target parallel  | distribute      | *                                  |
3158   // | for simd         |parallel for simd|                                    |
3159   // | target parallel  | distribute simd | *                                  |
3160   // | for simd         |                 |                                    |
3161   // | target parallel  | target parallel | *                                  |
3162   // | for simd         | for simd        |                                    |
3163   // | target parallel  | target simd     | *                                  |
3164   // | for simd         |                 |                                    |
3165   // | target parallel  | teams distribute| *                                  |
3166   // | for simd         |                 |                                    |
3167   // +------------------+-----------------+------------------------------------+
3168   // | target simd      | parallel        |                                    |
3169   // | target simd      | for             |                                    |
3170   // | target simd      | for simd        |                                    |
3171   // | target simd      | master          |                                    |
3172   // | target simd      | critical        |                                    |
3173   // | target simd      | simd            |                                    |
3174   // | target simd      | sections        |                                    |
3175   // | target simd      | section         |                                    |
3176   // | target simd      | single          |                                    |
3177   // | target simd      | parallel for    |                                    |
3178   // | target simd      |parallel for simd|                                    |
3179   // | target simd      |parallel sections|                                    |
3180   // | target simd      | task            |                                    |
3181   // | target simd      | taskyield       |                                    |
3182   // | target simd      | barrier         |                                    |
3183   // | target simd      | taskwait        |                                    |
3184   // | target simd      | taskgroup       |                                    |
3185   // | target simd      | flush           |                                    |
3186   // | target simd      | ordered         | + (with simd clause)               |
3187   // | target simd      | atomic          |                                    |
3188   // | target simd      | target          |                                    |
3189   // | target simd      | target parallel |                                    |
3190   // | target simd      | target parallel |                                    |
3191   // |                  | for             |                                    |
3192   // | target simd      | target enter    |                                    |
3193   // |                  | data            |                                    |
3194   // | target simd      | target exit     |                                    |
3195   // |                  | data            |                                    |
3196   // | target simd      | teams           |                                    |
3197   // | target simd      | cancellation    |                                    |
3198   // |                  | point           |                                    |
3199   // | target simd      | cancel          |                                    |
3200   // | target simd      | taskloop        |                                    |
3201   // | target simd      | taskloop simd   |                                    |
3202   // | target simd      | distribute      |                                    |
3203   // | target simd      | distribute      |                                    |
3204   // |                  | parallel for    |                                    |
3205   // | target simd      | distribute      |                                    |
3206   // |                  |parallel for simd|                                    |
3207   // | target simd      | distribute simd |                                    |
3208   // | target simd      | target parallel |                                    |
3209   // |                  | for simd        |                                    |
3210   // | target simd      | target simd     |                                    |
3211   // | target simd      | teams distribute|                                    |
3212   // +------------------+-----------------+------------------------------------+
3213   // | teams distribute | parallel        |                                    |
3214   // | teams distribute | for             |                                    |
3215   // | teams distribute | for simd        |                                    |
3216   // | teams distribute | master          |                                    |
3217   // | teams distribute | critical        |                                    |
3218   // | teams distribute | simd            |                                    |
3219   // | teams distribute | sections        |                                    |
3220   // | teams distribute | section         |                                    |
3221   // | teams distribute | single          |                                    |
3222   // | teams distribute | parallel for    |                                    |
3223   // | teams distribute |parallel for simd|                                    |
3224   // | teams distribute |parallel sections|                                    |
3225   // | teams distribute | task            |                                    |
3226   // | teams distribute | taskyield       |                                    |
3227   // | teams distribute | barrier         |                                    |
3228   // | teams distribute | taskwait        |                                    |
3229   // | teams distribute | taskgroup       |                                    |
3230   // | teams distribute | flush           |                                    |
3231   // | teams distribute | ordered         | + (with simd clause)               |
3232   // | teams distribute | atomic          |                                    |
3233   // | teams distribute | target          |                                    |
3234   // | teams distribute | target parallel |                                    |
3235   // | teams distribute | target parallel |                                    |
3236   // |                  | for             |                                    |
3237   // | teams distribute | target enter    |                                    |
3238   // |                  | data            |                                    |
3239   // | teams distribute | target exit     |                                    |
3240   // |                  | data            |                                    |
3241   // | teams distribute | teams           |                                    |
3242   // | teams distribute | cancellation    |                                    |
3243   // |                  | point           |                                    |
3244   // | teams distribute | cancel          |                                    |
3245   // | teams distribute | taskloop        |                                    |
3246   // | teams distribute | taskloop simd   |                                    |
3247   // | teams distribute | distribute      |                                    |
3248   // | teams distribute | distribute      |                                    |
3249   // |                  | parallel for    |                                    |
3250   // | teams distribute | distribute      |                                    |
3251   // |                  |parallel for simd|                                    |
3252   // | teams distribute | distribute simd |                                    |
3253   // | teams distribute | target parallel |                                    |
3254   // |                  | for simd        |                                    |
3255   // | teams distribute | teams distribute|                                    |
3256   // +------------------+-----------------+------------------------------------+
3257   if (Stack->getCurScope()) {
3258     auto ParentRegion = Stack->getParentDirective();
3259     auto OffendingRegion = ParentRegion;
3260     bool NestingProhibited = false;
3261     bool CloseNesting = true;
3262     bool OrphanSeen = false;
3263     enum {
3264       NoRecommend,
3265       ShouldBeInParallelRegion,
3266       ShouldBeInOrderedRegion,
3267       ShouldBeInTargetRegion,
3268       ShouldBeInTeamsRegion
3269     } Recommend = NoRecommend;
3270     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
3271       // OpenMP [2.16, Nesting of Regions]
3272       // OpenMP constructs may not be nested inside a simd region.
3273       // OpenMP [2.8.1,simd Construct, Restrictions]
3274       // An ordered construct with the simd clause is the only OpenMP
3275       // construct that can appear in the simd region.
3276       // Allowing a SIMD construct nested in another SIMD construct is an
3277       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3278       // message.
3279       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3280                                  ? diag::err_omp_prohibited_region_simd
3281                                  : diag::warn_omp_nesting_simd);
3282       return CurrentRegion != OMPD_simd;
3283     }
3284     if (ParentRegion == OMPD_atomic) {
3285       // OpenMP [2.16, Nesting of Regions]
3286       // OpenMP constructs may not be nested inside an atomic region.
3287       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3288       return true;
3289     }
3290     if (CurrentRegion == OMPD_section) {
3291       // OpenMP [2.7.2, sections Construct, Restrictions]
3292       // Orphaned section directives are prohibited. That is, the section
3293       // directives must appear within the sections construct and must not be
3294       // encountered elsewhere in the sections region.
3295       if (ParentRegion != OMPD_sections &&
3296           ParentRegion != OMPD_parallel_sections) {
3297         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3298             << (ParentRegion != OMPD_unknown)
3299             << getOpenMPDirectiveName(ParentRegion);
3300         return true;
3301       }
3302       return false;
3303     }
3304     // Allow some constructs (except teams) to be orphaned (they could be
3305     // used in functions, called from OpenMP regions with the required
3306     // preconditions).
3307     if (ParentRegion == OMPD_unknown && !isOpenMPTeamsDirective(CurrentRegion))
3308       return false;
3309     if (CurrentRegion == OMPD_cancellation_point ||
3310         CurrentRegion == OMPD_cancel) {
3311       // OpenMP [2.16, Nesting of Regions]
3312       // A cancellation point construct for which construct-type-clause is
3313       // taskgroup must be nested inside a task construct. A cancellation
3314       // point construct for which construct-type-clause is not taskgroup must
3315       // be closely nested inside an OpenMP construct that matches the type
3316       // specified in construct-type-clause.
3317       // A cancel construct for which construct-type-clause is taskgroup must be
3318       // nested inside a task construct. A cancel construct for which
3319       // construct-type-clause is not taskgroup must be closely nested inside an
3320       // OpenMP construct that matches the type specified in
3321       // construct-type-clause.
3322       NestingProhibited =
3323           !((CancelRegion == OMPD_parallel &&
3324              (ParentRegion == OMPD_parallel ||
3325               ParentRegion == OMPD_target_parallel)) ||
3326             (CancelRegion == OMPD_for &&
3327              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3328               ParentRegion == OMPD_target_parallel_for)) ||
3329             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3330             (CancelRegion == OMPD_sections &&
3331              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3332               ParentRegion == OMPD_parallel_sections)));
3333     } else if (CurrentRegion == OMPD_master) {
3334       // OpenMP [2.16, Nesting of Regions]
3335       // A master region may not be closely nested inside a worksharing,
3336       // atomic, or explicit task region.
3337       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3338                           isOpenMPTaskingDirective(ParentRegion);
3339     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3340       // OpenMP [2.16, Nesting of Regions]
3341       // A critical region may not be nested (closely or otherwise) inside a
3342       // critical region with the same name. Note that this restriction is not
3343       // sufficient to prevent deadlock.
3344       SourceLocation PreviousCriticalLoc;
3345       bool DeadLock = Stack->hasDirective(
3346           [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3347                                               const DeclarationNameInfo &DNI,
3348                                               SourceLocation Loc) -> bool {
3349             if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3350               PreviousCriticalLoc = Loc;
3351               return true;
3352             } else
3353               return false;
3354           },
3355           false /* skip top directive */);
3356       if (DeadLock) {
3357         SemaRef.Diag(StartLoc,
3358                      diag::err_omp_prohibited_region_critical_same_name)
3359             << CurrentName.getName();
3360         if (PreviousCriticalLoc.isValid())
3361           SemaRef.Diag(PreviousCriticalLoc,
3362                        diag::note_omp_previous_critical_region);
3363         return true;
3364       }
3365     } else if (CurrentRegion == OMPD_barrier) {
3366       // OpenMP [2.16, Nesting of Regions]
3367       // A barrier region may not be closely nested inside a worksharing,
3368       // explicit task, critical, ordered, atomic, or master region.
3369       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3370                           isOpenMPTaskingDirective(ParentRegion) ||
3371                           ParentRegion == OMPD_master ||
3372                           ParentRegion == OMPD_critical ||
3373                           ParentRegion == OMPD_ordered;
3374     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
3375                !isOpenMPParallelDirective(CurrentRegion)) {
3376       // OpenMP [2.16, Nesting of Regions]
3377       // A worksharing region may not be closely nested inside a worksharing,
3378       // explicit task, critical, ordered, atomic, or master region.
3379       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3380                           isOpenMPTaskingDirective(ParentRegion) ||
3381                           ParentRegion == OMPD_master ||
3382                           ParentRegion == OMPD_critical ||
3383                           ParentRegion == OMPD_ordered;
3384       Recommend = ShouldBeInParallelRegion;
3385     } else if (CurrentRegion == OMPD_ordered) {
3386       // OpenMP [2.16, Nesting of Regions]
3387       // An ordered region may not be closely nested inside a critical,
3388       // atomic, or explicit task region.
3389       // An ordered region must be closely nested inside a loop region (or
3390       // parallel loop region) with an ordered clause.
3391       // OpenMP [2.8.1,simd Construct, Restrictions]
3392       // An ordered construct with the simd clause is the only OpenMP construct
3393       // that can appear in the simd region.
3394       NestingProhibited = ParentRegion == OMPD_critical ||
3395                           isOpenMPTaskingDirective(ParentRegion) ||
3396                           !(isOpenMPSimdDirective(ParentRegion) ||
3397                             Stack->isParentOrderedRegion());
3398       Recommend = ShouldBeInOrderedRegion;
3399     } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3400       // OpenMP [2.16, Nesting of Regions]
3401       // If specified, a teams construct must be contained within a target
3402       // construct.
3403       NestingProhibited = ParentRegion != OMPD_target;
3404       OrphanSeen = ParentRegion == OMPD_unknown;
3405       Recommend = ShouldBeInTargetRegion;
3406       Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3407     }
3408     if (!NestingProhibited && ParentRegion == OMPD_teams) {
3409       // OpenMP [2.16, Nesting of Regions]
3410       // distribute, parallel, parallel sections, parallel workshare, and the
3411       // parallel loop and parallel loop SIMD constructs are the only OpenMP
3412       // constructs that can be closely nested in the teams region.
3413       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3414                           !isOpenMPDistributeDirective(CurrentRegion);
3415       Recommend = ShouldBeInParallelRegion;
3416     }
3417     if (!NestingProhibited &&
3418         isOpenMPNestingDistributeDirective(CurrentRegion)) {
3419       // OpenMP 4.5 [2.17 Nesting of Regions]
3420       // The region associated with the distribute construct must be strictly
3421       // nested inside a teams region
3422       NestingProhibited = ParentRegion != OMPD_teams;
3423       Recommend = ShouldBeInTeamsRegion;
3424     }
3425     if (!NestingProhibited &&
3426         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3427          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3428       // OpenMP 4.5 [2.17 Nesting of Regions]
3429       // If a target, target update, target data, target enter data, or
3430       // target exit data construct is encountered during execution of a
3431       // target region, the behavior is unspecified.
3432       NestingProhibited = Stack->hasDirective(
3433           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3434                              SourceLocation) -> bool {
3435             if (isOpenMPTargetExecutionDirective(K)) {
3436               OffendingRegion = K;
3437               return true;
3438             } else
3439               return false;
3440           },
3441           false /* don't skip top directive */);
3442       CloseNesting = false;
3443     }
3444     if (NestingProhibited) {
3445       if (OrphanSeen) {
3446         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3447             << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3448       } else {
3449         SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3450             << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3451             << Recommend << getOpenMPDirectiveName(CurrentRegion);
3452       }
3453       return true;
3454     }
3455   }
3456   return false;
3457 }
3458 
3459 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3460                            ArrayRef<OMPClause *> Clauses,
3461                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3462   bool ErrorFound = false;
3463   unsigned NamedModifiersNumber = 0;
3464   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3465       OMPD_unknown + 1);
3466   SmallVector<SourceLocation, 4> NameModifierLoc;
3467   for (const auto *C : Clauses) {
3468     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3469       // At most one if clause without a directive-name-modifier can appear on
3470       // the directive.
3471       OpenMPDirectiveKind CurNM = IC->getNameModifier();
3472       if (FoundNameModifiers[CurNM]) {
3473         S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3474             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3475             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3476         ErrorFound = true;
3477       } else if (CurNM != OMPD_unknown) {
3478         NameModifierLoc.push_back(IC->getNameModifierLoc());
3479         ++NamedModifiersNumber;
3480       }
3481       FoundNameModifiers[CurNM] = IC;
3482       if (CurNM == OMPD_unknown)
3483         continue;
3484       // Check if the specified name modifier is allowed for the current
3485       // directive.
3486       // At most one if clause with the particular directive-name-modifier can
3487       // appear on the directive.
3488       bool MatchFound = false;
3489       for (auto NM : AllowedNameModifiers) {
3490         if (CurNM == NM) {
3491           MatchFound = true;
3492           break;
3493         }
3494       }
3495       if (!MatchFound) {
3496         S.Diag(IC->getNameModifierLoc(),
3497                diag::err_omp_wrong_if_directive_name_modifier)
3498             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3499         ErrorFound = true;
3500       }
3501     }
3502   }
3503   // If any if clause on the directive includes a directive-name-modifier then
3504   // all if clauses on the directive must include a directive-name-modifier.
3505   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3506     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3507       S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3508              diag::err_omp_no_more_if_clause);
3509     } else {
3510       std::string Values;
3511       std::string Sep(", ");
3512       unsigned AllowedCnt = 0;
3513       unsigned TotalAllowedNum =
3514           AllowedNameModifiers.size() - NamedModifiersNumber;
3515       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3516            ++Cnt) {
3517         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3518         if (!FoundNameModifiers[NM]) {
3519           Values += "'";
3520           Values += getOpenMPDirectiveName(NM);
3521           Values += "'";
3522           if (AllowedCnt + 2 == TotalAllowedNum)
3523             Values += " or ";
3524           else if (AllowedCnt + 1 != TotalAllowedNum)
3525             Values += Sep;
3526           ++AllowedCnt;
3527         }
3528       }
3529       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3530              diag::err_omp_unnamed_if_clause)
3531           << (TotalAllowedNum > 1) << Values;
3532     }
3533     for (auto Loc : NameModifierLoc) {
3534       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3535     }
3536     ErrorFound = true;
3537   }
3538   return ErrorFound;
3539 }
3540 
3541 StmtResult Sema::ActOnOpenMPExecutableDirective(
3542     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3543     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3544     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3545   StmtResult Res = StmtError();
3546   if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3547                             StartLoc))
3548     return StmtError();
3549 
3550   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3551   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
3552   bool ErrorFound = false;
3553   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3554   if (AStmt) {
3555     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3556 
3557     // Check default data sharing attributes for referenced variables.
3558     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3559     DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3560     if (DSAChecker.isErrorFound())
3561       return StmtError();
3562     // Generate list of implicitly defined firstprivate variables.
3563     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3564 
3565     if (!DSAChecker.getImplicitFirstprivate().empty()) {
3566       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3567               DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3568               SourceLocation(), SourceLocation())) {
3569         ClausesWithImplicit.push_back(Implicit);
3570         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3571                      DSAChecker.getImplicitFirstprivate().size();
3572       } else
3573         ErrorFound = true;
3574     }
3575   }
3576 
3577   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3578   switch (Kind) {
3579   case OMPD_parallel:
3580     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3581                                        EndLoc);
3582     AllowedNameModifiers.push_back(OMPD_parallel);
3583     break;
3584   case OMPD_simd:
3585     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3586                                    VarsWithInheritedDSA);
3587     break;
3588   case OMPD_for:
3589     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3590                                   VarsWithInheritedDSA);
3591     break;
3592   case OMPD_for_simd:
3593     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3594                                       EndLoc, VarsWithInheritedDSA);
3595     break;
3596   case OMPD_sections:
3597     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3598                                        EndLoc);
3599     break;
3600   case OMPD_section:
3601     assert(ClausesWithImplicit.empty() &&
3602            "No clauses are allowed for 'omp section' directive");
3603     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3604     break;
3605   case OMPD_single:
3606     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3607                                      EndLoc);
3608     break;
3609   case OMPD_master:
3610     assert(ClausesWithImplicit.empty() &&
3611            "No clauses are allowed for 'omp master' directive");
3612     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3613     break;
3614   case OMPD_critical:
3615     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3616                                        StartLoc, EndLoc);
3617     break;
3618   case OMPD_parallel_for:
3619     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3620                                           EndLoc, VarsWithInheritedDSA);
3621     AllowedNameModifiers.push_back(OMPD_parallel);
3622     break;
3623   case OMPD_parallel_for_simd:
3624     Res = ActOnOpenMPParallelForSimdDirective(
3625         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3626     AllowedNameModifiers.push_back(OMPD_parallel);
3627     break;
3628   case OMPD_parallel_sections:
3629     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3630                                                StartLoc, EndLoc);
3631     AllowedNameModifiers.push_back(OMPD_parallel);
3632     break;
3633   case OMPD_task:
3634     Res =
3635         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3636     AllowedNameModifiers.push_back(OMPD_task);
3637     break;
3638   case OMPD_taskyield:
3639     assert(ClausesWithImplicit.empty() &&
3640            "No clauses are allowed for 'omp taskyield' directive");
3641     assert(AStmt == nullptr &&
3642            "No associated statement allowed for 'omp taskyield' directive");
3643     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3644     break;
3645   case OMPD_barrier:
3646     assert(ClausesWithImplicit.empty() &&
3647            "No clauses are allowed for 'omp barrier' directive");
3648     assert(AStmt == nullptr &&
3649            "No associated statement allowed for 'omp barrier' directive");
3650     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3651     break;
3652   case OMPD_taskwait:
3653     assert(ClausesWithImplicit.empty() &&
3654            "No clauses are allowed for 'omp taskwait' directive");
3655     assert(AStmt == nullptr &&
3656            "No associated statement allowed for 'omp taskwait' directive");
3657     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3658     break;
3659   case OMPD_taskgroup:
3660     assert(ClausesWithImplicit.empty() &&
3661            "No clauses are allowed for 'omp taskgroup' directive");
3662     Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3663     break;
3664   case OMPD_flush:
3665     assert(AStmt == nullptr &&
3666            "No associated statement allowed for 'omp flush' directive");
3667     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3668     break;
3669   case OMPD_ordered:
3670     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3671                                       EndLoc);
3672     break;
3673   case OMPD_atomic:
3674     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3675                                      EndLoc);
3676     break;
3677   case OMPD_teams:
3678     Res =
3679         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3680     break;
3681   case OMPD_target:
3682     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3683                                      EndLoc);
3684     AllowedNameModifiers.push_back(OMPD_target);
3685     break;
3686   case OMPD_target_parallel:
3687     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3688                                              StartLoc, EndLoc);
3689     AllowedNameModifiers.push_back(OMPD_target);
3690     AllowedNameModifiers.push_back(OMPD_parallel);
3691     break;
3692   case OMPD_target_parallel_for:
3693     Res = ActOnOpenMPTargetParallelForDirective(
3694         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3695     AllowedNameModifiers.push_back(OMPD_target);
3696     AllowedNameModifiers.push_back(OMPD_parallel);
3697     break;
3698   case OMPD_cancellation_point:
3699     assert(ClausesWithImplicit.empty() &&
3700            "No clauses are allowed for 'omp cancellation point' directive");
3701     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3702                                "cancellation point' directive");
3703     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3704     break;
3705   case OMPD_cancel:
3706     assert(AStmt == nullptr &&
3707            "No associated statement allowed for 'omp cancel' directive");
3708     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3709                                      CancelRegion);
3710     AllowedNameModifiers.push_back(OMPD_cancel);
3711     break;
3712   case OMPD_target_data:
3713     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3714                                          EndLoc);
3715     AllowedNameModifiers.push_back(OMPD_target_data);
3716     break;
3717   case OMPD_target_enter_data:
3718     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3719                                               EndLoc);
3720     AllowedNameModifiers.push_back(OMPD_target_enter_data);
3721     break;
3722   case OMPD_target_exit_data:
3723     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3724                                              EndLoc);
3725     AllowedNameModifiers.push_back(OMPD_target_exit_data);
3726     break;
3727   case OMPD_taskloop:
3728     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3729                                        EndLoc, VarsWithInheritedDSA);
3730     AllowedNameModifiers.push_back(OMPD_taskloop);
3731     break;
3732   case OMPD_taskloop_simd:
3733     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3734                                            EndLoc, VarsWithInheritedDSA);
3735     AllowedNameModifiers.push_back(OMPD_taskloop);
3736     break;
3737   case OMPD_distribute:
3738     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3739                                          EndLoc, VarsWithInheritedDSA);
3740     break;
3741   case OMPD_target_update:
3742     assert(!AStmt && "Statement is not allowed for target update");
3743     Res =
3744         ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3745     AllowedNameModifiers.push_back(OMPD_target_update);
3746     break;
3747   case OMPD_distribute_parallel_for:
3748     Res = ActOnOpenMPDistributeParallelForDirective(
3749         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3750     AllowedNameModifiers.push_back(OMPD_parallel);
3751     break;
3752   case OMPD_distribute_parallel_for_simd:
3753     Res = ActOnOpenMPDistributeParallelForSimdDirective(
3754         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3755     AllowedNameModifiers.push_back(OMPD_parallel);
3756     break;
3757   case OMPD_distribute_simd:
3758     Res = ActOnOpenMPDistributeSimdDirective(
3759         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3760     break;
3761   case OMPD_target_parallel_for_simd:
3762     Res = ActOnOpenMPTargetParallelForSimdDirective(
3763         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3764     AllowedNameModifiers.push_back(OMPD_target);
3765     AllowedNameModifiers.push_back(OMPD_parallel);
3766     break;
3767   case OMPD_target_simd:
3768     Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3769                                          EndLoc, VarsWithInheritedDSA);
3770     AllowedNameModifiers.push_back(OMPD_target);
3771     break;
3772   case OMPD_teams_distribute:
3773     Res = ActOnOpenMPTeamsDistributeDirective(
3774         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3775     break;
3776   case OMPD_declare_target:
3777   case OMPD_end_declare_target:
3778   case OMPD_threadprivate:
3779   case OMPD_declare_reduction:
3780   case OMPD_declare_simd:
3781     llvm_unreachable("OpenMP Directive is not allowed");
3782   case OMPD_unknown:
3783     llvm_unreachable("Unknown OpenMP directive");
3784   }
3785 
3786   for (auto P : VarsWithInheritedDSA) {
3787     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3788         << P.first << P.second->getSourceRange();
3789   }
3790   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3791 
3792   if (!AllowedNameModifiers.empty())
3793     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3794                  ErrorFound;
3795 
3796   if (ErrorFound)
3797     return StmtError();
3798   return Res;
3799 }
3800 
3801 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3802     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3803     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3804     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3805     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3806   assert(Aligneds.size() == Alignments.size());
3807   assert(Linears.size() == LinModifiers.size());
3808   assert(Linears.size() == Steps.size());
3809   if (!DG || DG.get().isNull())
3810     return DeclGroupPtrTy();
3811 
3812   if (!DG.get().isSingleDecl()) {
3813     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3814     return DG;
3815   }
3816   auto *ADecl = DG.get().getSingleDecl();
3817   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3818     ADecl = FTD->getTemplatedDecl();
3819 
3820   auto *FD = dyn_cast<FunctionDecl>(ADecl);
3821   if (!FD) {
3822     Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3823     return DeclGroupPtrTy();
3824   }
3825 
3826   // OpenMP [2.8.2, declare simd construct, Description]
3827   // The parameter of the simdlen clause must be a constant positive integer
3828   // expression.
3829   ExprResult SL;
3830   if (Simdlen)
3831     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3832   // OpenMP [2.8.2, declare simd construct, Description]
3833   // The special this pointer can be used as if was one of the arguments to the
3834   // function in any of the linear, aligned, or uniform clauses.
3835   // The uniform clause declares one or more arguments to have an invariant
3836   // value for all concurrent invocations of the function in the execution of a
3837   // single SIMD loop.
3838   llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3839   Expr *UniformedLinearThis = nullptr;
3840   for (auto *E : Uniforms) {
3841     E = E->IgnoreParenImpCasts();
3842     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3843       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3844         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3845             FD->getParamDecl(PVD->getFunctionScopeIndex())
3846                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3847           UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
3848           continue;
3849         }
3850     if (isa<CXXThisExpr>(E)) {
3851       UniformedLinearThis = E;
3852       continue;
3853     }
3854     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3855         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3856   }
3857   // OpenMP [2.8.2, declare simd construct, Description]
3858   // The aligned clause declares that the object to which each list item points
3859   // is aligned to the number of bytes expressed in the optional parameter of
3860   // the aligned clause.
3861   // The special this pointer can be used as if was one of the arguments to the
3862   // function in any of the linear, aligned, or uniform clauses.
3863   // The type of list items appearing in the aligned clause must be array,
3864   // pointer, reference to array, or reference to pointer.
3865   llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3866   Expr *AlignedThis = nullptr;
3867   for (auto *E : Aligneds) {
3868     E = E->IgnoreParenImpCasts();
3869     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3870       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3871         auto *CanonPVD = PVD->getCanonicalDecl();
3872         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3873             FD->getParamDecl(PVD->getFunctionScopeIndex())
3874                     ->getCanonicalDecl() == CanonPVD) {
3875           // OpenMP  [2.8.1, simd construct, Restrictions]
3876           // A list-item cannot appear in more than one aligned clause.
3877           if (AlignedArgs.count(CanonPVD) > 0) {
3878             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3879                 << 1 << E->getSourceRange();
3880             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3881                  diag::note_omp_explicit_dsa)
3882                 << getOpenMPClauseName(OMPC_aligned);
3883             continue;
3884           }
3885           AlignedArgs[CanonPVD] = E;
3886           QualType QTy = PVD->getType()
3887                              .getNonReferenceType()
3888                              .getUnqualifiedType()
3889                              .getCanonicalType();
3890           const Type *Ty = QTy.getTypePtrOrNull();
3891           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3892             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3893                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3894             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3895           }
3896           continue;
3897         }
3898       }
3899     if (isa<CXXThisExpr>(E)) {
3900       if (AlignedThis) {
3901         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3902             << 2 << E->getSourceRange();
3903         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3904             << getOpenMPClauseName(OMPC_aligned);
3905       }
3906       AlignedThis = E;
3907       continue;
3908     }
3909     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3910         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3911   }
3912   // The optional parameter of the aligned clause, alignment, must be a constant
3913   // positive integer expression. If no optional parameter is specified,
3914   // implementation-defined default alignments for SIMD instructions on the
3915   // target platforms are assumed.
3916   SmallVector<Expr *, 4> NewAligns;
3917   for (auto *E : Alignments) {
3918     ExprResult Align;
3919     if (E)
3920       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3921     NewAligns.push_back(Align.get());
3922   }
3923   // OpenMP [2.8.2, declare simd construct, Description]
3924   // The linear clause declares one or more list items to be private to a SIMD
3925   // lane and to have a linear relationship with respect to the iteration space
3926   // of a loop.
3927   // The special this pointer can be used as if was one of the arguments to the
3928   // function in any of the linear, aligned, or uniform clauses.
3929   // When a linear-step expression is specified in a linear clause it must be
3930   // either a constant integer expression or an integer-typed parameter that is
3931   // specified in a uniform clause on the directive.
3932   llvm::DenseMap<Decl *, Expr *> LinearArgs;
3933   const bool IsUniformedThis = UniformedLinearThis != nullptr;
3934   auto MI = LinModifiers.begin();
3935   for (auto *E : Linears) {
3936     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3937     ++MI;
3938     E = E->IgnoreParenImpCasts();
3939     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3940       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3941         auto *CanonPVD = PVD->getCanonicalDecl();
3942         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3943             FD->getParamDecl(PVD->getFunctionScopeIndex())
3944                     ->getCanonicalDecl() == CanonPVD) {
3945           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
3946           // A list-item cannot appear in more than one linear clause.
3947           if (LinearArgs.count(CanonPVD) > 0) {
3948             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3949                 << getOpenMPClauseName(OMPC_linear)
3950                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3951             Diag(LinearArgs[CanonPVD]->getExprLoc(),
3952                  diag::note_omp_explicit_dsa)
3953                 << getOpenMPClauseName(OMPC_linear);
3954             continue;
3955           }
3956           // Each argument can appear in at most one uniform or linear clause.
3957           if (UniformedArgs.count(CanonPVD) > 0) {
3958             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3959                 << getOpenMPClauseName(OMPC_linear)
3960                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3961             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3962                  diag::note_omp_explicit_dsa)
3963                 << getOpenMPClauseName(OMPC_uniform);
3964             continue;
3965           }
3966           LinearArgs[CanonPVD] = E;
3967           if (E->isValueDependent() || E->isTypeDependent() ||
3968               E->isInstantiationDependent() ||
3969               E->containsUnexpandedParameterPack())
3970             continue;
3971           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3972                                       PVD->getOriginalType());
3973           continue;
3974         }
3975       }
3976     if (isa<CXXThisExpr>(E)) {
3977       if (UniformedLinearThis) {
3978         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3979             << getOpenMPClauseName(OMPC_linear)
3980             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3981             << E->getSourceRange();
3982         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3983             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3984                                                    : OMPC_linear);
3985         continue;
3986       }
3987       UniformedLinearThis = E;
3988       if (E->isValueDependent() || E->isTypeDependent() ||
3989           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3990         continue;
3991       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3992                                   E->getType());
3993       continue;
3994     }
3995     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3996         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3997   }
3998   Expr *Step = nullptr;
3999   Expr *NewStep = nullptr;
4000   SmallVector<Expr *, 4> NewSteps;
4001   for (auto *E : Steps) {
4002     // Skip the same step expression, it was checked already.
4003     if (Step == E || !E) {
4004       NewSteps.push_back(E ? NewStep : nullptr);
4005       continue;
4006     }
4007     Step = E;
4008     if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
4009       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4010         auto *CanonPVD = PVD->getCanonicalDecl();
4011         if (UniformedArgs.count(CanonPVD) == 0) {
4012           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4013               << Step->getSourceRange();
4014         } else if (E->isValueDependent() || E->isTypeDependent() ||
4015                    E->isInstantiationDependent() ||
4016                    E->containsUnexpandedParameterPack() ||
4017                    CanonPVD->getType()->hasIntegerRepresentation())
4018           NewSteps.push_back(Step);
4019         else {
4020           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4021               << Step->getSourceRange();
4022         }
4023         continue;
4024       }
4025     NewStep = Step;
4026     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4027         !Step->isInstantiationDependent() &&
4028         !Step->containsUnexpandedParameterPack()) {
4029       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4030                     .get();
4031       if (NewStep)
4032         NewStep = VerifyIntegerConstantExpression(NewStep).get();
4033     }
4034     NewSteps.push_back(NewStep);
4035   }
4036   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4037       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
4038       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
4039       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4040       const_cast<Expr **>(Linears.data()), Linears.size(),
4041       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4042       NewSteps.data(), NewSteps.size(), SR);
4043   ADecl->addAttr(NewAttr);
4044   return ConvertDeclToDeclGroup(ADecl);
4045 }
4046 
4047 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4048                                               Stmt *AStmt,
4049                                               SourceLocation StartLoc,
4050                                               SourceLocation EndLoc) {
4051   if (!AStmt)
4052     return StmtError();
4053 
4054   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4055   // 1.2.2 OpenMP Language Terminology
4056   // Structured block - An executable statement with a single entry at the
4057   // top and a single exit at the bottom.
4058   // The point of exit cannot be a branch out of the structured block.
4059   // longjmp() and throw() must not violate the entry/exit criteria.
4060   CS->getCapturedDecl()->setNothrow();
4061 
4062   getCurFunction()->setHasBranchProtectedScope();
4063 
4064   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4065                                       DSAStack->isCancelRegion());
4066 }
4067 
4068 namespace {
4069 /// \brief Helper class for checking canonical form of the OpenMP loops and
4070 /// extracting iteration space of each loop in the loop nest, that will be used
4071 /// for IR generation.
4072 class OpenMPIterationSpaceChecker {
4073   /// \brief Reference to Sema.
4074   Sema &SemaRef;
4075   /// \brief A location for diagnostics (when there is no some better location).
4076   SourceLocation DefaultLoc;
4077   /// \brief A location for diagnostics (when increment is not compatible).
4078   SourceLocation ConditionLoc;
4079   /// \brief A source location for referring to loop init later.
4080   SourceRange InitSrcRange;
4081   /// \brief A source location for referring to condition later.
4082   SourceRange ConditionSrcRange;
4083   /// \brief A source location for referring to increment later.
4084   SourceRange IncrementSrcRange;
4085   /// \brief Loop variable.
4086   ValueDecl *LCDecl = nullptr;
4087   /// \brief Reference to loop variable.
4088   Expr *LCRef = nullptr;
4089   /// \brief Lower bound (initializer for the var).
4090   Expr *LB = nullptr;
4091   /// \brief Upper bound.
4092   Expr *UB = nullptr;
4093   /// \brief Loop step (increment).
4094   Expr *Step = nullptr;
4095   /// \brief This flag is true when condition is one of:
4096   ///   Var <  UB
4097   ///   Var <= UB
4098   ///   UB  >  Var
4099   ///   UB  >= Var
4100   bool TestIsLessOp = false;
4101   /// \brief This flag is true when condition is strict ( < or > ).
4102   bool TestIsStrictOp = false;
4103   /// \brief This flag is true when step is subtracted on each iteration.
4104   bool SubtractStep = false;
4105 
4106 public:
4107   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
4108       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
4109   /// \brief Check init-expr for canonical loop form and save loop counter
4110   /// variable - #Var and its initialization value - #LB.
4111   bool CheckInit(Stmt *S, bool EmitDiags = true);
4112   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
4113   /// for less/greater and for strict/non-strict comparison.
4114   bool CheckCond(Expr *S);
4115   /// \brief Check incr-expr for canonical loop form and return true if it
4116   /// does not conform, otherwise save loop step (#Step).
4117   bool CheckInc(Expr *S);
4118   /// \brief Return the loop counter variable.
4119   ValueDecl *GetLoopDecl() const { return LCDecl; }
4120   /// \brief Return the reference expression to loop counter variable.
4121   Expr *GetLoopDeclRefExpr() const { return LCRef; }
4122   /// \brief Source range of the loop init.
4123   SourceRange GetInitSrcRange() const { return InitSrcRange; }
4124   /// \brief Source range of the loop condition.
4125   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
4126   /// \brief Source range of the loop increment.
4127   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
4128   /// \brief True if the step should be subtracted.
4129   bool ShouldSubtractStep() const { return SubtractStep; }
4130   /// \brief Build the expression to calculate the number of iterations.
4131   Expr *
4132   BuildNumIterations(Scope *S, const bool LimitedType,
4133                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
4134   /// \brief Build the precondition expression for the loops.
4135   Expr *BuildPreCond(Scope *S, Expr *Cond,
4136                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
4137   /// \brief Build reference expression to the counter be used for codegen.
4138   DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
4139                                DSAStackTy &DSA) const;
4140   /// \brief Build reference expression to the private counter be used for
4141   /// codegen.
4142   Expr *BuildPrivateCounterVar() const;
4143   /// \brief Build initialization of the counter be used for codegen.
4144   Expr *BuildCounterInit() const;
4145   /// \brief Build step of the counter be used for codegen.
4146   Expr *BuildCounterStep() const;
4147   /// \brief Return true if any expression is dependent.
4148   bool Dependent() const;
4149 
4150 private:
4151   /// \brief Check the right-hand side of an assignment in the increment
4152   /// expression.
4153   bool CheckIncRHS(Expr *RHS);
4154   /// \brief Helper to set loop counter variable and its initializer.
4155   bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
4156   /// \brief Helper to set upper bound.
4157   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
4158              SourceLocation SL);
4159   /// \brief Helper to set loop increment.
4160   bool SetStep(Expr *NewStep, bool Subtract);
4161 };
4162 
4163 bool OpenMPIterationSpaceChecker::Dependent() const {
4164   if (!LCDecl) {
4165     assert(!LB && !UB && !Step);
4166     return false;
4167   }
4168   return LCDecl->getType()->isDependentType() ||
4169          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4170          (Step && Step->isValueDependent());
4171 }
4172 
4173 static Expr *getExprAsWritten(Expr *E) {
4174   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4175     E = ExprTemp->getSubExpr();
4176 
4177   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4178     E = MTE->GetTemporaryExpr();
4179 
4180   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4181     E = Binder->getSubExpr();
4182 
4183   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4184     E = ICE->getSubExprAsWritten();
4185   return E->IgnoreParens();
4186 }
4187 
4188 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4189                                                  Expr *NewLCRefExpr,
4190                                                  Expr *NewLB) {
4191   // State consistency checking to ensure correct usage.
4192   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
4193          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4194   if (!NewLCDecl || !NewLB)
4195     return true;
4196   LCDecl = getCanonicalDecl(NewLCDecl);
4197   LCRef = NewLCRefExpr;
4198   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4199     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4200       if ((Ctor->isCopyOrMoveConstructor() ||
4201            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4202           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4203         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
4204   LB = NewLB;
4205   return false;
4206 }
4207 
4208 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
4209                                         SourceRange SR, SourceLocation SL) {
4210   // State consistency checking to ensure correct usage.
4211   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4212          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4213   if (!NewUB)
4214     return true;
4215   UB = NewUB;
4216   TestIsLessOp = LessOp;
4217   TestIsStrictOp = StrictOp;
4218   ConditionSrcRange = SR;
4219   ConditionLoc = SL;
4220   return false;
4221 }
4222 
4223 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4224   // State consistency checking to ensure correct usage.
4225   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
4226   if (!NewStep)
4227     return true;
4228   if (!NewStep->isValueDependent()) {
4229     // Check that the step is integer expression.
4230     SourceLocation StepLoc = NewStep->getLocStart();
4231     ExprResult Val =
4232         SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4233     if (Val.isInvalid())
4234       return true;
4235     NewStep = Val.get();
4236 
4237     // OpenMP [2.6, Canonical Loop Form, Restrictions]
4238     //  If test-expr is of form var relational-op b and relational-op is < or
4239     //  <= then incr-expr must cause var to increase on each iteration of the
4240     //  loop. If test-expr is of form var relational-op b and relational-op is
4241     //  > or >= then incr-expr must cause var to decrease on each iteration of
4242     //  the loop.
4243     //  If test-expr is of form b relational-op var and relational-op is < or
4244     //  <= then incr-expr must cause var to decrease on each iteration of the
4245     //  loop. If test-expr is of form b relational-op var and relational-op is
4246     //  > or >= then incr-expr must cause var to increase on each iteration of
4247     //  the loop.
4248     llvm::APSInt Result;
4249     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4250     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4251     bool IsConstNeg =
4252         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
4253     bool IsConstPos =
4254         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
4255     bool IsConstZero = IsConstant && !Result.getBoolValue();
4256     if (UB && (IsConstZero ||
4257                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
4258                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
4259       SemaRef.Diag(NewStep->getExprLoc(),
4260                    diag::err_omp_loop_incr_not_compatible)
4261           << LCDecl << TestIsLessOp << NewStep->getSourceRange();
4262       SemaRef.Diag(ConditionLoc,
4263                    diag::note_omp_loop_cond_requres_compatible_incr)
4264           << TestIsLessOp << ConditionSrcRange;
4265       return true;
4266     }
4267     if (TestIsLessOp == Subtract) {
4268       NewStep =
4269           SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4270               .get();
4271       Subtract = !Subtract;
4272     }
4273   }
4274 
4275   Step = NewStep;
4276   SubtractStep = Subtract;
4277   return false;
4278 }
4279 
4280 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
4281   // Check init-expr for canonical loop form and save loop counter
4282   // variable - #Var and its initialization value - #LB.
4283   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4284   //   var = lb
4285   //   integer-type var = lb
4286   //   random-access-iterator-type var = lb
4287   //   pointer-type var = lb
4288   //
4289   if (!S) {
4290     if (EmitDiags) {
4291       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4292     }
4293     return true;
4294   }
4295   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4296     if (!ExprTemp->cleanupsHaveSideEffects())
4297       S = ExprTemp->getSubExpr();
4298 
4299   InitSrcRange = S->getSourceRange();
4300   if (Expr *E = dyn_cast<Expr>(S))
4301     S = E->IgnoreParens();
4302   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4303     if (BO->getOpcode() == BO_Assign) {
4304       auto *LHS = BO->getLHS()->IgnoreParens();
4305       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4306         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4307           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4308             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4309         return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4310       }
4311       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4312         if (ME->isArrow() &&
4313             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4314           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4315       }
4316     }
4317   } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
4318     if (DS->isSingleDecl()) {
4319       if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
4320         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
4321           // Accept non-canonical init form here but emit ext. warning.
4322           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
4323             SemaRef.Diag(S->getLocStart(),
4324                          diag::ext_omp_loop_not_canonical_init)
4325                 << S->getSourceRange();
4326           return SetLCDeclAndLB(Var, nullptr, Var->getInit());
4327         }
4328       }
4329     }
4330   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4331     if (CE->getOperator() == OO_Equal) {
4332       auto *LHS = CE->getArg(0);
4333       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4334         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4335           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4336             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4337         return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4338       }
4339       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4340         if (ME->isArrow() &&
4341             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4342           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4343       }
4344     }
4345   }
4346 
4347   if (Dependent() || SemaRef.CurContext->isDependentContext())
4348     return false;
4349   if (EmitDiags) {
4350     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4351         << S->getSourceRange();
4352   }
4353   return true;
4354 }
4355 
4356 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
4357 /// variable (which may be the loop variable) if possible.
4358 static const ValueDecl *GetInitLCDecl(Expr *E) {
4359   if (!E)
4360     return nullptr;
4361   E = getExprAsWritten(E);
4362   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4363     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4364       if ((Ctor->isCopyOrMoveConstructor() ||
4365            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4366           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4367         E = CE->getArg(0)->IgnoreParenImpCasts();
4368   if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4369     if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4370       if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4371         if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4372           return getCanonicalDecl(ME->getMemberDecl());
4373       return getCanonicalDecl(VD);
4374     }
4375   }
4376   if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4377     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4378       return getCanonicalDecl(ME->getMemberDecl());
4379   return nullptr;
4380 }
4381 
4382 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4383   // Check test-expr for canonical form, save upper-bound UB, flags for
4384   // less/greater and for strict/non-strict comparison.
4385   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4386   //   var relational-op b
4387   //   b relational-op var
4388   //
4389   if (!S) {
4390     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
4391     return true;
4392   }
4393   S = getExprAsWritten(S);
4394   SourceLocation CondLoc = S->getLocStart();
4395   if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4396     if (BO->isRelationalOp()) {
4397       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4398         return SetUB(BO->getRHS(),
4399                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4400                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4401                      BO->getSourceRange(), BO->getOperatorLoc());
4402       if (GetInitLCDecl(BO->getRHS()) == LCDecl)
4403         return SetUB(BO->getLHS(),
4404                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4405                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4406                      BO->getSourceRange(), BO->getOperatorLoc());
4407     }
4408   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4409     if (CE->getNumArgs() == 2) {
4410       auto Op = CE->getOperator();
4411       switch (Op) {
4412       case OO_Greater:
4413       case OO_GreaterEqual:
4414       case OO_Less:
4415       case OO_LessEqual:
4416         if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4417           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4418                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4419                        CE->getOperatorLoc());
4420         if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
4421           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4422                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4423                        CE->getOperatorLoc());
4424         break;
4425       default:
4426         break;
4427       }
4428     }
4429   }
4430   if (Dependent() || SemaRef.CurContext->isDependentContext())
4431     return false;
4432   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
4433       << S->getSourceRange() << LCDecl;
4434   return true;
4435 }
4436 
4437 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4438   // RHS of canonical loop form increment can be:
4439   //   var + incr
4440   //   incr + var
4441   //   var - incr
4442   //
4443   RHS = RHS->IgnoreParenImpCasts();
4444   if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
4445     if (BO->isAdditiveOp()) {
4446       bool IsAdd = BO->getOpcode() == BO_Add;
4447       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4448         return SetStep(BO->getRHS(), !IsAdd);
4449       if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
4450         return SetStep(BO->getLHS(), false);
4451     }
4452   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4453     bool IsAdd = CE->getOperator() == OO_Plus;
4454     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
4455       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4456         return SetStep(CE->getArg(1), !IsAdd);
4457       if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
4458         return SetStep(CE->getArg(0), false);
4459     }
4460   }
4461   if (Dependent() || SemaRef.CurContext->isDependentContext())
4462     return false;
4463   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
4464       << RHS->getSourceRange() << LCDecl;
4465   return true;
4466 }
4467 
4468 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4469   // Check incr-expr for canonical loop form and return true if it
4470   // does not conform.
4471   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4472   //   ++var
4473   //   var++
4474   //   --var
4475   //   var--
4476   //   var += incr
4477   //   var -= incr
4478   //   var = var + incr
4479   //   var = incr + var
4480   //   var = var - incr
4481   //
4482   if (!S) {
4483     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
4484     return true;
4485   }
4486   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4487     if (!ExprTemp->cleanupsHaveSideEffects())
4488       S = ExprTemp->getSubExpr();
4489 
4490   IncrementSrcRange = S->getSourceRange();
4491   S = S->IgnoreParens();
4492   if (auto *UO = dyn_cast<UnaryOperator>(S)) {
4493     if (UO->isIncrementDecrementOp() &&
4494         GetInitLCDecl(UO->getSubExpr()) == LCDecl)
4495       return SetStep(SemaRef
4496                          .ActOnIntegerConstant(UO->getLocStart(),
4497                                                (UO->isDecrementOp() ? -1 : 1))
4498                          .get(),
4499                      false);
4500   } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
4501     switch (BO->getOpcode()) {
4502     case BO_AddAssign:
4503     case BO_SubAssign:
4504       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4505         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4506       break;
4507     case BO_Assign:
4508       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4509         return CheckIncRHS(BO->getRHS());
4510       break;
4511     default:
4512       break;
4513     }
4514   } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4515     switch (CE->getOperator()) {
4516     case OO_PlusPlus:
4517     case OO_MinusMinus:
4518       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4519         return SetStep(SemaRef
4520                            .ActOnIntegerConstant(
4521                                CE->getLocStart(),
4522                                ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4523                            .get(),
4524                        false);
4525       break;
4526     case OO_PlusEqual:
4527     case OO_MinusEqual:
4528       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4529         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4530       break;
4531     case OO_Equal:
4532       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4533         return CheckIncRHS(CE->getArg(1));
4534       break;
4535     default:
4536       break;
4537     }
4538   }
4539   if (Dependent() || SemaRef.CurContext->isDependentContext())
4540     return false;
4541   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
4542       << S->getSourceRange() << LCDecl;
4543   return true;
4544 }
4545 
4546 static ExprResult
4547 tryBuildCapture(Sema &SemaRef, Expr *Capture,
4548                 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4549   if (SemaRef.CurContext->isDependentContext())
4550     return ExprResult(Capture);
4551   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4552     return SemaRef.PerformImplicitConversion(
4553         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4554         /*AllowExplicit=*/true);
4555   auto I = Captures.find(Capture);
4556   if (I != Captures.end())
4557     return buildCapture(SemaRef, Capture, I->second);
4558   DeclRefExpr *Ref = nullptr;
4559   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4560   Captures[Capture] = Ref;
4561   return Res;
4562 }
4563 
4564 /// \brief Build the expression to calculate the number of iterations.
4565 Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4566     Scope *S, const bool LimitedType,
4567     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4568   ExprResult Diff;
4569   auto VarType = LCDecl->getType().getNonReferenceType();
4570   if (VarType->isIntegerType() || VarType->isPointerType() ||
4571       SemaRef.getLangOpts().CPlusPlus) {
4572     // Upper - Lower
4573     auto *UBExpr = TestIsLessOp ? UB : LB;
4574     auto *LBExpr = TestIsLessOp ? LB : UB;
4575     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4576     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
4577     if (!Upper || !Lower)
4578       return nullptr;
4579 
4580     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4581 
4582     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4583       // BuildBinOp already emitted error, this one is to point user to upper
4584       // and lower bound, and to tell what is passed to 'operator-'.
4585       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4586           << Upper->getSourceRange() << Lower->getSourceRange();
4587       return nullptr;
4588     }
4589   }
4590 
4591   if (!Diff.isUsable())
4592     return nullptr;
4593 
4594   // Upper - Lower [- 1]
4595   if (TestIsStrictOp)
4596     Diff = SemaRef.BuildBinOp(
4597         S, DefaultLoc, BO_Sub, Diff.get(),
4598         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4599   if (!Diff.isUsable())
4600     return nullptr;
4601 
4602   // Upper - Lower [- 1] + Step
4603   auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4604   if (!NewStep.isUsable())
4605     return nullptr;
4606   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
4607   if (!Diff.isUsable())
4608     return nullptr;
4609 
4610   // Parentheses (for dumping/debugging purposes only).
4611   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4612   if (!Diff.isUsable())
4613     return nullptr;
4614 
4615   // (Upper - Lower [- 1] + Step) / Step
4616   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4617   if (!Diff.isUsable())
4618     return nullptr;
4619 
4620   // OpenMP runtime requires 32-bit or 64-bit loop variables.
4621   QualType Type = Diff.get()->getType();
4622   auto &C = SemaRef.Context;
4623   bool UseVarType = VarType->hasIntegerRepresentation() &&
4624                     C.getTypeSize(Type) > C.getTypeSize(VarType);
4625   if (!Type->isIntegerType() || UseVarType) {
4626     unsigned NewSize =
4627         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4628     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4629                                : Type->hasSignedIntegerRepresentation();
4630     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
4631     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4632       Diff = SemaRef.PerformImplicitConversion(
4633           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4634       if (!Diff.isUsable())
4635         return nullptr;
4636     }
4637   }
4638   if (LimitedType) {
4639     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4640     if (NewSize != C.getTypeSize(Type)) {
4641       if (NewSize < C.getTypeSize(Type)) {
4642         assert(NewSize == 64 && "incorrect loop var size");
4643         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4644             << InitSrcRange << ConditionSrcRange;
4645       }
4646       QualType NewType = C.getIntTypeForBitwidth(
4647           NewSize, Type->hasSignedIntegerRepresentation() ||
4648                        C.getTypeSize(Type) < NewSize);
4649       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4650         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4651                                                  Sema::AA_Converting, true);
4652         if (!Diff.isUsable())
4653           return nullptr;
4654       }
4655     }
4656   }
4657 
4658   return Diff.get();
4659 }
4660 
4661 Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4662     Scope *S, Expr *Cond,
4663     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4664   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4665   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4666   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4667 
4668   auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4669   auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4670   if (!NewLB.isUsable() || !NewUB.isUsable())
4671     return nullptr;
4672 
4673   auto CondExpr = SemaRef.BuildBinOp(
4674       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4675                                   : (TestIsStrictOp ? BO_GT : BO_GE),
4676       NewLB.get(), NewUB.get());
4677   if (CondExpr.isUsable()) {
4678     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4679                                                 SemaRef.Context.BoolTy))
4680       CondExpr = SemaRef.PerformImplicitConversion(
4681           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4682           /*AllowExplicit=*/true);
4683   }
4684   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4685   // Otherwise use original loop conditon and evaluate it in runtime.
4686   return CondExpr.isUsable() ? CondExpr.get() : Cond;
4687 }
4688 
4689 /// \brief Build reference expression to the counter be used for codegen.
4690 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
4691     llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
4692   auto *VD = dyn_cast<VarDecl>(LCDecl);
4693   if (!VD) {
4694     VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4695     auto *Ref = buildDeclRefExpr(
4696         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4697     DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4698     // If the loop control decl is explicitly marked as private, do not mark it
4699     // as captured again.
4700     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4701       Captures.insert(std::make_pair(LCRef, Ref));
4702     return Ref;
4703   }
4704   return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4705                           DefaultLoc);
4706 }
4707 
4708 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
4709   if (LCDecl && !LCDecl->isInvalidDecl()) {
4710     auto Type = LCDecl->getType().getNonReferenceType();
4711     auto *PrivateVar =
4712         buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4713                      LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
4714     if (PrivateVar->isInvalidDecl())
4715       return nullptr;
4716     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4717   }
4718   return nullptr;
4719 }
4720 
4721 /// \brief Build instillation of the counter be used for codegen.
4722 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4723 
4724 /// \brief Build step of the counter be used for codegen.
4725 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4726 
4727 /// \brief Iteration space of a single for loop.
4728 struct LoopIterationSpace final {
4729   /// \brief Condition of the loop.
4730   Expr *PreCond = nullptr;
4731   /// \brief This expression calculates the number of iterations in the loop.
4732   /// It is always possible to calculate it before starting the loop.
4733   Expr *NumIterations = nullptr;
4734   /// \brief The loop counter variable.
4735   Expr *CounterVar = nullptr;
4736   /// \brief Private loop counter variable.
4737   Expr *PrivateCounterVar = nullptr;
4738   /// \brief This is initializer for the initial value of #CounterVar.
4739   Expr *CounterInit = nullptr;
4740   /// \brief This is step for the #CounterVar used to generate its update:
4741   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4742   Expr *CounterStep = nullptr;
4743   /// \brief Should step be subtracted?
4744   bool Subtract = false;
4745   /// \brief Source range of the loop init.
4746   SourceRange InitSrcRange;
4747   /// \brief Source range of the loop condition.
4748   SourceRange CondSrcRange;
4749   /// \brief Source range of the loop increment.
4750   SourceRange IncSrcRange;
4751 };
4752 
4753 } // namespace
4754 
4755 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4756   assert(getLangOpts().OpenMP && "OpenMP is not active.");
4757   assert(Init && "Expected loop in canonical form.");
4758   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4759   if (AssociatedLoops > 0 &&
4760       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4761     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4762     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4763       if (auto *D = ISC.GetLoopDecl()) {
4764         auto *VD = dyn_cast<VarDecl>(D);
4765         if (!VD) {
4766           if (auto *Private = IsOpenMPCapturedDecl(D))
4767             VD = Private;
4768           else {
4769             auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4770                                      /*WithInit=*/false);
4771             VD = cast<VarDecl>(Ref->getDecl());
4772           }
4773         }
4774         DSAStack->addLoopControlVariable(D, VD);
4775       }
4776     }
4777     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4778   }
4779 }
4780 
4781 /// \brief Called on a for stmt to check and extract its iteration space
4782 /// for further processing (such as collapsing).
4783 static bool CheckOpenMPIterationSpace(
4784     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4785     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4786     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
4787     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4788     LoopIterationSpace &ResultIterSpace,
4789     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4790   // OpenMP [2.6, Canonical Loop Form]
4791   //   for (init-expr; test-expr; incr-expr) structured-block
4792   auto *For = dyn_cast_or_null<ForStmt>(S);
4793   if (!For) {
4794     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
4795         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4796         << getOpenMPDirectiveName(DKind) << NestedLoopCount
4797         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4798     if (NestedLoopCount > 1) {
4799       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4800         SemaRef.Diag(DSA.getConstructLoc(),
4801                      diag::note_omp_collapse_ordered_expr)
4802             << 2 << CollapseLoopCountExpr->getSourceRange()
4803             << OrderedLoopCountExpr->getSourceRange();
4804       else if (CollapseLoopCountExpr)
4805         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4806                      diag::note_omp_collapse_ordered_expr)
4807             << 0 << CollapseLoopCountExpr->getSourceRange();
4808       else
4809         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4810                      diag::note_omp_collapse_ordered_expr)
4811             << 1 << OrderedLoopCountExpr->getSourceRange();
4812     }
4813     return true;
4814   }
4815   assert(For->getBody());
4816 
4817   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4818 
4819   // Check init.
4820   auto Init = For->getInit();
4821   if (ISC.CheckInit(Init))
4822     return true;
4823 
4824   bool HasErrors = false;
4825 
4826   // Check loop variable's type.
4827   if (auto *LCDecl = ISC.GetLoopDecl()) {
4828     auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
4829 
4830     // OpenMP [2.6, Canonical Loop Form]
4831     // Var is one of the following:
4832     //   A variable of signed or unsigned integer type.
4833     //   For C++, a variable of a random access iterator type.
4834     //   For C, a variable of a pointer type.
4835     auto VarType = LCDecl->getType().getNonReferenceType();
4836     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4837         !VarType->isPointerType() &&
4838         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4839       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4840           << SemaRef.getLangOpts().CPlusPlus;
4841       HasErrors = true;
4842     }
4843 
4844     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4845     // a Construct
4846     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4847     // parallel for construct is (are) private.
4848     // The loop iteration variable in the associated for-loop of a simd
4849     // construct with just one associated for-loop is linear with a
4850     // constant-linear-step that is the increment of the associated for-loop.
4851     // Exclude loop var from the list of variables with implicitly defined data
4852     // sharing attributes.
4853     VarsWithImplicitDSA.erase(LCDecl);
4854 
4855     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4856     // in a Construct, C/C++].
4857     // The loop iteration variable in the associated for-loop of a simd
4858     // construct with just one associated for-loop may be listed in a linear
4859     // clause with a constant-linear-step that is the increment of the
4860     // associated for-loop.
4861     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4862     // parallel for construct may be listed in a private or lastprivate clause.
4863     DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4864     // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4865     // declared in the loop and it is predetermined as a private.
4866     auto PredeterminedCKind =
4867         isOpenMPSimdDirective(DKind)
4868             ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4869             : OMPC_private;
4870     if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4871           DVar.CKind != PredeterminedCKind) ||
4872          ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4873            isOpenMPDistributeDirective(DKind)) &&
4874           !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4875           DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4876         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4877       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4878           << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4879           << getOpenMPClauseName(PredeterminedCKind);
4880       if (DVar.RefExpr == nullptr)
4881         DVar.CKind = PredeterminedCKind;
4882       ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4883       HasErrors = true;
4884     } else if (LoopDeclRefExpr != nullptr) {
4885       // Make the loop iteration variable private (for worksharing constructs),
4886       // linear (for simd directives with the only one associated loop) or
4887       // lastprivate (for simd directives with several collapsed or ordered
4888       // loops).
4889       if (DVar.CKind == OMPC_unknown)
4890         DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4891                           [](OpenMPDirectiveKind) -> bool { return true; },
4892                           /*FromParent=*/false);
4893       DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4894     }
4895 
4896     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4897 
4898     // Check test-expr.
4899     HasErrors |= ISC.CheckCond(For->getCond());
4900 
4901     // Check incr-expr.
4902     HasErrors |= ISC.CheckInc(For->getInc());
4903   }
4904 
4905   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4906     return HasErrors;
4907 
4908   // Build the loop's iteration space representation.
4909   ResultIterSpace.PreCond =
4910       ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4911   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
4912       DSA.getCurScope(),
4913       (isOpenMPWorksharingDirective(DKind) ||
4914        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4915       Captures);
4916   ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
4917   ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
4918   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4919   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4920   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4921   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4922   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4923   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4924 
4925   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4926                 ResultIterSpace.NumIterations == nullptr ||
4927                 ResultIterSpace.CounterVar == nullptr ||
4928                 ResultIterSpace.PrivateCounterVar == nullptr ||
4929                 ResultIterSpace.CounterInit == nullptr ||
4930                 ResultIterSpace.CounterStep == nullptr);
4931 
4932   return HasErrors;
4933 }
4934 
4935 /// \brief Build 'VarRef = Start.
4936 static ExprResult
4937 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4938                  ExprResult Start,
4939                  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4940   // Build 'VarRef = Start.
4941   auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4942   if (!NewStart.isUsable())
4943     return ExprError();
4944   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4945                                    VarRef.get()->getType())) {
4946     NewStart = SemaRef.PerformImplicitConversion(
4947         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4948         /*AllowExplicit=*/true);
4949     if (!NewStart.isUsable())
4950       return ExprError();
4951   }
4952 
4953   auto Init =
4954       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4955   return Init;
4956 }
4957 
4958 /// \brief Build 'VarRef = Start + Iter * Step'.
4959 static ExprResult
4960 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4961                    ExprResult VarRef, ExprResult Start, ExprResult Iter,
4962                    ExprResult Step, bool Subtract,
4963                    llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
4964   // Add parentheses (for debugging purposes only).
4965   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4966   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4967       !Step.isUsable())
4968     return ExprError();
4969 
4970   ExprResult NewStep = Step;
4971   if (Captures)
4972     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4973   if (NewStep.isInvalid())
4974     return ExprError();
4975   ExprResult Update =
4976       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4977   if (!Update.isUsable())
4978     return ExprError();
4979 
4980   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4981   // 'VarRef = Start (+|-) Iter * Step'.
4982   ExprResult NewStart = Start;
4983   if (Captures)
4984     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4985   if (NewStart.isInvalid())
4986     return ExprError();
4987 
4988   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4989   ExprResult SavedUpdate = Update;
4990   ExprResult UpdateVal;
4991   if (VarRef.get()->getType()->isOverloadableType() ||
4992       NewStart.get()->getType()->isOverloadableType() ||
4993       Update.get()->getType()->isOverloadableType()) {
4994     bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4995     SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4996     Update =
4997         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4998     if (Update.isUsable()) {
4999       UpdateVal =
5000           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5001                              VarRef.get(), SavedUpdate.get());
5002       if (UpdateVal.isUsable()) {
5003         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5004                                             UpdateVal.get());
5005       }
5006     }
5007     SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5008   }
5009 
5010   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5011   if (!Update.isUsable() || !UpdateVal.isUsable()) {
5012     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5013                                 NewStart.get(), SavedUpdate.get());
5014     if (!Update.isUsable())
5015       return ExprError();
5016 
5017     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5018                                      VarRef.get()->getType())) {
5019       Update = SemaRef.PerformImplicitConversion(
5020           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5021       if (!Update.isUsable())
5022         return ExprError();
5023     }
5024 
5025     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5026   }
5027   return Update;
5028 }
5029 
5030 /// \brief Convert integer expression \a E to make it have at least \a Bits
5031 /// bits.
5032 static ExprResult WidenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
5033   if (E == nullptr)
5034     return ExprError();
5035   auto &C = SemaRef.Context;
5036   QualType OldType = E->getType();
5037   unsigned HasBits = C.getTypeSize(OldType);
5038   if (HasBits >= Bits)
5039     return ExprResult(E);
5040   // OK to convert to signed, because new type has more bits than old.
5041   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5042   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5043                                            true);
5044 }
5045 
5046 /// \brief Check if the given expression \a E is a constant integer that fits
5047 /// into \a Bits bits.
5048 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
5049   if (E == nullptr)
5050     return false;
5051   llvm::APSInt Result;
5052   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5053     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5054   return false;
5055 }
5056 
5057 /// Build preinits statement for the given declarations.
5058 static Stmt *buildPreInits(ASTContext &Context,
5059                            SmallVectorImpl<Decl *> &PreInits) {
5060   if (!PreInits.empty()) {
5061     return new (Context) DeclStmt(
5062         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5063         SourceLocation(), SourceLocation());
5064   }
5065   return nullptr;
5066 }
5067 
5068 /// Build preinits statement for the given declarations.
5069 static Stmt *buildPreInits(ASTContext &Context,
5070                            llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
5071   if (!Captures.empty()) {
5072     SmallVector<Decl *, 16> PreInits;
5073     for (auto &Pair : Captures)
5074       PreInits.push_back(Pair.second->getDecl());
5075     return buildPreInits(Context, PreInits);
5076   }
5077   return nullptr;
5078 }
5079 
5080 /// Build postupdate expression for the given list of postupdates expressions.
5081 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5082   Expr *PostUpdate = nullptr;
5083   if (!PostUpdates.empty()) {
5084     for (auto *E : PostUpdates) {
5085       Expr *ConvE = S.BuildCStyleCastExpr(
5086                          E->getExprLoc(),
5087                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5088                          E->getExprLoc(), E)
5089                         .get();
5090       PostUpdate = PostUpdate
5091                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5092                                               PostUpdate, ConvE)
5093                              .get()
5094                        : ConvE;
5095     }
5096   }
5097   return PostUpdate;
5098 }
5099 
5100 /// \brief Called on a for stmt to check itself and nested loops (if any).
5101 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5102 /// number of collapsed loops otherwise.
5103 static unsigned
5104 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
5105                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5106                 DSAStackTy &DSA,
5107                 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
5108                 OMPLoopDirective::HelperExprs &Built) {
5109   unsigned NestedLoopCount = 1;
5110   if (CollapseLoopCountExpr) {
5111     // Found 'collapse' clause - calculate collapse number.
5112     llvm::APSInt Result;
5113     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
5114       NestedLoopCount = Result.getLimitedValue();
5115   }
5116   if (OrderedLoopCountExpr) {
5117     // Found 'ordered' clause - calculate collapse number.
5118     llvm::APSInt Result;
5119     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5120       if (Result.getLimitedValue() < NestedLoopCount) {
5121         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5122                      diag::err_omp_wrong_ordered_loop_count)
5123             << OrderedLoopCountExpr->getSourceRange();
5124         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5125                      diag::note_collapse_loop_count)
5126             << CollapseLoopCountExpr->getSourceRange();
5127       }
5128       NestedLoopCount = Result.getLimitedValue();
5129     }
5130   }
5131   // This is helper routine for loop directives (e.g., 'for', 'simd',
5132   // 'for simd', etc.).
5133   llvm::MapVector<Expr *, DeclRefExpr *> Captures;
5134   SmallVector<LoopIterationSpace, 4> IterSpaces;
5135   IterSpaces.resize(NestedLoopCount);
5136   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
5137   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
5138     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
5139                                   NestedLoopCount, CollapseLoopCountExpr,
5140                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
5141                                   IterSpaces[Cnt], Captures))
5142       return 0;
5143     // Move on to the next nested for loop, or to the loop body.
5144     // OpenMP [2.8.1, simd construct, Restrictions]
5145     // All loops associated with the construct must be perfectly nested; that
5146     // is, there must be no intervening code nor any OpenMP directive between
5147     // any two loops.
5148     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5149   }
5150 
5151   Built.clear(/* size */ NestedLoopCount);
5152 
5153   if (SemaRef.CurContext->isDependentContext())
5154     return NestedLoopCount;
5155 
5156   // An example of what is generated for the following code:
5157   //
5158   //   #pragma omp simd collapse(2) ordered(2)
5159   //   for (i = 0; i < NI; ++i)
5160   //     for (k = 0; k < NK; ++k)
5161   //       for (j = J0; j < NJ; j+=2) {
5162   //         <loop body>
5163   //       }
5164   //
5165   // We generate the code below.
5166   // Note: the loop body may be outlined in CodeGen.
5167   // Note: some counters may be C++ classes, operator- is used to find number of
5168   // iterations and operator+= to calculate counter value.
5169   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5170   // or i64 is currently supported).
5171   //
5172   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5173   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5174   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5175   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5176   //     // similar updates for vars in clauses (e.g. 'linear')
5177   //     <loop body (using local i and j)>
5178   //   }
5179   //   i = NI; // assign final values of counters
5180   //   j = NJ;
5181   //
5182 
5183   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5184   // the iteration counts of the collapsed for loops.
5185   // Precondition tests if there is at least one iteration (all conditions are
5186   // true).
5187   auto PreCond = ExprResult(IterSpaces[0].PreCond);
5188   auto N0 = IterSpaces[0].NumIterations;
5189   ExprResult LastIteration32 = WidenIterationCount(
5190       32 /* Bits */, SemaRef
5191                          .PerformImplicitConversion(
5192                              N0->IgnoreImpCasts(), N0->getType(),
5193                              Sema::AA_Converting, /*AllowExplicit=*/true)
5194                          .get(),
5195       SemaRef);
5196   ExprResult LastIteration64 = WidenIterationCount(
5197       64 /* Bits */, SemaRef
5198                          .PerformImplicitConversion(
5199                              N0->IgnoreImpCasts(), N0->getType(),
5200                              Sema::AA_Converting, /*AllowExplicit=*/true)
5201                          .get(),
5202       SemaRef);
5203 
5204   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5205     return NestedLoopCount;
5206 
5207   auto &C = SemaRef.Context;
5208   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5209 
5210   Scope *CurScope = DSA.getCurScope();
5211   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
5212     if (PreCond.isUsable()) {
5213       PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5214                                    PreCond.get(), IterSpaces[Cnt].PreCond);
5215     }
5216     auto N = IterSpaces[Cnt].NumIterations;
5217     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5218     if (LastIteration32.isUsable())
5219       LastIteration32 = SemaRef.BuildBinOp(
5220           CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5221           SemaRef
5222               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5223                                          Sema::AA_Converting,
5224                                          /*AllowExplicit=*/true)
5225               .get());
5226     if (LastIteration64.isUsable())
5227       LastIteration64 = SemaRef.BuildBinOp(
5228           CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5229           SemaRef
5230               .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5231                                          Sema::AA_Converting,
5232                                          /*AllowExplicit=*/true)
5233               .get());
5234   }
5235 
5236   // Choose either the 32-bit or 64-bit version.
5237   ExprResult LastIteration = LastIteration64;
5238   if (LastIteration32.isUsable() &&
5239       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5240       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5241        FitsInto(
5242            32 /* Bits */,
5243            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5244            LastIteration64.get(), SemaRef)))
5245     LastIteration = LastIteration32;
5246   QualType VType = LastIteration.get()->getType();
5247   QualType RealVType = VType;
5248   QualType StrideVType = VType;
5249   if (isOpenMPTaskLoopDirective(DKind)) {
5250     VType =
5251         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5252     StrideVType =
5253         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5254   }
5255 
5256   if (!LastIteration.isUsable())
5257     return 0;
5258 
5259   // Save the number of iterations.
5260   ExprResult NumIterations = LastIteration;
5261   {
5262     LastIteration = SemaRef.BuildBinOp(
5263         CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5264         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5265     if (!LastIteration.isUsable())
5266       return 0;
5267   }
5268 
5269   // Calculate the last iteration number beforehand instead of doing this on
5270   // each iteration. Do not do this if the number of iterations may be kfold-ed.
5271   llvm::APSInt Result;
5272   bool IsConstant =
5273       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5274   ExprResult CalcLastIteration;
5275   if (!IsConstant) {
5276     ExprResult SaveRef =
5277         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
5278     LastIteration = SaveRef;
5279 
5280     // Prepare SaveRef + 1.
5281     NumIterations = SemaRef.BuildBinOp(
5282         CurScope, SourceLocation(), BO_Add, SaveRef.get(),
5283         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5284     if (!NumIterations.isUsable())
5285       return 0;
5286   }
5287 
5288   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5289 
5290   // Build variables passed into runtime, necessary for worksharing directives.
5291   ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
5292   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5293       isOpenMPDistributeDirective(DKind)) {
5294     // Lower bound variable, initialized with zero.
5295     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5296     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
5297     SemaRef.AddInitializerToDecl(
5298         LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5299         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5300 
5301     // Upper bound variable, initialized with last iteration number.
5302     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5303     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
5304     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5305                                  /*DirectInit*/ false,
5306                                  /*TypeMayContainAuto*/ false);
5307 
5308     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5309     // This will be used to implement clause 'lastprivate'.
5310     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
5311     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5312     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
5313     SemaRef.AddInitializerToDecl(
5314         ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5315         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5316 
5317     // Stride variable returned by runtime (we initialize it to 1 by default).
5318     VarDecl *STDecl =
5319         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5320     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
5321     SemaRef.AddInitializerToDecl(
5322         STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5323         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5324 
5325     // Build expression: UB = min(UB, LastIteration)
5326     // It is necessary for CodeGen of directives with static scheduling.
5327     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5328                                                 UB.get(), LastIteration.get());
5329     ExprResult CondOp = SemaRef.ActOnConditionalOp(
5330         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5331     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5332                              CondOp.get());
5333     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
5334 
5335     // If we have a combined directive that combines 'distribute', 'for' or
5336     // 'simd' we need to be able to access the bounds of the schedule of the
5337     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5338     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5339     if (isOpenMPLoopBoundSharingDirective(DKind)) {
5340       auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5341 
5342       // We expect to have at least 2 more parameters than the 'parallel'
5343       // directive does - the lower and upper bounds of the previous schedule.
5344       assert(CD->getNumParams() >= 4 &&
5345              "Unexpected number of parameters in loop combined directive");
5346 
5347       // Set the proper type for the bounds given what we learned from the
5348       // enclosed loops.
5349       auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5350       auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5351 
5352       // Previous lower and upper bounds are obtained from the region
5353       // parameters.
5354       PrevLB =
5355           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5356       PrevUB =
5357           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5358     }
5359   }
5360 
5361   // Build the iteration variable and its initialization before loop.
5362   ExprResult IV;
5363   ExprResult Init;
5364   {
5365     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5366     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
5367     Expr *RHS =
5368         (isOpenMPWorksharingDirective(DKind) ||
5369          isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5370             ? LB.get()
5371             : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5372     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5373     Init = SemaRef.ActOnFinishFullExpr(Init.get());
5374   }
5375 
5376   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
5377   SourceLocation CondLoc;
5378   ExprResult Cond =
5379       (isOpenMPWorksharingDirective(DKind) ||
5380        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5381           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5382           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5383                                NumIterations.get());
5384 
5385   // Loop increment (IV = IV + 1)
5386   SourceLocation IncLoc;
5387   ExprResult Inc =
5388       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5389                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5390   if (!Inc.isUsable())
5391     return 0;
5392   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
5393   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5394   if (!Inc.isUsable())
5395     return 0;
5396 
5397   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5398   // Used for directives with static scheduling.
5399   ExprResult NextLB, NextUB;
5400   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5401       isOpenMPDistributeDirective(DKind)) {
5402     // LB + ST
5403     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5404     if (!NextLB.isUsable())
5405       return 0;
5406     // LB = LB + ST
5407     NextLB =
5408         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5409     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5410     if (!NextLB.isUsable())
5411       return 0;
5412     // UB + ST
5413     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5414     if (!NextUB.isUsable())
5415       return 0;
5416     // UB = UB + ST
5417     NextUB =
5418         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5419     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5420     if (!NextUB.isUsable())
5421       return 0;
5422   }
5423 
5424   // Build updates and final values of the loop counters.
5425   bool HasErrors = false;
5426   Built.Counters.resize(NestedLoopCount);
5427   Built.Inits.resize(NestedLoopCount);
5428   Built.Updates.resize(NestedLoopCount);
5429   Built.Finals.resize(NestedLoopCount);
5430   SmallVector<Expr *, 4> LoopMultipliers;
5431   {
5432     ExprResult Div;
5433     // Go from inner nested loop to outer.
5434     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5435       LoopIterationSpace &IS = IterSpaces[Cnt];
5436       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5437       // Build: Iter = (IV / Div) % IS.NumIters
5438       // where Div is product of previous iterations' IS.NumIters.
5439       ExprResult Iter;
5440       if (Div.isUsable()) {
5441         Iter =
5442             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5443       } else {
5444         Iter = IV;
5445         assert((Cnt == (int)NestedLoopCount - 1) &&
5446                "unusable div expected on first iteration only");
5447       }
5448 
5449       if (Cnt != 0 && Iter.isUsable())
5450         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5451                                   IS.NumIterations);
5452       if (!Iter.isUsable()) {
5453         HasErrors = true;
5454         break;
5455       }
5456 
5457       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
5458       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5459       auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5460                                           IS.CounterVar->getExprLoc(),
5461                                           /*RefersToCapture=*/true);
5462       ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
5463                                          IS.CounterInit, Captures);
5464       if (!Init.isUsable()) {
5465         HasErrors = true;
5466         break;
5467       }
5468       ExprResult Update = BuildCounterUpdate(
5469           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5470           IS.CounterStep, IS.Subtract, &Captures);
5471       if (!Update.isUsable()) {
5472         HasErrors = true;
5473         break;
5474       }
5475 
5476       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5477       ExprResult Final = BuildCounterUpdate(
5478           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
5479           IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
5480       if (!Final.isUsable()) {
5481         HasErrors = true;
5482         break;
5483       }
5484 
5485       // Build Div for the next iteration: Div <- Div * IS.NumIters
5486       if (Cnt != 0) {
5487         if (Div.isUnset())
5488           Div = IS.NumIterations;
5489         else
5490           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5491                                    IS.NumIterations);
5492 
5493         // Add parentheses (for debugging purposes only).
5494         if (Div.isUsable())
5495           Div = tryBuildCapture(SemaRef, Div.get(), Captures);
5496         if (!Div.isUsable()) {
5497           HasErrors = true;
5498           break;
5499         }
5500         LoopMultipliers.push_back(Div.get());
5501       }
5502       if (!Update.isUsable() || !Final.isUsable()) {
5503         HasErrors = true;
5504         break;
5505       }
5506       // Save results
5507       Built.Counters[Cnt] = IS.CounterVar;
5508       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
5509       Built.Inits[Cnt] = Init.get();
5510       Built.Updates[Cnt] = Update.get();
5511       Built.Finals[Cnt] = Final.get();
5512     }
5513   }
5514 
5515   if (HasErrors)
5516     return 0;
5517 
5518   // Save results
5519   Built.IterationVarRef = IV.get();
5520   Built.LastIteration = LastIteration.get();
5521   Built.NumIterations = NumIterations.get();
5522   Built.CalcLastIteration =
5523       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
5524   Built.PreCond = PreCond.get();
5525   Built.PreInits = buildPreInits(C, Captures);
5526   Built.Cond = Cond.get();
5527   Built.Init = Init.get();
5528   Built.Inc = Inc.get();
5529   Built.LB = LB.get();
5530   Built.UB = UB.get();
5531   Built.IL = IL.get();
5532   Built.ST = ST.get();
5533   Built.EUB = EUB.get();
5534   Built.NLB = NextLB.get();
5535   Built.NUB = NextUB.get();
5536   Built.PrevLB = PrevLB.get();
5537   Built.PrevUB = PrevUB.get();
5538 
5539   Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5540   // Fill data for doacross depend clauses.
5541   for (auto Pair : DSA.getDoacrossDependClauses()) {
5542     if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5543       Pair.first->setCounterValue(CounterVal);
5544     else {
5545       if (NestedLoopCount != Pair.second.size() ||
5546           NestedLoopCount != LoopMultipliers.size() + 1) {
5547         // Erroneous case - clause has some problems.
5548         Pair.first->setCounterValue(CounterVal);
5549         continue;
5550       }
5551       assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5552       auto I = Pair.second.rbegin();
5553       auto IS = IterSpaces.rbegin();
5554       auto ILM = LoopMultipliers.rbegin();
5555       Expr *UpCounterVal = CounterVal;
5556       Expr *Multiplier = nullptr;
5557       for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5558         if (I->first) {
5559           assert(IS->CounterStep);
5560           Expr *NormalizedOffset =
5561               SemaRef
5562                   .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5563                               I->first, IS->CounterStep)
5564                   .get();
5565           if (Multiplier) {
5566             NormalizedOffset =
5567                 SemaRef
5568                     .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5569                                 NormalizedOffset, Multiplier)
5570                     .get();
5571           }
5572           assert(I->second == OO_Plus || I->second == OO_Minus);
5573           BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5574           UpCounterVal = SemaRef
5575                              .BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5576                                          UpCounterVal, NormalizedOffset)
5577                              .get();
5578         }
5579         Multiplier = *ILM;
5580         ++I;
5581         ++IS;
5582         ++ILM;
5583       }
5584       Pair.first->setCounterValue(UpCounterVal);
5585     }
5586   }
5587 
5588   return NestedLoopCount;
5589 }
5590 
5591 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
5592   auto CollapseClauses =
5593       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5594   if (CollapseClauses.begin() != CollapseClauses.end())
5595     return (*CollapseClauses.begin())->getNumForLoops();
5596   return nullptr;
5597 }
5598 
5599 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
5600   auto OrderedClauses =
5601       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5602   if (OrderedClauses.begin() != OrderedClauses.end())
5603     return (*OrderedClauses.begin())->getNumForLoops();
5604   return nullptr;
5605 }
5606 
5607 static bool checkSimdlenSafelenSpecified(Sema &S,
5608                                          const ArrayRef<OMPClause *> Clauses) {
5609   OMPSafelenClause *Safelen = nullptr;
5610   OMPSimdlenClause *Simdlen = nullptr;
5611 
5612   for (auto *Clause : Clauses) {
5613     if (Clause->getClauseKind() == OMPC_safelen)
5614       Safelen = cast<OMPSafelenClause>(Clause);
5615     else if (Clause->getClauseKind() == OMPC_simdlen)
5616       Simdlen = cast<OMPSimdlenClause>(Clause);
5617     if (Safelen && Simdlen)
5618       break;
5619   }
5620 
5621   if (Simdlen && Safelen) {
5622     llvm::APSInt SimdlenRes, SafelenRes;
5623     auto SimdlenLength = Simdlen->getSimdlen();
5624     auto SafelenLength = Safelen->getSafelen();
5625     if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5626         SimdlenLength->isInstantiationDependent() ||
5627         SimdlenLength->containsUnexpandedParameterPack())
5628       return false;
5629     if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5630         SafelenLength->isInstantiationDependent() ||
5631         SafelenLength->containsUnexpandedParameterPack())
5632       return false;
5633     SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5634     SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5635     // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5636     // If both simdlen and safelen clauses are specified, the value of the
5637     // simdlen parameter must be less than or equal to the value of the safelen
5638     // parameter.
5639     if (SimdlenRes > SafelenRes) {
5640       S.Diag(SimdlenLength->getExprLoc(),
5641              diag::err_omp_wrong_simdlen_safelen_values)
5642           << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5643       return true;
5644     }
5645   }
5646   return false;
5647 }
5648 
5649 StmtResult Sema::ActOnOpenMPSimdDirective(
5650     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5651     SourceLocation EndLoc,
5652     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5653   if (!AStmt)
5654     return StmtError();
5655 
5656   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5657   OMPLoopDirective::HelperExprs B;
5658   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5659   // define the nested loops number.
5660   unsigned NestedLoopCount = CheckOpenMPLoop(
5661       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5662       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5663   if (NestedLoopCount == 0)
5664     return StmtError();
5665 
5666   assert((CurContext->isDependentContext() || B.builtAll()) &&
5667          "omp simd loop exprs were not built");
5668 
5669   if (!CurContext->isDependentContext()) {
5670     // Finalize the clauses that need pre-built expressions for CodeGen.
5671     for (auto C : Clauses) {
5672       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5673         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5674                                      B.NumIterations, *this, CurScope,
5675                                      DSAStack))
5676           return StmtError();
5677     }
5678   }
5679 
5680   if (checkSimdlenSafelenSpecified(*this, Clauses))
5681     return StmtError();
5682 
5683   getCurFunction()->setHasBranchProtectedScope();
5684   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5685                                   Clauses, AStmt, B);
5686 }
5687 
5688 StmtResult Sema::ActOnOpenMPForDirective(
5689     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5690     SourceLocation EndLoc,
5691     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5692   if (!AStmt)
5693     return StmtError();
5694 
5695   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5696   OMPLoopDirective::HelperExprs B;
5697   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5698   // define the nested loops number.
5699   unsigned NestedLoopCount = CheckOpenMPLoop(
5700       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5701       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5702   if (NestedLoopCount == 0)
5703     return StmtError();
5704 
5705   assert((CurContext->isDependentContext() || B.builtAll()) &&
5706          "omp for loop exprs were not built");
5707 
5708   if (!CurContext->isDependentContext()) {
5709     // Finalize the clauses that need pre-built expressions for CodeGen.
5710     for (auto C : Clauses) {
5711       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5712         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5713                                      B.NumIterations, *this, CurScope,
5714                                      DSAStack))
5715           return StmtError();
5716     }
5717   }
5718 
5719   getCurFunction()->setHasBranchProtectedScope();
5720   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5721                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
5722 }
5723 
5724 StmtResult Sema::ActOnOpenMPForSimdDirective(
5725     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5726     SourceLocation EndLoc,
5727     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5728   if (!AStmt)
5729     return StmtError();
5730 
5731   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5732   OMPLoopDirective::HelperExprs B;
5733   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5734   // define the nested loops number.
5735   unsigned NestedLoopCount =
5736       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5737                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5738                       VarsWithImplicitDSA, B);
5739   if (NestedLoopCount == 0)
5740     return StmtError();
5741 
5742   assert((CurContext->isDependentContext() || B.builtAll()) &&
5743          "omp for simd loop exprs were not built");
5744 
5745   if (!CurContext->isDependentContext()) {
5746     // Finalize the clauses that need pre-built expressions for CodeGen.
5747     for (auto C : Clauses) {
5748       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5749         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5750                                      B.NumIterations, *this, CurScope,
5751                                      DSAStack))
5752           return StmtError();
5753     }
5754   }
5755 
5756   if (checkSimdlenSafelenSpecified(*this, Clauses))
5757     return StmtError();
5758 
5759   getCurFunction()->setHasBranchProtectedScope();
5760   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5761                                      Clauses, AStmt, B);
5762 }
5763 
5764 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5765                                               Stmt *AStmt,
5766                                               SourceLocation StartLoc,
5767                                               SourceLocation EndLoc) {
5768   if (!AStmt)
5769     return StmtError();
5770 
5771   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5772   auto BaseStmt = AStmt;
5773   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5774     BaseStmt = CS->getCapturedStmt();
5775   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5776     auto S = C->children();
5777     if (S.begin() == S.end())
5778       return StmtError();
5779     // All associated statements must be '#pragma omp section' except for
5780     // the first one.
5781     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5782       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5783         if (SectionStmt)
5784           Diag(SectionStmt->getLocStart(),
5785                diag::err_omp_sections_substmt_not_section);
5786         return StmtError();
5787       }
5788       cast<OMPSectionDirective>(SectionStmt)
5789           ->setHasCancel(DSAStack->isCancelRegion());
5790     }
5791   } else {
5792     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5793     return StmtError();
5794   }
5795 
5796   getCurFunction()->setHasBranchProtectedScope();
5797 
5798   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5799                                       DSAStack->isCancelRegion());
5800 }
5801 
5802 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5803                                              SourceLocation StartLoc,
5804                                              SourceLocation EndLoc) {
5805   if (!AStmt)
5806     return StmtError();
5807 
5808   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5809 
5810   getCurFunction()->setHasBranchProtectedScope();
5811   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5812 
5813   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5814                                      DSAStack->isCancelRegion());
5815 }
5816 
5817 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5818                                             Stmt *AStmt,
5819                                             SourceLocation StartLoc,
5820                                             SourceLocation EndLoc) {
5821   if (!AStmt)
5822     return StmtError();
5823 
5824   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5825 
5826   getCurFunction()->setHasBranchProtectedScope();
5827 
5828   // OpenMP [2.7.3, single Construct, Restrictions]
5829   // The copyprivate clause must not be used with the nowait clause.
5830   OMPClause *Nowait = nullptr;
5831   OMPClause *Copyprivate = nullptr;
5832   for (auto *Clause : Clauses) {
5833     if (Clause->getClauseKind() == OMPC_nowait)
5834       Nowait = Clause;
5835     else if (Clause->getClauseKind() == OMPC_copyprivate)
5836       Copyprivate = Clause;
5837     if (Copyprivate && Nowait) {
5838       Diag(Copyprivate->getLocStart(),
5839            diag::err_omp_single_copyprivate_with_nowait);
5840       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5841       return StmtError();
5842     }
5843   }
5844 
5845   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5846 }
5847 
5848 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5849                                             SourceLocation StartLoc,
5850                                             SourceLocation EndLoc) {
5851   if (!AStmt)
5852     return StmtError();
5853 
5854   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5855 
5856   getCurFunction()->setHasBranchProtectedScope();
5857 
5858   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5859 }
5860 
5861 StmtResult Sema::ActOnOpenMPCriticalDirective(
5862     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5863     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5864   if (!AStmt)
5865     return StmtError();
5866 
5867   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5868 
5869   bool ErrorFound = false;
5870   llvm::APSInt Hint;
5871   SourceLocation HintLoc;
5872   bool DependentHint = false;
5873   for (auto *C : Clauses) {
5874     if (C->getClauseKind() == OMPC_hint) {
5875       if (!DirName.getName()) {
5876         Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5877         ErrorFound = true;
5878       }
5879       Expr *E = cast<OMPHintClause>(C)->getHint();
5880       if (E->isTypeDependent() || E->isValueDependent() ||
5881           E->isInstantiationDependent())
5882         DependentHint = true;
5883       else {
5884         Hint = E->EvaluateKnownConstInt(Context);
5885         HintLoc = C->getLocStart();
5886       }
5887     }
5888   }
5889   if (ErrorFound)
5890     return StmtError();
5891   auto Pair = DSAStack->getCriticalWithHint(DirName);
5892   if (Pair.first && DirName.getName() && !DependentHint) {
5893     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5894       Diag(StartLoc, diag::err_omp_critical_with_hint);
5895       if (HintLoc.isValid()) {
5896         Diag(HintLoc, diag::note_omp_critical_hint_here)
5897             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5898       } else
5899         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5900       if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5901         Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5902             << 1
5903             << C->getHint()->EvaluateKnownConstInt(Context).toString(
5904                    /*Radix=*/10, /*Signed=*/false);
5905       } else
5906         Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5907     }
5908   }
5909 
5910   getCurFunction()->setHasBranchProtectedScope();
5911 
5912   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5913                                            Clauses, AStmt);
5914   if (!Pair.first && DirName.getName() && !DependentHint)
5915     DSAStack->addCriticalWithHint(Dir, Hint);
5916   return Dir;
5917 }
5918 
5919 StmtResult Sema::ActOnOpenMPParallelForDirective(
5920     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5921     SourceLocation EndLoc,
5922     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5923   if (!AStmt)
5924     return StmtError();
5925 
5926   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5927   // 1.2.2 OpenMP Language Terminology
5928   // Structured block - An executable statement with a single entry at the
5929   // top and a single exit at the bottom.
5930   // The point of exit cannot be a branch out of the structured block.
5931   // longjmp() and throw() must not violate the entry/exit criteria.
5932   CS->getCapturedDecl()->setNothrow();
5933 
5934   OMPLoopDirective::HelperExprs B;
5935   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5936   // define the nested loops number.
5937   unsigned NestedLoopCount =
5938       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5939                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5940                       VarsWithImplicitDSA, B);
5941   if (NestedLoopCount == 0)
5942     return StmtError();
5943 
5944   assert((CurContext->isDependentContext() || B.builtAll()) &&
5945          "omp parallel for loop exprs were not built");
5946 
5947   if (!CurContext->isDependentContext()) {
5948     // Finalize the clauses that need pre-built expressions for CodeGen.
5949     for (auto C : Clauses) {
5950       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5951         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5952                                      B.NumIterations, *this, CurScope,
5953                                      DSAStack))
5954           return StmtError();
5955     }
5956   }
5957 
5958   getCurFunction()->setHasBranchProtectedScope();
5959   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5960                                          NestedLoopCount, Clauses, AStmt, B,
5961                                          DSAStack->isCancelRegion());
5962 }
5963 
5964 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5965     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5966     SourceLocation EndLoc,
5967     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5968   if (!AStmt)
5969     return StmtError();
5970 
5971   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5972   // 1.2.2 OpenMP Language Terminology
5973   // Structured block - An executable statement with a single entry at the
5974   // top and a single exit at the bottom.
5975   // The point of exit cannot be a branch out of the structured block.
5976   // longjmp() and throw() must not violate the entry/exit criteria.
5977   CS->getCapturedDecl()->setNothrow();
5978 
5979   OMPLoopDirective::HelperExprs B;
5980   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5981   // define the nested loops number.
5982   unsigned NestedLoopCount =
5983       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5984                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5985                       VarsWithImplicitDSA, B);
5986   if (NestedLoopCount == 0)
5987     return StmtError();
5988 
5989   if (!CurContext->isDependentContext()) {
5990     // Finalize the clauses that need pre-built expressions for CodeGen.
5991     for (auto C : Clauses) {
5992       if (auto *LC = dyn_cast<OMPLinearClause>(C))
5993         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5994                                      B.NumIterations, *this, CurScope,
5995                                      DSAStack))
5996           return StmtError();
5997     }
5998   }
5999 
6000   if (checkSimdlenSafelenSpecified(*this, Clauses))
6001     return StmtError();
6002 
6003   getCurFunction()->setHasBranchProtectedScope();
6004   return OMPParallelForSimdDirective::Create(
6005       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
6006 }
6007 
6008 StmtResult
6009 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6010                                            Stmt *AStmt, SourceLocation StartLoc,
6011                                            SourceLocation EndLoc) {
6012   if (!AStmt)
6013     return StmtError();
6014 
6015   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6016   auto BaseStmt = AStmt;
6017   while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
6018     BaseStmt = CS->getCapturedStmt();
6019   if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
6020     auto S = C->children();
6021     if (S.begin() == S.end())
6022       return StmtError();
6023     // All associated statements must be '#pragma omp section' except for
6024     // the first one.
6025     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
6026       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6027         if (SectionStmt)
6028           Diag(SectionStmt->getLocStart(),
6029                diag::err_omp_parallel_sections_substmt_not_section);
6030         return StmtError();
6031       }
6032       cast<OMPSectionDirective>(SectionStmt)
6033           ->setHasCancel(DSAStack->isCancelRegion());
6034     }
6035   } else {
6036     Diag(AStmt->getLocStart(),
6037          diag::err_omp_parallel_sections_not_compound_stmt);
6038     return StmtError();
6039   }
6040 
6041   getCurFunction()->setHasBranchProtectedScope();
6042 
6043   return OMPParallelSectionsDirective::Create(
6044       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
6045 }
6046 
6047 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6048                                           Stmt *AStmt, SourceLocation StartLoc,
6049                                           SourceLocation EndLoc) {
6050   if (!AStmt)
6051     return StmtError();
6052 
6053   auto *CS = cast<CapturedStmt>(AStmt);
6054   // 1.2.2 OpenMP Language Terminology
6055   // Structured block - An executable statement with a single entry at the
6056   // top and a single exit at the bottom.
6057   // The point of exit cannot be a branch out of the structured block.
6058   // longjmp() and throw() must not violate the entry/exit criteria.
6059   CS->getCapturedDecl()->setNothrow();
6060 
6061   getCurFunction()->setHasBranchProtectedScope();
6062 
6063   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6064                                   DSAStack->isCancelRegion());
6065 }
6066 
6067 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6068                                                SourceLocation EndLoc) {
6069   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6070 }
6071 
6072 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6073                                              SourceLocation EndLoc) {
6074   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6075 }
6076 
6077 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6078                                               SourceLocation EndLoc) {
6079   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6080 }
6081 
6082 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
6083                                                SourceLocation StartLoc,
6084                                                SourceLocation EndLoc) {
6085   if (!AStmt)
6086     return StmtError();
6087 
6088   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6089 
6090   getCurFunction()->setHasBranchProtectedScope();
6091 
6092   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
6093 }
6094 
6095 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6096                                            SourceLocation StartLoc,
6097                                            SourceLocation EndLoc) {
6098   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6099   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6100 }
6101 
6102 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6103                                              Stmt *AStmt,
6104                                              SourceLocation StartLoc,
6105                                              SourceLocation EndLoc) {
6106   OMPClause *DependFound = nullptr;
6107   OMPClause *DependSourceClause = nullptr;
6108   OMPClause *DependSinkClause = nullptr;
6109   bool ErrorFound = false;
6110   OMPThreadsClause *TC = nullptr;
6111   OMPSIMDClause *SC = nullptr;
6112   for (auto *C : Clauses) {
6113     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6114       DependFound = C;
6115       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6116         if (DependSourceClause) {
6117           Diag(C->getLocStart(), diag::err_omp_more_one_clause)
6118               << getOpenMPDirectiveName(OMPD_ordered)
6119               << getOpenMPClauseName(OMPC_depend) << 2;
6120           ErrorFound = true;
6121         } else
6122           DependSourceClause = C;
6123         if (DependSinkClause) {
6124           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6125               << 0;
6126           ErrorFound = true;
6127         }
6128       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6129         if (DependSourceClause) {
6130           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
6131               << 1;
6132           ErrorFound = true;
6133         }
6134         DependSinkClause = C;
6135       }
6136     } else if (C->getClauseKind() == OMPC_threads)
6137       TC = cast<OMPThreadsClause>(C);
6138     else if (C->getClauseKind() == OMPC_simd)
6139       SC = cast<OMPSIMDClause>(C);
6140   }
6141   if (!ErrorFound && !SC &&
6142       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
6143     // OpenMP [2.8.1,simd Construct, Restrictions]
6144     // An ordered construct with the simd clause is the only OpenMP construct
6145     // that can appear in the simd region.
6146     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
6147     ErrorFound = true;
6148   } else if (DependFound && (TC || SC)) {
6149     Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
6150         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6151     ErrorFound = true;
6152   } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6153     Diag(DependFound->getLocStart(),
6154          diag::err_omp_ordered_directive_without_param);
6155     ErrorFound = true;
6156   } else if (TC || Clauses.empty()) {
6157     if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6158       SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6159       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6160           << (TC != nullptr);
6161       Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6162       ErrorFound = true;
6163     }
6164   }
6165   if ((!AStmt && !DependFound) || ErrorFound)
6166     return StmtError();
6167 
6168   if (AStmt) {
6169     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6170 
6171     getCurFunction()->setHasBranchProtectedScope();
6172   }
6173 
6174   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6175 }
6176 
6177 namespace {
6178 /// \brief Helper class for checking expression in 'omp atomic [update]'
6179 /// construct.
6180 class OpenMPAtomicUpdateChecker {
6181   /// \brief Error results for atomic update expressions.
6182   enum ExprAnalysisErrorCode {
6183     /// \brief A statement is not an expression statement.
6184     NotAnExpression,
6185     /// \brief Expression is not builtin binary or unary operation.
6186     NotABinaryOrUnaryExpression,
6187     /// \brief Unary operation is not post-/pre- increment/decrement operation.
6188     NotAnUnaryIncDecExpression,
6189     /// \brief An expression is not of scalar type.
6190     NotAScalarType,
6191     /// \brief A binary operation is not an assignment operation.
6192     NotAnAssignmentOp,
6193     /// \brief RHS part of the binary operation is not a binary expression.
6194     NotABinaryExpression,
6195     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6196     /// expression.
6197     NotABinaryOperator,
6198     /// \brief RHS binary operation does not have reference to the updated LHS
6199     /// part.
6200     NotAnUpdateExpression,
6201     /// \brief No errors is found.
6202     NoError
6203   };
6204   /// \brief Reference to Sema.
6205   Sema &SemaRef;
6206   /// \brief A location for note diagnostics (when error is found).
6207   SourceLocation NoteLoc;
6208   /// \brief 'x' lvalue part of the source atomic expression.
6209   Expr *X;
6210   /// \brief 'expr' rvalue part of the source atomic expression.
6211   Expr *E;
6212   /// \brief Helper expression of the form
6213   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6214   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6215   Expr *UpdateExpr;
6216   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6217   /// important for non-associative operations.
6218   bool IsXLHSInRHSPart;
6219   BinaryOperatorKind Op;
6220   SourceLocation OpLoc;
6221   /// \brief true if the source expression is a postfix unary operation, false
6222   /// if it is a prefix unary operation.
6223   bool IsPostfixUpdate;
6224 
6225 public:
6226   OpenMPAtomicUpdateChecker(Sema &SemaRef)
6227       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
6228         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
6229   /// \brief Check specified statement that it is suitable for 'atomic update'
6230   /// constructs and extract 'x', 'expr' and Operation from the original
6231   /// expression. If DiagId and NoteId == 0, then only check is performed
6232   /// without error notification.
6233   /// \param DiagId Diagnostic which should be emitted if error is found.
6234   /// \param NoteId Diagnostic note for the main error message.
6235   /// \return true if statement is not an update expression, false otherwise.
6236   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
6237   /// \brief Return the 'x' lvalue part of the source atomic expression.
6238   Expr *getX() const { return X; }
6239   /// \brief Return the 'expr' rvalue part of the source atomic expression.
6240   Expr *getExpr() const { return E; }
6241   /// \brief Return the update expression used in calculation of the updated
6242   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6243   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6244   Expr *getUpdateExpr() const { return UpdateExpr; }
6245   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6246   /// false otherwise.
6247   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6248 
6249   /// \brief true if the source expression is a postfix unary operation, false
6250   /// if it is a prefix unary operation.
6251   bool isPostfixUpdate() const { return IsPostfixUpdate; }
6252 
6253 private:
6254   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6255                             unsigned NoteId = 0);
6256 };
6257 } // namespace
6258 
6259 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6260     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6261   ExprAnalysisErrorCode ErrorFound = NoError;
6262   SourceLocation ErrorLoc, NoteLoc;
6263   SourceRange ErrorRange, NoteRange;
6264   // Allowed constructs are:
6265   //  x = x binop expr;
6266   //  x = expr binop x;
6267   if (AtomicBinOp->getOpcode() == BO_Assign) {
6268     X = AtomicBinOp->getLHS();
6269     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6270             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6271       if (AtomicInnerBinOp->isMultiplicativeOp() ||
6272           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6273           AtomicInnerBinOp->isBitwiseOp()) {
6274         Op = AtomicInnerBinOp->getOpcode();
6275         OpLoc = AtomicInnerBinOp->getOperatorLoc();
6276         auto *LHS = AtomicInnerBinOp->getLHS();
6277         auto *RHS = AtomicInnerBinOp->getRHS();
6278         llvm::FoldingSetNodeID XId, LHSId, RHSId;
6279         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6280                                           /*Canonical=*/true);
6281         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6282                                             /*Canonical=*/true);
6283         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6284                                             /*Canonical=*/true);
6285         if (XId == LHSId) {
6286           E = RHS;
6287           IsXLHSInRHSPart = true;
6288         } else if (XId == RHSId) {
6289           E = LHS;
6290           IsXLHSInRHSPart = false;
6291         } else {
6292           ErrorLoc = AtomicInnerBinOp->getExprLoc();
6293           ErrorRange = AtomicInnerBinOp->getSourceRange();
6294           NoteLoc = X->getExprLoc();
6295           NoteRange = X->getSourceRange();
6296           ErrorFound = NotAnUpdateExpression;
6297         }
6298       } else {
6299         ErrorLoc = AtomicInnerBinOp->getExprLoc();
6300         ErrorRange = AtomicInnerBinOp->getSourceRange();
6301         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6302         NoteRange = SourceRange(NoteLoc, NoteLoc);
6303         ErrorFound = NotABinaryOperator;
6304       }
6305     } else {
6306       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6307       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6308       ErrorFound = NotABinaryExpression;
6309     }
6310   } else {
6311     ErrorLoc = AtomicBinOp->getExprLoc();
6312     ErrorRange = AtomicBinOp->getSourceRange();
6313     NoteLoc = AtomicBinOp->getOperatorLoc();
6314     NoteRange = SourceRange(NoteLoc, NoteLoc);
6315     ErrorFound = NotAnAssignmentOp;
6316   }
6317   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6318     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6319     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6320     return true;
6321   } else if (SemaRef.CurContext->isDependentContext())
6322     E = X = UpdateExpr = nullptr;
6323   return ErrorFound != NoError;
6324 }
6325 
6326 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6327                                                unsigned NoteId) {
6328   ExprAnalysisErrorCode ErrorFound = NoError;
6329   SourceLocation ErrorLoc, NoteLoc;
6330   SourceRange ErrorRange, NoteRange;
6331   // Allowed constructs are:
6332   //  x++;
6333   //  x--;
6334   //  ++x;
6335   //  --x;
6336   //  x binop= expr;
6337   //  x = x binop expr;
6338   //  x = expr binop x;
6339   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6340     AtomicBody = AtomicBody->IgnoreParenImpCasts();
6341     if (AtomicBody->getType()->isScalarType() ||
6342         AtomicBody->isInstantiationDependent()) {
6343       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6344               AtomicBody->IgnoreParenImpCasts())) {
6345         // Check for Compound Assignment Operation
6346         Op = BinaryOperator::getOpForCompoundAssignment(
6347             AtomicCompAssignOp->getOpcode());
6348         OpLoc = AtomicCompAssignOp->getOperatorLoc();
6349         E = AtomicCompAssignOp->getRHS();
6350         X = AtomicCompAssignOp->getLHS()->IgnoreParens();
6351         IsXLHSInRHSPart = true;
6352       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6353                      AtomicBody->IgnoreParenImpCasts())) {
6354         // Check for Binary Operation
6355         if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6356           return true;
6357       } else if (auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
6358                      AtomicBody->IgnoreParenImpCasts())) {
6359         // Check for Unary Operation
6360         if (AtomicUnaryOp->isIncrementDecrementOp()) {
6361           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
6362           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6363           OpLoc = AtomicUnaryOp->getOperatorLoc();
6364           X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
6365           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6366           IsXLHSInRHSPart = true;
6367         } else {
6368           ErrorFound = NotAnUnaryIncDecExpression;
6369           ErrorLoc = AtomicUnaryOp->getExprLoc();
6370           ErrorRange = AtomicUnaryOp->getSourceRange();
6371           NoteLoc = AtomicUnaryOp->getOperatorLoc();
6372           NoteRange = SourceRange(NoteLoc, NoteLoc);
6373         }
6374       } else if (!AtomicBody->isInstantiationDependent()) {
6375         ErrorFound = NotABinaryOrUnaryExpression;
6376         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6377         NoteRange = ErrorRange = AtomicBody->getSourceRange();
6378       }
6379     } else {
6380       ErrorFound = NotAScalarType;
6381       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6382       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6383     }
6384   } else {
6385     ErrorFound = NotAnExpression;
6386     NoteLoc = ErrorLoc = S->getLocStart();
6387     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6388   }
6389   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6390     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6391     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6392     return true;
6393   } else if (SemaRef.CurContext->isDependentContext())
6394     E = X = UpdateExpr = nullptr;
6395   if (ErrorFound == NoError && E && X) {
6396     // Build an update expression of form 'OpaqueValueExpr(x) binop
6397     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6398     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6399     auto *OVEX = new (SemaRef.getASTContext())
6400         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6401     auto *OVEExpr = new (SemaRef.getASTContext())
6402         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6403     auto Update =
6404         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6405                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
6406     if (Update.isInvalid())
6407       return true;
6408     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6409                                                Sema::AA_Casting);
6410     if (Update.isInvalid())
6411       return true;
6412     UpdateExpr = Update.get();
6413   }
6414   return ErrorFound != NoError;
6415 }
6416 
6417 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6418                                             Stmt *AStmt,
6419                                             SourceLocation StartLoc,
6420                                             SourceLocation EndLoc) {
6421   if (!AStmt)
6422     return StmtError();
6423 
6424   auto *CS = cast<CapturedStmt>(AStmt);
6425   // 1.2.2 OpenMP Language Terminology
6426   // Structured block - An executable statement with a single entry at the
6427   // top and a single exit at the bottom.
6428   // The point of exit cannot be a branch out of the structured block.
6429   // longjmp() and throw() must not violate the entry/exit criteria.
6430   OpenMPClauseKind AtomicKind = OMPC_unknown;
6431   SourceLocation AtomicKindLoc;
6432   for (auto *C : Clauses) {
6433     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
6434         C->getClauseKind() == OMPC_update ||
6435         C->getClauseKind() == OMPC_capture) {
6436       if (AtomicKind != OMPC_unknown) {
6437         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6438             << SourceRange(C->getLocStart(), C->getLocEnd());
6439         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6440             << getOpenMPClauseName(AtomicKind);
6441       } else {
6442         AtomicKind = C->getClauseKind();
6443         AtomicKindLoc = C->getLocStart();
6444       }
6445     }
6446   }
6447 
6448   auto Body = CS->getCapturedStmt();
6449   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6450     Body = EWC->getSubExpr();
6451 
6452   Expr *X = nullptr;
6453   Expr *V = nullptr;
6454   Expr *E = nullptr;
6455   Expr *UE = nullptr;
6456   bool IsXLHSInRHSPart = false;
6457   bool IsPostfixUpdate = false;
6458   // OpenMP [2.12.6, atomic Construct]
6459   // In the next expressions:
6460   // * x and v (as applicable) are both l-value expressions with scalar type.
6461   // * During the execution of an atomic region, multiple syntactic
6462   // occurrences of x must designate the same storage location.
6463   // * Neither of v and expr (as applicable) may access the storage location
6464   // designated by x.
6465   // * Neither of x and expr (as applicable) may access the storage location
6466   // designated by v.
6467   // * expr is an expression with scalar type.
6468   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6469   // * binop, binop=, ++, and -- are not overloaded operators.
6470   // * The expression x binop expr must be numerically equivalent to x binop
6471   // (expr). This requirement is satisfied if the operators in expr have
6472   // precedence greater than binop, or by using parentheses around expr or
6473   // subexpressions of expr.
6474   // * The expression expr binop x must be numerically equivalent to (expr)
6475   // binop x. This requirement is satisfied if the operators in expr have
6476   // precedence equal to or greater than binop, or by using parentheses around
6477   // expr or subexpressions of expr.
6478   // * For forms that allow multiple occurrences of x, the number of times
6479   // that x is evaluated is unspecified.
6480   if (AtomicKind == OMPC_read) {
6481     enum {
6482       NotAnExpression,
6483       NotAnAssignmentOp,
6484       NotAScalarType,
6485       NotAnLValue,
6486       NoError
6487     } ErrorFound = NoError;
6488     SourceLocation ErrorLoc, NoteLoc;
6489     SourceRange ErrorRange, NoteRange;
6490     // If clause is read:
6491     //  v = x;
6492     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6493       auto *AtomicBinOp =
6494           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6495       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6496         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6497         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6498         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6499             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6500           if (!X->isLValue() || !V->isLValue()) {
6501             auto NotLValueExpr = X->isLValue() ? V : X;
6502             ErrorFound = NotAnLValue;
6503             ErrorLoc = AtomicBinOp->getExprLoc();
6504             ErrorRange = AtomicBinOp->getSourceRange();
6505             NoteLoc = NotLValueExpr->getExprLoc();
6506             NoteRange = NotLValueExpr->getSourceRange();
6507           }
6508         } else if (!X->isInstantiationDependent() ||
6509                    !V->isInstantiationDependent()) {
6510           auto NotScalarExpr =
6511               (X->isInstantiationDependent() || X->getType()->isScalarType())
6512                   ? V
6513                   : X;
6514           ErrorFound = NotAScalarType;
6515           ErrorLoc = AtomicBinOp->getExprLoc();
6516           ErrorRange = AtomicBinOp->getSourceRange();
6517           NoteLoc = NotScalarExpr->getExprLoc();
6518           NoteRange = NotScalarExpr->getSourceRange();
6519         }
6520       } else if (!AtomicBody->isInstantiationDependent()) {
6521         ErrorFound = NotAnAssignmentOp;
6522         ErrorLoc = AtomicBody->getExprLoc();
6523         ErrorRange = AtomicBody->getSourceRange();
6524         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6525                               : AtomicBody->getExprLoc();
6526         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6527                                 : AtomicBody->getSourceRange();
6528       }
6529     } else {
6530       ErrorFound = NotAnExpression;
6531       NoteLoc = ErrorLoc = Body->getLocStart();
6532       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6533     }
6534     if (ErrorFound != NoError) {
6535       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6536           << ErrorRange;
6537       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6538                                                       << NoteRange;
6539       return StmtError();
6540     } else if (CurContext->isDependentContext())
6541       V = X = nullptr;
6542   } else if (AtomicKind == OMPC_write) {
6543     enum {
6544       NotAnExpression,
6545       NotAnAssignmentOp,
6546       NotAScalarType,
6547       NotAnLValue,
6548       NoError
6549     } ErrorFound = NoError;
6550     SourceLocation ErrorLoc, NoteLoc;
6551     SourceRange ErrorRange, NoteRange;
6552     // If clause is write:
6553     //  x = expr;
6554     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6555       auto *AtomicBinOp =
6556           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6557       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6558         X = AtomicBinOp->getLHS();
6559         E = AtomicBinOp->getRHS();
6560         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6561             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6562           if (!X->isLValue()) {
6563             ErrorFound = NotAnLValue;
6564             ErrorLoc = AtomicBinOp->getExprLoc();
6565             ErrorRange = AtomicBinOp->getSourceRange();
6566             NoteLoc = X->getExprLoc();
6567             NoteRange = X->getSourceRange();
6568           }
6569         } else if (!X->isInstantiationDependent() ||
6570                    !E->isInstantiationDependent()) {
6571           auto NotScalarExpr =
6572               (X->isInstantiationDependent() || X->getType()->isScalarType())
6573                   ? E
6574                   : X;
6575           ErrorFound = NotAScalarType;
6576           ErrorLoc = AtomicBinOp->getExprLoc();
6577           ErrorRange = AtomicBinOp->getSourceRange();
6578           NoteLoc = NotScalarExpr->getExprLoc();
6579           NoteRange = NotScalarExpr->getSourceRange();
6580         }
6581       } else if (!AtomicBody->isInstantiationDependent()) {
6582         ErrorFound = NotAnAssignmentOp;
6583         ErrorLoc = AtomicBody->getExprLoc();
6584         ErrorRange = AtomicBody->getSourceRange();
6585         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6586                               : AtomicBody->getExprLoc();
6587         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6588                                 : AtomicBody->getSourceRange();
6589       }
6590     } else {
6591       ErrorFound = NotAnExpression;
6592       NoteLoc = ErrorLoc = Body->getLocStart();
6593       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6594     }
6595     if (ErrorFound != NoError) {
6596       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6597           << ErrorRange;
6598       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6599                                                       << NoteRange;
6600       return StmtError();
6601     } else if (CurContext->isDependentContext())
6602       E = X = nullptr;
6603   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
6604     // If clause is update:
6605     //  x++;
6606     //  x--;
6607     //  ++x;
6608     //  --x;
6609     //  x binop= expr;
6610     //  x = x binop expr;
6611     //  x = expr binop x;
6612     OpenMPAtomicUpdateChecker Checker(*this);
6613     if (Checker.checkStatement(
6614             Body, (AtomicKind == OMPC_update)
6615                       ? diag::err_omp_atomic_update_not_expression_statement
6616                       : diag::err_omp_atomic_not_expression_statement,
6617             diag::note_omp_atomic_update))
6618       return StmtError();
6619     if (!CurContext->isDependentContext()) {
6620       E = Checker.getExpr();
6621       X = Checker.getX();
6622       UE = Checker.getUpdateExpr();
6623       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6624     }
6625   } else if (AtomicKind == OMPC_capture) {
6626     enum {
6627       NotAnAssignmentOp,
6628       NotACompoundStatement,
6629       NotTwoSubstatements,
6630       NotASpecificExpression,
6631       NoError
6632     } ErrorFound = NoError;
6633     SourceLocation ErrorLoc, NoteLoc;
6634     SourceRange ErrorRange, NoteRange;
6635     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6636       // If clause is a capture:
6637       //  v = x++;
6638       //  v = x--;
6639       //  v = ++x;
6640       //  v = --x;
6641       //  v = x binop= expr;
6642       //  v = x = x binop expr;
6643       //  v = x = expr binop x;
6644       auto *AtomicBinOp =
6645           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6646       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6647         V = AtomicBinOp->getLHS();
6648         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6649         OpenMPAtomicUpdateChecker Checker(*this);
6650         if (Checker.checkStatement(
6651                 Body, diag::err_omp_atomic_capture_not_expression_statement,
6652                 diag::note_omp_atomic_update))
6653           return StmtError();
6654         E = Checker.getExpr();
6655         X = Checker.getX();
6656         UE = Checker.getUpdateExpr();
6657         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6658         IsPostfixUpdate = Checker.isPostfixUpdate();
6659       } else if (!AtomicBody->isInstantiationDependent()) {
6660         ErrorLoc = AtomicBody->getExprLoc();
6661         ErrorRange = AtomicBody->getSourceRange();
6662         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6663                               : AtomicBody->getExprLoc();
6664         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6665                                 : AtomicBody->getSourceRange();
6666         ErrorFound = NotAnAssignmentOp;
6667       }
6668       if (ErrorFound != NoError) {
6669         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6670             << ErrorRange;
6671         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6672         return StmtError();
6673       } else if (CurContext->isDependentContext()) {
6674         UE = V = E = X = nullptr;
6675       }
6676     } else {
6677       // If clause is a capture:
6678       //  { v = x; x = expr; }
6679       //  { v = x; x++; }
6680       //  { v = x; x--; }
6681       //  { v = x; ++x; }
6682       //  { v = x; --x; }
6683       //  { v = x; x binop= expr; }
6684       //  { v = x; x = x binop expr; }
6685       //  { v = x; x = expr binop x; }
6686       //  { x++; v = x; }
6687       //  { x--; v = x; }
6688       //  { ++x; v = x; }
6689       //  { --x; v = x; }
6690       //  { x binop= expr; v = x; }
6691       //  { x = x binop expr; v = x; }
6692       //  { x = expr binop x; v = x; }
6693       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6694         // Check that this is { expr1; expr2; }
6695         if (CS->size() == 2) {
6696           auto *First = CS->body_front();
6697           auto *Second = CS->body_back();
6698           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6699             First = EWC->getSubExpr()->IgnoreParenImpCasts();
6700           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6701             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6702           // Need to find what subexpression is 'v' and what is 'x'.
6703           OpenMPAtomicUpdateChecker Checker(*this);
6704           bool IsUpdateExprFound = !Checker.checkStatement(Second);
6705           BinaryOperator *BinOp = nullptr;
6706           if (IsUpdateExprFound) {
6707             BinOp = dyn_cast<BinaryOperator>(First);
6708             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6709           }
6710           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6711             //  { v = x; x++; }
6712             //  { v = x; x--; }
6713             //  { v = x; ++x; }
6714             //  { v = x; --x; }
6715             //  { v = x; x binop= expr; }
6716             //  { v = x; x = x binop expr; }
6717             //  { v = x; x = expr binop x; }
6718             // Check that the first expression has form v = x.
6719             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6720             llvm::FoldingSetNodeID XId, PossibleXId;
6721             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6722             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6723             IsUpdateExprFound = XId == PossibleXId;
6724             if (IsUpdateExprFound) {
6725               V = BinOp->getLHS();
6726               X = Checker.getX();
6727               E = Checker.getExpr();
6728               UE = Checker.getUpdateExpr();
6729               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6730               IsPostfixUpdate = true;
6731             }
6732           }
6733           if (!IsUpdateExprFound) {
6734             IsUpdateExprFound = !Checker.checkStatement(First);
6735             BinOp = nullptr;
6736             if (IsUpdateExprFound) {
6737               BinOp = dyn_cast<BinaryOperator>(Second);
6738               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6739             }
6740             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6741               //  { x++; v = x; }
6742               //  { x--; v = x; }
6743               //  { ++x; v = x; }
6744               //  { --x; v = x; }
6745               //  { x binop= expr; v = x; }
6746               //  { x = x binop expr; v = x; }
6747               //  { x = expr binop x; v = x; }
6748               // Check that the second expression has form v = x.
6749               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6750               llvm::FoldingSetNodeID XId, PossibleXId;
6751               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6752               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6753               IsUpdateExprFound = XId == PossibleXId;
6754               if (IsUpdateExprFound) {
6755                 V = BinOp->getLHS();
6756                 X = Checker.getX();
6757                 E = Checker.getExpr();
6758                 UE = Checker.getUpdateExpr();
6759                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6760                 IsPostfixUpdate = false;
6761               }
6762             }
6763           }
6764           if (!IsUpdateExprFound) {
6765             //  { v = x; x = expr; }
6766             auto *FirstExpr = dyn_cast<Expr>(First);
6767             auto *SecondExpr = dyn_cast<Expr>(Second);
6768             if (!FirstExpr || !SecondExpr ||
6769                 !(FirstExpr->isInstantiationDependent() ||
6770                   SecondExpr->isInstantiationDependent())) {
6771               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6772               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
6773                 ErrorFound = NotAnAssignmentOp;
6774                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6775                                                 : First->getLocStart();
6776                 NoteRange = ErrorRange = FirstBinOp
6777                                              ? FirstBinOp->getSourceRange()
6778                                              : SourceRange(ErrorLoc, ErrorLoc);
6779               } else {
6780                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6781                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6782                   ErrorFound = NotAnAssignmentOp;
6783                   NoteLoc = ErrorLoc = SecondBinOp
6784                                            ? SecondBinOp->getOperatorLoc()
6785                                            : Second->getLocStart();
6786                   NoteRange = ErrorRange =
6787                       SecondBinOp ? SecondBinOp->getSourceRange()
6788                                   : SourceRange(ErrorLoc, ErrorLoc);
6789                 } else {
6790                   auto *PossibleXRHSInFirst =
6791                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
6792                   auto *PossibleXLHSInSecond =
6793                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
6794                   llvm::FoldingSetNodeID X1Id, X2Id;
6795                   PossibleXRHSInFirst->Profile(X1Id, Context,
6796                                                /*Canonical=*/true);
6797                   PossibleXLHSInSecond->Profile(X2Id, Context,
6798                                                 /*Canonical=*/true);
6799                   IsUpdateExprFound = X1Id == X2Id;
6800                   if (IsUpdateExprFound) {
6801                     V = FirstBinOp->getLHS();
6802                     X = SecondBinOp->getLHS();
6803                     E = SecondBinOp->getRHS();
6804                     UE = nullptr;
6805                     IsXLHSInRHSPart = false;
6806                     IsPostfixUpdate = true;
6807                   } else {
6808                     ErrorFound = NotASpecificExpression;
6809                     ErrorLoc = FirstBinOp->getExprLoc();
6810                     ErrorRange = FirstBinOp->getSourceRange();
6811                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6812                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
6813                   }
6814                 }
6815               }
6816             }
6817           }
6818         } else {
6819           NoteLoc = ErrorLoc = Body->getLocStart();
6820           NoteRange = ErrorRange =
6821               SourceRange(Body->getLocStart(), Body->getLocStart());
6822           ErrorFound = NotTwoSubstatements;
6823         }
6824       } else {
6825         NoteLoc = ErrorLoc = Body->getLocStart();
6826         NoteRange = ErrorRange =
6827             SourceRange(Body->getLocStart(), Body->getLocStart());
6828         ErrorFound = NotACompoundStatement;
6829       }
6830       if (ErrorFound != NoError) {
6831         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6832             << ErrorRange;
6833         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6834         return StmtError();
6835       } else if (CurContext->isDependentContext()) {
6836         UE = V = E = X = nullptr;
6837       }
6838     }
6839   }
6840 
6841   getCurFunction()->setHasBranchProtectedScope();
6842 
6843   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6844                                     X, V, E, UE, IsXLHSInRHSPart,
6845                                     IsPostfixUpdate);
6846 }
6847 
6848 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6849                                             Stmt *AStmt,
6850                                             SourceLocation StartLoc,
6851                                             SourceLocation EndLoc) {
6852   if (!AStmt)
6853     return StmtError();
6854 
6855   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6856   // 1.2.2 OpenMP Language Terminology
6857   // Structured block - An executable statement with a single entry at the
6858   // top and a single exit at the bottom.
6859   // The point of exit cannot be a branch out of the structured block.
6860   // longjmp() and throw() must not violate the entry/exit criteria.
6861   CS->getCapturedDecl()->setNothrow();
6862 
6863   // OpenMP [2.16, Nesting of Regions]
6864   // If specified, a teams construct must be contained within a target
6865   // construct. That target construct must contain no statements or directives
6866   // outside of the teams construct.
6867   if (DSAStack->hasInnerTeamsRegion()) {
6868     auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6869     bool OMPTeamsFound = true;
6870     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6871       auto I = CS->body_begin();
6872       while (I != CS->body_end()) {
6873         auto *OED = dyn_cast<OMPExecutableDirective>(*I);
6874         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6875           OMPTeamsFound = false;
6876           break;
6877         }
6878         ++I;
6879       }
6880       assert(I != CS->body_end() && "Not found statement");
6881       S = *I;
6882     } else {
6883       auto *OED = dyn_cast<OMPExecutableDirective>(S);
6884       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
6885     }
6886     if (!OMPTeamsFound) {
6887       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6888       Diag(DSAStack->getInnerTeamsRegionLoc(),
6889            diag::note_omp_nested_teams_construct_here);
6890       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6891           << isa<OMPExecutableDirective>(S);
6892       return StmtError();
6893     }
6894   }
6895 
6896   getCurFunction()->setHasBranchProtectedScope();
6897 
6898   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6899 }
6900 
6901 StmtResult
6902 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6903                                          Stmt *AStmt, SourceLocation StartLoc,
6904                                          SourceLocation EndLoc) {
6905   if (!AStmt)
6906     return StmtError();
6907 
6908   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6909   // 1.2.2 OpenMP Language Terminology
6910   // Structured block - An executable statement with a single entry at the
6911   // top and a single exit at the bottom.
6912   // The point of exit cannot be a branch out of the structured block.
6913   // longjmp() and throw() must not violate the entry/exit criteria.
6914   CS->getCapturedDecl()->setNothrow();
6915 
6916   getCurFunction()->setHasBranchProtectedScope();
6917 
6918   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6919                                             AStmt);
6920 }
6921 
6922 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6923     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6924     SourceLocation EndLoc,
6925     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6926   if (!AStmt)
6927     return StmtError();
6928 
6929   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6930   // 1.2.2 OpenMP Language Terminology
6931   // Structured block - An executable statement with a single entry at the
6932   // top and a single exit at the bottom.
6933   // The point of exit cannot be a branch out of the structured block.
6934   // longjmp() and throw() must not violate the entry/exit criteria.
6935   CS->getCapturedDecl()->setNothrow();
6936 
6937   OMPLoopDirective::HelperExprs B;
6938   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6939   // define the nested loops number.
6940   unsigned NestedLoopCount =
6941       CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6942                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6943                       VarsWithImplicitDSA, B);
6944   if (NestedLoopCount == 0)
6945     return StmtError();
6946 
6947   assert((CurContext->isDependentContext() || B.builtAll()) &&
6948          "omp target parallel for loop exprs were not built");
6949 
6950   if (!CurContext->isDependentContext()) {
6951     // Finalize the clauses that need pre-built expressions for CodeGen.
6952     for (auto C : Clauses) {
6953       if (auto *LC = dyn_cast<OMPLinearClause>(C))
6954         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6955                                      B.NumIterations, *this, CurScope,
6956                                      DSAStack))
6957           return StmtError();
6958     }
6959   }
6960 
6961   getCurFunction()->setHasBranchProtectedScope();
6962   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6963                                                NestedLoopCount, Clauses, AStmt,
6964                                                B, DSAStack->isCancelRegion());
6965 }
6966 
6967 /// \brief Check for existence of a map clause in the list of clauses.
6968 static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6969   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6970        I != E; ++I) {
6971     if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6972       return true;
6973     }
6974   }
6975 
6976   return false;
6977 }
6978 
6979 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6980                                                 Stmt *AStmt,
6981                                                 SourceLocation StartLoc,
6982                                                 SourceLocation EndLoc) {
6983   if (!AStmt)
6984     return StmtError();
6985 
6986   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6987 
6988   // OpenMP [2.10.1, Restrictions, p. 97]
6989   // At least one map clause must appear on the directive.
6990   if (!HasMapClause(Clauses)) {
6991     Diag(StartLoc, diag::err_omp_no_map_for_directive)
6992         << getOpenMPDirectiveName(OMPD_target_data);
6993     return StmtError();
6994   }
6995 
6996   getCurFunction()->setHasBranchProtectedScope();
6997 
6998   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6999                                         AStmt);
7000 }
7001 
7002 StmtResult
7003 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7004                                           SourceLocation StartLoc,
7005                                           SourceLocation EndLoc) {
7006   // OpenMP [2.10.2, Restrictions, p. 99]
7007   // At least one map clause must appear on the directive.
7008   if (!HasMapClause(Clauses)) {
7009     Diag(StartLoc, diag::err_omp_no_map_for_directive)
7010         << getOpenMPDirectiveName(OMPD_target_enter_data);
7011     return StmtError();
7012   }
7013 
7014   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
7015                                              Clauses);
7016 }
7017 
7018 StmtResult
7019 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7020                                          SourceLocation StartLoc,
7021                                          SourceLocation EndLoc) {
7022   // OpenMP [2.10.3, Restrictions, p. 102]
7023   // At least one map clause must appear on the directive.
7024   if (!HasMapClause(Clauses)) {
7025     Diag(StartLoc, diag::err_omp_no_map_for_directive)
7026         << getOpenMPDirectiveName(OMPD_target_exit_data);
7027     return StmtError();
7028   }
7029 
7030   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
7031 }
7032 
7033 StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7034                                                   SourceLocation StartLoc,
7035                                                   SourceLocation EndLoc) {
7036   bool seenMotionClause = false;
7037   for (auto *C : Clauses) {
7038     if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
7039       seenMotionClause = true;
7040   }
7041   if (!seenMotionClause) {
7042     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7043     return StmtError();
7044   }
7045   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
7046 }
7047 
7048 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7049                                            Stmt *AStmt, SourceLocation StartLoc,
7050                                            SourceLocation EndLoc) {
7051   if (!AStmt)
7052     return StmtError();
7053 
7054   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7055   // 1.2.2 OpenMP Language Terminology
7056   // Structured block - An executable statement with a single entry at the
7057   // top and a single exit at the bottom.
7058   // The point of exit cannot be a branch out of the structured block.
7059   // longjmp() and throw() must not violate the entry/exit criteria.
7060   CS->getCapturedDecl()->setNothrow();
7061 
7062   getCurFunction()->setHasBranchProtectedScope();
7063 
7064   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7065 }
7066 
7067 StmtResult
7068 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7069                                             SourceLocation EndLoc,
7070                                             OpenMPDirectiveKind CancelRegion) {
7071   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7072       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7073     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7074         << getOpenMPDirectiveName(CancelRegion);
7075     return StmtError();
7076   }
7077   if (DSAStack->isParentNowaitRegion()) {
7078     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7079     return StmtError();
7080   }
7081   if (DSAStack->isParentOrderedRegion()) {
7082     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7083     return StmtError();
7084   }
7085   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7086                                                CancelRegion);
7087 }
7088 
7089 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7090                                             SourceLocation StartLoc,
7091                                             SourceLocation EndLoc,
7092                                             OpenMPDirectiveKind CancelRegion) {
7093   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
7094       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
7095     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
7096         << getOpenMPDirectiveName(CancelRegion);
7097     return StmtError();
7098   }
7099   if (DSAStack->isParentNowaitRegion()) {
7100     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7101     return StmtError();
7102   }
7103   if (DSAStack->isParentOrderedRegion()) {
7104     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7105     return StmtError();
7106   }
7107   DSAStack->setParentCancelRegion(/*Cancel=*/true);
7108   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7109                                     CancelRegion);
7110 }
7111 
7112 static bool checkGrainsizeNumTasksClauses(Sema &S,
7113                                           ArrayRef<OMPClause *> Clauses) {
7114   OMPClause *PrevClause = nullptr;
7115   bool ErrorFound = false;
7116   for (auto *C : Clauses) {
7117     if (C->getClauseKind() == OMPC_grainsize ||
7118         C->getClauseKind() == OMPC_num_tasks) {
7119       if (!PrevClause)
7120         PrevClause = C;
7121       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
7122         S.Diag(C->getLocStart(),
7123                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7124             << getOpenMPClauseName(C->getClauseKind())
7125             << getOpenMPClauseName(PrevClause->getClauseKind());
7126         S.Diag(PrevClause->getLocStart(),
7127                diag::note_omp_previous_grainsize_num_tasks)
7128             << getOpenMPClauseName(PrevClause->getClauseKind());
7129         ErrorFound = true;
7130       }
7131     }
7132   }
7133   return ErrorFound;
7134 }
7135 
7136 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7137     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7138     SourceLocation EndLoc,
7139     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7140   if (!AStmt)
7141     return StmtError();
7142 
7143   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7144   OMPLoopDirective::HelperExprs B;
7145   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7146   // define the nested loops number.
7147   unsigned NestedLoopCount =
7148       CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
7149                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7150                       VarsWithImplicitDSA, B);
7151   if (NestedLoopCount == 0)
7152     return StmtError();
7153 
7154   assert((CurContext->isDependentContext() || B.builtAll()) &&
7155          "omp for loop exprs were not built");
7156 
7157   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7158   // The grainsize clause and num_tasks clause are mutually exclusive and may
7159   // not appear on the same taskloop directive.
7160   if (checkGrainsizeNumTasksClauses(*this, Clauses))
7161     return StmtError();
7162 
7163   getCurFunction()->setHasBranchProtectedScope();
7164   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7165                                       NestedLoopCount, Clauses, AStmt, B);
7166 }
7167 
7168 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7169     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7170     SourceLocation EndLoc,
7171     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7172   if (!AStmt)
7173     return StmtError();
7174 
7175   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7176   OMPLoopDirective::HelperExprs B;
7177   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7178   // define the nested loops number.
7179   unsigned NestedLoopCount =
7180       CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7181                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7182                       VarsWithImplicitDSA, B);
7183   if (NestedLoopCount == 0)
7184     return StmtError();
7185 
7186   assert((CurContext->isDependentContext() || B.builtAll()) &&
7187          "omp for loop exprs were not built");
7188 
7189   if (!CurContext->isDependentContext()) {
7190     // Finalize the clauses that need pre-built expressions for CodeGen.
7191     for (auto C : Clauses) {
7192       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7193         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7194                                      B.NumIterations, *this, CurScope,
7195                                      DSAStack))
7196           return StmtError();
7197     }
7198   }
7199 
7200   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7201   // The grainsize clause and num_tasks clause are mutually exclusive and may
7202   // not appear on the same taskloop directive.
7203   if (checkGrainsizeNumTasksClauses(*this, Clauses))
7204     return StmtError();
7205 
7206   getCurFunction()->setHasBranchProtectedScope();
7207   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7208                                           NestedLoopCount, Clauses, AStmt, B);
7209 }
7210 
7211 StmtResult Sema::ActOnOpenMPDistributeDirective(
7212     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7213     SourceLocation EndLoc,
7214     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7215   if (!AStmt)
7216     return StmtError();
7217 
7218   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7219   OMPLoopDirective::HelperExprs B;
7220   // In presence of clause 'collapse' with number of loops, it will
7221   // define the nested loops number.
7222   unsigned NestedLoopCount =
7223       CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7224                       nullptr /*ordered not a clause on distribute*/, AStmt,
7225                       *this, *DSAStack, VarsWithImplicitDSA, B);
7226   if (NestedLoopCount == 0)
7227     return StmtError();
7228 
7229   assert((CurContext->isDependentContext() || B.builtAll()) &&
7230          "omp for loop exprs were not built");
7231 
7232   getCurFunction()->setHasBranchProtectedScope();
7233   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7234                                         NestedLoopCount, Clauses, AStmt, B);
7235 }
7236 
7237 StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7238     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7239     SourceLocation EndLoc,
7240     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7241   if (!AStmt)
7242     return StmtError();
7243 
7244   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7245   // 1.2.2 OpenMP Language Terminology
7246   // Structured block - An executable statement with a single entry at the
7247   // top and a single exit at the bottom.
7248   // The point of exit cannot be a branch out of the structured block.
7249   // longjmp() and throw() must not violate the entry/exit criteria.
7250   CS->getCapturedDecl()->setNothrow();
7251 
7252   OMPLoopDirective::HelperExprs B;
7253   // In presence of clause 'collapse' with number of loops, it will
7254   // define the nested loops number.
7255   unsigned NestedLoopCount = CheckOpenMPLoop(
7256       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7257       nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7258       VarsWithImplicitDSA, B);
7259   if (NestedLoopCount == 0)
7260     return StmtError();
7261 
7262   assert((CurContext->isDependentContext() || B.builtAll()) &&
7263          "omp for loop exprs were not built");
7264 
7265   getCurFunction()->setHasBranchProtectedScope();
7266   return OMPDistributeParallelForDirective::Create(
7267       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7268 }
7269 
7270 StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7271     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7272     SourceLocation EndLoc,
7273     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7274   if (!AStmt)
7275     return StmtError();
7276 
7277   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7278   // 1.2.2 OpenMP Language Terminology
7279   // Structured block - An executable statement with a single entry at the
7280   // top and a single exit at the bottom.
7281   // The point of exit cannot be a branch out of the structured block.
7282   // longjmp() and throw() must not violate the entry/exit criteria.
7283   CS->getCapturedDecl()->setNothrow();
7284 
7285   OMPLoopDirective::HelperExprs B;
7286   // In presence of clause 'collapse' with number of loops, it will
7287   // define the nested loops number.
7288   unsigned NestedLoopCount = CheckOpenMPLoop(
7289       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7290       nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7291       VarsWithImplicitDSA, B);
7292   if (NestedLoopCount == 0)
7293     return StmtError();
7294 
7295   assert((CurContext->isDependentContext() || B.builtAll()) &&
7296          "omp for loop exprs were not built");
7297 
7298   if (checkSimdlenSafelenSpecified(*this, Clauses))
7299     return StmtError();
7300 
7301   getCurFunction()->setHasBranchProtectedScope();
7302   return OMPDistributeParallelForSimdDirective::Create(
7303       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7304 }
7305 
7306 StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7307     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7308     SourceLocation EndLoc,
7309     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7310   if (!AStmt)
7311     return StmtError();
7312 
7313   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7314   // 1.2.2 OpenMP Language Terminology
7315   // Structured block - An executable statement with a single entry at the
7316   // top and a single exit at the bottom.
7317   // The point of exit cannot be a branch out of the structured block.
7318   // longjmp() and throw() must not violate the entry/exit criteria.
7319   CS->getCapturedDecl()->setNothrow();
7320 
7321   OMPLoopDirective::HelperExprs B;
7322   // In presence of clause 'collapse' with number of loops, it will
7323   // define the nested loops number.
7324   unsigned NestedLoopCount =
7325       CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7326                       nullptr /*ordered not a clause on distribute*/, AStmt,
7327                       *this, *DSAStack, VarsWithImplicitDSA, B);
7328   if (NestedLoopCount == 0)
7329     return StmtError();
7330 
7331   assert((CurContext->isDependentContext() || B.builtAll()) &&
7332          "omp for loop exprs were not built");
7333 
7334   if (checkSimdlenSafelenSpecified(*this, Clauses))
7335     return StmtError();
7336 
7337   getCurFunction()->setHasBranchProtectedScope();
7338   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7339                                             NestedLoopCount, Clauses, AStmt, B);
7340 }
7341 
7342 StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7343     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7344     SourceLocation EndLoc,
7345     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7346   if (!AStmt)
7347     return StmtError();
7348 
7349   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7350   // 1.2.2 OpenMP Language Terminology
7351   // Structured block - An executable statement with a single entry at the
7352   // top and a single exit at the bottom.
7353   // The point of exit cannot be a branch out of the structured block.
7354   // longjmp() and throw() must not violate the entry/exit criteria.
7355   CS->getCapturedDecl()->setNothrow();
7356 
7357   OMPLoopDirective::HelperExprs B;
7358   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7359   // define the nested loops number.
7360   unsigned NestedLoopCount = CheckOpenMPLoop(
7361       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7362       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7363       VarsWithImplicitDSA, B);
7364   if (NestedLoopCount == 0)
7365     return StmtError();
7366 
7367   assert((CurContext->isDependentContext() || B.builtAll()) &&
7368          "omp target parallel for simd loop exprs were not built");
7369 
7370   if (!CurContext->isDependentContext()) {
7371     // Finalize the clauses that need pre-built expressions for CodeGen.
7372     for (auto C : Clauses) {
7373       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7374         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7375                                      B.NumIterations, *this, CurScope,
7376                                      DSAStack))
7377           return StmtError();
7378     }
7379   }
7380   if (checkSimdlenSafelenSpecified(*this, Clauses))
7381     return StmtError();
7382 
7383   getCurFunction()->setHasBranchProtectedScope();
7384   return OMPTargetParallelForSimdDirective::Create(
7385       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7386 }
7387 
7388 StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7389     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7390     SourceLocation EndLoc,
7391     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7392   if (!AStmt)
7393     return StmtError();
7394 
7395   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7396   // 1.2.2 OpenMP Language Terminology
7397   // Structured block - An executable statement with a single entry at the
7398   // top and a single exit at the bottom.
7399   // The point of exit cannot be a branch out of the structured block.
7400   // longjmp() and throw() must not violate the entry/exit criteria.
7401   CS->getCapturedDecl()->setNothrow();
7402 
7403   OMPLoopDirective::HelperExprs B;
7404   // In presence of clause 'collapse' with number of loops, it will define the
7405   // nested loops number.
7406   unsigned NestedLoopCount =
7407       CheckOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
7408                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7409                       VarsWithImplicitDSA, B);
7410   if (NestedLoopCount == 0)
7411     return StmtError();
7412 
7413   assert((CurContext->isDependentContext() || B.builtAll()) &&
7414          "omp target simd loop exprs were not built");
7415 
7416   if (!CurContext->isDependentContext()) {
7417     // Finalize the clauses that need pre-built expressions for CodeGen.
7418     for (auto C : Clauses) {
7419       if (auto *LC = dyn_cast<OMPLinearClause>(C))
7420         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7421                                      B.NumIterations, *this, CurScope,
7422                                      DSAStack))
7423           return StmtError();
7424     }
7425   }
7426 
7427   if (checkSimdlenSafelenSpecified(*this, Clauses))
7428     return StmtError();
7429 
7430   getCurFunction()->setHasBranchProtectedScope();
7431   return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7432                                         NestedLoopCount, Clauses, AStmt, B);
7433 }
7434 
7435 StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7436     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7437     SourceLocation EndLoc,
7438     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7439   if (!AStmt)
7440     return StmtError();
7441 
7442   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7443   // 1.2.2 OpenMP Language Terminology
7444   // Structured block - An executable statement with a single entry at the
7445   // top and a single exit at the bottom.
7446   // The point of exit cannot be a branch out of the structured block.
7447   // longjmp() and throw() must not violate the entry/exit criteria.
7448   CS->getCapturedDecl()->setNothrow();
7449 
7450   OMPLoopDirective::HelperExprs B;
7451   // In presence of clause 'collapse' with number of loops, it will
7452   // define the nested loops number.
7453   unsigned NestedLoopCount =
7454       CheckOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
7455                       nullptr /*ordered not a clause on distribute*/, AStmt,
7456                       *this, *DSAStack, VarsWithImplicitDSA, B);
7457   if (NestedLoopCount == 0)
7458     return StmtError();
7459 
7460   assert((CurContext->isDependentContext() || B.builtAll()) &&
7461          "omp teams distribute loop exprs were not built");
7462 
7463   getCurFunction()->setHasBranchProtectedScope();
7464   return OMPTeamsDistributeDirective::Create(
7465       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7466 }
7467 
7468 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
7469                                              SourceLocation StartLoc,
7470                                              SourceLocation LParenLoc,
7471                                              SourceLocation EndLoc) {
7472   OMPClause *Res = nullptr;
7473   switch (Kind) {
7474   case OMPC_final:
7475     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7476     break;
7477   case OMPC_num_threads:
7478     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7479     break;
7480   case OMPC_safelen:
7481     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7482     break;
7483   case OMPC_simdlen:
7484     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7485     break;
7486   case OMPC_collapse:
7487     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7488     break;
7489   case OMPC_ordered:
7490     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7491     break;
7492   case OMPC_device:
7493     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7494     break;
7495   case OMPC_num_teams:
7496     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7497     break;
7498   case OMPC_thread_limit:
7499     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7500     break;
7501   case OMPC_priority:
7502     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7503     break;
7504   case OMPC_grainsize:
7505     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7506     break;
7507   case OMPC_num_tasks:
7508     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7509     break;
7510   case OMPC_hint:
7511     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7512     break;
7513   case OMPC_if:
7514   case OMPC_default:
7515   case OMPC_proc_bind:
7516   case OMPC_schedule:
7517   case OMPC_private:
7518   case OMPC_firstprivate:
7519   case OMPC_lastprivate:
7520   case OMPC_shared:
7521   case OMPC_reduction:
7522   case OMPC_linear:
7523   case OMPC_aligned:
7524   case OMPC_copyin:
7525   case OMPC_copyprivate:
7526   case OMPC_nowait:
7527   case OMPC_untied:
7528   case OMPC_mergeable:
7529   case OMPC_threadprivate:
7530   case OMPC_flush:
7531   case OMPC_read:
7532   case OMPC_write:
7533   case OMPC_update:
7534   case OMPC_capture:
7535   case OMPC_seq_cst:
7536   case OMPC_depend:
7537   case OMPC_threads:
7538   case OMPC_simd:
7539   case OMPC_map:
7540   case OMPC_nogroup:
7541   case OMPC_dist_schedule:
7542   case OMPC_defaultmap:
7543   case OMPC_unknown:
7544   case OMPC_uniform:
7545   case OMPC_to:
7546   case OMPC_from:
7547   case OMPC_use_device_ptr:
7548   case OMPC_is_device_ptr:
7549     llvm_unreachable("Clause is not allowed.");
7550   }
7551   return Res;
7552 }
7553 
7554 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7555                                      Expr *Condition, SourceLocation StartLoc,
7556                                      SourceLocation LParenLoc,
7557                                      SourceLocation NameModifierLoc,
7558                                      SourceLocation ColonLoc,
7559                                      SourceLocation EndLoc) {
7560   Expr *ValExpr = Condition;
7561   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7562       !Condition->isInstantiationDependent() &&
7563       !Condition->containsUnexpandedParameterPack()) {
7564     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7565     if (Val.isInvalid())
7566       return nullptr;
7567 
7568     ValExpr = MakeFullExpr(Val.get()).get();
7569   }
7570 
7571   return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7572                                    NameModifierLoc, ColonLoc, EndLoc);
7573 }
7574 
7575 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7576                                         SourceLocation StartLoc,
7577                                         SourceLocation LParenLoc,
7578                                         SourceLocation EndLoc) {
7579   Expr *ValExpr = Condition;
7580   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7581       !Condition->isInstantiationDependent() &&
7582       !Condition->containsUnexpandedParameterPack()) {
7583     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7584     if (Val.isInvalid())
7585       return nullptr;
7586 
7587     ValExpr = MakeFullExpr(Val.get()).get();
7588   }
7589 
7590   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7591 }
7592 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7593                                                         Expr *Op) {
7594   if (!Op)
7595     return ExprError();
7596 
7597   class IntConvertDiagnoser : public ICEConvertDiagnoser {
7598   public:
7599     IntConvertDiagnoser()
7600         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
7601     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7602                                          QualType T) override {
7603       return S.Diag(Loc, diag::err_omp_not_integral) << T;
7604     }
7605     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7606                                              QualType T) override {
7607       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7608     }
7609     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7610                                                QualType T,
7611                                                QualType ConvTy) override {
7612       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7613     }
7614     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7615                                            QualType ConvTy) override {
7616       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7617              << ConvTy->isEnumeralType() << ConvTy;
7618     }
7619     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7620                                             QualType T) override {
7621       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7622     }
7623     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7624                                         QualType ConvTy) override {
7625       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7626              << ConvTy->isEnumeralType() << ConvTy;
7627     }
7628     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7629                                              QualType) override {
7630       llvm_unreachable("conversion functions are permitted");
7631     }
7632   } ConvertDiagnoser;
7633   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7634 }
7635 
7636 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
7637                                       OpenMPClauseKind CKind,
7638                                       bool StrictlyPositive) {
7639   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7640       !ValExpr->isInstantiationDependent()) {
7641     SourceLocation Loc = ValExpr->getExprLoc();
7642     ExprResult Value =
7643         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7644     if (Value.isInvalid())
7645       return false;
7646 
7647     ValExpr = Value.get();
7648     // The expression must evaluate to a non-negative integer value.
7649     llvm::APSInt Result;
7650     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
7651         Result.isSigned() &&
7652         !((!StrictlyPositive && Result.isNonNegative()) ||
7653           (StrictlyPositive && Result.isStrictlyPositive()))) {
7654       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
7655           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7656           << ValExpr->getSourceRange();
7657       return false;
7658     }
7659   }
7660   return true;
7661 }
7662 
7663 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7664                                              SourceLocation StartLoc,
7665                                              SourceLocation LParenLoc,
7666                                              SourceLocation EndLoc) {
7667   Expr *ValExpr = NumThreads;
7668 
7669   // OpenMP [2.5, Restrictions]
7670   //  The num_threads expression must evaluate to a positive integer value.
7671   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7672                                  /*StrictlyPositive=*/true))
7673     return nullptr;
7674 
7675   return new (Context)
7676       OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7677 }
7678 
7679 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
7680                                                        OpenMPClauseKind CKind,
7681                                                        bool StrictlyPositive) {
7682   if (!E)
7683     return ExprError();
7684   if (E->isValueDependent() || E->isTypeDependent() ||
7685       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7686     return E;
7687   llvm::APSInt Result;
7688   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7689   if (ICE.isInvalid())
7690     return ExprError();
7691   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7692       (!StrictlyPositive && !Result.isNonNegative())) {
7693     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
7694         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7695         << E->getSourceRange();
7696     return ExprError();
7697   }
7698   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7699     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7700         << E->getSourceRange();
7701     return ExprError();
7702   }
7703   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7704     DSAStack->setAssociatedLoops(Result.getExtValue());
7705   else if (CKind == OMPC_ordered)
7706     DSAStack->setAssociatedLoops(Result.getExtValue());
7707   return ICE;
7708 }
7709 
7710 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7711                                           SourceLocation LParenLoc,
7712                                           SourceLocation EndLoc) {
7713   // OpenMP [2.8.1, simd construct, Description]
7714   // The parameter of the safelen clause must be a constant
7715   // positive integer expression.
7716   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7717   if (Safelen.isInvalid())
7718     return nullptr;
7719   return new (Context)
7720       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
7721 }
7722 
7723 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7724                                           SourceLocation LParenLoc,
7725                                           SourceLocation EndLoc) {
7726   // OpenMP [2.8.1, simd construct, Description]
7727   // The parameter of the simdlen clause must be a constant
7728   // positive integer expression.
7729   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7730   if (Simdlen.isInvalid())
7731     return nullptr;
7732   return new (Context)
7733       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7734 }
7735 
7736 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7737                                            SourceLocation StartLoc,
7738                                            SourceLocation LParenLoc,
7739                                            SourceLocation EndLoc) {
7740   // OpenMP [2.7.1, loop construct, Description]
7741   // OpenMP [2.8.1, simd construct, Description]
7742   // OpenMP [2.9.6, distribute construct, Description]
7743   // The parameter of the collapse clause must be a constant
7744   // positive integer expression.
7745   ExprResult NumForLoopsResult =
7746       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7747   if (NumForLoopsResult.isInvalid())
7748     return nullptr;
7749   return new (Context)
7750       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
7751 }
7752 
7753 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7754                                           SourceLocation EndLoc,
7755                                           SourceLocation LParenLoc,
7756                                           Expr *NumForLoops) {
7757   // OpenMP [2.7.1, loop construct, Description]
7758   // OpenMP [2.8.1, simd construct, Description]
7759   // OpenMP [2.9.6, distribute construct, Description]
7760   // The parameter of the ordered clause must be a constant
7761   // positive integer expression if any.
7762   if (NumForLoops && LParenLoc.isValid()) {
7763     ExprResult NumForLoopsResult =
7764         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7765     if (NumForLoopsResult.isInvalid())
7766       return nullptr;
7767     NumForLoops = NumForLoopsResult.get();
7768   } else
7769     NumForLoops = nullptr;
7770   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
7771   return new (Context)
7772       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7773 }
7774 
7775 OMPClause *Sema::ActOnOpenMPSimpleClause(
7776     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7777     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
7778   OMPClause *Res = nullptr;
7779   switch (Kind) {
7780   case OMPC_default:
7781     Res =
7782         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7783                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
7784     break;
7785   case OMPC_proc_bind:
7786     Res = ActOnOpenMPProcBindClause(
7787         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7788         LParenLoc, EndLoc);
7789     break;
7790   case OMPC_if:
7791   case OMPC_final:
7792   case OMPC_num_threads:
7793   case OMPC_safelen:
7794   case OMPC_simdlen:
7795   case OMPC_collapse:
7796   case OMPC_schedule:
7797   case OMPC_private:
7798   case OMPC_firstprivate:
7799   case OMPC_lastprivate:
7800   case OMPC_shared:
7801   case OMPC_reduction:
7802   case OMPC_linear:
7803   case OMPC_aligned:
7804   case OMPC_copyin:
7805   case OMPC_copyprivate:
7806   case OMPC_ordered:
7807   case OMPC_nowait:
7808   case OMPC_untied:
7809   case OMPC_mergeable:
7810   case OMPC_threadprivate:
7811   case OMPC_flush:
7812   case OMPC_read:
7813   case OMPC_write:
7814   case OMPC_update:
7815   case OMPC_capture:
7816   case OMPC_seq_cst:
7817   case OMPC_depend:
7818   case OMPC_device:
7819   case OMPC_threads:
7820   case OMPC_simd:
7821   case OMPC_map:
7822   case OMPC_num_teams:
7823   case OMPC_thread_limit:
7824   case OMPC_priority:
7825   case OMPC_grainsize:
7826   case OMPC_nogroup:
7827   case OMPC_num_tasks:
7828   case OMPC_hint:
7829   case OMPC_dist_schedule:
7830   case OMPC_defaultmap:
7831   case OMPC_unknown:
7832   case OMPC_uniform:
7833   case OMPC_to:
7834   case OMPC_from:
7835   case OMPC_use_device_ptr:
7836   case OMPC_is_device_ptr:
7837     llvm_unreachable("Clause is not allowed.");
7838   }
7839   return Res;
7840 }
7841 
7842 static std::string
7843 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7844                         ArrayRef<unsigned> Exclude = llvm::None) {
7845   std::string Values;
7846   unsigned Bound = Last >= 2 ? Last - 2 : 0;
7847   unsigned Skipped = Exclude.size();
7848   auto S = Exclude.begin(), E = Exclude.end();
7849   for (unsigned i = First; i < Last; ++i) {
7850     if (std::find(S, E, i) != E) {
7851       --Skipped;
7852       continue;
7853     }
7854     Values += "'";
7855     Values += getOpenMPSimpleClauseTypeName(K, i);
7856     Values += "'";
7857     if (i == Bound - Skipped)
7858       Values += " or ";
7859     else if (i != Bound + 1 - Skipped)
7860       Values += ", ";
7861   }
7862   return Values;
7863 }
7864 
7865 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7866                                           SourceLocation KindKwLoc,
7867                                           SourceLocation StartLoc,
7868                                           SourceLocation LParenLoc,
7869                                           SourceLocation EndLoc) {
7870   if (Kind == OMPC_DEFAULT_unknown) {
7871     static_assert(OMPC_DEFAULT_unknown > 0,
7872                   "OMPC_DEFAULT_unknown not greater than 0");
7873     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7874         << getListOfPossibleValues(OMPC_default, /*First=*/0,
7875                                    /*Last=*/OMPC_DEFAULT_unknown)
7876         << getOpenMPClauseName(OMPC_default);
7877     return nullptr;
7878   }
7879   switch (Kind) {
7880   case OMPC_DEFAULT_none:
7881     DSAStack->setDefaultDSANone(KindKwLoc);
7882     break;
7883   case OMPC_DEFAULT_shared:
7884     DSAStack->setDefaultDSAShared(KindKwLoc);
7885     break;
7886   case OMPC_DEFAULT_unknown:
7887     llvm_unreachable("Clause kind is not allowed.");
7888     break;
7889   }
7890   return new (Context)
7891       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7892 }
7893 
7894 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7895                                            SourceLocation KindKwLoc,
7896                                            SourceLocation StartLoc,
7897                                            SourceLocation LParenLoc,
7898                                            SourceLocation EndLoc) {
7899   if (Kind == OMPC_PROC_BIND_unknown) {
7900     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7901         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7902                                    /*Last=*/OMPC_PROC_BIND_unknown)
7903         << getOpenMPClauseName(OMPC_proc_bind);
7904     return nullptr;
7905   }
7906   return new (Context)
7907       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7908 }
7909 
7910 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
7911     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
7912     SourceLocation StartLoc, SourceLocation LParenLoc,
7913     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
7914     SourceLocation EndLoc) {
7915   OMPClause *Res = nullptr;
7916   switch (Kind) {
7917   case OMPC_schedule:
7918     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7919     assert(Argument.size() == NumberOfElements &&
7920            ArgumentLoc.size() == NumberOfElements);
7921     Res = ActOnOpenMPScheduleClause(
7922         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7923         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7924         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7925         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7926         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
7927     break;
7928   case OMPC_if:
7929     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7930     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7931                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7932                               DelimLoc, EndLoc);
7933     break;
7934   case OMPC_dist_schedule:
7935     Res = ActOnOpenMPDistScheduleClause(
7936         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7937         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7938     break;
7939   case OMPC_defaultmap:
7940     enum { Modifier, DefaultmapKind };
7941     Res = ActOnOpenMPDefaultmapClause(
7942         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7943         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7944         StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
7945         EndLoc);
7946     break;
7947   case OMPC_final:
7948   case OMPC_num_threads:
7949   case OMPC_safelen:
7950   case OMPC_simdlen:
7951   case OMPC_collapse:
7952   case OMPC_default:
7953   case OMPC_proc_bind:
7954   case OMPC_private:
7955   case OMPC_firstprivate:
7956   case OMPC_lastprivate:
7957   case OMPC_shared:
7958   case OMPC_reduction:
7959   case OMPC_linear:
7960   case OMPC_aligned:
7961   case OMPC_copyin:
7962   case OMPC_copyprivate:
7963   case OMPC_ordered:
7964   case OMPC_nowait:
7965   case OMPC_untied:
7966   case OMPC_mergeable:
7967   case OMPC_threadprivate:
7968   case OMPC_flush:
7969   case OMPC_read:
7970   case OMPC_write:
7971   case OMPC_update:
7972   case OMPC_capture:
7973   case OMPC_seq_cst:
7974   case OMPC_depend:
7975   case OMPC_device:
7976   case OMPC_threads:
7977   case OMPC_simd:
7978   case OMPC_map:
7979   case OMPC_num_teams:
7980   case OMPC_thread_limit:
7981   case OMPC_priority:
7982   case OMPC_grainsize:
7983   case OMPC_nogroup:
7984   case OMPC_num_tasks:
7985   case OMPC_hint:
7986   case OMPC_unknown:
7987   case OMPC_uniform:
7988   case OMPC_to:
7989   case OMPC_from:
7990   case OMPC_use_device_ptr:
7991   case OMPC_is_device_ptr:
7992     llvm_unreachable("Clause is not allowed.");
7993   }
7994   return Res;
7995 }
7996 
7997 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7998                                    OpenMPScheduleClauseModifier M2,
7999                                    SourceLocation M1Loc, SourceLocation M2Loc) {
8000   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
8001     SmallVector<unsigned, 2> Excluded;
8002     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
8003       Excluded.push_back(M2);
8004     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
8005       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
8006     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
8007       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
8008     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
8009         << getListOfPossibleValues(OMPC_schedule,
8010                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
8011                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8012                                    Excluded)
8013         << getOpenMPClauseName(OMPC_schedule);
8014     return true;
8015   }
8016   return false;
8017 }
8018 
8019 OMPClause *Sema::ActOnOpenMPScheduleClause(
8020     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
8021     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
8022     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
8023     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
8024   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
8025       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
8026     return nullptr;
8027   // OpenMP, 2.7.1, Loop Construct, Restrictions
8028   // Either the monotonic modifier or the nonmonotonic modifier can be specified
8029   // but not both.
8030   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
8031       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
8032        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
8033       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
8034        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
8035     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
8036         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
8037         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
8038     return nullptr;
8039   }
8040   if (Kind == OMPC_SCHEDULE_unknown) {
8041     std::string Values;
8042     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
8043       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
8044       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8045                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
8046                                        Exclude);
8047     } else {
8048       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
8049                                        /*Last=*/OMPC_SCHEDULE_unknown);
8050     }
8051     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
8052         << Values << getOpenMPClauseName(OMPC_schedule);
8053     return nullptr;
8054   }
8055   // OpenMP, 2.7.1, Loop Construct, Restrictions
8056   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
8057   // schedule(guided).
8058   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
8059        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
8060       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
8061     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
8062          diag::err_omp_schedule_nonmonotonic_static);
8063     return nullptr;
8064   }
8065   Expr *ValExpr = ChunkSize;
8066   Stmt *HelperValStmt = nullptr;
8067   if (ChunkSize) {
8068     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
8069         !ChunkSize->isInstantiationDependent() &&
8070         !ChunkSize->containsUnexpandedParameterPack()) {
8071       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
8072       ExprResult Val =
8073           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
8074       if (Val.isInvalid())
8075         return nullptr;
8076 
8077       ValExpr = Val.get();
8078 
8079       // OpenMP [2.7.1, Restrictions]
8080       //  chunk_size must be a loop invariant integer expression with a positive
8081       //  value.
8082       llvm::APSInt Result;
8083       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
8084         if (Result.isSigned() && !Result.isStrictlyPositive()) {
8085           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
8086               << "schedule" << 1 << ChunkSize->getSourceRange();
8087           return nullptr;
8088         }
8089       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
8090                  !CurContext->isDependentContext()) {
8091         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
8092         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8093         HelperValStmt = buildPreInits(Context, Captures);
8094       }
8095     }
8096   }
8097 
8098   return new (Context)
8099       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
8100                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
8101 }
8102 
8103 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
8104                                    SourceLocation StartLoc,
8105                                    SourceLocation EndLoc) {
8106   OMPClause *Res = nullptr;
8107   switch (Kind) {
8108   case OMPC_ordered:
8109     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
8110     break;
8111   case OMPC_nowait:
8112     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
8113     break;
8114   case OMPC_untied:
8115     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
8116     break;
8117   case OMPC_mergeable:
8118     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
8119     break;
8120   case OMPC_read:
8121     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
8122     break;
8123   case OMPC_write:
8124     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
8125     break;
8126   case OMPC_update:
8127     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
8128     break;
8129   case OMPC_capture:
8130     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
8131     break;
8132   case OMPC_seq_cst:
8133     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
8134     break;
8135   case OMPC_threads:
8136     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
8137     break;
8138   case OMPC_simd:
8139     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
8140     break;
8141   case OMPC_nogroup:
8142     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
8143     break;
8144   case OMPC_if:
8145   case OMPC_final:
8146   case OMPC_num_threads:
8147   case OMPC_safelen:
8148   case OMPC_simdlen:
8149   case OMPC_collapse:
8150   case OMPC_schedule:
8151   case OMPC_private:
8152   case OMPC_firstprivate:
8153   case OMPC_lastprivate:
8154   case OMPC_shared:
8155   case OMPC_reduction:
8156   case OMPC_linear:
8157   case OMPC_aligned:
8158   case OMPC_copyin:
8159   case OMPC_copyprivate:
8160   case OMPC_default:
8161   case OMPC_proc_bind:
8162   case OMPC_threadprivate:
8163   case OMPC_flush:
8164   case OMPC_depend:
8165   case OMPC_device:
8166   case OMPC_map:
8167   case OMPC_num_teams:
8168   case OMPC_thread_limit:
8169   case OMPC_priority:
8170   case OMPC_grainsize:
8171   case OMPC_num_tasks:
8172   case OMPC_hint:
8173   case OMPC_dist_schedule:
8174   case OMPC_defaultmap:
8175   case OMPC_unknown:
8176   case OMPC_uniform:
8177   case OMPC_to:
8178   case OMPC_from:
8179   case OMPC_use_device_ptr:
8180   case OMPC_is_device_ptr:
8181     llvm_unreachable("Clause is not allowed.");
8182   }
8183   return Res;
8184 }
8185 
8186 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
8187                                          SourceLocation EndLoc) {
8188   DSAStack->setNowaitRegion();
8189   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
8190 }
8191 
8192 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
8193                                          SourceLocation EndLoc) {
8194   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
8195 }
8196 
8197 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
8198                                             SourceLocation EndLoc) {
8199   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
8200 }
8201 
8202 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
8203                                        SourceLocation EndLoc) {
8204   return new (Context) OMPReadClause(StartLoc, EndLoc);
8205 }
8206 
8207 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
8208                                         SourceLocation EndLoc) {
8209   return new (Context) OMPWriteClause(StartLoc, EndLoc);
8210 }
8211 
8212 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
8213                                          SourceLocation EndLoc) {
8214   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
8215 }
8216 
8217 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
8218                                           SourceLocation EndLoc) {
8219   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
8220 }
8221 
8222 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8223                                          SourceLocation EndLoc) {
8224   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8225 }
8226 
8227 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8228                                           SourceLocation EndLoc) {
8229   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8230 }
8231 
8232 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8233                                        SourceLocation EndLoc) {
8234   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8235 }
8236 
8237 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8238                                           SourceLocation EndLoc) {
8239   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8240 }
8241 
8242 OMPClause *Sema::ActOnOpenMPVarListClause(
8243     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8244     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8245     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
8246     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
8247     OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8248     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8249     SourceLocation DepLinMapLoc) {
8250   OMPClause *Res = nullptr;
8251   switch (Kind) {
8252   case OMPC_private:
8253     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8254     break;
8255   case OMPC_firstprivate:
8256     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8257     break;
8258   case OMPC_lastprivate:
8259     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8260     break;
8261   case OMPC_shared:
8262     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8263     break;
8264   case OMPC_reduction:
8265     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8266                                      EndLoc, ReductionIdScopeSpec, ReductionId);
8267     break;
8268   case OMPC_linear:
8269     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
8270                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
8271     break;
8272   case OMPC_aligned:
8273     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8274                                    ColonLoc, EndLoc);
8275     break;
8276   case OMPC_copyin:
8277     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8278     break;
8279   case OMPC_copyprivate:
8280     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8281     break;
8282   case OMPC_flush:
8283     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8284     break;
8285   case OMPC_depend:
8286     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8287                                   StartLoc, LParenLoc, EndLoc);
8288     break;
8289   case OMPC_map:
8290     Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8291                                DepLinMapLoc, ColonLoc, VarList, StartLoc,
8292                                LParenLoc, EndLoc);
8293     break;
8294   case OMPC_to:
8295     Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8296     break;
8297   case OMPC_from:
8298     Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8299     break;
8300   case OMPC_use_device_ptr:
8301     Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8302     break;
8303   case OMPC_is_device_ptr:
8304     Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8305     break;
8306   case OMPC_if:
8307   case OMPC_final:
8308   case OMPC_num_threads:
8309   case OMPC_safelen:
8310   case OMPC_simdlen:
8311   case OMPC_collapse:
8312   case OMPC_default:
8313   case OMPC_proc_bind:
8314   case OMPC_schedule:
8315   case OMPC_ordered:
8316   case OMPC_nowait:
8317   case OMPC_untied:
8318   case OMPC_mergeable:
8319   case OMPC_threadprivate:
8320   case OMPC_read:
8321   case OMPC_write:
8322   case OMPC_update:
8323   case OMPC_capture:
8324   case OMPC_seq_cst:
8325   case OMPC_device:
8326   case OMPC_threads:
8327   case OMPC_simd:
8328   case OMPC_num_teams:
8329   case OMPC_thread_limit:
8330   case OMPC_priority:
8331   case OMPC_grainsize:
8332   case OMPC_nogroup:
8333   case OMPC_num_tasks:
8334   case OMPC_hint:
8335   case OMPC_dist_schedule:
8336   case OMPC_defaultmap:
8337   case OMPC_unknown:
8338   case OMPC_uniform:
8339     llvm_unreachable("Clause is not allowed.");
8340   }
8341   return Res;
8342 }
8343 
8344 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
8345                                        ExprObjectKind OK, SourceLocation Loc) {
8346   ExprResult Res = BuildDeclRefExpr(
8347       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8348   if (!Res.isUsable())
8349     return ExprError();
8350   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8351     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8352     if (!Res.isUsable())
8353       return ExprError();
8354   }
8355   if (VK != VK_LValue && Res.get()->isGLValue()) {
8356     Res = DefaultLvalueConversion(Res.get());
8357     if (!Res.isUsable())
8358       return ExprError();
8359   }
8360   return Res;
8361 }
8362 
8363 static std::pair<ValueDecl *, bool>
8364 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8365                SourceRange &ERange, bool AllowArraySection = false) {
8366   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8367       RefExpr->containsUnexpandedParameterPack())
8368     return std::make_pair(nullptr, true);
8369 
8370   // OpenMP [3.1, C/C++]
8371   //  A list item is a variable name.
8372   // OpenMP  [2.9.3.3, Restrictions, p.1]
8373   //  A variable that is part of another variable (as an array or
8374   //  structure element) cannot appear in a private clause.
8375   RefExpr = RefExpr->IgnoreParens();
8376   enum {
8377     NoArrayExpr = -1,
8378     ArraySubscript = 0,
8379     OMPArraySection = 1
8380   } IsArrayExpr = NoArrayExpr;
8381   if (AllowArraySection) {
8382     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8383       auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8384       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8385         Base = TempASE->getBase()->IgnoreParenImpCasts();
8386       RefExpr = Base;
8387       IsArrayExpr = ArraySubscript;
8388     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8389       auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8390       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8391         Base = TempOASE->getBase()->IgnoreParenImpCasts();
8392       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8393         Base = TempASE->getBase()->IgnoreParenImpCasts();
8394       RefExpr = Base;
8395       IsArrayExpr = OMPArraySection;
8396     }
8397   }
8398   ELoc = RefExpr->getExprLoc();
8399   ERange = RefExpr->getSourceRange();
8400   RefExpr = RefExpr->IgnoreParenImpCasts();
8401   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8402   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8403   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8404       (S.getCurrentThisType().isNull() || !ME ||
8405        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8406        !isa<FieldDecl>(ME->getMemberDecl()))) {
8407     if (IsArrayExpr != NoArrayExpr)
8408       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8409                                                          << ERange;
8410     else {
8411       S.Diag(ELoc,
8412              AllowArraySection
8413                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
8414                  : diag::err_omp_expected_var_name_member_expr)
8415           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8416     }
8417     return std::make_pair(nullptr, false);
8418   }
8419   return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8420 }
8421 
8422 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8423                                           SourceLocation StartLoc,
8424                                           SourceLocation LParenLoc,
8425                                           SourceLocation EndLoc) {
8426   SmallVector<Expr *, 8> Vars;
8427   SmallVector<Expr *, 8> PrivateCopies;
8428   for (auto &RefExpr : VarList) {
8429     assert(RefExpr && "NULL expr in OpenMP private clause.");
8430     SourceLocation ELoc;
8431     SourceRange ERange;
8432     Expr *SimpleRefExpr = RefExpr;
8433     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8434     if (Res.second) {
8435       // It will be analyzed later.
8436       Vars.push_back(RefExpr);
8437       PrivateCopies.push_back(nullptr);
8438     }
8439     ValueDecl *D = Res.first;
8440     if (!D)
8441       continue;
8442 
8443     QualType Type = D->getType();
8444     auto *VD = dyn_cast<VarDecl>(D);
8445 
8446     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8447     //  A variable that appears in a private clause must not have an incomplete
8448     //  type or a reference type.
8449     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
8450       continue;
8451     Type = Type.getNonReferenceType();
8452 
8453     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8454     // in a Construct]
8455     //  Variables with the predetermined data-sharing attributes may not be
8456     //  listed in data-sharing attributes clauses, except for the cases
8457     //  listed below. For these exceptions only, listing a predetermined
8458     //  variable in a data-sharing attribute clause is allowed and overrides
8459     //  the variable's predetermined data-sharing attributes.
8460     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8461     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
8462       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8463                                           << getOpenMPClauseName(OMPC_private);
8464       ReportOriginalDSA(*this, DSAStack, D, DVar);
8465       continue;
8466     }
8467 
8468     // Variably modified types are not supported for tasks.
8469     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
8470         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
8471       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8472           << getOpenMPClauseName(OMPC_private) << Type
8473           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8474       bool IsDecl =
8475           !VD ||
8476           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8477       Diag(D->getLocation(),
8478            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8479           << D;
8480       continue;
8481     }
8482 
8483     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8484     // A list item cannot appear in both a map clause and a data-sharing
8485     // attribute clause on the same construct
8486     if (DSAStack->getCurrentDirective() == OMPD_target) {
8487       OpenMPClauseKind ConflictKind;
8488       if (DSAStack->checkMappableExprComponentListsForDecl(
8489               VD, /*CurrentRegionOnly=*/true,
8490               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8491                   OpenMPClauseKind WhereFoundClauseKind) -> bool {
8492                 ConflictKind = WhereFoundClauseKind;
8493                 return true;
8494               })) {
8495         Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
8496             << getOpenMPClauseName(OMPC_private)
8497             << getOpenMPClauseName(ConflictKind)
8498             << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8499         ReportOriginalDSA(*this, DSAStack, D, DVar);
8500         continue;
8501       }
8502     }
8503 
8504     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8505     //  A variable of class type (or array thereof) that appears in a private
8506     //  clause requires an accessible, unambiguous default constructor for the
8507     //  class type.
8508     // Generate helper private variable and initialize it with the default
8509     // value. The address of the original variable is replaced by the address of
8510     // the new private variable in CodeGen. This new variable is not added to
8511     // IdResolver, so the code in the OpenMP region uses original variable for
8512     // proper diagnostics.
8513     Type = Type.getUnqualifiedType();
8514     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8515                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
8516     ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
8517     if (VDPrivate->isInvalidDecl())
8518       continue;
8519     auto VDPrivateRefExpr = buildDeclRefExpr(
8520         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
8521 
8522     DeclRefExpr *Ref = nullptr;
8523     if (!VD && !CurContext->isDependentContext())
8524       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8525     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
8526     Vars.push_back((VD || CurContext->isDependentContext())
8527                        ? RefExpr->IgnoreParens()
8528                        : Ref);
8529     PrivateCopies.push_back(VDPrivateRefExpr);
8530   }
8531 
8532   if (Vars.empty())
8533     return nullptr;
8534 
8535   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8536                                   PrivateCopies);
8537 }
8538 
8539 namespace {
8540 class DiagsUninitializedSeveretyRAII {
8541 private:
8542   DiagnosticsEngine &Diags;
8543   SourceLocation SavedLoc;
8544   bool IsIgnored;
8545 
8546 public:
8547   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8548                                  bool IsIgnored)
8549       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8550     if (!IsIgnored) {
8551       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8552                         /*Map*/ diag::Severity::Ignored, Loc);
8553     }
8554   }
8555   ~DiagsUninitializedSeveretyRAII() {
8556     if (!IsIgnored)
8557       Diags.popMappings(SavedLoc);
8558   }
8559 };
8560 }
8561 
8562 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8563                                                SourceLocation StartLoc,
8564                                                SourceLocation LParenLoc,
8565                                                SourceLocation EndLoc) {
8566   SmallVector<Expr *, 8> Vars;
8567   SmallVector<Expr *, 8> PrivateCopies;
8568   SmallVector<Expr *, 8> Inits;
8569   SmallVector<Decl *, 4> ExprCaptures;
8570   bool IsImplicitClause =
8571       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8572   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8573 
8574   for (auto &RefExpr : VarList) {
8575     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
8576     SourceLocation ELoc;
8577     SourceRange ERange;
8578     Expr *SimpleRefExpr = RefExpr;
8579     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8580     if (Res.second) {
8581       // It will be analyzed later.
8582       Vars.push_back(RefExpr);
8583       PrivateCopies.push_back(nullptr);
8584       Inits.push_back(nullptr);
8585     }
8586     ValueDecl *D = Res.first;
8587     if (!D)
8588       continue;
8589 
8590     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
8591     QualType Type = D->getType();
8592     auto *VD = dyn_cast<VarDecl>(D);
8593 
8594     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8595     //  A variable that appears in a private clause must not have an incomplete
8596     //  type or a reference type.
8597     if (RequireCompleteType(ELoc, Type,
8598                             diag::err_omp_firstprivate_incomplete_type))
8599       continue;
8600     Type = Type.getNonReferenceType();
8601 
8602     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8603     //  A variable of class type (or array thereof) that appears in a private
8604     //  clause requires an accessible, unambiguous copy constructor for the
8605     //  class type.
8606     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
8607 
8608     // If an implicit firstprivate variable found it was checked already.
8609     DSAStackTy::DSAVarData TopDVar;
8610     if (!IsImplicitClause) {
8611       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8612       TopDVar = DVar;
8613       bool IsConstant = ElemType.isConstant(Context);
8614       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8615       //  A list item that specifies a given variable may not appear in more
8616       // than one clause on the same directive, except that a variable may be
8617       //  specified in both firstprivate and lastprivate clauses.
8618       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
8619           DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
8620         Diag(ELoc, diag::err_omp_wrong_dsa)
8621             << getOpenMPClauseName(DVar.CKind)
8622             << getOpenMPClauseName(OMPC_firstprivate);
8623         ReportOriginalDSA(*this, DSAStack, D, DVar);
8624         continue;
8625       }
8626 
8627       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8628       // in a Construct]
8629       //  Variables with the predetermined data-sharing attributes may not be
8630       //  listed in data-sharing attributes clauses, except for the cases
8631       //  listed below. For these exceptions only, listing a predetermined
8632       //  variable in a data-sharing attribute clause is allowed and overrides
8633       //  the variable's predetermined data-sharing attributes.
8634       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8635       // in a Construct, C/C++, p.2]
8636       //  Variables with const-qualified type having no mutable member may be
8637       //  listed in a firstprivate clause, even if they are static data members.
8638       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
8639           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8640         Diag(ELoc, diag::err_omp_wrong_dsa)
8641             << getOpenMPClauseName(DVar.CKind)
8642             << getOpenMPClauseName(OMPC_firstprivate);
8643         ReportOriginalDSA(*this, DSAStack, D, DVar);
8644         continue;
8645       }
8646 
8647       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8648       // OpenMP [2.9.3.4, Restrictions, p.2]
8649       //  A list item that is private within a parallel region must not appear
8650       //  in a firstprivate clause on a worksharing construct if any of the
8651       //  worksharing regions arising from the worksharing construct ever bind
8652       //  to any of the parallel regions arising from the parallel construct.
8653       if (isOpenMPWorksharingDirective(CurrDir) &&
8654           !isOpenMPParallelDirective(CurrDir)) {
8655         DVar = DSAStack->getImplicitDSA(D, true);
8656         if (DVar.CKind != OMPC_shared &&
8657             (isOpenMPParallelDirective(DVar.DKind) ||
8658              DVar.DKind == OMPD_unknown)) {
8659           Diag(ELoc, diag::err_omp_required_access)
8660               << getOpenMPClauseName(OMPC_firstprivate)
8661               << getOpenMPClauseName(OMPC_shared);
8662           ReportOriginalDSA(*this, DSAStack, D, DVar);
8663           continue;
8664         }
8665       }
8666       // OpenMP [2.9.3.4, Restrictions, p.3]
8667       //  A list item that appears in a reduction clause of a parallel construct
8668       //  must not appear in a firstprivate clause on a worksharing or task
8669       //  construct if any of the worksharing or task regions arising from the
8670       //  worksharing or task construct ever bind to any of the parallel regions
8671       //  arising from the parallel construct.
8672       // OpenMP [2.9.3.4, Restrictions, p.4]
8673       //  A list item that appears in a reduction clause in worksharing
8674       //  construct must not appear in a firstprivate clause in a task construct
8675       //  encountered during execution of any of the worksharing regions arising
8676       //  from the worksharing construct.
8677       if (isOpenMPTaskingDirective(CurrDir)) {
8678         DVar = DSAStack->hasInnermostDSA(
8679             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8680             [](OpenMPDirectiveKind K) -> bool {
8681               return isOpenMPParallelDirective(K) ||
8682                      isOpenMPWorksharingDirective(K);
8683             },
8684             false);
8685         if (DVar.CKind == OMPC_reduction &&
8686             (isOpenMPParallelDirective(DVar.DKind) ||
8687              isOpenMPWorksharingDirective(DVar.DKind))) {
8688           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8689               << getOpenMPDirectiveName(DVar.DKind);
8690           ReportOriginalDSA(*this, DSAStack, D, DVar);
8691           continue;
8692         }
8693       }
8694 
8695       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8696       // A list item that is private within a teams region must not appear in a
8697       // firstprivate clause on a distribute construct if any of the distribute
8698       // regions arising from the distribute construct ever bind to any of the
8699       // teams regions arising from the teams construct.
8700       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8701       // A list item that appears in a reduction clause of a teams construct
8702       // must not appear in a firstprivate clause on a distribute construct if
8703       // any of the distribute regions arising from the distribute construct
8704       // ever bind to any of the teams regions arising from the teams construct.
8705       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8706       // A list item may appear in a firstprivate or lastprivate clause but not
8707       // both.
8708       if (CurrDir == OMPD_distribute) {
8709         DVar = DSAStack->hasInnermostDSA(
8710             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8711             [](OpenMPDirectiveKind K) -> bool {
8712               return isOpenMPTeamsDirective(K);
8713             },
8714             false);
8715         if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8716           Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
8717           ReportOriginalDSA(*this, DSAStack, D, DVar);
8718           continue;
8719         }
8720         DVar = DSAStack->hasInnermostDSA(
8721             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8722             [](OpenMPDirectiveKind K) -> bool {
8723               return isOpenMPTeamsDirective(K);
8724             },
8725             false);
8726         if (DVar.CKind == OMPC_reduction &&
8727             isOpenMPTeamsDirective(DVar.DKind)) {
8728           Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
8729           ReportOriginalDSA(*this, DSAStack, D, DVar);
8730           continue;
8731         }
8732         DVar = DSAStack->getTopDSA(D, false);
8733         if (DVar.CKind == OMPC_lastprivate) {
8734           Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8735           ReportOriginalDSA(*this, DSAStack, D, DVar);
8736           continue;
8737         }
8738       }
8739       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8740       // A list item cannot appear in both a map clause and a data-sharing
8741       // attribute clause on the same construct
8742       if (CurrDir == OMPD_target) {
8743         OpenMPClauseKind ConflictKind;
8744         if (DSAStack->checkMappableExprComponentListsForDecl(
8745                 VD, /*CurrentRegionOnly=*/true,
8746                 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
8747                     OpenMPClauseKind WhereFoundClauseKind) -> bool {
8748                   ConflictKind = WhereFoundClauseKind;
8749                   return true;
8750                 })) {
8751           Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
8752               << getOpenMPClauseName(OMPC_firstprivate)
8753               << getOpenMPClauseName(ConflictKind)
8754               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8755           ReportOriginalDSA(*this, DSAStack, D, DVar);
8756           continue;
8757         }
8758       }
8759     }
8760 
8761     // Variably modified types are not supported for tasks.
8762     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
8763         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
8764       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8765           << getOpenMPClauseName(OMPC_firstprivate) << Type
8766           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8767       bool IsDecl =
8768           !VD ||
8769           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8770       Diag(D->getLocation(),
8771            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8772           << D;
8773       continue;
8774     }
8775 
8776     Type = Type.getUnqualifiedType();
8777     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8778                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
8779     // Generate helper private variable and initialize it with the value of the
8780     // original variable. The address of the original variable is replaced by
8781     // the address of the new private variable in the CodeGen. This new variable
8782     // is not added to IdResolver, so the code in the OpenMP region uses
8783     // original variable for proper diagnostics and variable capturing.
8784     Expr *VDInitRefExpr = nullptr;
8785     // For arrays generate initializer for single element and replace it by the
8786     // original array element in CodeGen.
8787     if (Type->isArrayType()) {
8788       auto VDInit =
8789           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
8790       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
8791       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
8792       ElemType = ElemType.getUnqualifiedType();
8793       auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
8794                                       ".firstprivate.temp");
8795       InitializedEntity Entity =
8796           InitializedEntity::InitializeVariable(VDInitTemp);
8797       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8798 
8799       InitializationSequence InitSeq(*this, Entity, Kind, Init);
8800       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8801       if (Result.isInvalid())
8802         VDPrivate->setInvalidDecl();
8803       else
8804         VDPrivate->setInit(Result.getAs<Expr>());
8805       // Remove temp variable declaration.
8806       Context.Deallocate(VDInitTemp);
8807     } else {
8808       auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8809                                   ".firstprivate.temp");
8810       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8811                                        RefExpr->getExprLoc());
8812       AddInitializerToDecl(VDPrivate,
8813                            DefaultLvalueConversion(VDInitRefExpr).get(),
8814                            /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8815     }
8816     if (VDPrivate->isInvalidDecl()) {
8817       if (IsImplicitClause) {
8818         Diag(RefExpr->getExprLoc(),
8819              diag::note_omp_task_predetermined_firstprivate_here);
8820       }
8821       continue;
8822     }
8823     CurContext->addDecl(VDPrivate);
8824     auto VDPrivateRefExpr = buildDeclRefExpr(
8825         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8826         RefExpr->getExprLoc());
8827     DeclRefExpr *Ref = nullptr;
8828     if (!VD && !CurContext->isDependentContext()) {
8829       if (TopDVar.CKind == OMPC_lastprivate)
8830         Ref = TopDVar.PrivateCopy;
8831       else {
8832         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8833         if (!IsOpenMPCapturedDecl(D))
8834           ExprCaptures.push_back(Ref->getDecl());
8835       }
8836     }
8837     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
8838     Vars.push_back((VD || CurContext->isDependentContext())
8839                        ? RefExpr->IgnoreParens()
8840                        : Ref);
8841     PrivateCopies.push_back(VDPrivateRefExpr);
8842     Inits.push_back(VDInitRefExpr);
8843   }
8844 
8845   if (Vars.empty())
8846     return nullptr;
8847 
8848   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8849                                        Vars, PrivateCopies, Inits,
8850                                        buildPreInits(Context, ExprCaptures));
8851 }
8852 
8853 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8854                                               SourceLocation StartLoc,
8855                                               SourceLocation LParenLoc,
8856                                               SourceLocation EndLoc) {
8857   SmallVector<Expr *, 8> Vars;
8858   SmallVector<Expr *, 8> SrcExprs;
8859   SmallVector<Expr *, 8> DstExprs;
8860   SmallVector<Expr *, 8> AssignmentOps;
8861   SmallVector<Decl *, 4> ExprCaptures;
8862   SmallVector<Expr *, 4> ExprPostUpdates;
8863   for (auto &RefExpr : VarList) {
8864     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
8865     SourceLocation ELoc;
8866     SourceRange ERange;
8867     Expr *SimpleRefExpr = RefExpr;
8868     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8869     if (Res.second) {
8870       // It will be analyzed later.
8871       Vars.push_back(RefExpr);
8872       SrcExprs.push_back(nullptr);
8873       DstExprs.push_back(nullptr);
8874       AssignmentOps.push_back(nullptr);
8875     }
8876     ValueDecl *D = Res.first;
8877     if (!D)
8878       continue;
8879 
8880     QualType Type = D->getType();
8881     auto *VD = dyn_cast<VarDecl>(D);
8882 
8883     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8884     //  A variable that appears in a lastprivate clause must not have an
8885     //  incomplete type or a reference type.
8886     if (RequireCompleteType(ELoc, Type,
8887                             diag::err_omp_lastprivate_incomplete_type))
8888       continue;
8889     Type = Type.getNonReferenceType();
8890 
8891     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8892     // in a Construct]
8893     //  Variables with the predetermined data-sharing attributes may not be
8894     //  listed in data-sharing attributes clauses, except for the cases
8895     //  listed below.
8896     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8897     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8898         DVar.CKind != OMPC_firstprivate &&
8899         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8900       Diag(ELoc, diag::err_omp_wrong_dsa)
8901           << getOpenMPClauseName(DVar.CKind)
8902           << getOpenMPClauseName(OMPC_lastprivate);
8903       ReportOriginalDSA(*this, DSAStack, D, DVar);
8904       continue;
8905     }
8906 
8907     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8908     // OpenMP [2.14.3.5, Restrictions, p.2]
8909     // A list item that is private within a parallel region, or that appears in
8910     // the reduction clause of a parallel construct, must not appear in a
8911     // lastprivate clause on a worksharing construct if any of the corresponding
8912     // worksharing regions ever binds to any of the corresponding parallel
8913     // regions.
8914     DSAStackTy::DSAVarData TopDVar = DVar;
8915     if (isOpenMPWorksharingDirective(CurrDir) &&
8916         !isOpenMPParallelDirective(CurrDir)) {
8917       DVar = DSAStack->getImplicitDSA(D, true);
8918       if (DVar.CKind != OMPC_shared) {
8919         Diag(ELoc, diag::err_omp_required_access)
8920             << getOpenMPClauseName(OMPC_lastprivate)
8921             << getOpenMPClauseName(OMPC_shared);
8922         ReportOriginalDSA(*this, DSAStack, D, DVar);
8923         continue;
8924       }
8925     }
8926 
8927     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8928     // A list item may appear in a firstprivate or lastprivate clause but not
8929     // both.
8930     if (CurrDir == OMPD_distribute) {
8931       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8932       if (DVar.CKind == OMPC_firstprivate) {
8933         Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8934         ReportOriginalDSA(*this, DSAStack, D, DVar);
8935         continue;
8936       }
8937     }
8938 
8939     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
8940     //  A variable of class type (or array thereof) that appears in a
8941     //  lastprivate clause requires an accessible, unambiguous default
8942     //  constructor for the class type, unless the list item is also specified
8943     //  in a firstprivate clause.
8944     //  A variable of class type (or array thereof) that appears in a
8945     //  lastprivate clause requires an accessible, unambiguous copy assignment
8946     //  operator for the class type.
8947     Type = Context.getBaseElementType(Type).getNonReferenceType();
8948     auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
8949                                Type.getUnqualifiedType(), ".lastprivate.src",
8950                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8951     auto *PseudoSrcExpr =
8952         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
8953     auto *DstVD =
8954         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
8955                      D->hasAttrs() ? &D->getAttrs() : nullptr);
8956     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
8957     // For arrays generate assignment operation for single element and replace
8958     // it by the original array element in CodeGen.
8959     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
8960                                    PseudoDstExpr, PseudoSrcExpr);
8961     if (AssignmentOp.isInvalid())
8962       continue;
8963     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
8964                                        /*DiscardedValue=*/true);
8965     if (AssignmentOp.isInvalid())
8966       continue;
8967 
8968     DeclRefExpr *Ref = nullptr;
8969     if (!VD && !CurContext->isDependentContext()) {
8970       if (TopDVar.CKind == OMPC_firstprivate)
8971         Ref = TopDVar.PrivateCopy;
8972       else {
8973         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8974         if (!IsOpenMPCapturedDecl(D))
8975           ExprCaptures.push_back(Ref->getDecl());
8976       }
8977       if (TopDVar.CKind == OMPC_firstprivate ||
8978           (!IsOpenMPCapturedDecl(D) &&
8979            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
8980         ExprResult RefRes = DefaultLvalueConversion(Ref);
8981         if (!RefRes.isUsable())
8982           continue;
8983         ExprResult PostUpdateRes =
8984             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8985                        RefRes.get());
8986         if (!PostUpdateRes.isUsable())
8987           continue;
8988         ExprPostUpdates.push_back(
8989             IgnoredValueConversions(PostUpdateRes.get()).get());
8990       }
8991     }
8992     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8993     Vars.push_back((VD || CurContext->isDependentContext())
8994                        ? RefExpr->IgnoreParens()
8995                        : Ref);
8996     SrcExprs.push_back(PseudoSrcExpr);
8997     DstExprs.push_back(PseudoDstExpr);
8998     AssignmentOps.push_back(AssignmentOp.get());
8999   }
9000 
9001   if (Vars.empty())
9002     return nullptr;
9003 
9004   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9005                                       Vars, SrcExprs, DstExprs, AssignmentOps,
9006                                       buildPreInits(Context, ExprCaptures),
9007                                       buildPostUpdate(*this, ExprPostUpdates));
9008 }
9009 
9010 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
9011                                          SourceLocation StartLoc,
9012                                          SourceLocation LParenLoc,
9013                                          SourceLocation EndLoc) {
9014   SmallVector<Expr *, 8> Vars;
9015   for (auto &RefExpr : VarList) {
9016     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
9017     SourceLocation ELoc;
9018     SourceRange ERange;
9019     Expr *SimpleRefExpr = RefExpr;
9020     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
9021     if (Res.second) {
9022       // It will be analyzed later.
9023       Vars.push_back(RefExpr);
9024     }
9025     ValueDecl *D = Res.first;
9026     if (!D)
9027       continue;
9028 
9029     auto *VD = dyn_cast<VarDecl>(D);
9030     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9031     // in a Construct]
9032     //  Variables with the predetermined data-sharing attributes may not be
9033     //  listed in data-sharing attributes clauses, except for the cases
9034     //  listed below. For these exceptions only, listing a predetermined
9035     //  variable in a data-sharing attribute clause is allowed and overrides
9036     //  the variable's predetermined data-sharing attributes.
9037     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9038     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
9039         DVar.RefExpr) {
9040       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9041                                           << getOpenMPClauseName(OMPC_shared);
9042       ReportOriginalDSA(*this, DSAStack, D, DVar);
9043       continue;
9044     }
9045 
9046     DeclRefExpr *Ref = nullptr;
9047     if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
9048       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9049     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
9050     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
9051                        ? RefExpr->IgnoreParens()
9052                        : Ref);
9053   }
9054 
9055   if (Vars.empty())
9056     return nullptr;
9057 
9058   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
9059 }
9060 
9061 namespace {
9062 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
9063   DSAStackTy *Stack;
9064 
9065 public:
9066   bool VisitDeclRefExpr(DeclRefExpr *E) {
9067     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
9068       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
9069       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
9070         return false;
9071       if (DVar.CKind != OMPC_unknown)
9072         return true;
9073       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
9074           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
9075           false);
9076       if (DVarPrivate.CKind != OMPC_unknown)
9077         return true;
9078       return false;
9079     }
9080     return false;
9081   }
9082   bool VisitStmt(Stmt *S) {
9083     for (auto Child : S->children()) {
9084       if (Child && Visit(Child))
9085         return true;
9086     }
9087     return false;
9088   }
9089   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
9090 };
9091 } // namespace
9092 
9093 namespace {
9094 // Transform MemberExpression for specified FieldDecl of current class to
9095 // DeclRefExpr to specified OMPCapturedExprDecl.
9096 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
9097   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
9098   ValueDecl *Field;
9099   DeclRefExpr *CapturedExpr;
9100 
9101 public:
9102   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
9103       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
9104 
9105   ExprResult TransformMemberExpr(MemberExpr *E) {
9106     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
9107         E->getMemberDecl() == Field) {
9108       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
9109       return CapturedExpr;
9110     }
9111     return BaseTransform::TransformMemberExpr(E);
9112   }
9113   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
9114 };
9115 } // namespace
9116 
9117 template <typename T>
9118 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
9119                             const llvm::function_ref<T(ValueDecl *)> &Gen) {
9120   for (auto &Set : Lookups) {
9121     for (auto *D : Set) {
9122       if (auto Res = Gen(cast<ValueDecl>(D)))
9123         return Res;
9124     }
9125   }
9126   return T();
9127 }
9128 
9129 static ExprResult
9130 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
9131                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
9132                          const DeclarationNameInfo &ReductionId, QualType Ty,
9133                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
9134   if (ReductionIdScopeSpec.isInvalid())
9135     return ExprError();
9136   SmallVector<UnresolvedSet<8>, 4> Lookups;
9137   if (S) {
9138     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
9139     Lookup.suppressDiagnostics();
9140     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
9141       auto *D = Lookup.getRepresentativeDecl();
9142       do {
9143         S = S->getParent();
9144       } while (S && !S->isDeclScope(D));
9145       if (S)
9146         S = S->getParent();
9147       Lookups.push_back(UnresolvedSet<8>());
9148       Lookups.back().append(Lookup.begin(), Lookup.end());
9149       Lookup.clear();
9150     }
9151   } else if (auto *ULE =
9152                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
9153     Lookups.push_back(UnresolvedSet<8>());
9154     Decl *PrevD = nullptr;
9155     for (auto *D : ULE->decls()) {
9156       if (D == PrevD)
9157         Lookups.push_back(UnresolvedSet<8>());
9158       else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
9159         Lookups.back().addDecl(DRD);
9160       PrevD = D;
9161     }
9162   }
9163   if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
9164       Ty->containsUnexpandedParameterPack() ||
9165       filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
9166         return !D->isInvalidDecl() &&
9167                (D->getType()->isDependentType() ||
9168                 D->getType()->isInstantiationDependentType() ||
9169                 D->getType()->containsUnexpandedParameterPack());
9170       })) {
9171     UnresolvedSet<8> ResSet;
9172     for (auto &Set : Lookups) {
9173       ResSet.append(Set.begin(), Set.end());
9174       // The last item marks the end of all declarations at the specified scope.
9175       ResSet.addDecl(Set[Set.size() - 1]);
9176     }
9177     return UnresolvedLookupExpr::Create(
9178         SemaRef.Context, /*NamingClass=*/nullptr,
9179         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
9180         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
9181   }
9182   if (auto *VD = filterLookupForUDR<ValueDecl *>(
9183           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
9184             if (!D->isInvalidDecl() &&
9185                 SemaRef.Context.hasSameType(D->getType(), Ty))
9186               return D;
9187             return nullptr;
9188           }))
9189     return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9190   if (auto *VD = filterLookupForUDR<ValueDecl *>(
9191           Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
9192             if (!D->isInvalidDecl() &&
9193                 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
9194                 !Ty.isMoreQualifiedThan(D->getType()))
9195               return D;
9196             return nullptr;
9197           })) {
9198     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
9199                        /*DetectVirtual=*/false);
9200     if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
9201       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
9202               VD->getType().getUnqualifiedType()))) {
9203         if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
9204                                          /*DiagID=*/0) !=
9205             Sema::AR_inaccessible) {
9206           SemaRef.BuildBasePathArray(Paths, BasePath);
9207           return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
9208         }
9209       }
9210     }
9211   }
9212   if (ReductionIdScopeSpec.isSet()) {
9213     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
9214     return ExprError();
9215   }
9216   return ExprEmpty();
9217 }
9218 
9219 OMPClause *Sema::ActOnOpenMPReductionClause(
9220     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
9221     SourceLocation ColonLoc, SourceLocation EndLoc,
9222     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
9223     ArrayRef<Expr *> UnresolvedReductions) {
9224   auto DN = ReductionId.getName();
9225   auto OOK = DN.getCXXOverloadedOperator();
9226   BinaryOperatorKind BOK = BO_Comma;
9227 
9228   // OpenMP [2.14.3.6, reduction clause]
9229   // C
9230   // reduction-identifier is either an identifier or one of the following
9231   // operators: +, -, *,  &, |, ^, && and ||
9232   // C++
9233   // reduction-identifier is either an id-expression or one of the following
9234   // operators: +, -, *, &, |, ^, && and ||
9235   // FIXME: Only 'min' and 'max' identifiers are supported for now.
9236   switch (OOK) {
9237   case OO_Plus:
9238   case OO_Minus:
9239     BOK = BO_Add;
9240     break;
9241   case OO_Star:
9242     BOK = BO_Mul;
9243     break;
9244   case OO_Amp:
9245     BOK = BO_And;
9246     break;
9247   case OO_Pipe:
9248     BOK = BO_Or;
9249     break;
9250   case OO_Caret:
9251     BOK = BO_Xor;
9252     break;
9253   case OO_AmpAmp:
9254     BOK = BO_LAnd;
9255     break;
9256   case OO_PipePipe:
9257     BOK = BO_LOr;
9258     break;
9259   case OO_New:
9260   case OO_Delete:
9261   case OO_Array_New:
9262   case OO_Array_Delete:
9263   case OO_Slash:
9264   case OO_Percent:
9265   case OO_Tilde:
9266   case OO_Exclaim:
9267   case OO_Equal:
9268   case OO_Less:
9269   case OO_Greater:
9270   case OO_LessEqual:
9271   case OO_GreaterEqual:
9272   case OO_PlusEqual:
9273   case OO_MinusEqual:
9274   case OO_StarEqual:
9275   case OO_SlashEqual:
9276   case OO_PercentEqual:
9277   case OO_CaretEqual:
9278   case OO_AmpEqual:
9279   case OO_PipeEqual:
9280   case OO_LessLess:
9281   case OO_GreaterGreater:
9282   case OO_LessLessEqual:
9283   case OO_GreaterGreaterEqual:
9284   case OO_EqualEqual:
9285   case OO_ExclaimEqual:
9286   case OO_PlusPlus:
9287   case OO_MinusMinus:
9288   case OO_Comma:
9289   case OO_ArrowStar:
9290   case OO_Arrow:
9291   case OO_Call:
9292   case OO_Subscript:
9293   case OO_Conditional:
9294   case OO_Coawait:
9295   case NUM_OVERLOADED_OPERATORS:
9296     llvm_unreachable("Unexpected reduction identifier");
9297   case OO_None:
9298     if (auto II = DN.getAsIdentifierInfo()) {
9299       if (II->isStr("max"))
9300         BOK = BO_GT;
9301       else if (II->isStr("min"))
9302         BOK = BO_LT;
9303     }
9304     break;
9305   }
9306   SourceRange ReductionIdRange;
9307   if (ReductionIdScopeSpec.isValid())
9308     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
9309   ReductionIdRange.setEnd(ReductionId.getEndLoc());
9310 
9311   SmallVector<Expr *, 8> Vars;
9312   SmallVector<Expr *, 8> Privates;
9313   SmallVector<Expr *, 8> LHSs;
9314   SmallVector<Expr *, 8> RHSs;
9315   SmallVector<Expr *, 8> ReductionOps;
9316   SmallVector<Decl *, 4> ExprCaptures;
9317   SmallVector<Expr *, 4> ExprPostUpdates;
9318   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9319   bool FirstIter = true;
9320   for (auto RefExpr : VarList) {
9321     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
9322     // OpenMP [2.1, C/C++]
9323     //  A list item is a variable or array section, subject to the restrictions
9324     //  specified in Section 2.4 on page 42 and in each of the sections
9325     // describing clauses and directives for which a list appears.
9326     // OpenMP  [2.14.3.3, Restrictions, p.1]
9327     //  A variable that is part of another variable (as an array or
9328     //  structure element) cannot appear in a private clause.
9329     if (!FirstIter && IR != ER)
9330       ++IR;
9331     FirstIter = false;
9332     SourceLocation ELoc;
9333     SourceRange ERange;
9334     Expr *SimpleRefExpr = RefExpr;
9335     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9336                               /*AllowArraySection=*/true);
9337     if (Res.second) {
9338       // It will be analyzed later.
9339       Vars.push_back(RefExpr);
9340       Privates.push_back(nullptr);
9341       LHSs.push_back(nullptr);
9342       RHSs.push_back(nullptr);
9343       // Try to find 'declare reduction' corresponding construct before using
9344       // builtin/overloaded operators.
9345       QualType Type = Context.DependentTy;
9346       CXXCastPath BasePath;
9347       ExprResult DeclareReductionRef = buildDeclareReductionRef(
9348           *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9349           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9350       if (CurContext->isDependentContext() &&
9351           (DeclareReductionRef.isUnset() ||
9352            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9353         ReductionOps.push_back(DeclareReductionRef.get());
9354       else
9355         ReductionOps.push_back(nullptr);
9356     }
9357     ValueDecl *D = Res.first;
9358     if (!D)
9359       continue;
9360 
9361     QualType Type;
9362     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9363     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9364     if (ASE)
9365       Type = ASE->getType().getNonReferenceType();
9366     else if (OASE) {
9367       auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9368       if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9369         Type = ATy->getElementType();
9370       else
9371         Type = BaseType->getPointeeType();
9372       Type = Type.getNonReferenceType();
9373     } else
9374       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9375     auto *VD = dyn_cast<VarDecl>(D);
9376 
9377     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9378     //  A variable that appears in a private clause must not have an incomplete
9379     //  type or a reference type.
9380     if (RequireCompleteType(ELoc, Type,
9381                             diag::err_omp_reduction_incomplete_type))
9382       continue;
9383     // OpenMP [2.14.3.6, reduction clause, Restrictions]
9384     // A list item that appears in a reduction clause must not be
9385     // const-qualified.
9386     if (Type.getNonReferenceType().isConstant(Context)) {
9387       Diag(ELoc, diag::err_omp_const_reduction_list_item)
9388           << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
9389       if (!ASE && !OASE) {
9390         bool IsDecl = !VD ||
9391                       VD->isThisDeclarationADefinition(Context) ==
9392                           VarDecl::DeclarationOnly;
9393         Diag(D->getLocation(),
9394              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9395             << D;
9396       }
9397       continue;
9398     }
9399     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9400     //  If a list-item is a reference type then it must bind to the same object
9401     //  for all threads of the team.
9402     if (!ASE && !OASE && VD) {
9403       VarDecl *VDDef = VD->getDefinition();
9404       if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
9405         DSARefChecker Check(DSAStack);
9406         if (Check.Visit(VDDef->getInit())) {
9407           Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9408           Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9409           continue;
9410         }
9411       }
9412     }
9413 
9414     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9415     // in a Construct]
9416     //  Variables with the predetermined data-sharing attributes may not be
9417     //  listed in data-sharing attributes clauses, except for the cases
9418     //  listed below. For these exceptions only, listing a predetermined
9419     //  variable in a data-sharing attribute clause is allowed and overrides
9420     //  the variable's predetermined data-sharing attributes.
9421     // OpenMP [2.14.3.6, Restrictions, p.3]
9422     //  Any number of reduction clauses can be specified on the directive,
9423     //  but a list item can appear only once in the reduction clauses for that
9424     //  directive.
9425     DSAStackTy::DSAVarData DVar;
9426     DVar = DSAStack->getTopDSA(D, false);
9427     if (DVar.CKind == OMPC_reduction) {
9428       Diag(ELoc, diag::err_omp_once_referenced)
9429           << getOpenMPClauseName(OMPC_reduction);
9430       if (DVar.RefExpr)
9431         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
9432     } else if (DVar.CKind != OMPC_unknown) {
9433       Diag(ELoc, diag::err_omp_wrong_dsa)
9434           << getOpenMPClauseName(DVar.CKind)
9435           << getOpenMPClauseName(OMPC_reduction);
9436       ReportOriginalDSA(*this, DSAStack, D, DVar);
9437       continue;
9438     }
9439 
9440     // OpenMP [2.14.3.6, Restrictions, p.1]
9441     //  A list item that appears in a reduction clause of a worksharing
9442     //  construct must be shared in the parallel regions to which any of the
9443     //  worksharing regions arising from the worksharing construct bind.
9444     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9445     if (isOpenMPWorksharingDirective(CurrDir) &&
9446         !isOpenMPParallelDirective(CurrDir)) {
9447       DVar = DSAStack->getImplicitDSA(D, true);
9448       if (DVar.CKind != OMPC_shared) {
9449         Diag(ELoc, diag::err_omp_required_access)
9450             << getOpenMPClauseName(OMPC_reduction)
9451             << getOpenMPClauseName(OMPC_shared);
9452         ReportOriginalDSA(*this, DSAStack, D, DVar);
9453         continue;
9454       }
9455     }
9456 
9457     // Try to find 'declare reduction' corresponding construct before using
9458     // builtin/overloaded operators.
9459     CXXCastPath BasePath;
9460     ExprResult DeclareReductionRef = buildDeclareReductionRef(
9461         *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9462         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9463     if (DeclareReductionRef.isInvalid())
9464       continue;
9465     if (CurContext->isDependentContext() &&
9466         (DeclareReductionRef.isUnset() ||
9467          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9468       Vars.push_back(RefExpr);
9469       Privates.push_back(nullptr);
9470       LHSs.push_back(nullptr);
9471       RHSs.push_back(nullptr);
9472       ReductionOps.push_back(DeclareReductionRef.get());
9473       continue;
9474     }
9475     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9476       // Not allowed reduction identifier is found.
9477       Diag(ReductionId.getLocStart(),
9478            diag::err_omp_unknown_reduction_identifier)
9479           << Type << ReductionIdRange;
9480       continue;
9481     }
9482 
9483     // OpenMP [2.14.3.6, reduction clause, Restrictions]
9484     // The type of a list item that appears in a reduction clause must be valid
9485     // for the reduction-identifier. For a max or min reduction in C, the type
9486     // of the list item must be an allowed arithmetic data type: char, int,
9487     // float, double, or _Bool, possibly modified with long, short, signed, or
9488     // unsigned. For a max or min reduction in C++, the type of the list item
9489     // must be an allowed arithmetic data type: char, wchar_t, int, float,
9490     // double, or bool, possibly modified with long, short, signed, or unsigned.
9491     if (DeclareReductionRef.isUnset()) {
9492       if ((BOK == BO_GT || BOK == BO_LT) &&
9493           !(Type->isScalarType() ||
9494             (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9495         Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9496             << getLangOpts().CPlusPlus;
9497         if (!ASE && !OASE) {
9498           bool IsDecl = !VD ||
9499                         VD->isThisDeclarationADefinition(Context) ==
9500                             VarDecl::DeclarationOnly;
9501           Diag(D->getLocation(),
9502                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9503               << D;
9504         }
9505         continue;
9506       }
9507       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9508           !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9509         Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9510         if (!ASE && !OASE) {
9511           bool IsDecl = !VD ||
9512                         VD->isThisDeclarationADefinition(Context) ==
9513                             VarDecl::DeclarationOnly;
9514           Diag(D->getLocation(),
9515                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9516               << D;
9517         }
9518         continue;
9519       }
9520     }
9521 
9522     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
9523     auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
9524                                D->hasAttrs() ? &D->getAttrs() : nullptr);
9525     auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9526                                D->hasAttrs() ? &D->getAttrs() : nullptr);
9527     auto PrivateTy = Type;
9528     if (OASE ||
9529         (!ASE &&
9530          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
9531       // For arrays/array sections only:
9532       // Create pseudo array type for private copy. The size for this array will
9533       // be generated during codegen.
9534       // For array subscripts or single variables Private Ty is the same as Type
9535       // (type of the variable or single array element).
9536       PrivateTy = Context.getVariableArrayType(
9537           Type, new (Context) OpaqueValueExpr(SourceLocation(),
9538                                               Context.getSizeType(), VK_RValue),
9539           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
9540     } else if (!ASE && !OASE &&
9541                Context.getAsArrayType(D->getType().getNonReferenceType()))
9542       PrivateTy = D->getType().getNonReferenceType();
9543     // Private copy.
9544     auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9545                                    D->hasAttrs() ? &D->getAttrs() : nullptr);
9546     // Add initializer for private variable.
9547     Expr *Init = nullptr;
9548     auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9549     auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9550     if (DeclareReductionRef.isUsable()) {
9551       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9552       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9553       if (DRD->getInitializer()) {
9554         Init = DRDRef;
9555         RHSVD->setInit(DRDRef);
9556         RHSVD->setInitStyle(VarDecl::CallInit);
9557       }
9558     } else {
9559       switch (BOK) {
9560       case BO_Add:
9561       case BO_Xor:
9562       case BO_Or:
9563       case BO_LOr:
9564         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9565         if (Type->isScalarType() || Type->isAnyComplexType())
9566           Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9567         break;
9568       case BO_Mul:
9569       case BO_LAnd:
9570         if (Type->isScalarType() || Type->isAnyComplexType()) {
9571           // '*' and '&&' reduction ops - initializer is '1'.
9572           Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
9573         }
9574         break;
9575       case BO_And: {
9576         // '&' reduction op - initializer is '~0'.
9577         QualType OrigType = Type;
9578         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9579           Type = ComplexTy->getElementType();
9580         if (Type->isRealFloatingType()) {
9581           llvm::APFloat InitValue =
9582               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9583                                              /*isIEEE=*/true);
9584           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9585                                          Type, ELoc);
9586         } else if (Type->isScalarType()) {
9587           auto Size = Context.getTypeSize(Type);
9588           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9589           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9590           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9591         }
9592         if (Init && OrigType->isAnyComplexType()) {
9593           // Init = 0xFFFF + 0xFFFFi;
9594           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9595           Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9596         }
9597         Type = OrigType;
9598         break;
9599       }
9600       case BO_LT:
9601       case BO_GT: {
9602         // 'min' reduction op - initializer is 'Largest representable number in
9603         // the reduction list item type'.
9604         // 'max' reduction op - initializer is 'Least representable number in
9605         // the reduction list item type'.
9606         if (Type->isIntegerType() || Type->isPointerType()) {
9607           bool IsSigned = Type->hasSignedIntegerRepresentation();
9608           auto Size = Context.getTypeSize(Type);
9609           QualType IntTy =
9610               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9611           llvm::APInt InitValue =
9612               (BOK != BO_LT)
9613                   ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9614                              : llvm::APInt::getMinValue(Size)
9615                   : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9616                              : llvm::APInt::getMaxValue(Size);
9617           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9618           if (Type->isPointerType()) {
9619             // Cast to pointer type.
9620             auto CastExpr = BuildCStyleCastExpr(
9621                 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9622                 SourceLocation(), Init);
9623             if (CastExpr.isInvalid())
9624               continue;
9625             Init = CastExpr.get();
9626           }
9627         } else if (Type->isRealFloatingType()) {
9628           llvm::APFloat InitValue = llvm::APFloat::getLargest(
9629               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9630           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9631                                          Type, ELoc);
9632         }
9633         break;
9634       }
9635       case BO_PtrMemD:
9636       case BO_PtrMemI:
9637       case BO_MulAssign:
9638       case BO_Div:
9639       case BO_Rem:
9640       case BO_Sub:
9641       case BO_Shl:
9642       case BO_Shr:
9643       case BO_LE:
9644       case BO_GE:
9645       case BO_EQ:
9646       case BO_NE:
9647       case BO_AndAssign:
9648       case BO_XorAssign:
9649       case BO_OrAssign:
9650       case BO_Assign:
9651       case BO_AddAssign:
9652       case BO_SubAssign:
9653       case BO_DivAssign:
9654       case BO_RemAssign:
9655       case BO_ShlAssign:
9656       case BO_ShrAssign:
9657       case BO_Comma:
9658         llvm_unreachable("Unexpected reduction operation");
9659       }
9660     }
9661     if (Init && DeclareReductionRef.isUnset()) {
9662       AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9663                            /*TypeMayContainAuto=*/false);
9664     } else if (!Init)
9665       ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
9666     if (RHSVD->isInvalidDecl())
9667       continue;
9668     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
9669       Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9670                                                             << ReductionIdRange;
9671       bool IsDecl =
9672           !VD ||
9673           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9674       Diag(D->getLocation(),
9675            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9676           << D;
9677       continue;
9678     }
9679     // Store initializer for single element in private copy. Will be used during
9680     // codegen.
9681     PrivateVD->setInit(RHSVD->getInit());
9682     PrivateVD->setInitStyle(RHSVD->getInitStyle());
9683     auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
9684     ExprResult ReductionOp;
9685     if (DeclareReductionRef.isUsable()) {
9686       QualType RedTy = DeclareReductionRef.get()->getType();
9687       QualType PtrRedTy = Context.getPointerType(RedTy);
9688       ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9689       ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9690       if (!BasePath.empty()) {
9691         LHS = DefaultLvalueConversion(LHS.get());
9692         RHS = DefaultLvalueConversion(RHS.get());
9693         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9694                                        CK_UncheckedDerivedToBase, LHS.get(),
9695                                        &BasePath, LHS.get()->getValueKind());
9696         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9697                                        CK_UncheckedDerivedToBase, RHS.get(),
9698                                        &BasePath, RHS.get()->getValueKind());
9699       }
9700       FunctionProtoType::ExtProtoInfo EPI;
9701       QualType Params[] = {PtrRedTy, PtrRedTy};
9702       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9703       auto *OVE = new (Context) OpaqueValueExpr(
9704           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9705           DefaultLvalueConversion(DeclareReductionRef.get()).get());
9706       Expr *Args[] = {LHS.get(), RHS.get()};
9707       ReductionOp = new (Context)
9708           CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9709     } else {
9710       ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9711                                ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9712       if (ReductionOp.isUsable()) {
9713         if (BOK != BO_LT && BOK != BO_GT) {
9714           ReductionOp =
9715               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9716                          BO_Assign, LHSDRE, ReductionOp.get());
9717         } else {
9718           auto *ConditionalOp = new (Context) ConditionalOperator(
9719               ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9720               RHSDRE, Type, VK_LValue, OK_Ordinary);
9721           ReductionOp =
9722               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9723                          BO_Assign, LHSDRE, ConditionalOp);
9724         }
9725         ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9726       }
9727       if (ReductionOp.isInvalid())
9728         continue;
9729     }
9730 
9731     DeclRefExpr *Ref = nullptr;
9732     Expr *VarsExpr = RefExpr->IgnoreParens();
9733     if (!VD && !CurContext->isDependentContext()) {
9734       if (ASE || OASE) {
9735         TransformExprToCaptures RebuildToCapture(*this, D);
9736         VarsExpr =
9737             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9738         Ref = RebuildToCapture.getCapturedExpr();
9739       } else {
9740         VarsExpr = Ref =
9741             buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9742       }
9743       if (!IsOpenMPCapturedDecl(D)) {
9744         ExprCaptures.push_back(Ref->getDecl());
9745         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9746           ExprResult RefRes = DefaultLvalueConversion(Ref);
9747           if (!RefRes.isUsable())
9748             continue;
9749           ExprResult PostUpdateRes =
9750               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9751                          SimpleRefExpr, RefRes.get());
9752           if (!PostUpdateRes.isUsable())
9753             continue;
9754           ExprPostUpdates.push_back(
9755               IgnoredValueConversions(PostUpdateRes.get()).get());
9756         }
9757       }
9758     }
9759     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9760     Vars.push_back(VarsExpr);
9761     Privates.push_back(PrivateDRE);
9762     LHSs.push_back(LHSDRE);
9763     RHSs.push_back(RHSDRE);
9764     ReductionOps.push_back(ReductionOp.get());
9765   }
9766 
9767   if (Vars.empty())
9768     return nullptr;
9769 
9770   return OMPReductionClause::Create(
9771       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
9772       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
9773       LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9774       buildPostUpdate(*this, ExprPostUpdates));
9775 }
9776 
9777 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9778                                      SourceLocation LinLoc) {
9779   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9780       LinKind == OMPC_LINEAR_unknown) {
9781     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9782     return true;
9783   }
9784   return false;
9785 }
9786 
9787 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9788                                  OpenMPLinearClauseKind LinKind,
9789                                  QualType Type) {
9790   auto *VD = dyn_cast_or_null<VarDecl>(D);
9791   // A variable must not have an incomplete type or a reference type.
9792   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9793     return true;
9794   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9795       !Type->isReferenceType()) {
9796     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9797         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9798     return true;
9799   }
9800   Type = Type.getNonReferenceType();
9801 
9802   // A list item must not be const-qualified.
9803   if (Type.isConstant(Context)) {
9804     Diag(ELoc, diag::err_omp_const_variable)
9805         << getOpenMPClauseName(OMPC_linear);
9806     if (D) {
9807       bool IsDecl =
9808           !VD ||
9809           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9810       Diag(D->getLocation(),
9811            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9812           << D;
9813     }
9814     return true;
9815   }
9816 
9817   // A list item must be of integral or pointer type.
9818   Type = Type.getUnqualifiedType().getCanonicalType();
9819   const auto *Ty = Type.getTypePtrOrNull();
9820   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9821               !Ty->isPointerType())) {
9822     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9823     if (D) {
9824       bool IsDecl =
9825           !VD ||
9826           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9827       Diag(D->getLocation(),
9828            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9829           << D;
9830     }
9831     return true;
9832   }
9833   return false;
9834 }
9835 
9836 OMPClause *Sema::ActOnOpenMPLinearClause(
9837     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9838     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9839     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9840   SmallVector<Expr *, 8> Vars;
9841   SmallVector<Expr *, 8> Privates;
9842   SmallVector<Expr *, 8> Inits;
9843   SmallVector<Decl *, 4> ExprCaptures;
9844   SmallVector<Expr *, 4> ExprPostUpdates;
9845   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
9846     LinKind = OMPC_LINEAR_val;
9847   for (auto &RefExpr : VarList) {
9848     assert(RefExpr && "NULL expr in OpenMP linear clause.");
9849     SourceLocation ELoc;
9850     SourceRange ERange;
9851     Expr *SimpleRefExpr = RefExpr;
9852     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9853                               /*AllowArraySection=*/false);
9854     if (Res.second) {
9855       // It will be analyzed later.
9856       Vars.push_back(RefExpr);
9857       Privates.push_back(nullptr);
9858       Inits.push_back(nullptr);
9859     }
9860     ValueDecl *D = Res.first;
9861     if (!D)
9862       continue;
9863 
9864     QualType Type = D->getType();
9865     auto *VD = dyn_cast<VarDecl>(D);
9866 
9867     // OpenMP [2.14.3.7, linear clause]
9868     //  A list-item cannot appear in more than one linear clause.
9869     //  A list-item that appears in a linear clause cannot appear in any
9870     //  other data-sharing attribute clause.
9871     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9872     if (DVar.RefExpr) {
9873       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9874                                           << getOpenMPClauseName(OMPC_linear);
9875       ReportOriginalDSA(*this, DSAStack, D, DVar);
9876       continue;
9877     }
9878 
9879     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
9880       continue;
9881     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
9882 
9883     // Build private copy of original var.
9884     auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9885                                  D->hasAttrs() ? &D->getAttrs() : nullptr);
9886     auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
9887     // Build var to save initial value.
9888     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
9889     Expr *InitExpr;
9890     DeclRefExpr *Ref = nullptr;
9891     if (!VD && !CurContext->isDependentContext()) {
9892       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9893       if (!IsOpenMPCapturedDecl(D)) {
9894         ExprCaptures.push_back(Ref->getDecl());
9895         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9896           ExprResult RefRes = DefaultLvalueConversion(Ref);
9897           if (!RefRes.isUsable())
9898             continue;
9899           ExprResult PostUpdateRes =
9900               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9901                          SimpleRefExpr, RefRes.get());
9902           if (!PostUpdateRes.isUsable())
9903             continue;
9904           ExprPostUpdates.push_back(
9905               IgnoredValueConversions(PostUpdateRes.get()).get());
9906         }
9907       }
9908     }
9909     if (LinKind == OMPC_LINEAR_uval)
9910       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
9911     else
9912       InitExpr = VD ? SimpleRefExpr : Ref;
9913     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
9914                          /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9915     auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9916 
9917     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9918     Vars.push_back((VD || CurContext->isDependentContext())
9919                        ? RefExpr->IgnoreParens()
9920                        : Ref);
9921     Privates.push_back(PrivateRef);
9922     Inits.push_back(InitRef);
9923   }
9924 
9925   if (Vars.empty())
9926     return nullptr;
9927 
9928   Expr *StepExpr = Step;
9929   Expr *CalcStepExpr = nullptr;
9930   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9931       !Step->isInstantiationDependent() &&
9932       !Step->containsUnexpandedParameterPack()) {
9933     SourceLocation StepLoc = Step->getLocStart();
9934     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
9935     if (Val.isInvalid())
9936       return nullptr;
9937     StepExpr = Val.get();
9938 
9939     // Build var to save the step value.
9940     VarDecl *SaveVar =
9941         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
9942     ExprResult SaveRef =
9943         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
9944     ExprResult CalcStep =
9945         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
9946     CalcStep = ActOnFinishFullExpr(CalcStep.get());
9947 
9948     // Warn about zero linear step (it would be probably better specified as
9949     // making corresponding variables 'const').
9950     llvm::APSInt Result;
9951     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9952     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
9953       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9954                                                      << (Vars.size() > 1);
9955     if (!IsConstant && CalcStep.isUsable()) {
9956       // Calculate the step beforehand instead of doing this on each iteration.
9957       // (This is not used if the number of iterations may be kfold-ed).
9958       CalcStepExpr = CalcStep.get();
9959     }
9960   }
9961 
9962   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9963                                  ColonLoc, EndLoc, Vars, Privates, Inits,
9964                                  StepExpr, CalcStepExpr,
9965                                  buildPreInits(Context, ExprCaptures),
9966                                  buildPostUpdate(*this, ExprPostUpdates));
9967 }
9968 
9969 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9970                                      Expr *NumIterations, Sema &SemaRef,
9971                                      Scope *S, DSAStackTy *Stack) {
9972   // Walk the vars and build update/final expressions for the CodeGen.
9973   SmallVector<Expr *, 8> Updates;
9974   SmallVector<Expr *, 8> Finals;
9975   Expr *Step = Clause.getStep();
9976   Expr *CalcStep = Clause.getCalcStep();
9977   // OpenMP [2.14.3.7, linear clause]
9978   // If linear-step is not specified it is assumed to be 1.
9979   if (Step == nullptr)
9980     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
9981   else if (CalcStep) {
9982     Step = cast<BinaryOperator>(CalcStep)->getLHS();
9983   }
9984   bool HasErrors = false;
9985   auto CurInit = Clause.inits().begin();
9986   auto CurPrivate = Clause.privates().begin();
9987   auto LinKind = Clause.getModifier();
9988   for (auto &RefExpr : Clause.varlists()) {
9989     SourceLocation ELoc;
9990     SourceRange ERange;
9991     Expr *SimpleRefExpr = RefExpr;
9992     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9993                               /*AllowArraySection=*/false);
9994     ValueDecl *D = Res.first;
9995     if (Res.second || !D) {
9996       Updates.push_back(nullptr);
9997       Finals.push_back(nullptr);
9998       HasErrors = true;
9999       continue;
10000     }
10001     if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
10002       D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
10003               ->getMemberDecl();
10004     }
10005     auto &&Info = Stack->isLoopControlVariable(D);
10006     Expr *InitExpr = *CurInit;
10007 
10008     // Build privatized reference to the current linear var.
10009     auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
10010     Expr *CapturedRef;
10011     if (LinKind == OMPC_LINEAR_uval)
10012       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
10013     else
10014       CapturedRef =
10015           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
10016                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
10017                            /*RefersToCapture=*/true);
10018 
10019     // Build update: Var = InitExpr + IV * Step
10020     ExprResult Update;
10021     if (!Info.first) {
10022       Update =
10023           BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
10024                              InitExpr, IV, Step, /* Subtract */ false);
10025     } else
10026       Update = *CurPrivate;
10027     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
10028                                          /*DiscardedValue=*/true);
10029 
10030     // Build final: Var = InitExpr + NumIterations * Step
10031     ExprResult Final;
10032     if (!Info.first) {
10033       Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
10034                                  InitExpr, NumIterations, Step,
10035                                  /* Subtract */ false);
10036     } else
10037       Final = *CurPrivate;
10038     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
10039                                         /*DiscardedValue=*/true);
10040 
10041     if (!Update.isUsable() || !Final.isUsable()) {
10042       Updates.push_back(nullptr);
10043       Finals.push_back(nullptr);
10044       HasErrors = true;
10045     } else {
10046       Updates.push_back(Update.get());
10047       Finals.push_back(Final.get());
10048     }
10049     ++CurInit;
10050     ++CurPrivate;
10051   }
10052   Clause.setUpdates(Updates);
10053   Clause.setFinals(Finals);
10054   return HasErrors;
10055 }
10056 
10057 OMPClause *Sema::ActOnOpenMPAlignedClause(
10058     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
10059     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
10060 
10061   SmallVector<Expr *, 8> Vars;
10062   for (auto &RefExpr : VarList) {
10063     assert(RefExpr && "NULL expr in OpenMP linear clause.");
10064     SourceLocation ELoc;
10065     SourceRange ERange;
10066     Expr *SimpleRefExpr = RefExpr;
10067     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10068                               /*AllowArraySection=*/false);
10069     if (Res.second) {
10070       // It will be analyzed later.
10071       Vars.push_back(RefExpr);
10072     }
10073     ValueDecl *D = Res.first;
10074     if (!D)
10075       continue;
10076 
10077     QualType QType = D->getType();
10078     auto *VD = dyn_cast<VarDecl>(D);
10079 
10080     // OpenMP  [2.8.1, simd construct, Restrictions]
10081     // The type of list items appearing in the aligned clause must be
10082     // array, pointer, reference to array, or reference to pointer.
10083     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
10084     const Type *Ty = QType.getTypePtrOrNull();
10085     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
10086       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
10087           << QType << getLangOpts().CPlusPlus << ERange;
10088       bool IsDecl =
10089           !VD ||
10090           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10091       Diag(D->getLocation(),
10092            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10093           << D;
10094       continue;
10095     }
10096 
10097     // OpenMP  [2.8.1, simd construct, Restrictions]
10098     // A list-item cannot appear in more than one aligned clause.
10099     if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
10100       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
10101       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
10102           << getOpenMPClauseName(OMPC_aligned);
10103       continue;
10104     }
10105 
10106     DeclRefExpr *Ref = nullptr;
10107     if (!VD && IsOpenMPCapturedDecl(D))
10108       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
10109     Vars.push_back(DefaultFunctionArrayConversion(
10110                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
10111                        .get());
10112   }
10113 
10114   // OpenMP [2.8.1, simd construct, Description]
10115   // The parameter of the aligned clause, alignment, must be a constant
10116   // positive integer expression.
10117   // If no optional parameter is specified, implementation-defined default
10118   // alignments for SIMD instructions on the target platforms are assumed.
10119   if (Alignment != nullptr) {
10120     ExprResult AlignResult =
10121         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
10122     if (AlignResult.isInvalid())
10123       return nullptr;
10124     Alignment = AlignResult.get();
10125   }
10126   if (Vars.empty())
10127     return nullptr;
10128 
10129   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
10130                                   EndLoc, Vars, Alignment);
10131 }
10132 
10133 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
10134                                          SourceLocation StartLoc,
10135                                          SourceLocation LParenLoc,
10136                                          SourceLocation EndLoc) {
10137   SmallVector<Expr *, 8> Vars;
10138   SmallVector<Expr *, 8> SrcExprs;
10139   SmallVector<Expr *, 8> DstExprs;
10140   SmallVector<Expr *, 8> AssignmentOps;
10141   for (auto &RefExpr : VarList) {
10142     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
10143     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
10144       // It will be analyzed later.
10145       Vars.push_back(RefExpr);
10146       SrcExprs.push_back(nullptr);
10147       DstExprs.push_back(nullptr);
10148       AssignmentOps.push_back(nullptr);
10149       continue;
10150     }
10151 
10152     SourceLocation ELoc = RefExpr->getExprLoc();
10153     // OpenMP [2.1, C/C++]
10154     //  A list item is a variable name.
10155     // OpenMP  [2.14.4.1, Restrictions, p.1]
10156     //  A list item that appears in a copyin clause must be threadprivate.
10157     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
10158     if (!DE || !isa<VarDecl>(DE->getDecl())) {
10159       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
10160           << 0 << RefExpr->getSourceRange();
10161       continue;
10162     }
10163 
10164     Decl *D = DE->getDecl();
10165     VarDecl *VD = cast<VarDecl>(D);
10166 
10167     QualType Type = VD->getType();
10168     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
10169       // It will be analyzed later.
10170       Vars.push_back(DE);
10171       SrcExprs.push_back(nullptr);
10172       DstExprs.push_back(nullptr);
10173       AssignmentOps.push_back(nullptr);
10174       continue;
10175     }
10176 
10177     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
10178     //  A list item that appears in a copyin clause must be threadprivate.
10179     if (!DSAStack->isThreadPrivate(VD)) {
10180       Diag(ELoc, diag::err_omp_required_access)
10181           << getOpenMPClauseName(OMPC_copyin)
10182           << getOpenMPDirectiveName(OMPD_threadprivate);
10183       continue;
10184     }
10185 
10186     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10187     //  A variable of class type (or array thereof) that appears in a
10188     //  copyin clause requires an accessible, unambiguous copy assignment
10189     //  operator for the class type.
10190     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
10191     auto *SrcVD =
10192         buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
10193                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
10194     auto *PseudoSrcExpr = buildDeclRefExpr(
10195         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
10196     auto *DstVD =
10197         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
10198                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
10199     auto *PseudoDstExpr =
10200         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
10201     // For arrays generate assignment operation for single element and replace
10202     // it by the original array element in CodeGen.
10203     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
10204                                    PseudoDstExpr, PseudoSrcExpr);
10205     if (AssignmentOp.isInvalid())
10206       continue;
10207     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
10208                                        /*DiscardedValue=*/true);
10209     if (AssignmentOp.isInvalid())
10210       continue;
10211 
10212     DSAStack->addDSA(VD, DE, OMPC_copyin);
10213     Vars.push_back(DE);
10214     SrcExprs.push_back(PseudoSrcExpr);
10215     DstExprs.push_back(PseudoDstExpr);
10216     AssignmentOps.push_back(AssignmentOp.get());
10217   }
10218 
10219   if (Vars.empty())
10220     return nullptr;
10221 
10222   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10223                                  SrcExprs, DstExprs, AssignmentOps);
10224 }
10225 
10226 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
10227                                               SourceLocation StartLoc,
10228                                               SourceLocation LParenLoc,
10229                                               SourceLocation EndLoc) {
10230   SmallVector<Expr *, 8> Vars;
10231   SmallVector<Expr *, 8> SrcExprs;
10232   SmallVector<Expr *, 8> DstExprs;
10233   SmallVector<Expr *, 8> AssignmentOps;
10234   for (auto &RefExpr : VarList) {
10235     assert(RefExpr && "NULL expr in OpenMP linear clause.");
10236     SourceLocation ELoc;
10237     SourceRange ERange;
10238     Expr *SimpleRefExpr = RefExpr;
10239     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10240                               /*AllowArraySection=*/false);
10241     if (Res.second) {
10242       // It will be analyzed later.
10243       Vars.push_back(RefExpr);
10244       SrcExprs.push_back(nullptr);
10245       DstExprs.push_back(nullptr);
10246       AssignmentOps.push_back(nullptr);
10247     }
10248     ValueDecl *D = Res.first;
10249     if (!D)
10250       continue;
10251 
10252     QualType Type = D->getType();
10253     auto *VD = dyn_cast<VarDecl>(D);
10254 
10255     // OpenMP [2.14.4.2, Restrictions, p.2]
10256     //  A list item that appears in a copyprivate clause may not appear in a
10257     //  private or firstprivate clause on the single construct.
10258     if (!VD || !DSAStack->isThreadPrivate(VD)) {
10259       auto DVar = DSAStack->getTopDSA(D, false);
10260       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10261           DVar.RefExpr) {
10262         Diag(ELoc, diag::err_omp_wrong_dsa)
10263             << getOpenMPClauseName(DVar.CKind)
10264             << getOpenMPClauseName(OMPC_copyprivate);
10265         ReportOriginalDSA(*this, DSAStack, D, DVar);
10266         continue;
10267       }
10268 
10269       // OpenMP [2.11.4.2, Restrictions, p.1]
10270       //  All list items that appear in a copyprivate clause must be either
10271       //  threadprivate or private in the enclosing context.
10272       if (DVar.CKind == OMPC_unknown) {
10273         DVar = DSAStack->getImplicitDSA(D, false);
10274         if (DVar.CKind == OMPC_shared) {
10275           Diag(ELoc, diag::err_omp_required_access)
10276               << getOpenMPClauseName(OMPC_copyprivate)
10277               << "threadprivate or private in the enclosing context";
10278           ReportOriginalDSA(*this, DSAStack, D, DVar);
10279           continue;
10280         }
10281       }
10282     }
10283 
10284     // Variably modified types are not supported.
10285     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
10286       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10287           << getOpenMPClauseName(OMPC_copyprivate) << Type
10288           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10289       bool IsDecl =
10290           !VD ||
10291           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10292       Diag(D->getLocation(),
10293            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10294           << D;
10295       continue;
10296     }
10297 
10298     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10299     //  A variable of class type (or array thereof) that appears in a
10300     //  copyin clause requires an accessible, unambiguous copy assignment
10301     //  operator for the class type.
10302     Type = Context.getBaseElementType(Type.getNonReferenceType())
10303                .getUnqualifiedType();
10304     auto *SrcVD =
10305         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10306                      D->hasAttrs() ? &D->getAttrs() : nullptr);
10307     auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
10308     auto *DstVD =
10309         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10310                      D->hasAttrs() ? &D->getAttrs() : nullptr);
10311     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
10312     auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10313                                    PseudoDstExpr, PseudoSrcExpr);
10314     if (AssignmentOp.isInvalid())
10315       continue;
10316     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
10317                                        /*DiscardedValue=*/true);
10318     if (AssignmentOp.isInvalid())
10319       continue;
10320 
10321     // No need to mark vars as copyprivate, they are already threadprivate or
10322     // implicitly private.
10323     assert(VD || IsOpenMPCapturedDecl(D));
10324     Vars.push_back(
10325         VD ? RefExpr->IgnoreParens()
10326            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
10327     SrcExprs.push_back(PseudoSrcExpr);
10328     DstExprs.push_back(PseudoDstExpr);
10329     AssignmentOps.push_back(AssignmentOp.get());
10330   }
10331 
10332   if (Vars.empty())
10333     return nullptr;
10334 
10335   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10336                                       Vars, SrcExprs, DstExprs, AssignmentOps);
10337 }
10338 
10339 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10340                                         SourceLocation StartLoc,
10341                                         SourceLocation LParenLoc,
10342                                         SourceLocation EndLoc) {
10343   if (VarList.empty())
10344     return nullptr;
10345 
10346   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10347 }
10348 
10349 OMPClause *
10350 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10351                               SourceLocation DepLoc, SourceLocation ColonLoc,
10352                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10353                               SourceLocation LParenLoc, SourceLocation EndLoc) {
10354   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
10355       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
10356     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
10357         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
10358     return nullptr;
10359   }
10360   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
10361       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10362        DepKind == OMPC_DEPEND_sink)) {
10363     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
10364     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
10365         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10366                                    /*Last=*/OMPC_DEPEND_unknown, Except)
10367         << getOpenMPClauseName(OMPC_depend);
10368     return nullptr;
10369   }
10370   SmallVector<Expr *, 8> Vars;
10371   DSAStackTy::OperatorOffsetTy OpsOffs;
10372   llvm::APSInt DepCounter(/*BitWidth=*/32);
10373   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10374   if (DepKind == OMPC_DEPEND_sink) {
10375     if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10376       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10377       TotalDepCount.setIsUnsigned(/*Val=*/true);
10378     }
10379   }
10380   if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10381       DSAStack->getParentOrderedRegionParam()) {
10382     for (auto &RefExpr : VarList) {
10383       assert(RefExpr && "NULL expr in OpenMP shared clause.");
10384       if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
10385         // It will be analyzed later.
10386         Vars.push_back(RefExpr);
10387         continue;
10388       }
10389 
10390       SourceLocation ELoc = RefExpr->getExprLoc();
10391       auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10392       if (DepKind == OMPC_DEPEND_sink) {
10393         if (DepCounter >= TotalDepCount) {
10394           Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10395           continue;
10396         }
10397         ++DepCounter;
10398         // OpenMP  [2.13.9, Summary]
10399         // depend(dependence-type : vec), where dependence-type is:
10400         // 'sink' and where vec is the iteration vector, which has the form:
10401         //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10402         // where n is the value specified by the ordered clause in the loop
10403         // directive, xi denotes the loop iteration variable of the i-th nested
10404         // loop associated with the loop directive, and di is a constant
10405         // non-negative integer.
10406         if (CurContext->isDependentContext()) {
10407           // It will be analyzed later.
10408           Vars.push_back(RefExpr);
10409           continue;
10410         }
10411         SimpleExpr = SimpleExpr->IgnoreImplicit();
10412         OverloadedOperatorKind OOK = OO_None;
10413         SourceLocation OOLoc;
10414         Expr *LHS = SimpleExpr;
10415         Expr *RHS = nullptr;
10416         if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10417           OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10418           OOLoc = BO->getOperatorLoc();
10419           LHS = BO->getLHS()->IgnoreParenImpCasts();
10420           RHS = BO->getRHS()->IgnoreParenImpCasts();
10421         } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10422           OOK = OCE->getOperator();
10423           OOLoc = OCE->getOperatorLoc();
10424           LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10425           RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10426         } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10427           OOK = MCE->getMethodDecl()
10428                     ->getNameInfo()
10429                     .getName()
10430                     .getCXXOverloadedOperator();
10431           OOLoc = MCE->getCallee()->getExprLoc();
10432           LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10433           RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10434         }
10435         SourceLocation ELoc;
10436         SourceRange ERange;
10437         auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10438                                   /*AllowArraySection=*/false);
10439         if (Res.second) {
10440           // It will be analyzed later.
10441           Vars.push_back(RefExpr);
10442         }
10443         ValueDecl *D = Res.first;
10444         if (!D)
10445           continue;
10446 
10447         if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10448           Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10449           continue;
10450         }
10451         if (RHS) {
10452           ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10453               RHS, OMPC_depend, /*StrictlyPositive=*/false);
10454           if (RHSRes.isInvalid())
10455             continue;
10456         }
10457         if (!CurContext->isDependentContext() &&
10458             DSAStack->getParentOrderedRegionParam() &&
10459             DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10460           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10461               << DSAStack->getParentLoopControlVariable(
10462                      DepCounter.getZExtValue());
10463           continue;
10464         }
10465         OpsOffs.push_back({RHS, OOK});
10466       } else {
10467         // OpenMP  [2.11.1.1, Restrictions, p.3]
10468         //  A variable that is part of another variable (such as a field of a
10469         //  structure) but is not an array element or an array section cannot
10470         //  appear  in a depend clause.
10471         auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10472         auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10473         auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10474         if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10475             (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
10476             (ASE &&
10477              !ASE->getBase()
10478                   ->getType()
10479                   .getNonReferenceType()
10480                   ->isPointerType() &&
10481              !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
10482           Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10483               << 0 << RefExpr->getSourceRange();
10484           continue;
10485         }
10486       }
10487       Vars.push_back(RefExpr->IgnoreParenImpCasts());
10488     }
10489 
10490     if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10491         TotalDepCount > VarList.size() &&
10492         DSAStack->getParentOrderedRegionParam()) {
10493       Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10494           << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10495     }
10496     if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10497         Vars.empty())
10498       return nullptr;
10499   }
10500   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10501                                     DepKind, DepLoc, ColonLoc, Vars);
10502   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10503     DSAStack->addDoacrossDependClause(C, OpsOffs);
10504   return C;
10505 }
10506 
10507 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10508                                          SourceLocation LParenLoc,
10509                                          SourceLocation EndLoc) {
10510   Expr *ValExpr = Device;
10511 
10512   // OpenMP [2.9.1, Restrictions]
10513   // The device expression must evaluate to a non-negative integer value.
10514   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10515                                  /*StrictlyPositive=*/false))
10516     return nullptr;
10517 
10518   return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10519 }
10520 
10521 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10522                                    DSAStackTy *Stack, CXXRecordDecl *RD) {
10523   if (!RD || RD->isInvalidDecl())
10524     return true;
10525 
10526   auto QTy = SemaRef.Context.getRecordType(RD);
10527   if (RD->isDynamicClass()) {
10528     SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10529     SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10530     return false;
10531   }
10532   auto *DC = RD;
10533   bool IsCorrect = true;
10534   for (auto *I : DC->decls()) {
10535     if (I) {
10536       if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10537         if (MD->isStatic()) {
10538           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10539           SemaRef.Diag(MD->getLocation(),
10540                        diag::note_omp_static_member_in_target);
10541           IsCorrect = false;
10542         }
10543       } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10544         if (VD->isStaticDataMember()) {
10545           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10546           SemaRef.Diag(VD->getLocation(),
10547                        diag::note_omp_static_member_in_target);
10548           IsCorrect = false;
10549         }
10550       }
10551     }
10552   }
10553 
10554   for (auto &I : RD->bases()) {
10555     if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10556                                 I.getType()->getAsCXXRecordDecl()))
10557       IsCorrect = false;
10558   }
10559   return IsCorrect;
10560 }
10561 
10562 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10563                               DSAStackTy *Stack, QualType QTy) {
10564   NamedDecl *ND;
10565   if (QTy->isIncompleteType(&ND)) {
10566     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10567     return false;
10568   } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10569     if (!RD->isInvalidDecl() && !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10570       return false;
10571   }
10572   return true;
10573 }
10574 
10575 /// \brief Return true if it can be proven that the provided array expression
10576 /// (array section or array subscript) does NOT specify the whole size of the
10577 /// array whose base type is \a BaseQTy.
10578 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10579                                                         const Expr *E,
10580                                                         QualType BaseQTy) {
10581   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10582 
10583   // If this is an array subscript, it refers to the whole size if the size of
10584   // the dimension is constant and equals 1. Also, an array section assumes the
10585   // format of an array subscript if no colon is used.
10586   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10587     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10588       return ATy->getSize().getSExtValue() != 1;
10589     // Size can't be evaluated statically.
10590     return false;
10591   }
10592 
10593   assert(OASE && "Expecting array section if not an array subscript.");
10594   auto *LowerBound = OASE->getLowerBound();
10595   auto *Length = OASE->getLength();
10596 
10597   // If there is a lower bound that does not evaluates to zero, we are not
10598   // covering the whole dimension.
10599   if (LowerBound) {
10600     llvm::APSInt ConstLowerBound;
10601     if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10602       return false; // Can't get the integer value as a constant.
10603     if (ConstLowerBound.getSExtValue())
10604       return true;
10605   }
10606 
10607   // If we don't have a length we covering the whole dimension.
10608   if (!Length)
10609     return false;
10610 
10611   // If the base is a pointer, we don't have a way to get the size of the
10612   // pointee.
10613   if (BaseQTy->isPointerType())
10614     return false;
10615 
10616   // We can only check if the length is the same as the size of the dimension
10617   // if we have a constant array.
10618   auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10619   if (!CATy)
10620     return false;
10621 
10622   llvm::APSInt ConstLength;
10623   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10624     return false; // Can't get the integer value as a constant.
10625 
10626   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10627 }
10628 
10629 // Return true if it can be proven that the provided array expression (array
10630 // section or array subscript) does NOT specify a single element of the array
10631 // whose base type is \a BaseQTy.
10632 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10633                                                         const Expr *E,
10634                                                         QualType BaseQTy) {
10635   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10636 
10637   // An array subscript always refer to a single element. Also, an array section
10638   // assumes the format of an array subscript if no colon is used.
10639   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10640     return false;
10641 
10642   assert(OASE && "Expecting array section if not an array subscript.");
10643   auto *Length = OASE->getLength();
10644 
10645   // If we don't have a length we have to check if the array has unitary size
10646   // for this dimension. Also, we should always expect a length if the base type
10647   // is pointer.
10648   if (!Length) {
10649     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10650       return ATy->getSize().getSExtValue() != 1;
10651     // We cannot assume anything.
10652     return false;
10653   }
10654 
10655   // Check if the length evaluates to 1.
10656   llvm::APSInt ConstLength;
10657   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10658     return false; // Can't get the integer value as a constant.
10659 
10660   return ConstLength.getSExtValue() != 1;
10661 }
10662 
10663 // Return the expression of the base of the mappable expression or null if it
10664 // cannot be determined and do all the necessary checks to see if the expression
10665 // is valid as a standalone mappable expression. In the process, record all the
10666 // components of the expression.
10667 static Expr *CheckMapClauseExpressionBase(
10668     Sema &SemaRef, Expr *E,
10669     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10670     OpenMPClauseKind CKind) {
10671   SourceLocation ELoc = E->getExprLoc();
10672   SourceRange ERange = E->getSourceRange();
10673 
10674   // The base of elements of list in a map clause have to be either:
10675   //  - a reference to variable or field.
10676   //  - a member expression.
10677   //  - an array expression.
10678   //
10679   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10680   // reference to 'r'.
10681   //
10682   // If we have:
10683   //
10684   // struct SS {
10685   //   Bla S;
10686   //   foo() {
10687   //     #pragma omp target map (S.Arr[:12]);
10688   //   }
10689   // }
10690   //
10691   // We want to retrieve the member expression 'this->S';
10692 
10693   Expr *RelevantExpr = nullptr;
10694 
10695   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10696   //  If a list item is an array section, it must specify contiguous storage.
10697   //
10698   // For this restriction it is sufficient that we make sure only references
10699   // to variables or fields and array expressions, and that no array sections
10700   // exist except in the rightmost expression (unless they cover the whole
10701   // dimension of the array). E.g. these would be invalid:
10702   //
10703   //   r.ArrS[3:5].Arr[6:7]
10704   //
10705   //   r.ArrS[3:5].x
10706   //
10707   // but these would be valid:
10708   //   r.ArrS[3].Arr[6:7]
10709   //
10710   //   r.ArrS[3].x
10711 
10712   bool AllowUnitySizeArraySection = true;
10713   bool AllowWholeSizeArraySection = true;
10714 
10715   while (!RelevantExpr) {
10716     E = E->IgnoreParenImpCasts();
10717 
10718     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10719       if (!isa<VarDecl>(CurE->getDecl()))
10720         break;
10721 
10722       RelevantExpr = CurE;
10723 
10724       // If we got a reference to a declaration, we should not expect any array
10725       // section before that.
10726       AllowUnitySizeArraySection = false;
10727       AllowWholeSizeArraySection = false;
10728 
10729       // Record the component.
10730       CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10731           CurE, CurE->getDecl()));
10732       continue;
10733     }
10734 
10735     if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10736       auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10737 
10738       if (isa<CXXThisExpr>(BaseE))
10739         // We found a base expression: this->Val.
10740         RelevantExpr = CurE;
10741       else
10742         E = BaseE;
10743 
10744       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10745         SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10746             << CurE->getSourceRange();
10747         break;
10748       }
10749 
10750       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10751 
10752       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10753       //  A bit-field cannot appear in a map clause.
10754       //
10755       if (FD->isBitField()) {
10756         SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10757             << CurE->getSourceRange() << getOpenMPClauseName(CKind);
10758         break;
10759       }
10760 
10761       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10762       //  If the type of a list item is a reference to a type T then the type
10763       //  will be considered to be T for all purposes of this clause.
10764       QualType CurType = BaseE->getType().getNonReferenceType();
10765 
10766       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10767       //  A list item cannot be a variable that is a member of a structure with
10768       //  a union type.
10769       //
10770       if (auto *RT = CurType->getAs<RecordType>())
10771         if (RT->isUnionType()) {
10772           SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10773               << CurE->getSourceRange();
10774           break;
10775         }
10776 
10777       // If we got a member expression, we should not expect any array section
10778       // before that:
10779       //
10780       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10781       //  If a list item is an element of a structure, only the rightmost symbol
10782       //  of the variable reference can be an array section.
10783       //
10784       AllowUnitySizeArraySection = false;
10785       AllowWholeSizeArraySection = false;
10786 
10787       // Record the component.
10788       CurComponents.push_back(
10789           OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
10790       continue;
10791     }
10792 
10793     if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10794       E = CurE->getBase()->IgnoreParenImpCasts();
10795 
10796       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10797         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10798             << 0 << CurE->getSourceRange();
10799         break;
10800       }
10801 
10802       // If we got an array subscript that express the whole dimension we
10803       // can have any array expressions before. If it only expressing part of
10804       // the dimension, we can only have unitary-size array expressions.
10805       if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10806                                                       E->getType()))
10807         AllowWholeSizeArraySection = false;
10808 
10809       // Record the component - we don't have any declaration associated.
10810       CurComponents.push_back(
10811           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
10812       continue;
10813     }
10814 
10815     if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
10816       E = CurE->getBase()->IgnoreParenImpCasts();
10817 
10818       auto CurType =
10819           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10820 
10821       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10822       //  If the type of a list item is a reference to a type T then the type
10823       //  will be considered to be T for all purposes of this clause.
10824       if (CurType->isReferenceType())
10825         CurType = CurType->getPointeeType();
10826 
10827       bool IsPointer = CurType->isAnyPointerType();
10828 
10829       if (!IsPointer && !CurType->isArrayType()) {
10830         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10831             << 0 << CurE->getSourceRange();
10832         break;
10833       }
10834 
10835       bool NotWhole =
10836           CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10837       bool NotUnity =
10838           CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10839 
10840       if (AllowWholeSizeArraySection) {
10841         // Any array section is currently allowed. Allowing a whole size array
10842         // section implies allowing a unity array section as well.
10843         //
10844         // If this array section refers to the whole dimension we can still
10845         // accept other array sections before this one, except if the base is a
10846         // pointer. Otherwise, only unitary sections are accepted.
10847         if (NotWhole || IsPointer)
10848           AllowWholeSizeArraySection = false;
10849       } else if (AllowUnitySizeArraySection && NotUnity) {
10850         // A unity or whole array section is not allowed and that is not
10851         // compatible with the properties of the current array section.
10852         SemaRef.Diag(
10853             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10854             << CurE->getSourceRange();
10855         break;
10856       }
10857 
10858       // Record the component - we don't have any declaration associated.
10859       CurComponents.push_back(
10860           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
10861       continue;
10862     }
10863 
10864     // If nothing else worked, this is not a valid map clause expression.
10865     SemaRef.Diag(ELoc,
10866                  diag::err_omp_expected_named_var_member_or_array_expression)
10867         << ERange;
10868     break;
10869   }
10870 
10871   return RelevantExpr;
10872 }
10873 
10874 // Return true if expression E associated with value VD has conflicts with other
10875 // map information.
10876 static bool CheckMapConflicts(
10877     Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10878     bool CurrentRegionOnly,
10879     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10880     OpenMPClauseKind CKind) {
10881   assert(VD && E);
10882   SourceLocation ELoc = E->getExprLoc();
10883   SourceRange ERange = E->getSourceRange();
10884 
10885   // In order to easily check the conflicts we need to match each component of
10886   // the expression under test with the components of the expressions that are
10887   // already in the stack.
10888 
10889   assert(!CurComponents.empty() && "Map clause expression with no components!");
10890   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
10891          "Map clause expression with unexpected base!");
10892 
10893   // Variables to help detecting enclosing problems in data environment nests.
10894   bool IsEnclosedByDataEnvironmentExpr = false;
10895   const Expr *EnclosingExpr = nullptr;
10896 
10897   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10898       VD, CurrentRegionOnly,
10899       [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10900               StackComponents,
10901           OpenMPClauseKind) -> bool {
10902 
10903         assert(!StackComponents.empty() &&
10904                "Map clause expression with no components!");
10905         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
10906                "Map clause expression with unexpected base!");
10907 
10908         // The whole expression in the stack.
10909         auto *RE = StackComponents.front().getAssociatedExpression();
10910 
10911         // Expressions must start from the same base. Here we detect at which
10912         // point both expressions diverge from each other and see if we can
10913         // detect if the memory referred to both expressions is contiguous and
10914         // do not overlap.
10915         auto CI = CurComponents.rbegin();
10916         auto CE = CurComponents.rend();
10917         auto SI = StackComponents.rbegin();
10918         auto SE = StackComponents.rend();
10919         for (; CI != CE && SI != SE; ++CI, ++SI) {
10920 
10921           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10922           //  At most one list item can be an array item derived from a given
10923           //  variable in map clauses of the same construct.
10924           if (CurrentRegionOnly &&
10925               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10926                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10927               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10928                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10929             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
10930                          diag::err_omp_multiple_array_items_in_map_clause)
10931                 << CI->getAssociatedExpression()->getSourceRange();
10932             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10933                          diag::note_used_here)
10934                 << SI->getAssociatedExpression()->getSourceRange();
10935             return true;
10936           }
10937 
10938           // Do both expressions have the same kind?
10939           if (CI->getAssociatedExpression()->getStmtClass() !=
10940               SI->getAssociatedExpression()->getStmtClass())
10941             break;
10942 
10943           // Are we dealing with different variables/fields?
10944           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
10945             break;
10946         }
10947         // Check if the extra components of the expressions in the enclosing
10948         // data environment are redundant for the current base declaration.
10949         // If they are, the maps completely overlap, which is legal.
10950         for (; SI != SE; ++SI) {
10951           QualType Type;
10952           if (auto *ASE =
10953                   dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
10954             Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
10955           } else if (auto *OASE = dyn_cast<OMPArraySectionExpr>(
10956                          SI->getAssociatedExpression())) {
10957             auto *E = OASE->getBase()->IgnoreParenImpCasts();
10958             Type =
10959                 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10960           }
10961           if (Type.isNull() || Type->isAnyPointerType() ||
10962               CheckArrayExpressionDoesNotReferToWholeSize(
10963                   SemaRef, SI->getAssociatedExpression(), Type))
10964             break;
10965         }
10966 
10967         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10968         //  List items of map clauses in the same construct must not share
10969         //  original storage.
10970         //
10971         // If the expressions are exactly the same or one is a subset of the
10972         // other, it means they are sharing storage.
10973         if (CI == CE && SI == SE) {
10974           if (CurrentRegionOnly) {
10975             if (CKind == OMPC_map)
10976               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10977             else {
10978               assert(CKind == OMPC_to || CKind == OMPC_from);
10979               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10980                   << ERange;
10981             }
10982             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10983                 << RE->getSourceRange();
10984             return true;
10985           } else {
10986             // If we find the same expression in the enclosing data environment,
10987             // that is legal.
10988             IsEnclosedByDataEnvironmentExpr = true;
10989             return false;
10990           }
10991         }
10992 
10993         QualType DerivedType =
10994             std::prev(CI)->getAssociatedDeclaration()->getType();
10995         SourceLocation DerivedLoc =
10996             std::prev(CI)->getAssociatedExpression()->getExprLoc();
10997 
10998         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10999         //  If the type of a list item is a reference to a type T then the type
11000         //  will be considered to be T for all purposes of this clause.
11001         DerivedType = DerivedType.getNonReferenceType();
11002 
11003         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
11004         //  A variable for which the type is pointer and an array section
11005         //  derived from that variable must not appear as list items of map
11006         //  clauses of the same construct.
11007         //
11008         // Also, cover one of the cases in:
11009         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11010         //  If any part of the original storage of a list item has corresponding
11011         //  storage in the device data environment, all of the original storage
11012         //  must have corresponding storage in the device data environment.
11013         //
11014         if (DerivedType->isAnyPointerType()) {
11015           if (CI == CE || SI == SE) {
11016             SemaRef.Diag(
11017                 DerivedLoc,
11018                 diag::err_omp_pointer_mapped_along_with_derived_section)
11019                 << DerivedLoc;
11020           } else {
11021             assert(CI != CE && SI != SE);
11022             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
11023                 << DerivedLoc;
11024           }
11025           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11026               << RE->getSourceRange();
11027           return true;
11028         }
11029 
11030         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
11031         //  List items of map clauses in the same construct must not share
11032         //  original storage.
11033         //
11034         // An expression is a subset of the other.
11035         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
11036           if (CKind == OMPC_map)
11037             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
11038           else {
11039             assert(CKind == OMPC_to || CKind == OMPC_from);
11040             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
11041                 << ERange;
11042           }
11043           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
11044               << RE->getSourceRange();
11045           return true;
11046         }
11047 
11048         // The current expression uses the same base as other expression in the
11049         // data environment but does not contain it completely.
11050         if (!CurrentRegionOnly && SI != SE)
11051           EnclosingExpr = RE;
11052 
11053         // The current expression is a subset of the expression in the data
11054         // environment.
11055         IsEnclosedByDataEnvironmentExpr |=
11056             (!CurrentRegionOnly && CI != CE && SI == SE);
11057 
11058         return false;
11059       });
11060 
11061   if (CurrentRegionOnly)
11062     return FoundError;
11063 
11064   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
11065   //  If any part of the original storage of a list item has corresponding
11066   //  storage in the device data environment, all of the original storage must
11067   //  have corresponding storage in the device data environment.
11068   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
11069   //  If a list item is an element of a structure, and a different element of
11070   //  the structure has a corresponding list item in the device data environment
11071   //  prior to a task encountering the construct associated with the map clause,
11072   //  then the list item must also have a corresponding list item in the device
11073   //  data environment prior to the task encountering the construct.
11074   //
11075   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
11076     SemaRef.Diag(ELoc,
11077                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
11078         << ERange;
11079     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
11080         << EnclosingExpr->getSourceRange();
11081     return true;
11082   }
11083 
11084   return FoundError;
11085 }
11086 
11087 namespace {
11088 // Utility struct that gathers all the related lists associated with a mappable
11089 // expression.
11090 struct MappableVarListInfo final {
11091   // The list of expressions.
11092   ArrayRef<Expr *> VarList;
11093   // The list of processed expressions.
11094   SmallVector<Expr *, 16> ProcessedVarList;
11095   // The mappble components for each expression.
11096   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
11097   // The base declaration of the variable.
11098   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
11099 
11100   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
11101     // We have a list of components and base declarations for each entry in the
11102     // variable list.
11103     VarComponents.reserve(VarList.size());
11104     VarBaseDeclarations.reserve(VarList.size());
11105   }
11106 };
11107 }
11108 
11109 // Check the validity of the provided variable list for the provided clause kind
11110 // \a CKind. In the check process the valid expressions, and mappable expression
11111 // components and variables are extracted and used to fill \a Vars,
11112 // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
11113 // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
11114 static void
11115 checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
11116                             OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
11117                             SourceLocation StartLoc,
11118                             OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
11119                             bool IsMapTypeImplicit = false) {
11120   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
11121   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
11122          "Unexpected clause kind with mappable expressions!");
11123 
11124   // Keep track of the mappable components and base declarations in this clause.
11125   // Each entry in the list is going to have a list of components associated. We
11126   // record each set of the components so that we can build the clause later on.
11127   // In the end we should have the same amount of declarations and component
11128   // lists.
11129 
11130   for (auto &RE : MVLI.VarList) {
11131     assert(RE && "Null expr in omp to/from/map clause");
11132     SourceLocation ELoc = RE->getExprLoc();
11133 
11134     auto *VE = RE->IgnoreParenLValueCasts();
11135 
11136     if (VE->isValueDependent() || VE->isTypeDependent() ||
11137         VE->isInstantiationDependent() ||
11138         VE->containsUnexpandedParameterPack()) {
11139       // We can only analyze this information once the missing information is
11140       // resolved.
11141       MVLI.ProcessedVarList.push_back(RE);
11142       continue;
11143     }
11144 
11145     auto *SimpleExpr = RE->IgnoreParenCasts();
11146 
11147     if (!RE->IgnoreParenImpCasts()->isLValue()) {
11148       SemaRef.Diag(ELoc,
11149                    diag::err_omp_expected_named_var_member_or_array_expression)
11150           << RE->getSourceRange();
11151       continue;
11152     }
11153 
11154     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
11155     ValueDecl *CurDeclaration = nullptr;
11156 
11157     // Obtain the array or member expression bases if required. Also, fill the
11158     // components array with all the components identified in the process.
11159     auto *BE =
11160         CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
11161     if (!BE)
11162       continue;
11163 
11164     assert(!CurComponents.empty() &&
11165            "Invalid mappable expression information.");
11166 
11167     // For the following checks, we rely on the base declaration which is
11168     // expected to be associated with the last component. The declaration is
11169     // expected to be a variable or a field (if 'this' is being mapped).
11170     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
11171     assert(CurDeclaration && "Null decl on map clause.");
11172     assert(
11173         CurDeclaration->isCanonicalDecl() &&
11174         "Expecting components to have associated only canonical declarations.");
11175 
11176     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
11177     auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
11178 
11179     assert((VD || FD) && "Only variables or fields are expected here!");
11180     (void)FD;
11181 
11182     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
11183     // threadprivate variables cannot appear in a map clause.
11184     // OpenMP 4.5 [2.10.5, target update Construct]
11185     // threadprivate variables cannot appear in a from clause.
11186     if (VD && DSAS->isThreadPrivate(VD)) {
11187       auto DVar = DSAS->getTopDSA(VD, false);
11188       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
11189           << getOpenMPClauseName(CKind);
11190       ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
11191       continue;
11192     }
11193 
11194     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11195     //  A list item cannot appear in both a map clause and a data-sharing
11196     //  attribute clause on the same construct.
11197 
11198     // Check conflicts with other map clause expressions. We check the conflicts
11199     // with the current construct separately from the enclosing data
11200     // environment, because the restrictions are different. We only have to
11201     // check conflicts across regions for the map clauses.
11202     if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11203                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
11204       break;
11205     if (CKind == OMPC_map &&
11206         CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
11207                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
11208       break;
11209 
11210     // OpenMP 4.5 [2.10.5, target update Construct]
11211     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
11212     //  If the type of a list item is a reference to a type T then the type will
11213     //  be considered to be T for all purposes of this clause.
11214     QualType Type = CurDeclaration->getType().getNonReferenceType();
11215 
11216     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
11217     // A list item in a to or from clause must have a mappable type.
11218     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
11219     //  A list item must have a mappable type.
11220     if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
11221                            DSAS, Type))
11222       continue;
11223 
11224     if (CKind == OMPC_map) {
11225       // target enter data
11226       // OpenMP [2.10.2, Restrictions, p. 99]
11227       // A map-type must be specified in all map clauses and must be either
11228       // to or alloc.
11229       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
11230       if (DKind == OMPD_target_enter_data &&
11231           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
11232         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11233             << (IsMapTypeImplicit ? 1 : 0)
11234             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11235             << getOpenMPDirectiveName(DKind);
11236         continue;
11237       }
11238 
11239       // target exit_data
11240       // OpenMP [2.10.3, Restrictions, p. 102]
11241       // A map-type must be specified in all map clauses and must be either
11242       // from, release, or delete.
11243       if (DKind == OMPD_target_exit_data &&
11244           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
11245             MapType == OMPC_MAP_delete)) {
11246         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11247             << (IsMapTypeImplicit ? 1 : 0)
11248             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11249             << getOpenMPDirectiveName(DKind);
11250         continue;
11251       }
11252 
11253       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11254       // A list item cannot appear in both a map clause and a data-sharing
11255       // attribute clause on the same construct
11256       if (DKind == OMPD_target && VD) {
11257         auto DVar = DSAS->getTopDSA(VD, false);
11258         if (isOpenMPPrivate(DVar.CKind)) {
11259           SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
11260               << getOpenMPClauseName(DVar.CKind)
11261               << getOpenMPClauseName(OMPC_map)
11262               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11263           ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11264           continue;
11265         }
11266       }
11267     }
11268 
11269     // Save the current expression.
11270     MVLI.ProcessedVarList.push_back(RE);
11271 
11272     // Store the components in the stack so that they can be used to check
11273     // against other clauses later on.
11274     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
11275                                           /*WhereFoundClauseKind=*/OMPC_map);
11276 
11277     // Save the components and declaration to create the clause. For purposes of
11278     // the clause creation, any component list that has has base 'this' uses
11279     // null as base declaration.
11280     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11281     MVLI.VarComponents.back().append(CurComponents.begin(),
11282                                      CurComponents.end());
11283     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11284                                                            : CurDeclaration);
11285   }
11286 }
11287 
11288 OMPClause *
11289 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11290                            OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11291                            SourceLocation MapLoc, SourceLocation ColonLoc,
11292                            ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11293                            SourceLocation LParenLoc, SourceLocation EndLoc) {
11294   MappableVarListInfo MVLI(VarList);
11295   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11296                               MapType, IsMapTypeImplicit);
11297 
11298   // We need to produce a map clause even if we don't have variables so that
11299   // other diagnostics related with non-existing map clauses are accurate.
11300   return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11301                               MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11302                               MVLI.VarComponents, MapTypeModifier, MapType,
11303                               IsMapTypeImplicit, MapLoc);
11304 }
11305 
11306 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11307                                                TypeResult ParsedType) {
11308   assert(ParsedType.isUsable());
11309 
11310   QualType ReductionType = GetTypeFromParser(ParsedType.get());
11311   if (ReductionType.isNull())
11312     return QualType();
11313 
11314   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11315   // A type name in a declare reduction directive cannot be a function type, an
11316   // array type, a reference type, or a type qualified with const, volatile or
11317   // restrict.
11318   if (ReductionType.hasQualifiers()) {
11319     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11320     return QualType();
11321   }
11322 
11323   if (ReductionType->isFunctionType()) {
11324     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11325     return QualType();
11326   }
11327   if (ReductionType->isReferenceType()) {
11328     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11329     return QualType();
11330   }
11331   if (ReductionType->isArrayType()) {
11332     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11333     return QualType();
11334   }
11335   return ReductionType;
11336 }
11337 
11338 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11339     Scope *S, DeclContext *DC, DeclarationName Name,
11340     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11341     AccessSpecifier AS, Decl *PrevDeclInScope) {
11342   SmallVector<Decl *, 8> Decls;
11343   Decls.reserve(ReductionTypes.size());
11344 
11345   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11346                       ForRedeclaration);
11347   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11348   // A reduction-identifier may not be re-declared in the current scope for the
11349   // same type or for a type that is compatible according to the base language
11350   // rules.
11351   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11352   OMPDeclareReductionDecl *PrevDRD = nullptr;
11353   bool InCompoundScope = true;
11354   if (S != nullptr) {
11355     // Find previous declaration with the same name not referenced in other
11356     // declarations.
11357     FunctionScopeInfo *ParentFn = getEnclosingFunction();
11358     InCompoundScope =
11359         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11360     LookupName(Lookup, S);
11361     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11362                          /*AllowInlineNamespace=*/false);
11363     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11364     auto Filter = Lookup.makeFilter();
11365     while (Filter.hasNext()) {
11366       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11367       if (InCompoundScope) {
11368         auto I = UsedAsPrevious.find(PrevDecl);
11369         if (I == UsedAsPrevious.end())
11370           UsedAsPrevious[PrevDecl] = false;
11371         if (auto *D = PrevDecl->getPrevDeclInScope())
11372           UsedAsPrevious[D] = true;
11373       }
11374       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11375           PrevDecl->getLocation();
11376     }
11377     Filter.done();
11378     if (InCompoundScope) {
11379       for (auto &PrevData : UsedAsPrevious) {
11380         if (!PrevData.second) {
11381           PrevDRD = PrevData.first;
11382           break;
11383         }
11384       }
11385     }
11386   } else if (PrevDeclInScope != nullptr) {
11387     auto *PrevDRDInScope = PrevDRD =
11388         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11389     do {
11390       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11391           PrevDRDInScope->getLocation();
11392       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11393     } while (PrevDRDInScope != nullptr);
11394   }
11395   for (auto &TyData : ReductionTypes) {
11396     auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11397     bool Invalid = false;
11398     if (I != PreviousRedeclTypes.end()) {
11399       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11400           << TyData.first;
11401       Diag(I->second, diag::note_previous_definition);
11402       Invalid = true;
11403     }
11404     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11405     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11406                                                 Name, TyData.first, PrevDRD);
11407     DC->addDecl(DRD);
11408     DRD->setAccess(AS);
11409     Decls.push_back(DRD);
11410     if (Invalid)
11411       DRD->setInvalidDecl();
11412     else
11413       PrevDRD = DRD;
11414   }
11415 
11416   return DeclGroupPtrTy::make(
11417       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11418 }
11419 
11420 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11421   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11422 
11423   // Enter new function scope.
11424   PushFunctionScope();
11425   getCurFunction()->setHasBranchProtectedScope();
11426   getCurFunction()->setHasOMPDeclareReductionCombiner();
11427 
11428   if (S != nullptr)
11429     PushDeclContext(S, DRD);
11430   else
11431     CurContext = DRD;
11432 
11433   PushExpressionEvaluationContext(PotentiallyEvaluated);
11434 
11435   QualType ReductionType = DRD->getType();
11436   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11437   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11438   // uses semantics of argument handles by value, but it should be passed by
11439   // reference. C lang does not support references, so pass all parameters as
11440   // pointers.
11441   // Create 'T omp_in;' variable.
11442   auto *OmpInParm =
11443       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
11444   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11445   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11446   // uses semantics of argument handles by value, but it should be passed by
11447   // reference. C lang does not support references, so pass all parameters as
11448   // pointers.
11449   // Create 'T omp_out;' variable.
11450   auto *OmpOutParm =
11451       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11452   if (S != nullptr) {
11453     PushOnScopeChains(OmpInParm, S);
11454     PushOnScopeChains(OmpOutParm, S);
11455   } else {
11456     DRD->addDecl(OmpInParm);
11457     DRD->addDecl(OmpOutParm);
11458   }
11459 }
11460 
11461 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11462   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11463   DiscardCleanupsInEvaluationContext();
11464   PopExpressionEvaluationContext();
11465 
11466   PopDeclContext();
11467   PopFunctionScopeInfo();
11468 
11469   if (Combiner != nullptr)
11470     DRD->setCombiner(Combiner);
11471   else
11472     DRD->setInvalidDecl();
11473 }
11474 
11475 void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11476   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11477 
11478   // Enter new function scope.
11479   PushFunctionScope();
11480   getCurFunction()->setHasBranchProtectedScope();
11481 
11482   if (S != nullptr)
11483     PushDeclContext(S, DRD);
11484   else
11485     CurContext = DRD;
11486 
11487   PushExpressionEvaluationContext(PotentiallyEvaluated);
11488 
11489   QualType ReductionType = DRD->getType();
11490   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11491   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11492   // uses semantics of argument handles by value, but it should be passed by
11493   // reference. C lang does not support references, so pass all parameters as
11494   // pointers.
11495   // Create 'T omp_priv;' variable.
11496   auto *OmpPrivParm =
11497       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
11498   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11499   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11500   // uses semantics of argument handles by value, but it should be passed by
11501   // reference. C lang does not support references, so pass all parameters as
11502   // pointers.
11503   // Create 'T omp_orig;' variable.
11504   auto *OmpOrigParm =
11505       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
11506   if (S != nullptr) {
11507     PushOnScopeChains(OmpPrivParm, S);
11508     PushOnScopeChains(OmpOrigParm, S);
11509   } else {
11510     DRD->addDecl(OmpPrivParm);
11511     DRD->addDecl(OmpOrigParm);
11512   }
11513 }
11514 
11515 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11516                                                      Expr *Initializer) {
11517   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11518   DiscardCleanupsInEvaluationContext();
11519   PopExpressionEvaluationContext();
11520 
11521   PopDeclContext();
11522   PopFunctionScopeInfo();
11523 
11524   if (Initializer != nullptr)
11525     DRD->setInitializer(Initializer);
11526   else
11527     DRD->setInvalidDecl();
11528 }
11529 
11530 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11531     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11532   for (auto *D : DeclReductions.get()) {
11533     if (IsValid) {
11534       auto *DRD = cast<OMPDeclareReductionDecl>(D);
11535       if (S != nullptr)
11536         PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11537     } else
11538       D->setInvalidDecl();
11539   }
11540   return DeclReductions;
11541 }
11542 
11543 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11544                                            SourceLocation StartLoc,
11545                                            SourceLocation LParenLoc,
11546                                            SourceLocation EndLoc) {
11547   Expr *ValExpr = NumTeams;
11548 
11549   // OpenMP [teams Constrcut, Restrictions]
11550   // The num_teams expression must evaluate to a positive integer value.
11551   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11552                                  /*StrictlyPositive=*/true))
11553     return nullptr;
11554 
11555   return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11556 }
11557 
11558 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11559                                               SourceLocation StartLoc,
11560                                               SourceLocation LParenLoc,
11561                                               SourceLocation EndLoc) {
11562   Expr *ValExpr = ThreadLimit;
11563 
11564   // OpenMP [teams Constrcut, Restrictions]
11565   // The thread_limit expression must evaluate to a positive integer value.
11566   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11567                                  /*StrictlyPositive=*/true))
11568     return nullptr;
11569 
11570   return new (Context)
11571       OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11572 }
11573 
11574 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11575                                            SourceLocation StartLoc,
11576                                            SourceLocation LParenLoc,
11577                                            SourceLocation EndLoc) {
11578   Expr *ValExpr = Priority;
11579 
11580   // OpenMP [2.9.1, task Constrcut]
11581   // The priority-value is a non-negative numerical scalar expression.
11582   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11583                                  /*StrictlyPositive=*/false))
11584     return nullptr;
11585 
11586   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11587 }
11588 
11589 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11590                                             SourceLocation StartLoc,
11591                                             SourceLocation LParenLoc,
11592                                             SourceLocation EndLoc) {
11593   Expr *ValExpr = Grainsize;
11594 
11595   // OpenMP [2.9.2, taskloop Constrcut]
11596   // The parameter of the grainsize clause must be a positive integer
11597   // expression.
11598   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11599                                  /*StrictlyPositive=*/true))
11600     return nullptr;
11601 
11602   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11603 }
11604 
11605 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11606                                            SourceLocation StartLoc,
11607                                            SourceLocation LParenLoc,
11608                                            SourceLocation EndLoc) {
11609   Expr *ValExpr = NumTasks;
11610 
11611   // OpenMP [2.9.2, taskloop Constrcut]
11612   // The parameter of the num_tasks clause must be a positive integer
11613   // expression.
11614   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11615                                  /*StrictlyPositive=*/true))
11616     return nullptr;
11617 
11618   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11619 }
11620 
11621 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11622                                        SourceLocation LParenLoc,
11623                                        SourceLocation EndLoc) {
11624   // OpenMP [2.13.2, critical construct, Description]
11625   // ... where hint-expression is an integer constant expression that evaluates
11626   // to a valid lock hint.
11627   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11628   if (HintExpr.isInvalid())
11629     return nullptr;
11630   return new (Context)
11631       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11632 }
11633 
11634 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11635     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11636     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11637     SourceLocation EndLoc) {
11638   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11639     std::string Values;
11640     Values += "'";
11641     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11642     Values += "'";
11643     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11644         << Values << getOpenMPClauseName(OMPC_dist_schedule);
11645     return nullptr;
11646   }
11647   Expr *ValExpr = ChunkSize;
11648   Stmt *HelperValStmt = nullptr;
11649   if (ChunkSize) {
11650     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11651         !ChunkSize->isInstantiationDependent() &&
11652         !ChunkSize->containsUnexpandedParameterPack()) {
11653       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11654       ExprResult Val =
11655           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11656       if (Val.isInvalid())
11657         return nullptr;
11658 
11659       ValExpr = Val.get();
11660 
11661       // OpenMP [2.7.1, Restrictions]
11662       //  chunk_size must be a loop invariant integer expression with a positive
11663       //  value.
11664       llvm::APSInt Result;
11665       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11666         if (Result.isSigned() && !Result.isStrictlyPositive()) {
11667           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11668               << "dist_schedule" << ChunkSize->getSourceRange();
11669           return nullptr;
11670         }
11671       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11672                  !CurContext->isDependentContext()) {
11673         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11674         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11675         HelperValStmt = buildPreInits(Context, Captures);
11676       }
11677     }
11678   }
11679 
11680   return new (Context)
11681       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
11682                             Kind, ValExpr, HelperValStmt);
11683 }
11684 
11685 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11686     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11687     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11688     SourceLocation KindLoc, SourceLocation EndLoc) {
11689   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11690   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
11691     std::string Value;
11692     SourceLocation Loc;
11693     Value += "'";
11694     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11695       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11696                                              OMPC_DEFAULTMAP_MODIFIER_tofrom);
11697       Loc = MLoc;
11698     } else {
11699       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11700                                              OMPC_DEFAULTMAP_scalar);
11701       Loc = KindLoc;
11702     }
11703     Value += "'";
11704     Diag(Loc, diag::err_omp_unexpected_clause_value)
11705         << Value << getOpenMPClauseName(OMPC_defaultmap);
11706     return nullptr;
11707   }
11708 
11709   return new (Context)
11710       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11711 }
11712 
11713 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11714   DeclContext *CurLexicalContext = getCurLexicalContext();
11715   if (!CurLexicalContext->isFileContext() &&
11716       !CurLexicalContext->isExternCContext() &&
11717       !CurLexicalContext->isExternCXXContext()) {
11718     Diag(Loc, diag::err_omp_region_not_file_context);
11719     return false;
11720   }
11721   if (IsInOpenMPDeclareTargetContext) {
11722     Diag(Loc, diag::err_omp_enclosed_declare_target);
11723     return false;
11724   }
11725 
11726   IsInOpenMPDeclareTargetContext = true;
11727   return true;
11728 }
11729 
11730 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11731   assert(IsInOpenMPDeclareTargetContext &&
11732          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11733 
11734   IsInOpenMPDeclareTargetContext = false;
11735 }
11736 
11737 void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
11738                                         CXXScopeSpec &ScopeSpec,
11739                                         const DeclarationNameInfo &Id,
11740                                         OMPDeclareTargetDeclAttr::MapTypeTy MT,
11741                                         NamedDeclSetType &SameDirectiveDecls) {
11742   LookupResult Lookup(*this, Id, LookupOrdinaryName);
11743   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11744 
11745   if (Lookup.isAmbiguous())
11746     return;
11747   Lookup.suppressDiagnostics();
11748 
11749   if (!Lookup.isSingleResult()) {
11750     if (TypoCorrection Corrected =
11751             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11752                         llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11753                         CTK_ErrorRecovery)) {
11754       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11755                                   << Id.getName());
11756       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11757       return;
11758     }
11759 
11760     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11761     return;
11762   }
11763 
11764   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11765   if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11766     if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11767       Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11768 
11769     if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11770       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11771       ND->addAttr(A);
11772       if (ASTMutationListener *ML = Context.getASTMutationListener())
11773         ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11774       checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11775     } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11776       Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11777           << Id.getName();
11778     }
11779   } else
11780     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11781 }
11782 
11783 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11784                                      Sema &SemaRef, Decl *D) {
11785   if (!D)
11786     return;
11787   Decl *LD = nullptr;
11788   if (isa<TagDecl>(D)) {
11789     LD = cast<TagDecl>(D)->getDefinition();
11790   } else if (isa<VarDecl>(D)) {
11791     LD = cast<VarDecl>(D)->getDefinition();
11792 
11793     // If this is an implicit variable that is legal and we do not need to do
11794     // anything.
11795     if (cast<VarDecl>(D)->isImplicit()) {
11796       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11797           SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11798       D->addAttr(A);
11799       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11800         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11801       return;
11802     }
11803 
11804   } else if (isa<FunctionDecl>(D)) {
11805     const FunctionDecl *FD = nullptr;
11806     if (cast<FunctionDecl>(D)->hasBody(FD))
11807       LD = const_cast<FunctionDecl *>(FD);
11808 
11809     // If the definition is associated with the current declaration in the
11810     // target region (it can be e.g. a lambda) that is legal and we do not need
11811     // to do anything else.
11812     if (LD == D) {
11813       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11814           SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11815       D->addAttr(A);
11816       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11817         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11818       return;
11819     }
11820   }
11821   if (!LD)
11822     LD = D;
11823   if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11824       (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11825     // Outlined declaration is not declared target.
11826     if (LD->isOutOfLine()) {
11827       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11828       SemaRef.Diag(SL, diag::note_used_here) << SR;
11829     } else {
11830       DeclContext *DC = LD->getDeclContext();
11831       while (DC) {
11832         if (isa<FunctionDecl>(DC) &&
11833             cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11834           break;
11835         DC = DC->getParent();
11836       }
11837       if (DC)
11838         return;
11839 
11840       // Is not declared in target context.
11841       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11842       SemaRef.Diag(SL, diag::note_used_here) << SR;
11843     }
11844     // Mark decl as declared target to prevent further diagnostic.
11845     Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11846         SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11847     D->addAttr(A);
11848     if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11849       ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11850   }
11851 }
11852 
11853 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11854                                    Sema &SemaRef, DSAStackTy *Stack,
11855                                    ValueDecl *VD) {
11856   if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11857     return true;
11858   if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11859     return false;
11860   return true;
11861 }
11862 
11863 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11864   if (!D || D->isInvalidDecl())
11865     return;
11866   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11867   SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11868   // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11869   if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11870     if (DSAStack->isThreadPrivate(VD)) {
11871       Diag(SL, diag::err_omp_threadprivate_in_target);
11872       ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11873       return;
11874     }
11875   }
11876   if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11877     // Problem if any with var declared with incomplete type will be reported
11878     // as normal, so no need to check it here.
11879     if ((E || !VD->getType()->isIncompleteType()) &&
11880         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11881       // Mark decl as declared target to prevent further diagnostic.
11882       if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
11883         Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11884             Context, OMPDeclareTargetDeclAttr::MT_To);
11885         VD->addAttr(A);
11886         if (ASTMutationListener *ML = Context.getASTMutationListener())
11887           ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
11888       }
11889       return;
11890     }
11891   }
11892   if (!E) {
11893     // Checking declaration inside declare target region.
11894     if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11895         (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
11896       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11897           Context, OMPDeclareTargetDeclAttr::MT_To);
11898       D->addAttr(A);
11899       if (ASTMutationListener *ML = Context.getASTMutationListener())
11900         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11901     }
11902     return;
11903   }
11904   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11905 }
11906 
11907 OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11908                                      SourceLocation StartLoc,
11909                                      SourceLocation LParenLoc,
11910                                      SourceLocation EndLoc) {
11911   MappableVarListInfo MVLI(VarList);
11912   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11913   if (MVLI.ProcessedVarList.empty())
11914     return nullptr;
11915 
11916   return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11917                              MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11918                              MVLI.VarComponents);
11919 }
11920 
11921 OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11922                                        SourceLocation StartLoc,
11923                                        SourceLocation LParenLoc,
11924                                        SourceLocation EndLoc) {
11925   MappableVarListInfo MVLI(VarList);
11926   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11927   if (MVLI.ProcessedVarList.empty())
11928     return nullptr;
11929 
11930   return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11931                                MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11932                                MVLI.VarComponents);
11933 }
11934 
11935 OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11936                                                SourceLocation StartLoc,
11937                                                SourceLocation LParenLoc,
11938                                                SourceLocation EndLoc) {
11939   MappableVarListInfo MVLI(VarList);
11940   SmallVector<Expr *, 8> PrivateCopies;
11941   SmallVector<Expr *, 8> Inits;
11942 
11943   for (auto &RefExpr : VarList) {
11944     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11945     SourceLocation ELoc;
11946     SourceRange ERange;
11947     Expr *SimpleRefExpr = RefExpr;
11948     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11949     if (Res.second) {
11950       // It will be analyzed later.
11951       MVLI.ProcessedVarList.push_back(RefExpr);
11952       PrivateCopies.push_back(nullptr);
11953       Inits.push_back(nullptr);
11954     }
11955     ValueDecl *D = Res.first;
11956     if (!D)
11957       continue;
11958 
11959     QualType Type = D->getType();
11960     Type = Type.getNonReferenceType().getUnqualifiedType();
11961 
11962     auto *VD = dyn_cast<VarDecl>(D);
11963 
11964     // Item should be a pointer or reference to pointer.
11965     if (!Type->isPointerType()) {
11966       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11967           << 0 << RefExpr->getSourceRange();
11968       continue;
11969     }
11970 
11971     // Build the private variable and the expression that refers to it.
11972     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
11973                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
11974     if (VDPrivate->isInvalidDecl())
11975       continue;
11976 
11977     CurContext->addDecl(VDPrivate);
11978     auto VDPrivateRefExpr = buildDeclRefExpr(
11979         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
11980 
11981     // Add temporary variable to initialize the private copy of the pointer.
11982     auto *VDInit =
11983         buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
11984     auto *VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
11985                                            RefExpr->getExprLoc());
11986     AddInitializerToDecl(VDPrivate,
11987                          DefaultLvalueConversion(VDInitRefExpr).get(),
11988                          /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
11989 
11990     // If required, build a capture to implement the privatization initialized
11991     // with the current list item value.
11992     DeclRefExpr *Ref = nullptr;
11993     if (!VD)
11994       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11995     MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
11996     PrivateCopies.push_back(VDPrivateRefExpr);
11997     Inits.push_back(VDInitRefExpr);
11998 
11999     // We need to add a data sharing attribute for this variable to make sure it
12000     // is correctly captured. A variable that shows up in a use_device_ptr has
12001     // similar properties of a first private variable.
12002     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
12003 
12004     // Create a mappable component for the list item. List items in this clause
12005     // only need a component.
12006     MVLI.VarBaseDeclarations.push_back(D);
12007     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12008     MVLI.VarComponents.back().push_back(
12009         OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
12010   }
12011 
12012   if (MVLI.ProcessedVarList.empty())
12013     return nullptr;
12014 
12015   return OMPUseDevicePtrClause::Create(
12016       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12017       PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
12018 }
12019 
12020 OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
12021                                               SourceLocation StartLoc,
12022                                               SourceLocation LParenLoc,
12023                                               SourceLocation EndLoc) {
12024   MappableVarListInfo MVLI(VarList);
12025   for (auto &RefExpr : VarList) {
12026     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
12027     SourceLocation ELoc;
12028     SourceRange ERange;
12029     Expr *SimpleRefExpr = RefExpr;
12030     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
12031     if (Res.second) {
12032       // It will be analyzed later.
12033       MVLI.ProcessedVarList.push_back(RefExpr);
12034     }
12035     ValueDecl *D = Res.first;
12036     if (!D)
12037       continue;
12038 
12039     QualType Type = D->getType();
12040     // item should be a pointer or array or reference to pointer or array
12041     if (!Type.getNonReferenceType()->isPointerType() &&
12042         !Type.getNonReferenceType()->isArrayType()) {
12043       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
12044           << 0 << RefExpr->getSourceRange();
12045       continue;
12046     }
12047 
12048     // Check if the declaration in the clause does not show up in any data
12049     // sharing attribute.
12050     auto DVar = DSAStack->getTopDSA(D, false);
12051     if (isOpenMPPrivate(DVar.CKind)) {
12052       Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
12053           << getOpenMPClauseName(DVar.CKind)
12054           << getOpenMPClauseName(OMPC_is_device_ptr)
12055           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
12056       ReportOriginalDSA(*this, DSAStack, D, DVar);
12057       continue;
12058     }
12059 
12060     Expr *ConflictExpr;
12061     if (DSAStack->checkMappableExprComponentListsForDecl(
12062             D, /*CurrentRegionOnly=*/true,
12063             [&ConflictExpr](
12064                 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
12065                 OpenMPClauseKind) -> bool {
12066               ConflictExpr = R.front().getAssociatedExpression();
12067               return true;
12068             })) {
12069       Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
12070       Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
12071           << ConflictExpr->getSourceRange();
12072       continue;
12073     }
12074 
12075     // Store the components in the stack so that they can be used to check
12076     // against other clauses later on.
12077     OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
12078     DSAStack->addMappableExpressionComponents(
12079         D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
12080 
12081     // Record the expression we've just processed.
12082     MVLI.ProcessedVarList.push_back(SimpleRefExpr);
12083 
12084     // Create a mappable component for the list item. List items in this clause
12085     // only need a component. We use a null declaration to signal fields in
12086     // 'this'.
12087     assert((isa<DeclRefExpr>(SimpleRefExpr) ||
12088             isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
12089            "Unexpected device pointer expression!");
12090     MVLI.VarBaseDeclarations.push_back(
12091         isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
12092     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12093     MVLI.VarComponents.back().push_back(MC);
12094   }
12095 
12096   if (MVLI.ProcessedVarList.empty())
12097     return nullptr;
12098 
12099   return OMPIsDevicePtrClause::Create(
12100       Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
12101       MVLI.VarBaseDeclarations, MVLI.VarComponents);
12102 }
12103