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 template <class T> struct MatchesAny {
49   explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
50   bool operator()(T Kind) {
51     for (auto KindEl : Arr)
52       if (KindEl == Kind)
53         return true;
54     return false;
55   }
56 
57 private:
58   ArrayRef<T> Arr;
59 };
60 struct MatchesAlways {
61   MatchesAlways() {}
62   template <class T> bool operator()(T) { return true; }
63 };
64 
65 typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
66 typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
67 
68 /// \brief Stack for tracking declarations used in OpenMP directives and
69 /// clauses and their data-sharing attributes.
70 class DSAStackTy {
71 public:
72   struct DSAVarData {
73     OpenMPDirectiveKind DKind;
74     OpenMPClauseKind CKind;
75     Expr *RefExpr;
76     DeclRefExpr *PrivateCopy;
77     SourceLocation ImplicitDSALoc;
78     DSAVarData()
79         : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
80           PrivateCopy(nullptr), ImplicitDSALoc() {}
81   };
82 
83 private:
84   struct DSAInfo {
85     OpenMPClauseKind Attributes;
86     Expr *RefExpr;
87     DeclRefExpr *PrivateCopy;
88   };
89   typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
90   typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
91   typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
92   typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
93   typedef llvm::DenseMap<
94       ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
95       MappedExprComponentsTy;
96   typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
97       CriticalsWithHintsTy;
98 
99   struct SharingMapTy {
100     DeclSAMapTy SharingMap;
101     AlignedMapTy AlignedMap;
102     MappedExprComponentsTy MappedExprComponents;
103     LoopControlVariablesMapTy LCVMap;
104     DefaultDataSharingAttributes DefaultAttr;
105     SourceLocation DefaultAttrLoc;
106     OpenMPDirectiveKind Directive;
107     DeclarationNameInfo DirectiveName;
108     Scope *CurScope;
109     SourceLocation ConstructLoc;
110     /// \brief first argument (Expr *) contains optional argument of the
111     /// 'ordered' clause, the second one is true if the regions has 'ordered'
112     /// clause, false otherwise.
113     llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
114     bool NowaitRegion;
115     bool CancelRegion;
116     unsigned AssociatedLoops;
117     SourceLocation InnerTeamsRegionLoc;
118     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
119                  Scope *CurScope, SourceLocation Loc)
120         : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
121           Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
122           ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
123           CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
124     SharingMapTy()
125         : SharingMap(), AlignedMap(), LCVMap(), DefaultAttr(DSA_unspecified),
126           Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
127           ConstructLoc(), OrderedRegion(), NowaitRegion(false),
128           CancelRegion(false), AssociatedLoops(1), InnerTeamsRegionLoc() {}
129   };
130 
131   typedef SmallVector<SharingMapTy, 4> StackTy;
132 
133   /// \brief Stack of used declaration and their data-sharing attributes.
134   StackTy Stack;
135   /// \brief true, if check for DSA must be from parent directive, false, if
136   /// from current directive.
137   OpenMPClauseKind ClauseKindMode;
138   Sema &SemaRef;
139   bool ForceCapturing;
140   CriticalsWithHintsTy Criticals;
141 
142   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
143 
144   DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
145 
146   /// \brief Checks if the variable is a local for OpenMP region.
147   bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
148 
149 public:
150   explicit DSAStackTy(Sema &S)
151       : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
152         ForceCapturing(false) {}
153 
154   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
155   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
156 
157   bool isForceVarCapturing() const { return ForceCapturing; }
158   void setForceVarCapturing(bool V) { ForceCapturing = V; }
159 
160   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
161             Scope *CurScope, SourceLocation Loc) {
162     Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
163     Stack.back().DefaultAttrLoc = Loc;
164   }
165 
166   void pop() {
167     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
168     Stack.pop_back();
169   }
170 
171   void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
172     Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
173   }
174   const std::pair<OMPCriticalDirective *, llvm::APSInt>
175   getCriticalWithHint(const DeclarationNameInfo &Name) const {
176     auto I = Criticals.find(Name.getAsString());
177     if (I != Criticals.end())
178       return I->second;
179     return std::make_pair(nullptr, llvm::APSInt());
180   }
181   /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
182   /// add it and return NULL; otherwise return previous occurrence's expression
183   /// for diagnostics.
184   Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
185 
186   /// \brief Register specified variable as loop control variable.
187   void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
188   /// \brief Check if the specified variable is a loop control variable for
189   /// current region.
190   /// \return The index of the loop control variable in the list of associated
191   /// for-loops (from outer to inner).
192   LCDeclInfo isLoopControlVariable(ValueDecl *D);
193   /// \brief Check if the specified variable is a loop control variable for
194   /// parent region.
195   /// \return The index of the loop control variable in the list of associated
196   /// for-loops (from outer to inner).
197   LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
198   /// \brief Get the loop control variable for the I-th loop (or nullptr) in
199   /// parent directive.
200   ValueDecl *getParentLoopControlVariable(unsigned I);
201 
202   /// \brief Adds explicit data sharing attribute to the specified declaration.
203   void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
204               DeclRefExpr *PrivateCopy = nullptr);
205 
206   /// \brief Returns data sharing attributes from top of the stack for the
207   /// specified declaration.
208   DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
209   /// \brief Returns data-sharing attributes for the specified declaration.
210   DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
211   /// \brief Checks if the specified variables has data-sharing attributes which
212   /// match specified \a CPred predicate in any directive which matches \a DPred
213   /// predicate.
214   template <class ClausesPredicate, class DirectivesPredicate>
215   DSAVarData hasDSA(ValueDecl *D, ClausesPredicate CPred,
216                     DirectivesPredicate DPred, bool FromParent);
217   /// \brief Checks if the specified variables has data-sharing attributes which
218   /// match specified \a CPred predicate in any innermost directive which
219   /// matches \a DPred predicate.
220   template <class ClausesPredicate, class DirectivesPredicate>
221   DSAVarData hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
222                              DirectivesPredicate DPred, bool FromParent);
223   /// \brief Checks if the specified variables has explicit data-sharing
224   /// attributes which match specified \a CPred predicate at the specified
225   /// OpenMP region.
226   bool hasExplicitDSA(ValueDecl *D,
227                       const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
228                       unsigned Level);
229 
230   /// \brief Returns true if the directive at level \Level matches in the
231   /// specified \a DPred predicate.
232   bool hasExplicitDirective(
233       const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
234       unsigned Level);
235 
236   /// \brief Finds a directive which matches specified \a DPred predicate.
237   template <class NamedDirectivesPredicate>
238   bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
239 
240   /// \brief Returns currently analyzed directive.
241   OpenMPDirectiveKind getCurrentDirective() const {
242     return Stack.back().Directive;
243   }
244   /// \brief Returns parent directive.
245   OpenMPDirectiveKind getParentDirective() const {
246     if (Stack.size() > 2)
247       return Stack[Stack.size() - 2].Directive;
248     return OMPD_unknown;
249   }
250   /// \brief Return the directive associated with the provided scope.
251   OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
252 
253   /// \brief Set default data sharing attribute to none.
254   void setDefaultDSANone(SourceLocation Loc) {
255     Stack.back().DefaultAttr = DSA_none;
256     Stack.back().DefaultAttrLoc = Loc;
257   }
258   /// \brief Set default data sharing attribute to shared.
259   void setDefaultDSAShared(SourceLocation Loc) {
260     Stack.back().DefaultAttr = DSA_shared;
261     Stack.back().DefaultAttrLoc = Loc;
262   }
263 
264   DefaultDataSharingAttributes getDefaultDSA() const {
265     return Stack.back().DefaultAttr;
266   }
267   SourceLocation getDefaultDSALocation() const {
268     return Stack.back().DefaultAttrLoc;
269   }
270 
271   /// \brief Checks if the specified variable is a threadprivate.
272   bool isThreadPrivate(VarDecl *D) {
273     DSAVarData DVar = getTopDSA(D, false);
274     return isOpenMPThreadPrivate(DVar.CKind);
275   }
276 
277   /// \brief Marks current region as ordered (it has an 'ordered' clause).
278   void setOrderedRegion(bool IsOrdered, Expr *Param) {
279     Stack.back().OrderedRegion.setInt(IsOrdered);
280     Stack.back().OrderedRegion.setPointer(Param);
281   }
282   /// \brief Returns true, if parent region is ordered (has associated
283   /// 'ordered' clause), false - otherwise.
284   bool isParentOrderedRegion() const {
285     if (Stack.size() > 2)
286       return Stack[Stack.size() - 2].OrderedRegion.getInt();
287     return false;
288   }
289   /// \brief Returns optional parameter for the ordered region.
290   Expr *getParentOrderedRegionParam() const {
291     if (Stack.size() > 2)
292       return Stack[Stack.size() - 2].OrderedRegion.getPointer();
293     return nullptr;
294   }
295   /// \brief Marks current region as nowait (it has a 'nowait' clause).
296   void setNowaitRegion(bool IsNowait = true) {
297     Stack.back().NowaitRegion = IsNowait;
298   }
299   /// \brief Returns true, if parent region is nowait (has associated
300   /// 'nowait' clause), false - otherwise.
301   bool isParentNowaitRegion() const {
302     if (Stack.size() > 2)
303       return Stack[Stack.size() - 2].NowaitRegion;
304     return false;
305   }
306   /// \brief Marks parent region as cancel region.
307   void setParentCancelRegion(bool Cancel = true) {
308     if (Stack.size() > 2)
309       Stack[Stack.size() - 2].CancelRegion =
310           Stack[Stack.size() - 2].CancelRegion || Cancel;
311   }
312   /// \brief Return true if current region has inner cancel construct.
313   bool isCancelRegion() const {
314     return Stack.back().CancelRegion;
315   }
316 
317   /// \brief Set collapse value for the region.
318   void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
319   /// \brief Return collapse value for region.
320   unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
321 
322   /// \brief Marks current target region as one with closely nested teams
323   /// region.
324   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
325     if (Stack.size() > 2)
326       Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
327   }
328   /// \brief Returns true, if current region has closely nested teams region.
329   bool hasInnerTeamsRegion() const {
330     return getInnerTeamsRegionLoc().isValid();
331   }
332   /// \brief Returns location of the nested teams region (if any).
333   SourceLocation getInnerTeamsRegionLoc() const {
334     if (Stack.size() > 1)
335       return Stack.back().InnerTeamsRegionLoc;
336     return SourceLocation();
337   }
338 
339   Scope *getCurScope() const { return Stack.back().CurScope; }
340   Scope *getCurScope() { return Stack.back().CurScope; }
341   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
342 
343   // Do the check specified in \a Check to all component lists and return true
344   // if any issue is found.
345   bool checkMappableExprComponentListsForDecl(
346       ValueDecl *VD, bool CurrentRegionOnly,
347       const llvm::function_ref<bool(
348           OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
349     auto SI = Stack.rbegin();
350     auto SE = Stack.rend();
351 
352     if (SI == SE)
353       return false;
354 
355     if (CurrentRegionOnly) {
356       SE = std::next(SI);
357     } else {
358       ++SI;
359     }
360 
361     for (; SI != SE; ++SI) {
362       auto MI = SI->MappedExprComponents.find(VD);
363       if (MI != SI->MappedExprComponents.end())
364         for (auto &L : MI->second)
365           if (Check(L))
366             return true;
367     }
368     return false;
369   }
370 
371   // Create a new mappable expression component list associated with a given
372   // declaration and initialize it with the provided list of components.
373   void addMappableExpressionComponents(
374       ValueDecl *VD,
375       OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
376     assert(Stack.size() > 1 &&
377            "Not expecting to retrieve components from a empty stack!");
378     auto &MEC = Stack.back().MappedExprComponents[VD];
379     // Create new entry and append the new components there.
380     MEC.resize(MEC.size() + 1);
381     MEC.back().append(Components.begin(), Components.end());
382   }
383 };
384 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
385   return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
386          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
387 }
388 } // namespace
389 
390 static ValueDecl *getCanonicalDecl(ValueDecl *D) {
391   auto *VD = dyn_cast<VarDecl>(D);
392   auto *FD = dyn_cast<FieldDecl>(D);
393   if (VD  != nullptr) {
394     VD = VD->getCanonicalDecl();
395     D = VD;
396   } else {
397     assert(FD);
398     FD = FD->getCanonicalDecl();
399     D = FD;
400   }
401   return D;
402 }
403 
404 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
405                                           ValueDecl *D) {
406   D = getCanonicalDecl(D);
407   auto *VD = dyn_cast<VarDecl>(D);
408   auto *FD = dyn_cast<FieldDecl>(D);
409   DSAVarData DVar;
410   if (Iter == std::prev(Stack.rend())) {
411     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
412     // in a region but not in construct]
413     //  File-scope or namespace-scope variables referenced in called routines
414     //  in the region are shared unless they appear in a threadprivate
415     //  directive.
416     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
417       DVar.CKind = OMPC_shared;
418 
419     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
420     // in a region but not in construct]
421     //  Variables with static storage duration that are declared in called
422     //  routines in the region are shared.
423     if (VD && VD->hasGlobalStorage())
424       DVar.CKind = OMPC_shared;
425 
426     // Non-static data members are shared by default.
427     if (FD)
428       DVar.CKind = OMPC_shared;
429 
430     return DVar;
431   }
432 
433   DVar.DKind = Iter->Directive;
434   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
435   // in a Construct, C/C++, predetermined, p.1]
436   // Variables with automatic storage duration that are declared in a scope
437   // inside the construct are private.
438   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
439       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
440     DVar.CKind = OMPC_private;
441     return DVar;
442   }
443 
444   // Explicitly specified attributes and local variables with predetermined
445   // attributes.
446   if (Iter->SharingMap.count(D)) {
447     DVar.RefExpr = Iter->SharingMap[D].RefExpr;
448     DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
449     DVar.CKind = Iter->SharingMap[D].Attributes;
450     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
451     return DVar;
452   }
453 
454   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
455   // in a Construct, C/C++, implicitly determined, p.1]
456   //  In a parallel or task construct, the data-sharing attributes of these
457   //  variables are determined by the default clause, if present.
458   switch (Iter->DefaultAttr) {
459   case DSA_shared:
460     DVar.CKind = OMPC_shared;
461     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
462     return DVar;
463   case DSA_none:
464     return DVar;
465   case DSA_unspecified:
466     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
467     // in a Construct, implicitly determined, p.2]
468     //  In a parallel construct, if no default clause is present, these
469     //  variables are shared.
470     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
471     if (isOpenMPParallelDirective(DVar.DKind) ||
472         isOpenMPTeamsDirective(DVar.DKind)) {
473       DVar.CKind = OMPC_shared;
474       return DVar;
475     }
476 
477     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
478     // in a Construct, implicitly determined, p.4]
479     //  In a task construct, if no default clause is present, a variable that in
480     //  the enclosing context is determined to be shared by all implicit tasks
481     //  bound to the current team is shared.
482     if (isOpenMPTaskingDirective(DVar.DKind)) {
483       DSAVarData DVarTemp;
484       for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
485            I != EE; ++I) {
486         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
487         // Referenced in a Construct, implicitly determined, p.6]
488         //  In a task construct, if no default clause is present, a variable
489         //  whose data-sharing attribute is not determined by the rules above is
490         //  firstprivate.
491         DVarTemp = getDSA(I, D);
492         if (DVarTemp.CKind != OMPC_shared) {
493           DVar.RefExpr = nullptr;
494           DVar.CKind = OMPC_firstprivate;
495           return DVar;
496         }
497         if (isParallelOrTaskRegion(I->Directive))
498           break;
499       }
500       DVar.CKind =
501           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
502       return DVar;
503     }
504   }
505   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
506   // in a Construct, implicitly determined, p.3]
507   //  For constructs other than task, if no default clause is present, these
508   //  variables inherit their data-sharing attributes from the enclosing
509   //  context.
510   return getDSA(++Iter, D);
511 }
512 
513 Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
514   assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
515   D = getCanonicalDecl(D);
516   auto It = Stack.back().AlignedMap.find(D);
517   if (It == Stack.back().AlignedMap.end()) {
518     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
519     Stack.back().AlignedMap[D] = NewDE;
520     return nullptr;
521   } else {
522     assert(It->second && "Unexpected nullptr expr in the aligned map");
523     return It->second;
524   }
525   return nullptr;
526 }
527 
528 void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
529   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
530   D = getCanonicalDecl(D);
531   Stack.back().LCVMap.insert(
532       std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
533 }
534 
535 DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
536   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
537   D = getCanonicalDecl(D);
538   return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
539                                           : LCDeclInfo(0, nullptr);
540 }
541 
542 DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
543   assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
544   D = getCanonicalDecl(D);
545   return Stack[Stack.size() - 2].LCVMap.count(D) > 0
546              ? Stack[Stack.size() - 2].LCVMap[D]
547              : LCDeclInfo(0, nullptr);
548 }
549 
550 ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
551   assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
552   if (Stack[Stack.size() - 2].LCVMap.size() < I)
553     return nullptr;
554   for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
555     if (Pair.second.first == I)
556       return Pair.first;
557   }
558   return nullptr;
559 }
560 
561 void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
562                         DeclRefExpr *PrivateCopy) {
563   D = getCanonicalDecl(D);
564   if (A == OMPC_threadprivate) {
565     Stack[0].SharingMap[D].Attributes = A;
566     Stack[0].SharingMap[D].RefExpr = E;
567     Stack[0].SharingMap[D].PrivateCopy = nullptr;
568   } else {
569     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
570     Stack.back().SharingMap[D].Attributes = A;
571     Stack.back().SharingMap[D].RefExpr = E;
572     Stack.back().SharingMap[D].PrivateCopy = PrivateCopy;
573     if (PrivateCopy)
574       addDSA(PrivateCopy->getDecl(), PrivateCopy, A);
575   }
576 }
577 
578 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
579   D = D->getCanonicalDecl();
580   if (Stack.size() > 2) {
581     reverse_iterator I = Iter, E = std::prev(Stack.rend());
582     Scope *TopScope = nullptr;
583     while (I != E && !isParallelOrTaskRegion(I->Directive)) {
584       ++I;
585     }
586     if (I == E)
587       return false;
588     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
589     Scope *CurScope = getCurScope();
590     while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
591       CurScope = CurScope->getParent();
592     }
593     return CurScope != TopScope;
594   }
595   return false;
596 }
597 
598 /// \brief Build a variable declaration for OpenMP loop iteration variable.
599 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
600                              StringRef Name, const AttrVec *Attrs = nullptr) {
601   DeclContext *DC = SemaRef.CurContext;
602   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
603   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
604   VarDecl *Decl =
605       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
606   if (Attrs) {
607     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
608          I != E; ++I)
609       Decl->addAttr(*I);
610   }
611   Decl->setImplicit();
612   return Decl;
613 }
614 
615 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
616                                      SourceLocation Loc,
617                                      bool RefersToCapture = false) {
618   D->setReferenced();
619   D->markUsed(S.Context);
620   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
621                              SourceLocation(), D, RefersToCapture, Loc, Ty,
622                              VK_LValue);
623 }
624 
625 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
626   D = getCanonicalDecl(D);
627   DSAVarData DVar;
628 
629   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
630   // in a Construct, C/C++, predetermined, p.1]
631   //  Variables appearing in threadprivate directives are threadprivate.
632   auto *VD = dyn_cast<VarDecl>(D);
633   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
634        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
635          SemaRef.getLangOpts().OpenMPUseTLS &&
636          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
637       (VD && VD->getStorageClass() == SC_Register &&
638        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
639     addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
640                                D->getLocation()),
641            OMPC_threadprivate);
642   }
643   if (Stack[0].SharingMap.count(D)) {
644     DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
645     DVar.CKind = OMPC_threadprivate;
646     return DVar;
647   }
648 
649   if (Stack.size() == 1) {
650     // Not in OpenMP execution region and top scope was already checked.
651     return DVar;
652   }
653 
654   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
655   // in a Construct, C/C++, predetermined, p.4]
656   //  Static data members are shared.
657   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
658   // in a Construct, C/C++, predetermined, p.7]
659   //  Variables with static storage duration that are declared in a scope
660   //  inside the construct are shared.
661   if (VD && VD->isStaticDataMember()) {
662     DSAVarData DVarTemp =
663         hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
664     if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
665       return DVar;
666 
667     DVar.CKind = OMPC_shared;
668     return DVar;
669   }
670 
671   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
672   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
673   Type = SemaRef.getASTContext().getBaseElementType(Type);
674   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
675   // in a Construct, C/C++, predetermined, p.6]
676   //  Variables with const qualified type having no mutable member are
677   //  shared.
678   CXXRecordDecl *RD =
679       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
680   if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
681     if (auto *CTD = CTSD->getSpecializedTemplate())
682       RD = CTD->getTemplatedDecl();
683   if (IsConstant &&
684       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
685         RD->hasMutableFields())) {
686     // Variables with const-qualified type having no mutable member may be
687     // listed in a firstprivate clause, even if they are static data members.
688     DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
689                                  MatchesAlways(), FromParent);
690     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
691       return DVar;
692 
693     DVar.CKind = OMPC_shared;
694     return DVar;
695   }
696 
697   // Explicitly specified attributes and local variables with predetermined
698   // attributes.
699   auto StartI = std::next(Stack.rbegin());
700   auto EndI = std::prev(Stack.rend());
701   if (FromParent && StartI != EndI) {
702     StartI = std::next(StartI);
703   }
704   auto I = std::prev(StartI);
705   if (I->SharingMap.count(D)) {
706     DVar.RefExpr = I->SharingMap[D].RefExpr;
707     DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
708     DVar.CKind = I->SharingMap[D].Attributes;
709     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
710   }
711 
712   return DVar;
713 }
714 
715 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
716                                                   bool FromParent) {
717   D = getCanonicalDecl(D);
718   auto StartI = Stack.rbegin();
719   auto EndI = std::prev(Stack.rend());
720   if (FromParent && StartI != EndI) {
721     StartI = std::next(StartI);
722   }
723   return getDSA(StartI, D);
724 }
725 
726 template <class ClausesPredicate, class DirectivesPredicate>
727 DSAStackTy::DSAVarData DSAStackTy::hasDSA(ValueDecl *D, ClausesPredicate CPred,
728                                           DirectivesPredicate DPred,
729                                           bool FromParent) {
730   D = getCanonicalDecl(D);
731   auto StartI = std::next(Stack.rbegin());
732   auto EndI = Stack.rend();
733   if (FromParent && StartI != EndI) {
734     StartI = std::next(StartI);
735   }
736   for (auto I = StartI, EE = EndI; I != EE; ++I) {
737     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
738       continue;
739     DSAVarData DVar = getDSA(I, D);
740     if (CPred(DVar.CKind))
741       return DVar;
742   }
743   return DSAVarData();
744 }
745 
746 template <class ClausesPredicate, class DirectivesPredicate>
747 DSAStackTy::DSAVarData
748 DSAStackTy::hasInnermostDSA(ValueDecl *D, ClausesPredicate CPred,
749                             DirectivesPredicate DPred, bool FromParent) {
750   D = getCanonicalDecl(D);
751   auto StartI = std::next(Stack.rbegin());
752   auto EndI = Stack.rend();
753   if (FromParent && StartI != EndI) {
754     StartI = std::next(StartI);
755   }
756   for (auto I = StartI, EE = EndI; I != EE; ++I) {
757     if (!DPred(I->Directive))
758       break;
759     DSAVarData DVar = getDSA(I, D);
760     if (CPred(DVar.CKind))
761       return DVar;
762     return DSAVarData();
763   }
764   return DSAVarData();
765 }
766 
767 bool DSAStackTy::hasExplicitDSA(
768     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
769     unsigned Level) {
770   if (CPred(ClauseKindMode))
771     return true;
772   if (isClauseParsingMode())
773     ++Level;
774   D = getCanonicalDecl(D);
775   auto StartI = Stack.rbegin();
776   auto EndI = std::prev(Stack.rend());
777   if (std::distance(StartI, EndI) <= (int)Level)
778     return false;
779   std::advance(StartI, Level);
780   return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
781          CPred(StartI->SharingMap[D].Attributes);
782 }
783 
784 bool DSAStackTy::hasExplicitDirective(
785     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
786     unsigned Level) {
787   if (isClauseParsingMode())
788     ++Level;
789   auto StartI = Stack.rbegin();
790   auto EndI = std::prev(Stack.rend());
791   if (std::distance(StartI, EndI) <= (int)Level)
792     return false;
793   std::advance(StartI, Level);
794   return DPred(StartI->Directive);
795 }
796 
797 template <class NamedDirectivesPredicate>
798 bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
799   auto StartI = std::next(Stack.rbegin());
800   auto EndI = std::prev(Stack.rend());
801   if (FromParent && StartI != EndI) {
802     StartI = std::next(StartI);
803   }
804   for (auto I = StartI, EE = EndI; I != EE; ++I) {
805     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
806       return true;
807   }
808   return false;
809 }
810 
811 OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
812   for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
813     if (I->CurScope == S)
814       return I->Directive;
815   return OMPD_unknown;
816 }
817 
818 void Sema::InitDataSharingAttributesStack() {
819   VarDataSharingAttributesStack = new DSAStackTy(*this);
820 }
821 
822 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
823 
824 bool Sema::IsOpenMPCapturedByRef(ValueDecl *D,
825                                  const CapturedRegionScopeInfo *RSI) {
826   assert(LangOpts.OpenMP && "OpenMP is not allowed");
827 
828   auto &Ctx = getASTContext();
829   bool IsByRef = true;
830 
831   // Find the directive that is associated with the provided scope.
832   auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
833   auto Ty = D->getType();
834 
835   if (isOpenMPTargetExecutionDirective(DKind)) {
836     // This table summarizes how a given variable should be passed to the device
837     // given its type and the clauses where it appears. This table is based on
838     // the description in OpenMP 4.5 [2.10.4, target Construct] and
839     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
840     //
841     // =========================================================================
842     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
843     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
844     // =========================================================================
845     // | scl  |               |     |       |       -       |          | bycopy|
846     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
847     // | scl  |               |  x  |   -   |       -       |     -    | null  |
848     // | scl  |       x       |     |       |       -       |          | byref |
849     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
850     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
851     // | scl  |               |  -  |   -   |       -       |     x    | byref |
852     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
853     //
854     // | agg  |      n.a.     |     |       |       -       |          | byref |
855     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
856     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
857     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
858     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
859     //
860     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
861     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
862     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
863     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
864     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
865     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
866     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
867     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
868     // =========================================================================
869     // Legend:
870     //  scl - scalar
871     //  ptr - pointer
872     //  agg - aggregate
873     //  x - applies
874     //  - - invalid in this combination
875     //  [] - mapped with an array section
876     //  byref - should be mapped by reference
877     //  byval - should be mapped by value
878     //  null - initialize a local variable to null on the device
879     //
880     // Observations:
881     //  - All scalar declarations that show up in a map clause have to be passed
882     //    by reference, because they may have been mapped in the enclosing data
883     //    environment.
884     //  - If the scalar value does not fit the size of uintptr, it has to be
885     //    passed by reference, regardless the result in the table above.
886     //  - For pointers mapped by value that have either an implicit map or an
887     //    array section, the runtime library may pass the NULL value to the
888     //    device instead of the value passed to it by the compiler.
889 
890 
891     if (Ty->isReferenceType())
892       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
893 
894     // Locate map clauses and see if the variable being captured is referred to
895     // in any of those clauses. Here we only care about variables, not fields,
896     // because fields are part of aggregates.
897     bool IsVariableUsedInMapClause = false;
898     bool IsVariableAssociatedWithSection = false;
899 
900     DSAStack->checkMappableExprComponentListsForDecl(
901         D, /*CurrentRegionOnly=*/true,
902         [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
903                 MapExprComponents) {
904 
905           auto EI = MapExprComponents.rbegin();
906           auto EE = MapExprComponents.rend();
907 
908           assert(EI != EE && "Invalid map expression!");
909 
910           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
911             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
912 
913           ++EI;
914           if (EI == EE)
915             return false;
916 
917           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
918               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
919               isa<MemberExpr>(EI->getAssociatedExpression())) {
920             IsVariableAssociatedWithSection = true;
921             // There is nothing more we need to know about this variable.
922             return true;
923           }
924 
925           // Keep looking for more map info.
926           return false;
927         });
928 
929     if (IsVariableUsedInMapClause) {
930       // If variable is identified in a map clause it is always captured by
931       // reference except if it is a pointer that is dereferenced somehow.
932       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
933     } else {
934       // By default, all the data that has a scalar type is mapped by copy.
935       IsByRef = !Ty->isScalarType();
936     }
937   }
938 
939   // When passing data by copy, we need to make sure it fits the uintptr size
940   // and alignment, because the runtime library only deals with uintptr types.
941   // If it does not fit the uintptr size, we need to pass the data by reference
942   // instead.
943   if (!IsByRef &&
944       (Ctx.getTypeSizeInChars(Ty) >
945            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
946        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
947     IsByRef = true;
948 
949   return IsByRef;
950 }
951 
952 VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
953   assert(LangOpts.OpenMP && "OpenMP is not allowed");
954   D = getCanonicalDecl(D);
955 
956   // If we are attempting to capture a global variable in a directive with
957   // 'target' we return true so that this global is also mapped to the device.
958   //
959   // FIXME: If the declaration is enclosed in a 'declare target' directive,
960   // then it should not be captured. Therefore, an extra check has to be
961   // inserted here once support for 'declare target' is added.
962   //
963   auto *VD = dyn_cast<VarDecl>(D);
964   if (VD && !VD->hasLocalStorage()) {
965     if (DSAStack->getCurrentDirective() == OMPD_target &&
966         !DSAStack->isClauseParsingMode())
967       return VD;
968     if (DSAStack->getCurScope() &&
969         DSAStack->hasDirective(
970             [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
971                SourceLocation Loc) -> bool {
972               return isOpenMPTargetExecutionDirective(K);
973             },
974             false))
975       return VD;
976   }
977 
978   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
979       (!DSAStack->isClauseParsingMode() ||
980        DSAStack->getParentDirective() != OMPD_unknown)) {
981     auto &&Info = DSAStack->isLoopControlVariable(D);
982     if (Info.first ||
983         (VD && VD->hasLocalStorage() &&
984          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
985         (VD && DSAStack->isForceVarCapturing()))
986       return VD ? VD : Info.second;
987     auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
988     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
989       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
990     DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate, MatchesAlways(),
991                                    DSAStack->isClauseParsingMode());
992     if (DVarPrivate.CKind != OMPC_unknown)
993       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
994   }
995   return nullptr;
996 }
997 
998 bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
999   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1000   return DSAStack->hasExplicitDSA(
1001       D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
1002 }
1003 
1004 bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
1005   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1006   // Return true if the current level is no longer enclosed in a target region.
1007 
1008   auto *VD = dyn_cast<VarDecl>(D);
1009   return VD && !VD->hasLocalStorage() &&
1010          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1011                                         Level);
1012 }
1013 
1014 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1015 
1016 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1017                                const DeclarationNameInfo &DirName,
1018                                Scope *CurScope, SourceLocation Loc) {
1019   DSAStack->push(DKind, DirName, CurScope, Loc);
1020   PushExpressionEvaluationContext(PotentiallyEvaluated);
1021 }
1022 
1023 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1024   DSAStack->setClauseParsingMode(K);
1025 }
1026 
1027 void Sema::EndOpenMPClause() {
1028   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1029 }
1030 
1031 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1032   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1033   //  A variable of class type (or array thereof) that appears in a lastprivate
1034   //  clause requires an accessible, unambiguous default constructor for the
1035   //  class type, unless the list item is also specified in a firstprivate
1036   //  clause.
1037   if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1038     for (auto *C : D->clauses()) {
1039       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1040         SmallVector<Expr *, 8> PrivateCopies;
1041         for (auto *DE : Clause->varlists()) {
1042           if (DE->isValueDependent() || DE->isTypeDependent()) {
1043             PrivateCopies.push_back(nullptr);
1044             continue;
1045           }
1046           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1047           VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1048           QualType Type = VD->getType().getNonReferenceType();
1049           auto DVar = DSAStack->getTopDSA(VD, false);
1050           if (DVar.CKind == OMPC_lastprivate) {
1051             // Generate helper private variable and initialize it with the
1052             // default value. The address of the original variable is replaced
1053             // by the address of the new private variable in CodeGen. This new
1054             // variable is not added to IdResolver, so the code in the OpenMP
1055             // region uses original variable for proper diagnostics.
1056             auto *VDPrivate = buildVarDecl(
1057                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1058                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
1059             ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1060             if (VDPrivate->isInvalidDecl())
1061               continue;
1062             PrivateCopies.push_back(buildDeclRefExpr(
1063                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1064           } else {
1065             // The variable is also a firstprivate, so initialization sequence
1066             // for private copy is generated already.
1067             PrivateCopies.push_back(nullptr);
1068           }
1069         }
1070         // Set initializers to private copies if no errors were found.
1071         if (PrivateCopies.size() == Clause->varlist_size())
1072           Clause->setPrivateCopies(PrivateCopies);
1073       }
1074     }
1075   }
1076 
1077   DSAStack->pop();
1078   DiscardCleanupsInEvaluationContext();
1079   PopExpressionEvaluationContext();
1080 }
1081 
1082 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1083                                      Expr *NumIterations, Sema &SemaRef,
1084                                      Scope *S, DSAStackTy *Stack);
1085 
1086 namespace {
1087 
1088 class VarDeclFilterCCC : public CorrectionCandidateCallback {
1089 private:
1090   Sema &SemaRef;
1091 
1092 public:
1093   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
1094   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1095     NamedDecl *ND = Candidate.getCorrectionDecl();
1096     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1097       return VD->hasGlobalStorage() &&
1098              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1099                                    SemaRef.getCurScope());
1100     }
1101     return false;
1102   }
1103 };
1104 } // namespace
1105 
1106 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1107                                          CXXScopeSpec &ScopeSpec,
1108                                          const DeclarationNameInfo &Id) {
1109   LookupResult Lookup(*this, Id, LookupOrdinaryName);
1110   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1111 
1112   if (Lookup.isAmbiguous())
1113     return ExprError();
1114 
1115   VarDecl *VD;
1116   if (!Lookup.isSingleResult()) {
1117     if (TypoCorrection Corrected = CorrectTypo(
1118             Id, LookupOrdinaryName, CurScope, nullptr,
1119             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1120       diagnoseTypo(Corrected,
1121                    PDiag(Lookup.empty()
1122                              ? diag::err_undeclared_var_use_suggest
1123                              : diag::err_omp_expected_var_arg_suggest)
1124                        << Id.getName());
1125       VD = Corrected.getCorrectionDeclAs<VarDecl>();
1126     } else {
1127       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1128                                        : diag::err_omp_expected_var_arg)
1129           << Id.getName();
1130       return ExprError();
1131     }
1132   } else {
1133     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1134       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1135       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1136       return ExprError();
1137     }
1138   }
1139   Lookup.suppressDiagnostics();
1140 
1141   // OpenMP [2.9.2, Syntax, C/C++]
1142   //   Variables must be file-scope, namespace-scope, or static block-scope.
1143   if (!VD->hasGlobalStorage()) {
1144     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1145         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1146     bool IsDecl =
1147         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1148     Diag(VD->getLocation(),
1149          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1150         << VD;
1151     return ExprError();
1152   }
1153 
1154   VarDecl *CanonicalVD = VD->getCanonicalDecl();
1155   NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
1156   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1157   //   A threadprivate directive for file-scope variables must appear outside
1158   //   any definition or declaration.
1159   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1160       !getCurLexicalContext()->isTranslationUnit()) {
1161     Diag(Id.getLoc(), diag::err_omp_var_scope)
1162         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1163     bool IsDecl =
1164         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1165     Diag(VD->getLocation(),
1166          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1167         << VD;
1168     return ExprError();
1169   }
1170   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1171   //   A threadprivate directive for static class member variables must appear
1172   //   in the class definition, in the same scope in which the member
1173   //   variables are declared.
1174   if (CanonicalVD->isStaticDataMember() &&
1175       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1176     Diag(Id.getLoc(), diag::err_omp_var_scope)
1177         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1178     bool IsDecl =
1179         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1180     Diag(VD->getLocation(),
1181          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1182         << VD;
1183     return ExprError();
1184   }
1185   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1186   //   A threadprivate directive for namespace-scope variables must appear
1187   //   outside any definition or declaration other than the namespace
1188   //   definition itself.
1189   if (CanonicalVD->getDeclContext()->isNamespace() &&
1190       (!getCurLexicalContext()->isFileContext() ||
1191        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1192     Diag(Id.getLoc(), diag::err_omp_var_scope)
1193         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1194     bool IsDecl =
1195         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1196     Diag(VD->getLocation(),
1197          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1198         << VD;
1199     return ExprError();
1200   }
1201   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1202   //   A threadprivate directive for static block-scope variables must appear
1203   //   in the scope of the variable and not in a nested scope.
1204   if (CanonicalVD->isStaticLocal() && CurScope &&
1205       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1206     Diag(Id.getLoc(), diag::err_omp_var_scope)
1207         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1208     bool IsDecl =
1209         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1210     Diag(VD->getLocation(),
1211          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1212         << VD;
1213     return ExprError();
1214   }
1215 
1216   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1217   //   A threadprivate directive must lexically precede all references to any
1218   //   of the variables in its list.
1219   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1220     Diag(Id.getLoc(), diag::err_omp_var_used)
1221         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1222     return ExprError();
1223   }
1224 
1225   QualType ExprType = VD->getType().getNonReferenceType();
1226   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1227                              SourceLocation(), VD,
1228                              /*RefersToEnclosingVariableOrCapture=*/false,
1229                              Id.getLoc(), ExprType, VK_LValue);
1230 }
1231 
1232 Sema::DeclGroupPtrTy
1233 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1234                                         ArrayRef<Expr *> VarList) {
1235   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1236     CurContext->addDecl(D);
1237     return DeclGroupPtrTy::make(DeclGroupRef(D));
1238   }
1239   return nullptr;
1240 }
1241 
1242 namespace {
1243 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1244   Sema &SemaRef;
1245 
1246 public:
1247   bool VisitDeclRefExpr(const DeclRefExpr *E) {
1248     if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1249       if (VD->hasLocalStorage()) {
1250         SemaRef.Diag(E->getLocStart(),
1251                      diag::err_omp_local_var_in_threadprivate_init)
1252             << E->getSourceRange();
1253         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1254             << VD << VD->getSourceRange();
1255         return true;
1256       }
1257     }
1258     return false;
1259   }
1260   bool VisitStmt(const Stmt *S) {
1261     for (auto Child : S->children()) {
1262       if (Child && Visit(Child))
1263         return true;
1264     }
1265     return false;
1266   }
1267   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1268 };
1269 } // namespace
1270 
1271 OMPThreadPrivateDecl *
1272 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1273   SmallVector<Expr *, 8> Vars;
1274   for (auto &RefExpr : VarList) {
1275     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1276     VarDecl *VD = cast<VarDecl>(DE->getDecl());
1277     SourceLocation ILoc = DE->getExprLoc();
1278 
1279     // Mark variable as used.
1280     VD->setReferenced();
1281     VD->markUsed(Context);
1282 
1283     QualType QType = VD->getType();
1284     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1285       // It will be analyzed later.
1286       Vars.push_back(DE);
1287       continue;
1288     }
1289 
1290     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1291     //   A threadprivate variable must not have an incomplete type.
1292     if (RequireCompleteType(ILoc, VD->getType(),
1293                             diag::err_omp_threadprivate_incomplete_type)) {
1294       continue;
1295     }
1296 
1297     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1298     //   A threadprivate variable must not have a reference type.
1299     if (VD->getType()->isReferenceType()) {
1300       Diag(ILoc, diag::err_omp_ref_type_arg)
1301           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1302       bool IsDecl =
1303           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1304       Diag(VD->getLocation(),
1305            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1306           << VD;
1307       continue;
1308     }
1309 
1310     // Check if this is a TLS variable. If TLS is not being supported, produce
1311     // the corresponding diagnostic.
1312     if ((VD->getTLSKind() != VarDecl::TLS_None &&
1313          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1314            getLangOpts().OpenMPUseTLS &&
1315            getASTContext().getTargetInfo().isTLSSupported())) ||
1316         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1317          !VD->isLocalVarDecl())) {
1318       Diag(ILoc, diag::err_omp_var_thread_local)
1319           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1320       bool IsDecl =
1321           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1322       Diag(VD->getLocation(),
1323            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1324           << VD;
1325       continue;
1326     }
1327 
1328     // Check if initial value of threadprivate variable reference variable with
1329     // local storage (it is not supported by runtime).
1330     if (auto Init = VD->getAnyInitializer()) {
1331       LocalVarRefChecker Checker(*this);
1332       if (Checker.Visit(Init))
1333         continue;
1334     }
1335 
1336     Vars.push_back(RefExpr);
1337     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1338     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1339         Context, SourceRange(Loc, Loc)));
1340     if (auto *ML = Context.getASTMutationListener())
1341       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1342   }
1343   OMPThreadPrivateDecl *D = nullptr;
1344   if (!Vars.empty()) {
1345     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1346                                      Vars);
1347     D->setAccess(AS_public);
1348   }
1349   return D;
1350 }
1351 
1352 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1353                               const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1354                               bool IsLoopIterVar = false) {
1355   if (DVar.RefExpr) {
1356     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1357         << getOpenMPClauseName(DVar.CKind);
1358     return;
1359   }
1360   enum {
1361     PDSA_StaticMemberShared,
1362     PDSA_StaticLocalVarShared,
1363     PDSA_LoopIterVarPrivate,
1364     PDSA_LoopIterVarLinear,
1365     PDSA_LoopIterVarLastprivate,
1366     PDSA_ConstVarShared,
1367     PDSA_GlobalVarShared,
1368     PDSA_TaskVarFirstprivate,
1369     PDSA_LocalVarPrivate,
1370     PDSA_Implicit
1371   } Reason = PDSA_Implicit;
1372   bool ReportHint = false;
1373   auto ReportLoc = D->getLocation();
1374   auto *VD = dyn_cast<VarDecl>(D);
1375   if (IsLoopIterVar) {
1376     if (DVar.CKind == OMPC_private)
1377       Reason = PDSA_LoopIterVarPrivate;
1378     else if (DVar.CKind == OMPC_lastprivate)
1379       Reason = PDSA_LoopIterVarLastprivate;
1380     else
1381       Reason = PDSA_LoopIterVarLinear;
1382   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1383              DVar.CKind == OMPC_firstprivate) {
1384     Reason = PDSA_TaskVarFirstprivate;
1385     ReportLoc = DVar.ImplicitDSALoc;
1386   } else if (VD && VD->isStaticLocal())
1387     Reason = PDSA_StaticLocalVarShared;
1388   else if (VD && VD->isStaticDataMember())
1389     Reason = PDSA_StaticMemberShared;
1390   else if (VD && VD->isFileVarDecl())
1391     Reason = PDSA_GlobalVarShared;
1392   else if (D->getType().isConstant(SemaRef.getASTContext()))
1393     Reason = PDSA_ConstVarShared;
1394   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1395     ReportHint = true;
1396     Reason = PDSA_LocalVarPrivate;
1397   }
1398   if (Reason != PDSA_Implicit) {
1399     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1400         << Reason << ReportHint
1401         << getOpenMPDirectiveName(Stack->getCurrentDirective());
1402   } else if (DVar.ImplicitDSALoc.isValid()) {
1403     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1404         << getOpenMPClauseName(DVar.CKind);
1405   }
1406 }
1407 
1408 namespace {
1409 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1410   DSAStackTy *Stack;
1411   Sema &SemaRef;
1412   bool ErrorFound;
1413   CapturedStmt *CS;
1414   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1415   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1416 
1417 public:
1418   void VisitDeclRefExpr(DeclRefExpr *E) {
1419     if (E->isTypeDependent() || E->isValueDependent() ||
1420         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1421       return;
1422     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1423       // Skip internally declared variables.
1424       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1425         return;
1426 
1427       auto DVar = Stack->getTopDSA(VD, false);
1428       // Check if the variable has explicit DSA set and stop analysis if it so.
1429       if (DVar.RefExpr) return;
1430 
1431       auto ELoc = E->getExprLoc();
1432       auto DKind = Stack->getCurrentDirective();
1433       // The default(none) clause requires that each variable that is referenced
1434       // in the construct, and does not have a predetermined data-sharing
1435       // attribute, must have its data-sharing attribute explicitly determined
1436       // by being listed in a data-sharing attribute clause.
1437       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1438           isParallelOrTaskRegion(DKind) &&
1439           VarsWithInheritedDSA.count(VD) == 0) {
1440         VarsWithInheritedDSA[VD] = E;
1441         return;
1442       }
1443 
1444       // OpenMP [2.9.3.6, Restrictions, p.2]
1445       //  A list item that appears in a reduction clause of the innermost
1446       //  enclosing worksharing or parallel construct may not be accessed in an
1447       //  explicit task.
1448       DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
1449                                     [](OpenMPDirectiveKind K) -> bool {
1450                                       return isOpenMPParallelDirective(K) ||
1451                                              isOpenMPWorksharingDirective(K) ||
1452                                              isOpenMPTeamsDirective(K);
1453                                     },
1454                                     false);
1455       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1456         ErrorFound = true;
1457         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1458         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1459         return;
1460       }
1461 
1462       // Define implicit data-sharing attributes for task.
1463       DVar = Stack->getImplicitDSA(VD, false);
1464       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1465           !Stack->isLoopControlVariable(VD).first)
1466         ImplicitFirstprivate.push_back(E);
1467     }
1468   }
1469   void VisitMemberExpr(MemberExpr *E) {
1470     if (E->isTypeDependent() || E->isValueDependent() ||
1471         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1472       return;
1473     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1474       if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1475         auto DVar = Stack->getTopDSA(FD, false);
1476         // Check if the variable has explicit DSA set and stop analysis if it
1477         // so.
1478         if (DVar.RefExpr)
1479           return;
1480 
1481         auto ELoc = E->getExprLoc();
1482         auto DKind = Stack->getCurrentDirective();
1483         // OpenMP [2.9.3.6, Restrictions, p.2]
1484         //  A list item that appears in a reduction clause of the innermost
1485         //  enclosing worksharing or parallel construct may not be accessed in
1486         //  an  explicit task.
1487         DVar =
1488             Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1489                                    [](OpenMPDirectiveKind K) -> bool {
1490                                      return isOpenMPParallelDirective(K) ||
1491                                             isOpenMPWorksharingDirective(K) ||
1492                                             isOpenMPTeamsDirective(K);
1493                                    },
1494                                    false);
1495         if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1496           ErrorFound = true;
1497           SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1498           ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1499           return;
1500         }
1501 
1502         // Define implicit data-sharing attributes for task.
1503         DVar = Stack->getImplicitDSA(FD, false);
1504         if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1505             !Stack->isLoopControlVariable(FD).first)
1506           ImplicitFirstprivate.push_back(E);
1507       }
1508     }
1509   }
1510   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1511     for (auto *C : S->clauses()) {
1512       // Skip analysis of arguments of implicitly defined firstprivate clause
1513       // for task directives.
1514       if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1515         for (auto *CC : C->children()) {
1516           if (CC)
1517             Visit(CC);
1518         }
1519     }
1520   }
1521   void VisitStmt(Stmt *S) {
1522     for (auto *C : S->children()) {
1523       if (C && !isa<OMPExecutableDirective>(C))
1524         Visit(C);
1525     }
1526   }
1527 
1528   bool isErrorFound() { return ErrorFound; }
1529   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
1530   llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
1531     return VarsWithInheritedDSA;
1532   }
1533 
1534   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1535       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1536 };
1537 } // namespace
1538 
1539 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1540   switch (DKind) {
1541   case OMPD_parallel: {
1542     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1543     QualType KmpInt32PtrTy =
1544         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1545     Sema::CapturedParamNameType Params[] = {
1546         std::make_pair(".global_tid.", KmpInt32PtrTy),
1547         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1548         std::make_pair(StringRef(), QualType()) // __context with shared vars
1549     };
1550     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1551                              Params);
1552     break;
1553   }
1554   case OMPD_simd: {
1555     Sema::CapturedParamNameType Params[] = {
1556         std::make_pair(StringRef(), QualType()) // __context with shared vars
1557     };
1558     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1559                              Params);
1560     break;
1561   }
1562   case OMPD_for: {
1563     Sema::CapturedParamNameType Params[] = {
1564         std::make_pair(StringRef(), QualType()) // __context with shared vars
1565     };
1566     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1567                              Params);
1568     break;
1569   }
1570   case OMPD_for_simd: {
1571     Sema::CapturedParamNameType Params[] = {
1572         std::make_pair(StringRef(), QualType()) // __context with shared vars
1573     };
1574     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1575                              Params);
1576     break;
1577   }
1578   case OMPD_sections: {
1579     Sema::CapturedParamNameType Params[] = {
1580         std::make_pair(StringRef(), QualType()) // __context with shared vars
1581     };
1582     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1583                              Params);
1584     break;
1585   }
1586   case OMPD_section: {
1587     Sema::CapturedParamNameType Params[] = {
1588         std::make_pair(StringRef(), QualType()) // __context with shared vars
1589     };
1590     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1591                              Params);
1592     break;
1593   }
1594   case OMPD_single: {
1595     Sema::CapturedParamNameType Params[] = {
1596         std::make_pair(StringRef(), QualType()) // __context with shared vars
1597     };
1598     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1599                              Params);
1600     break;
1601   }
1602   case OMPD_master: {
1603     Sema::CapturedParamNameType Params[] = {
1604         std::make_pair(StringRef(), QualType()) // __context with shared vars
1605     };
1606     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1607                              Params);
1608     break;
1609   }
1610   case OMPD_critical: {
1611     Sema::CapturedParamNameType Params[] = {
1612         std::make_pair(StringRef(), QualType()) // __context with shared vars
1613     };
1614     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1615                              Params);
1616     break;
1617   }
1618   case OMPD_parallel_for: {
1619     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1620     QualType KmpInt32PtrTy =
1621         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1622     Sema::CapturedParamNameType Params[] = {
1623         std::make_pair(".global_tid.", KmpInt32PtrTy),
1624         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1625         std::make_pair(StringRef(), QualType()) // __context with shared vars
1626     };
1627     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1628                              Params);
1629     break;
1630   }
1631   case OMPD_parallel_for_simd: {
1632     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1633     QualType KmpInt32PtrTy =
1634         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1635     Sema::CapturedParamNameType Params[] = {
1636         std::make_pair(".global_tid.", KmpInt32PtrTy),
1637         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1638         std::make_pair(StringRef(), QualType()) // __context with shared vars
1639     };
1640     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1641                              Params);
1642     break;
1643   }
1644   case OMPD_parallel_sections: {
1645     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1646     QualType KmpInt32PtrTy =
1647         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1648     Sema::CapturedParamNameType Params[] = {
1649         std::make_pair(".global_tid.", KmpInt32PtrTy),
1650         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1651         std::make_pair(StringRef(), QualType()) // __context with shared vars
1652     };
1653     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1654                              Params);
1655     break;
1656   }
1657   case OMPD_task: {
1658     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1659     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1660     FunctionProtoType::ExtProtoInfo EPI;
1661     EPI.Variadic = true;
1662     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1663     Sema::CapturedParamNameType Params[] = {
1664         std::make_pair(".global_tid.", KmpInt32Ty),
1665         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1666         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1667         std::make_pair(".copy_fn.",
1668                        Context.getPointerType(CopyFnType).withConst()),
1669         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1670         std::make_pair(StringRef(), QualType()) // __context with shared vars
1671     };
1672     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1673                              Params);
1674     // Mark this captured region as inlined, because we don't use outlined
1675     // function directly.
1676     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1677         AlwaysInlineAttr::CreateImplicit(
1678             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1679     break;
1680   }
1681   case OMPD_ordered: {
1682     Sema::CapturedParamNameType Params[] = {
1683         std::make_pair(StringRef(), QualType()) // __context with shared vars
1684     };
1685     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1686                              Params);
1687     break;
1688   }
1689   case OMPD_atomic: {
1690     Sema::CapturedParamNameType Params[] = {
1691         std::make_pair(StringRef(), QualType()) // __context with shared vars
1692     };
1693     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1694                              Params);
1695     break;
1696   }
1697   case OMPD_target_data:
1698   case OMPD_target:
1699   case OMPD_target_parallel:
1700   case OMPD_target_parallel_for: {
1701     Sema::CapturedParamNameType Params[] = {
1702         std::make_pair(StringRef(), QualType()) // __context with shared vars
1703     };
1704     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1705                              Params);
1706     break;
1707   }
1708   case OMPD_teams: {
1709     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1710     QualType KmpInt32PtrTy =
1711         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1712     Sema::CapturedParamNameType Params[] = {
1713         std::make_pair(".global_tid.", KmpInt32PtrTy),
1714         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1715         std::make_pair(StringRef(), QualType()) // __context with shared vars
1716     };
1717     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1718                              Params);
1719     break;
1720   }
1721   case OMPD_taskgroup: {
1722     Sema::CapturedParamNameType Params[] = {
1723         std::make_pair(StringRef(), QualType()) // __context with shared vars
1724     };
1725     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1726                              Params);
1727     break;
1728   }
1729   case OMPD_taskloop:
1730   case OMPD_taskloop_simd: {
1731     QualType KmpInt32Ty =
1732         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1733     QualType KmpUInt64Ty =
1734         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1735     QualType KmpInt64Ty =
1736         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1737     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1738     FunctionProtoType::ExtProtoInfo EPI;
1739     EPI.Variadic = true;
1740     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1741     Sema::CapturedParamNameType Params[] = {
1742         std::make_pair(".global_tid.", KmpInt32Ty),
1743         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1744         std::make_pair(".privates.",
1745                        Context.VoidPtrTy.withConst().withRestrict()),
1746         std::make_pair(
1747             ".copy_fn.",
1748             Context.getPointerType(CopyFnType).withConst().withRestrict()),
1749         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1750         std::make_pair(".lb.", KmpUInt64Ty),
1751         std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1752         std::make_pair(".liter.", KmpInt32Ty),
1753         std::make_pair(StringRef(), QualType()) // __context with shared vars
1754     };
1755     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1756                              Params);
1757     // Mark this captured region as inlined, because we don't use outlined
1758     // function directly.
1759     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1760         AlwaysInlineAttr::CreateImplicit(
1761             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1762     break;
1763   }
1764   case OMPD_distribute: {
1765     Sema::CapturedParamNameType Params[] = {
1766         std::make_pair(StringRef(), QualType()) // __context with shared vars
1767     };
1768     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1769                              Params);
1770     break;
1771   }
1772   case OMPD_threadprivate:
1773   case OMPD_taskyield:
1774   case OMPD_barrier:
1775   case OMPD_taskwait:
1776   case OMPD_cancellation_point:
1777   case OMPD_cancel:
1778   case OMPD_flush:
1779   case OMPD_target_enter_data:
1780   case OMPD_target_exit_data:
1781   case OMPD_declare_reduction:
1782   case OMPD_declare_simd:
1783   case OMPD_declare_target:
1784   case OMPD_end_declare_target:
1785     llvm_unreachable("OpenMP Directive is not allowed");
1786   case OMPD_unknown:
1787     llvm_unreachable("Unknown OpenMP directive");
1788   }
1789 }
1790 
1791 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1792                                              Expr *CaptureExpr, bool WithInit,
1793                                              bool AsExpression) {
1794   assert(CaptureExpr);
1795   ASTContext &C = S.getASTContext();
1796   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
1797   QualType Ty = Init->getType();
1798   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1799     if (S.getLangOpts().CPlusPlus)
1800       Ty = C.getLValueReferenceType(Ty);
1801     else {
1802       Ty = C.getPointerType(Ty);
1803       ExprResult Res =
1804           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1805       if (!Res.isUsable())
1806         return nullptr;
1807       Init = Res.get();
1808     }
1809     WithInit = true;
1810   }
1811   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1812   if (!WithInit)
1813     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
1814   S.CurContext->addHiddenDecl(CED);
1815   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1816                          /*TypeMayContainAuto=*/true);
1817   return CED;
1818 }
1819 
1820 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1821                                  bool WithInit) {
1822   OMPCapturedExprDecl *CD;
1823   if (auto *VD = S.IsOpenMPCapturedDecl(D))
1824     CD = cast<OMPCapturedExprDecl>(VD);
1825   else
1826     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1827                           /*AsExpression=*/false);
1828   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1829                           CaptureExpr->getExprLoc());
1830 }
1831 
1832 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1833   if (!Ref) {
1834     auto *CD =
1835         buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1836                          CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1837     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1838                            CaptureExpr->getExprLoc());
1839   }
1840   ExprResult Res = Ref;
1841   if (!S.getLangOpts().CPlusPlus &&
1842       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1843       Ref->getType()->isPointerType())
1844     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1845   if (!Res.isUsable())
1846     return ExprError();
1847   return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
1848 }
1849 
1850 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1851                                       ArrayRef<OMPClause *> Clauses) {
1852   if (!S.isUsable()) {
1853     ActOnCapturedRegionError();
1854     return StmtError();
1855   }
1856 
1857   OMPOrderedClause *OC = nullptr;
1858   OMPScheduleClause *SC = nullptr;
1859   SmallVector<OMPLinearClause *, 4> LCs;
1860   // This is required for proper codegen.
1861   for (auto *Clause : Clauses) {
1862     if (isOpenMPPrivate(Clause->getClauseKind()) ||
1863         Clause->getClauseKind() == OMPC_copyprivate ||
1864         (getLangOpts().OpenMPUseTLS &&
1865          getASTContext().getTargetInfo().isTLSSupported() &&
1866          Clause->getClauseKind() == OMPC_copyin)) {
1867       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
1868       // Mark all variables in private list clauses as used in inner region.
1869       for (auto *VarRef : Clause->children()) {
1870         if (auto *E = cast_or_null<Expr>(VarRef)) {
1871           MarkDeclarationsReferencedInExpr(E);
1872         }
1873       }
1874       DSAStack->setForceVarCapturing(/*V=*/false);
1875     } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
1876       // Mark all variables in private list clauses as used in inner region.
1877       // Required for proper codegen of combined directives.
1878       // TODO: add processing for other clauses.
1879       if (auto *C = OMPClauseWithPreInit::get(Clause)) {
1880         if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1881           for (auto *D : DS->decls())
1882             MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1883         }
1884       }
1885       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1886         if (auto *E = C->getPostUpdateExpr())
1887           MarkDeclarationsReferencedInExpr(E);
1888       }
1889     }
1890     if (Clause->getClauseKind() == OMPC_schedule)
1891       SC = cast<OMPScheduleClause>(Clause);
1892     else if (Clause->getClauseKind() == OMPC_ordered)
1893       OC = cast<OMPOrderedClause>(Clause);
1894     else if (Clause->getClauseKind() == OMPC_linear)
1895       LCs.push_back(cast<OMPLinearClause>(Clause));
1896   }
1897   bool ErrorFound = false;
1898   // OpenMP, 2.7.1 Loop Construct, Restrictions
1899   // The nonmonotonic modifier cannot be specified if an ordered clause is
1900   // specified.
1901   if (SC &&
1902       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1903        SC->getSecondScheduleModifier() ==
1904            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1905       OC) {
1906     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1907              ? SC->getFirstScheduleModifierLoc()
1908              : SC->getSecondScheduleModifierLoc(),
1909          diag::err_omp_schedule_nonmonotonic_ordered)
1910         << SourceRange(OC->getLocStart(), OC->getLocEnd());
1911     ErrorFound = true;
1912   }
1913   if (!LCs.empty() && OC && OC->getNumForLoops()) {
1914     for (auto *C : LCs) {
1915       Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1916           << SourceRange(OC->getLocStart(), OC->getLocEnd());
1917     }
1918     ErrorFound = true;
1919   }
1920   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1921       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1922       OC->getNumForLoops()) {
1923     Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1924         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1925     ErrorFound = true;
1926   }
1927   if (ErrorFound) {
1928     ActOnCapturedRegionError();
1929     return StmtError();
1930   }
1931   return ActOnCapturedRegionEnd(S.get());
1932 }
1933 
1934 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1935                                   OpenMPDirectiveKind CurrentRegion,
1936                                   const DeclarationNameInfo &CurrentName,
1937                                   OpenMPDirectiveKind CancelRegion,
1938                                   SourceLocation StartLoc) {
1939   // Allowed nesting of constructs
1940   // +------------------+-----------------+------------------------------------+
1941   // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1942   // +------------------+-----------------+------------------------------------+
1943   // | parallel         | parallel        | *                                  |
1944   // | parallel         | for             | *                                  |
1945   // | parallel         | for simd        | *                                  |
1946   // | parallel         | master          | *                                  |
1947   // | parallel         | critical        | *                                  |
1948   // | parallel         | simd            | *                                  |
1949   // | parallel         | sections        | *                                  |
1950   // | parallel         | section         | +                                  |
1951   // | parallel         | single          | *                                  |
1952   // | parallel         | parallel for    | *                                  |
1953   // | parallel         |parallel for simd| *                                  |
1954   // | parallel         |parallel sections| *                                  |
1955   // | parallel         | task            | *                                  |
1956   // | parallel         | taskyield       | *                                  |
1957   // | parallel         | barrier         | *                                  |
1958   // | parallel         | taskwait        | *                                  |
1959   // | parallel         | taskgroup       | *                                  |
1960   // | parallel         | flush           | *                                  |
1961   // | parallel         | ordered         | +                                  |
1962   // | parallel         | atomic          | *                                  |
1963   // | parallel         | target          | *                                  |
1964   // | parallel         | target parallel | *                                  |
1965   // | parallel         | target parallel | *                                  |
1966   // |                  | for             |                                    |
1967   // | parallel         | target enter    | *                                  |
1968   // |                  | data            |                                    |
1969   // | parallel         | target exit     | *                                  |
1970   // |                  | data            |                                    |
1971   // | parallel         | teams           | +                                  |
1972   // | parallel         | cancellation    |                                    |
1973   // |                  | point           | !                                  |
1974   // | parallel         | cancel          | !                                  |
1975   // | parallel         | taskloop        | *                                  |
1976   // | parallel         | taskloop simd   | *                                  |
1977   // | parallel         | distribute      |                                    |
1978   // +------------------+-----------------+------------------------------------+
1979   // | for              | parallel        | *                                  |
1980   // | for              | for             | +                                  |
1981   // | for              | for simd        | +                                  |
1982   // | for              | master          | +                                  |
1983   // | for              | critical        | *                                  |
1984   // | for              | simd            | *                                  |
1985   // | for              | sections        | +                                  |
1986   // | for              | section         | +                                  |
1987   // | for              | single          | +                                  |
1988   // | for              | parallel for    | *                                  |
1989   // | for              |parallel for simd| *                                  |
1990   // | for              |parallel sections| *                                  |
1991   // | for              | task            | *                                  |
1992   // | for              | taskyield       | *                                  |
1993   // | for              | barrier         | +                                  |
1994   // | for              | taskwait        | *                                  |
1995   // | for              | taskgroup       | *                                  |
1996   // | for              | flush           | *                                  |
1997   // | for              | ordered         | * (if construct is ordered)        |
1998   // | for              | atomic          | *                                  |
1999   // | for              | target          | *                                  |
2000   // | for              | target parallel | *                                  |
2001   // | for              | target parallel | *                                  |
2002   // |                  | for             |                                    |
2003   // | for              | target enter    | *                                  |
2004   // |                  | data            |                                    |
2005   // | for              | target exit     | *                                  |
2006   // |                  | data            |                                    |
2007   // | for              | teams           | +                                  |
2008   // | for              | cancellation    |                                    |
2009   // |                  | point           | !                                  |
2010   // | for              | cancel          | !                                  |
2011   // | for              | taskloop        | *                                  |
2012   // | for              | taskloop simd   | *                                  |
2013   // | for              | distribute      |                                    |
2014   // +------------------+-----------------+------------------------------------+
2015   // | master           | parallel        | *                                  |
2016   // | master           | for             | +                                  |
2017   // | master           | for simd        | +                                  |
2018   // | master           | master          | *                                  |
2019   // | master           | critical        | *                                  |
2020   // | master           | simd            | *                                  |
2021   // | master           | sections        | +                                  |
2022   // | master           | section         | +                                  |
2023   // | master           | single          | +                                  |
2024   // | master           | parallel for    | *                                  |
2025   // | master           |parallel for simd| *                                  |
2026   // | master           |parallel sections| *                                  |
2027   // | master           | task            | *                                  |
2028   // | master           | taskyield       | *                                  |
2029   // | master           | barrier         | +                                  |
2030   // | master           | taskwait        | *                                  |
2031   // | master           | taskgroup       | *                                  |
2032   // | master           | flush           | *                                  |
2033   // | master           | ordered         | +                                  |
2034   // | master           | atomic          | *                                  |
2035   // | master           | target          | *                                  |
2036   // | master           | target parallel | *                                  |
2037   // | master           | target parallel | *                                  |
2038   // |                  | for             |                                    |
2039   // | master           | target enter    | *                                  |
2040   // |                  | data            |                                    |
2041   // | master           | target exit     | *                                  |
2042   // |                  | data            |                                    |
2043   // | master           | teams           | +                                  |
2044   // | master           | cancellation    |                                    |
2045   // |                  | point           |                                    |
2046   // | master           | cancel          |                                    |
2047   // | master           | taskloop        | *                                  |
2048   // | master           | taskloop simd   | *                                  |
2049   // | master           | distribute      |                                    |
2050   // +------------------+-----------------+------------------------------------+
2051   // | critical         | parallel        | *                                  |
2052   // | critical         | for             | +                                  |
2053   // | critical         | for simd        | +                                  |
2054   // | critical         | master          | *                                  |
2055   // | critical         | critical        | * (should have different names)    |
2056   // | critical         | simd            | *                                  |
2057   // | critical         | sections        | +                                  |
2058   // | critical         | section         | +                                  |
2059   // | critical         | single          | +                                  |
2060   // | critical         | parallel for    | *                                  |
2061   // | critical         |parallel for simd| *                                  |
2062   // | critical         |parallel sections| *                                  |
2063   // | critical         | task            | *                                  |
2064   // | critical         | taskyield       | *                                  |
2065   // | critical         | barrier         | +                                  |
2066   // | critical         | taskwait        | *                                  |
2067   // | critical         | taskgroup       | *                                  |
2068   // | critical         | ordered         | +                                  |
2069   // | critical         | atomic          | *                                  |
2070   // | critical         | target          | *                                  |
2071   // | critical         | target parallel | *                                  |
2072   // | critical         | target parallel | *                                  |
2073   // |                  | for             |                                    |
2074   // | critical         | target enter    | *                                  |
2075   // |                  | data            |                                    |
2076   // | critical         | target exit     | *                                  |
2077   // |                  | data            |                                    |
2078   // | critical         | teams           | +                                  |
2079   // | critical         | cancellation    |                                    |
2080   // |                  | point           |                                    |
2081   // | critical         | cancel          |                                    |
2082   // | critical         | taskloop        | *                                  |
2083   // | critical         | taskloop simd   | *                                  |
2084   // | critical         | distribute      |                                    |
2085   // +------------------+-----------------+------------------------------------+
2086   // | simd             | parallel        |                                    |
2087   // | simd             | for             |                                    |
2088   // | simd             | for simd        |                                    |
2089   // | simd             | master          |                                    |
2090   // | simd             | critical        |                                    |
2091   // | simd             | simd            | *                                  |
2092   // | simd             | sections        |                                    |
2093   // | simd             | section         |                                    |
2094   // | simd             | single          |                                    |
2095   // | simd             | parallel for    |                                    |
2096   // | simd             |parallel for simd|                                    |
2097   // | simd             |parallel sections|                                    |
2098   // | simd             | task            |                                    |
2099   // | simd             | taskyield       |                                    |
2100   // | simd             | barrier         |                                    |
2101   // | simd             | taskwait        |                                    |
2102   // | simd             | taskgroup       |                                    |
2103   // | simd             | flush           |                                    |
2104   // | simd             | ordered         | + (with simd clause)               |
2105   // | simd             | atomic          |                                    |
2106   // | simd             | target          |                                    |
2107   // | simd             | target parallel |                                    |
2108   // | simd             | target parallel |                                    |
2109   // |                  | for             |                                    |
2110   // | simd             | target enter    |                                    |
2111   // |                  | data            |                                    |
2112   // | simd             | target exit     |                                    |
2113   // |                  | data            |                                    |
2114   // | simd             | teams           |                                    |
2115   // | simd             | cancellation    |                                    |
2116   // |                  | point           |                                    |
2117   // | simd             | cancel          |                                    |
2118   // | simd             | taskloop        |                                    |
2119   // | simd             | taskloop simd   |                                    |
2120   // | simd             | distribute      |                                    |
2121   // +------------------+-----------------+------------------------------------+
2122   // | for simd         | parallel        |                                    |
2123   // | for simd         | for             |                                    |
2124   // | for simd         | for simd        |                                    |
2125   // | for simd         | master          |                                    |
2126   // | for simd         | critical        |                                    |
2127   // | for simd         | simd            | *                                  |
2128   // | for simd         | sections        |                                    |
2129   // | for simd         | section         |                                    |
2130   // | for simd         | single          |                                    |
2131   // | for simd         | parallel for    |                                    |
2132   // | for simd         |parallel for simd|                                    |
2133   // | for simd         |parallel sections|                                    |
2134   // | for simd         | task            |                                    |
2135   // | for simd         | taskyield       |                                    |
2136   // | for simd         | barrier         |                                    |
2137   // | for simd         | taskwait        |                                    |
2138   // | for simd         | taskgroup       |                                    |
2139   // | for simd         | flush           |                                    |
2140   // | for simd         | ordered         | + (with simd clause)               |
2141   // | for simd         | atomic          |                                    |
2142   // | for simd         | target          |                                    |
2143   // | for simd         | target parallel |                                    |
2144   // | for simd         | target parallel |                                    |
2145   // |                  | for             |                                    |
2146   // | for simd         | target enter    |                                    |
2147   // |                  | data            |                                    |
2148   // | for simd         | target exit     |                                    |
2149   // |                  | data            |                                    |
2150   // | for simd         | teams           |                                    |
2151   // | for simd         | cancellation    |                                    |
2152   // |                  | point           |                                    |
2153   // | for simd         | cancel          |                                    |
2154   // | for simd         | taskloop        |                                    |
2155   // | for simd         | taskloop simd   |                                    |
2156   // | for simd         | distribute      |                                    |
2157   // +------------------+-----------------+------------------------------------+
2158   // | parallel for simd| parallel        |                                    |
2159   // | parallel for simd| for             |                                    |
2160   // | parallel for simd| for simd        |                                    |
2161   // | parallel for simd| master          |                                    |
2162   // | parallel for simd| critical        |                                    |
2163   // | parallel for simd| simd            | *                                  |
2164   // | parallel for simd| sections        |                                    |
2165   // | parallel for simd| section         |                                    |
2166   // | parallel for simd| single          |                                    |
2167   // | parallel for simd| parallel for    |                                    |
2168   // | parallel for simd|parallel for simd|                                    |
2169   // | parallel for simd|parallel sections|                                    |
2170   // | parallel for simd| task            |                                    |
2171   // | parallel for simd| taskyield       |                                    |
2172   // | parallel for simd| barrier         |                                    |
2173   // | parallel for simd| taskwait        |                                    |
2174   // | parallel for simd| taskgroup       |                                    |
2175   // | parallel for simd| flush           |                                    |
2176   // | parallel for simd| ordered         | + (with simd clause)               |
2177   // | parallel for simd| atomic          |                                    |
2178   // | parallel for simd| target          |                                    |
2179   // | parallel for simd| target parallel |                                    |
2180   // | parallel for simd| target parallel |                                    |
2181   // |                  | for             |                                    |
2182   // | parallel for simd| target enter    |                                    |
2183   // |                  | data            |                                    |
2184   // | parallel for simd| target exit     |                                    |
2185   // |                  | data            |                                    |
2186   // | parallel for simd| teams           |                                    |
2187   // | parallel for simd| cancellation    |                                    |
2188   // |                  | point           |                                    |
2189   // | parallel for simd| cancel          |                                    |
2190   // | parallel for simd| taskloop        |                                    |
2191   // | parallel for simd| taskloop simd   |                                    |
2192   // | parallel for simd| 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   // +------------------+-----------------+------------------------------------+
2230   // | section          | parallel        | *                                  |
2231   // | section          | for             | +                                  |
2232   // | section          | for simd        | +                                  |
2233   // | section          | master          | +                                  |
2234   // | section          | critical        | *                                  |
2235   // | section          | simd            | *                                  |
2236   // | section          | sections        | +                                  |
2237   // | section          | section         | +                                  |
2238   // | section          | single          | +                                  |
2239   // | section          | parallel for    | *                                  |
2240   // | section          |parallel for simd| *                                  |
2241   // | section          |parallel sections| *                                  |
2242   // | section          | task            | *                                  |
2243   // | section          | taskyield       | *                                  |
2244   // | section          | barrier         | +                                  |
2245   // | section          | taskwait        | *                                  |
2246   // | section          | taskgroup       | *                                  |
2247   // | section          | flush           | *                                  |
2248   // | section          | ordered         | +                                  |
2249   // | section          | atomic          | *                                  |
2250   // | section          | target          | *                                  |
2251   // | section          | target parallel | *                                  |
2252   // | section          | target parallel | *                                  |
2253   // |                  | for             |                                    |
2254   // | section          | target enter    | *                                  |
2255   // |                  | data            |                                    |
2256   // | section          | target exit     | *                                  |
2257   // |                  | data            |                                    |
2258   // | section          | teams           | +                                  |
2259   // | section          | cancellation    |                                    |
2260   // |                  | point           | !                                  |
2261   // | section          | cancel          | !                                  |
2262   // | section          | taskloop        | *                                  |
2263   // | section          | taskloop simd   | *                                  |
2264   // | section          | distribute      |                                    |
2265   // +------------------+-----------------+------------------------------------+
2266   // | single           | parallel        | *                                  |
2267   // | single           | for             | +                                  |
2268   // | single           | for simd        | +                                  |
2269   // | single           | master          | +                                  |
2270   // | single           | critical        | *                                  |
2271   // | single           | simd            | *                                  |
2272   // | single           | sections        | +                                  |
2273   // | single           | section         | +                                  |
2274   // | single           | single          | +                                  |
2275   // | single           | parallel for    | *                                  |
2276   // | single           |parallel for simd| *                                  |
2277   // | single           |parallel sections| *                                  |
2278   // | single           | task            | *                                  |
2279   // | single           | taskyield       | *                                  |
2280   // | single           | barrier         | +                                  |
2281   // | single           | taskwait        | *                                  |
2282   // | single           | taskgroup       | *                                  |
2283   // | single           | flush           | *                                  |
2284   // | single           | ordered         | +                                  |
2285   // | single           | atomic          | *                                  |
2286   // | single           | target          | *                                  |
2287   // | single           | target parallel | *                                  |
2288   // | single           | target parallel | *                                  |
2289   // |                  | for             |                                    |
2290   // | single           | target enter    | *                                  |
2291   // |                  | data            |                                    |
2292   // | single           | target exit     | *                                  |
2293   // |                  | data            |                                    |
2294   // | single           | teams           | +                                  |
2295   // | single           | cancellation    |                                    |
2296   // |                  | point           |                                    |
2297   // | single           | cancel          |                                    |
2298   // | single           | taskloop        | *                                  |
2299   // | single           | taskloop simd   | *                                  |
2300   // | single           | distribute      |                                    |
2301   // +------------------+-----------------+------------------------------------+
2302   // | parallel for     | parallel        | *                                  |
2303   // | parallel for     | for             | +                                  |
2304   // | parallel for     | for simd        | +                                  |
2305   // | parallel for     | master          | +                                  |
2306   // | parallel for     | critical        | *                                  |
2307   // | parallel for     | simd            | *                                  |
2308   // | parallel for     | sections        | +                                  |
2309   // | parallel for     | section         | +                                  |
2310   // | parallel for     | single          | +                                  |
2311   // | parallel for     | parallel for    | *                                  |
2312   // | parallel for     |parallel for simd| *                                  |
2313   // | parallel for     |parallel sections| *                                  |
2314   // | parallel for     | task            | *                                  |
2315   // | parallel for     | taskyield       | *                                  |
2316   // | parallel for     | barrier         | +                                  |
2317   // | parallel for     | taskwait        | *                                  |
2318   // | parallel for     | taskgroup       | *                                  |
2319   // | parallel for     | flush           | *                                  |
2320   // | parallel for     | ordered         | * (if construct is ordered)        |
2321   // | parallel for     | atomic          | *                                  |
2322   // | parallel for     | target          | *                                  |
2323   // | parallel for     | target parallel | *                                  |
2324   // | parallel for     | target parallel | *                                  |
2325   // |                  | for             |                                    |
2326   // | parallel for     | target enter    | *                                  |
2327   // |                  | data            |                                    |
2328   // | parallel for     | target exit     | *                                  |
2329   // |                  | data            |                                    |
2330   // | parallel for     | teams           | +                                  |
2331   // | parallel for     | cancellation    |                                    |
2332   // |                  | point           | !                                  |
2333   // | parallel for     | cancel          | !                                  |
2334   // | parallel for     | taskloop        | *                                  |
2335   // | parallel for     | taskloop simd   | *                                  |
2336   // | parallel for     | distribute      |                                    |
2337   // +------------------+-----------------+------------------------------------+
2338   // | parallel sections| parallel        | *                                  |
2339   // | parallel sections| for             | +                                  |
2340   // | parallel sections| for simd        | +                                  |
2341   // | parallel sections| master          | +                                  |
2342   // | parallel sections| critical        | +                                  |
2343   // | parallel sections| simd            | *                                  |
2344   // | parallel sections| sections        | +                                  |
2345   // | parallel sections| section         | *                                  |
2346   // | parallel sections| single          | +                                  |
2347   // | parallel sections| parallel for    | *                                  |
2348   // | parallel sections|parallel for simd| *                                  |
2349   // | parallel sections|parallel sections| *                                  |
2350   // | parallel sections| task            | *                                  |
2351   // | parallel sections| taskyield       | *                                  |
2352   // | parallel sections| barrier         | +                                  |
2353   // | parallel sections| taskwait        | *                                  |
2354   // | parallel sections| taskgroup       | *                                  |
2355   // | parallel sections| flush           | *                                  |
2356   // | parallel sections| ordered         | +                                  |
2357   // | parallel sections| atomic          | *                                  |
2358   // | parallel sections| target          | *                                  |
2359   // | parallel sections| target parallel | *                                  |
2360   // | parallel sections| target parallel | *                                  |
2361   // |                  | for             |                                    |
2362   // | parallel sections| target enter    | *                                  |
2363   // |                  | data            |                                    |
2364   // | parallel sections| target exit     | *                                  |
2365   // |                  | data            |                                    |
2366   // | parallel sections| teams           | +                                  |
2367   // | parallel sections| cancellation    |                                    |
2368   // |                  | point           | !                                  |
2369   // | parallel sections| cancel          | !                                  |
2370   // | parallel sections| taskloop        | *                                  |
2371   // | parallel sections| taskloop simd   | *                                  |
2372   // | parallel sections| distribute      |                                    |
2373   // +------------------+-----------------+------------------------------------+
2374   // | task             | parallel        | *                                  |
2375   // | task             | for             | +                                  |
2376   // | task             | for simd        | +                                  |
2377   // | task             | master          | +                                  |
2378   // | task             | critical        | *                                  |
2379   // | task             | simd            | *                                  |
2380   // | task             | sections        | +                                  |
2381   // | task             | section         | +                                  |
2382   // | task             | single          | +                                  |
2383   // | task             | parallel for    | *                                  |
2384   // | task             |parallel for simd| *                                  |
2385   // | task             |parallel sections| *                                  |
2386   // | task             | task            | *                                  |
2387   // | task             | taskyield       | *                                  |
2388   // | task             | barrier         | +                                  |
2389   // | task             | taskwait        | *                                  |
2390   // | task             | taskgroup       | *                                  |
2391   // | task             | flush           | *                                  |
2392   // | task             | ordered         | +                                  |
2393   // | task             | atomic          | *                                  |
2394   // | task             | target          | *                                  |
2395   // | task             | target parallel | *                                  |
2396   // | task             | target parallel | *                                  |
2397   // |                  | for             |                                    |
2398   // | task             | target enter    | *                                  |
2399   // |                  | data            |                                    |
2400   // | task             | target exit     | *                                  |
2401   // |                  | data            |                                    |
2402   // | task             | teams           | +                                  |
2403   // | task             | cancellation    |                                    |
2404   // |                  | point           | !                                  |
2405   // | task             | cancel          | !                                  |
2406   // | task             | taskloop        | *                                  |
2407   // | task             | taskloop simd   | *                                  |
2408   // | task             | distribute      |                                    |
2409   // +------------------+-----------------+------------------------------------+
2410   // | ordered          | parallel        | *                                  |
2411   // | ordered          | for             | +                                  |
2412   // | ordered          | for simd        | +                                  |
2413   // | ordered          | master          | *                                  |
2414   // | ordered          | critical        | *                                  |
2415   // | ordered          | simd            | *                                  |
2416   // | ordered          | sections        | +                                  |
2417   // | ordered          | section         | +                                  |
2418   // | ordered          | single          | +                                  |
2419   // | ordered          | parallel for    | *                                  |
2420   // | ordered          |parallel for simd| *                                  |
2421   // | ordered          |parallel sections| *                                  |
2422   // | ordered          | task            | *                                  |
2423   // | ordered          | taskyield       | *                                  |
2424   // | ordered          | barrier         | +                                  |
2425   // | ordered          | taskwait        | *                                  |
2426   // | ordered          | taskgroup       | *                                  |
2427   // | ordered          | flush           | *                                  |
2428   // | ordered          | ordered         | +                                  |
2429   // | ordered          | atomic          | *                                  |
2430   // | ordered          | target          | *                                  |
2431   // | ordered          | target parallel | *                                  |
2432   // | ordered          | target parallel | *                                  |
2433   // |                  | for             |                                    |
2434   // | ordered          | target enter    | *                                  |
2435   // |                  | data            |                                    |
2436   // | ordered          | target exit     | *                                  |
2437   // |                  | data            |                                    |
2438   // | ordered          | teams           | +                                  |
2439   // | ordered          | cancellation    |                                    |
2440   // |                  | point           |                                    |
2441   // | ordered          | cancel          |                                    |
2442   // | ordered          | taskloop        | *                                  |
2443   // | ordered          | taskloop simd   | *                                  |
2444   // | ordered          | distribute      |                                    |
2445   // +------------------+-----------------+------------------------------------+
2446   // | atomic           | parallel        |                                    |
2447   // | atomic           | for             |                                    |
2448   // | atomic           | for simd        |                                    |
2449   // | atomic           | master          |                                    |
2450   // | atomic           | critical        |                                    |
2451   // | atomic           | simd            |                                    |
2452   // | atomic           | sections        |                                    |
2453   // | atomic           | section         |                                    |
2454   // | atomic           | single          |                                    |
2455   // | atomic           | parallel for    |                                    |
2456   // | atomic           |parallel for simd|                                    |
2457   // | atomic           |parallel sections|                                    |
2458   // | atomic           | task            |                                    |
2459   // | atomic           | taskyield       |                                    |
2460   // | atomic           | barrier         |                                    |
2461   // | atomic           | taskwait        |                                    |
2462   // | atomic           | taskgroup       |                                    |
2463   // | atomic           | flush           |                                    |
2464   // | atomic           | ordered         |                                    |
2465   // | atomic           | atomic          |                                    |
2466   // | atomic           | target          |                                    |
2467   // | atomic           | target parallel |                                    |
2468   // | atomic           | target parallel |                                    |
2469   // |                  | for             |                                    |
2470   // | atomic           | target enter    |                                    |
2471   // |                  | data            |                                    |
2472   // | atomic           | target exit     |                                    |
2473   // |                  | data            |                                    |
2474   // | atomic           | teams           |                                    |
2475   // | atomic           | cancellation    |                                    |
2476   // |                  | point           |                                    |
2477   // | atomic           | cancel          |                                    |
2478   // | atomic           | taskloop        |                                    |
2479   // | atomic           | taskloop simd   |                                    |
2480   // | atomic           | distribute      |                                    |
2481   // +------------------+-----------------+------------------------------------+
2482   // | target           | parallel        | *                                  |
2483   // | target           | for             | *                                  |
2484   // | target           | for simd        | *                                  |
2485   // | target           | master          | *                                  |
2486   // | target           | critical        | *                                  |
2487   // | target           | simd            | *                                  |
2488   // | target           | sections        | *                                  |
2489   // | target           | section         | *                                  |
2490   // | target           | single          | *                                  |
2491   // | target           | parallel for    | *                                  |
2492   // | target           |parallel for simd| *                                  |
2493   // | target           |parallel sections| *                                  |
2494   // | target           | task            | *                                  |
2495   // | target           | taskyield       | *                                  |
2496   // | target           | barrier         | *                                  |
2497   // | target           | taskwait        | *                                  |
2498   // | target           | taskgroup       | *                                  |
2499   // | target           | flush           | *                                  |
2500   // | target           | ordered         | *                                  |
2501   // | target           | atomic          | *                                  |
2502   // | target           | target          |                                    |
2503   // | target           | target parallel |                                    |
2504   // | target           | target parallel |                                    |
2505   // |                  | for             |                                    |
2506   // | target           | target enter    |                                    |
2507   // |                  | data            |                                    |
2508   // | target           | target exit     |                                    |
2509   // |                  | data            |                                    |
2510   // | target           | teams           | *                                  |
2511   // | target           | cancellation    |                                    |
2512   // |                  | point           |                                    |
2513   // | target           | cancel          |                                    |
2514   // | target           | taskloop        | *                                  |
2515   // | target           | taskloop simd   | *                                  |
2516   // | target           | distribute      |                                    |
2517   // +------------------+-----------------+------------------------------------+
2518   // | target parallel  | parallel        | *                                  |
2519   // | target parallel  | for             | *                                  |
2520   // | target parallel  | for simd        | *                                  |
2521   // | target parallel  | master          | *                                  |
2522   // | target parallel  | critical        | *                                  |
2523   // | target parallel  | simd            | *                                  |
2524   // | target parallel  | sections        | *                                  |
2525   // | target parallel  | section         | *                                  |
2526   // | target parallel  | single          | *                                  |
2527   // | target parallel  | parallel for    | *                                  |
2528   // | target parallel  |parallel for simd| *                                  |
2529   // | target parallel  |parallel sections| *                                  |
2530   // | target parallel  | task            | *                                  |
2531   // | target parallel  | taskyield       | *                                  |
2532   // | target parallel  | barrier         | *                                  |
2533   // | target parallel  | taskwait        | *                                  |
2534   // | target parallel  | taskgroup       | *                                  |
2535   // | target parallel  | flush           | *                                  |
2536   // | target parallel  | ordered         | *                                  |
2537   // | target parallel  | atomic          | *                                  |
2538   // | target parallel  | target          |                                    |
2539   // | target parallel  | target parallel |                                    |
2540   // | target parallel  | target parallel |                                    |
2541   // |                  | for             |                                    |
2542   // | target parallel  | target enter    |                                    |
2543   // |                  | data            |                                    |
2544   // | target parallel  | target exit     |                                    |
2545   // |                  | data            |                                    |
2546   // | target parallel  | teams           |                                    |
2547   // | target parallel  | cancellation    |                                    |
2548   // |                  | point           | !                                  |
2549   // | target parallel  | cancel          | !                                  |
2550   // | target parallel  | taskloop        | *                                  |
2551   // | target parallel  | taskloop simd   | *                                  |
2552   // | target parallel  | distribute      |                                    |
2553   // +------------------+-----------------+------------------------------------+
2554   // | target parallel  | parallel        | *                                  |
2555   // | for              |                 |                                    |
2556   // | target parallel  | for             | *                                  |
2557   // | for              |                 |                                    |
2558   // | target parallel  | for simd        | *                                  |
2559   // | for              |                 |                                    |
2560   // | target parallel  | master          | *                                  |
2561   // | for              |                 |                                    |
2562   // | target parallel  | critical        | *                                  |
2563   // | for              |                 |                                    |
2564   // | target parallel  | simd            | *                                  |
2565   // | for              |                 |                                    |
2566   // | target parallel  | sections        | *                                  |
2567   // | for              |                 |                                    |
2568   // | target parallel  | section         | *                                  |
2569   // | for              |                 |                                    |
2570   // | target parallel  | single          | *                                  |
2571   // | for              |                 |                                    |
2572   // | target parallel  | parallel for    | *                                  |
2573   // | for              |                 |                                    |
2574   // | target parallel  |parallel for simd| *                                  |
2575   // | for              |                 |                                    |
2576   // | target parallel  |parallel sections| *                                  |
2577   // | for              |                 |                                    |
2578   // | target parallel  | task            | *                                  |
2579   // | for              |                 |                                    |
2580   // | target parallel  | taskyield       | *                                  |
2581   // | for              |                 |                                    |
2582   // | target parallel  | barrier         | *                                  |
2583   // | for              |                 |                                    |
2584   // | target parallel  | taskwait        | *                                  |
2585   // | for              |                 |                                    |
2586   // | target parallel  | taskgroup       | *                                  |
2587   // | for              |                 |                                    |
2588   // | target parallel  | flush           | *                                  |
2589   // | for              |                 |                                    |
2590   // | target parallel  | ordered         | *                                  |
2591   // | for              |                 |                                    |
2592   // | target parallel  | atomic          | *                                  |
2593   // | for              |                 |                                    |
2594   // | target parallel  | target          |                                    |
2595   // | for              |                 |                                    |
2596   // | target parallel  | target parallel |                                    |
2597   // | for              |                 |                                    |
2598   // | target parallel  | target parallel |                                    |
2599   // | for              | for             |                                    |
2600   // | target parallel  | target enter    |                                    |
2601   // | for              | data            |                                    |
2602   // | target parallel  | target exit     |                                    |
2603   // | for              | data            |                                    |
2604   // | target parallel  | teams           |                                    |
2605   // | for              |                 |                                    |
2606   // | target parallel  | cancellation    |                                    |
2607   // | for              | point           | !                                  |
2608   // | target parallel  | cancel          | !                                  |
2609   // | for              |                 |                                    |
2610   // | target parallel  | taskloop        | *                                  |
2611   // | for              |                 |                                    |
2612   // | target parallel  | taskloop simd   | *                                  |
2613   // | for              |                 |                                    |
2614   // | target parallel  | distribute      |                                    |
2615   // | for              |                 |                                    |
2616   // +------------------+-----------------+------------------------------------+
2617   // | teams            | parallel        | *                                  |
2618   // | teams            | for             | +                                  |
2619   // | teams            | for simd        | +                                  |
2620   // | teams            | master          | +                                  |
2621   // | teams            | critical        | +                                  |
2622   // | teams            | simd            | +                                  |
2623   // | teams            | sections        | +                                  |
2624   // | teams            | section         | +                                  |
2625   // | teams            | single          | +                                  |
2626   // | teams            | parallel for    | *                                  |
2627   // | teams            |parallel for simd| *                                  |
2628   // | teams            |parallel sections| *                                  |
2629   // | teams            | task            | +                                  |
2630   // | teams            | taskyield       | +                                  |
2631   // | teams            | barrier         | +                                  |
2632   // | teams            | taskwait        | +                                  |
2633   // | teams            | taskgroup       | +                                  |
2634   // | teams            | flush           | +                                  |
2635   // | teams            | ordered         | +                                  |
2636   // | teams            | atomic          | +                                  |
2637   // | teams            | target          | +                                  |
2638   // | teams            | target parallel | +                                  |
2639   // | teams            | target parallel | +                                  |
2640   // |                  | for             |                                    |
2641   // | teams            | target enter    | +                                  |
2642   // |                  | data            |                                    |
2643   // | teams            | target exit     | +                                  |
2644   // |                  | data            |                                    |
2645   // | teams            | teams           | +                                  |
2646   // | teams            | cancellation    |                                    |
2647   // |                  | point           |                                    |
2648   // | teams            | cancel          |                                    |
2649   // | teams            | taskloop        | +                                  |
2650   // | teams            | taskloop simd   | +                                  |
2651   // | teams            | distribute      | !                                  |
2652   // +------------------+-----------------+------------------------------------+
2653   // | taskloop         | parallel        | *                                  |
2654   // | taskloop         | for             | +                                  |
2655   // | taskloop         | for simd        | +                                  |
2656   // | taskloop         | master          | +                                  |
2657   // | taskloop         | critical        | *                                  |
2658   // | taskloop         | simd            | *                                  |
2659   // | taskloop         | sections        | +                                  |
2660   // | taskloop         | section         | +                                  |
2661   // | taskloop         | single          | +                                  |
2662   // | taskloop         | parallel for    | *                                  |
2663   // | taskloop         |parallel for simd| *                                  |
2664   // | taskloop         |parallel sections| *                                  |
2665   // | taskloop         | task            | *                                  |
2666   // | taskloop         | taskyield       | *                                  |
2667   // | taskloop         | barrier         | +                                  |
2668   // | taskloop         | taskwait        | *                                  |
2669   // | taskloop         | taskgroup       | *                                  |
2670   // | taskloop         | flush           | *                                  |
2671   // | taskloop         | ordered         | +                                  |
2672   // | taskloop         | atomic          | *                                  |
2673   // | taskloop         | target          | *                                  |
2674   // | taskloop         | target parallel | *                                  |
2675   // | taskloop         | target parallel | *                                  |
2676   // |                  | for             |                                    |
2677   // | taskloop         | target enter    | *                                  |
2678   // |                  | data            |                                    |
2679   // | taskloop         | target exit     | *                                  |
2680   // |                  | data            |                                    |
2681   // | taskloop         | teams           | +                                  |
2682   // | taskloop         | cancellation    |                                    |
2683   // |                  | point           |                                    |
2684   // | taskloop         | cancel          |                                    |
2685   // | taskloop         | taskloop        | *                                  |
2686   // | taskloop         | distribute      |                                    |
2687   // +------------------+-----------------+------------------------------------+
2688   // | taskloop simd    | parallel        |                                    |
2689   // | taskloop simd    | for             |                                    |
2690   // | taskloop simd    | for simd        |                                    |
2691   // | taskloop simd    | master          |                                    |
2692   // | taskloop simd    | critical        |                                    |
2693   // | taskloop simd    | simd            | *                                  |
2694   // | taskloop simd    | sections        |                                    |
2695   // | taskloop simd    | section         |                                    |
2696   // | taskloop simd    | single          |                                    |
2697   // | taskloop simd    | parallel for    |                                    |
2698   // | taskloop simd    |parallel for simd|                                    |
2699   // | taskloop simd    |parallel sections|                                    |
2700   // | taskloop simd    | task            |                                    |
2701   // | taskloop simd    | taskyield       |                                    |
2702   // | taskloop simd    | barrier         |                                    |
2703   // | taskloop simd    | taskwait        |                                    |
2704   // | taskloop simd    | taskgroup       |                                    |
2705   // | taskloop simd    | flush           |                                    |
2706   // | taskloop simd    | ordered         | + (with simd clause)               |
2707   // | taskloop simd    | atomic          |                                    |
2708   // | taskloop simd    | target          |                                    |
2709   // | taskloop simd    | target parallel |                                    |
2710   // | taskloop simd    | target parallel |                                    |
2711   // |                  | for             |                                    |
2712   // | taskloop simd    | target enter    |                                    |
2713   // |                  | data            |                                    |
2714   // | taskloop simd    | target exit     |                                    |
2715   // |                  | data            |                                    |
2716   // | taskloop simd    | teams           |                                    |
2717   // | taskloop simd    | cancellation    |                                    |
2718   // |                  | point           |                                    |
2719   // | taskloop simd    | cancel          |                                    |
2720   // | taskloop simd    | taskloop        |                                    |
2721   // | taskloop simd    | taskloop simd   |                                    |
2722   // | taskloop simd    | distribute      |                                    |
2723   // +------------------+-----------------+------------------------------------+
2724   // | distribute       | parallel        | *                                  |
2725   // | distribute       | for             | *                                  |
2726   // | distribute       | for simd        | *                                  |
2727   // | distribute       | master          | *                                  |
2728   // | distribute       | critical        | *                                  |
2729   // | distribute       | simd            | *                                  |
2730   // | distribute       | sections        | *                                  |
2731   // | distribute       | section         | *                                  |
2732   // | distribute       | single          | *                                  |
2733   // | distribute       | parallel for    | *                                  |
2734   // | distribute       |parallel for simd| *                                  |
2735   // | distribute       |parallel sections| *                                  |
2736   // | distribute       | task            | *                                  |
2737   // | distribute       | taskyield       | *                                  |
2738   // | distribute       | barrier         | *                                  |
2739   // | distribute       | taskwait        | *                                  |
2740   // | distribute       | taskgroup       | *                                  |
2741   // | distribute       | flush           | *                                  |
2742   // | distribute       | ordered         | +                                  |
2743   // | distribute       | atomic          | *                                  |
2744   // | distribute       | target          |                                    |
2745   // | distribute       | target parallel |                                    |
2746   // | distribute       | target parallel |                                    |
2747   // |                  | for             |                                    |
2748   // | distribute       | target enter    |                                    |
2749   // |                  | data            |                                    |
2750   // | distribute       | target exit     |                                    |
2751   // |                  | data            |                                    |
2752   // | distribute       | teams           |                                    |
2753   // | distribute       | cancellation    | +                                  |
2754   // |                  | point           |                                    |
2755   // | distribute       | cancel          | +                                  |
2756   // | distribute       | taskloop        | *                                  |
2757   // | distribute       | taskloop simd   | *                                  |
2758   // | distribute       | distribute      |                                    |
2759   // +------------------+-----------------+------------------------------------+
2760   if (Stack->getCurScope()) {
2761     auto ParentRegion = Stack->getParentDirective();
2762     auto OffendingRegion = ParentRegion;
2763     bool NestingProhibited = false;
2764     bool CloseNesting = true;
2765     enum {
2766       NoRecommend,
2767       ShouldBeInParallelRegion,
2768       ShouldBeInOrderedRegion,
2769       ShouldBeInTargetRegion,
2770       ShouldBeInTeamsRegion
2771     } Recommend = NoRecommend;
2772     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2773         CurrentRegion != OMPD_simd) {
2774       // OpenMP [2.16, Nesting of Regions]
2775       // OpenMP constructs may not be nested inside a simd region.
2776       // OpenMP [2.8.1,simd Construct, Restrictions]
2777       // An ordered construct with the simd clause is the only OpenMP construct
2778       // that can appear in the simd region.
2779       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2780       return true;
2781     }
2782     if (ParentRegion == OMPD_atomic) {
2783       // OpenMP [2.16, Nesting of Regions]
2784       // OpenMP constructs may not be nested inside an atomic region.
2785       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2786       return true;
2787     }
2788     if (CurrentRegion == OMPD_section) {
2789       // OpenMP [2.7.2, sections Construct, Restrictions]
2790       // Orphaned section directives are prohibited. That is, the section
2791       // directives must appear within the sections construct and must not be
2792       // encountered elsewhere in the sections region.
2793       if (ParentRegion != OMPD_sections &&
2794           ParentRegion != OMPD_parallel_sections) {
2795         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2796             << (ParentRegion != OMPD_unknown)
2797             << getOpenMPDirectiveName(ParentRegion);
2798         return true;
2799       }
2800       return false;
2801     }
2802     // Allow some constructs to be orphaned (they could be used in functions,
2803     // called from OpenMP regions with the required preconditions).
2804     if (ParentRegion == OMPD_unknown)
2805       return false;
2806     if (CurrentRegion == OMPD_cancellation_point ||
2807         CurrentRegion == OMPD_cancel) {
2808       // OpenMP [2.16, Nesting of Regions]
2809       // A cancellation point construct for which construct-type-clause is
2810       // taskgroup must be nested inside a task construct. A cancellation
2811       // point construct for which construct-type-clause is not taskgroup must
2812       // be closely nested inside an OpenMP construct that matches the type
2813       // specified in construct-type-clause.
2814       // A cancel construct for which construct-type-clause is taskgroup must be
2815       // nested inside a task construct. A cancel construct for which
2816       // construct-type-clause is not taskgroup must be closely nested inside an
2817       // OpenMP construct that matches the type specified in
2818       // construct-type-clause.
2819       NestingProhibited =
2820           !((CancelRegion == OMPD_parallel &&
2821              (ParentRegion == OMPD_parallel ||
2822               ParentRegion == OMPD_target_parallel)) ||
2823             (CancelRegion == OMPD_for &&
2824              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2825               ParentRegion == OMPD_target_parallel_for)) ||
2826             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2827             (CancelRegion == OMPD_sections &&
2828              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2829               ParentRegion == OMPD_parallel_sections)));
2830     } else if (CurrentRegion == OMPD_master) {
2831       // OpenMP [2.16, Nesting of Regions]
2832       // A master region may not be closely nested inside a worksharing,
2833       // atomic, or explicit task region.
2834       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2835                           isOpenMPTaskingDirective(ParentRegion);
2836     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2837       // OpenMP [2.16, Nesting of Regions]
2838       // A critical region may not be nested (closely or otherwise) inside a
2839       // critical region with the same name. Note that this restriction is not
2840       // sufficient to prevent deadlock.
2841       SourceLocation PreviousCriticalLoc;
2842       bool DeadLock =
2843           Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2844                                   OpenMPDirectiveKind K,
2845                                   const DeclarationNameInfo &DNI,
2846                                   SourceLocation Loc)
2847                                   ->bool {
2848                                 if (K == OMPD_critical &&
2849                                     DNI.getName() == CurrentName.getName()) {
2850                                   PreviousCriticalLoc = Loc;
2851                                   return true;
2852                                 } else
2853                                   return false;
2854                               },
2855                               false /* skip top directive */);
2856       if (DeadLock) {
2857         SemaRef.Diag(StartLoc,
2858                      diag::err_omp_prohibited_region_critical_same_name)
2859             << CurrentName.getName();
2860         if (PreviousCriticalLoc.isValid())
2861           SemaRef.Diag(PreviousCriticalLoc,
2862                        diag::note_omp_previous_critical_region);
2863         return true;
2864       }
2865     } else if (CurrentRegion == OMPD_barrier) {
2866       // OpenMP [2.16, Nesting of Regions]
2867       // A barrier region may not be closely nested inside a worksharing,
2868       // explicit task, critical, ordered, atomic, or master region.
2869       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2870                           isOpenMPTaskingDirective(ParentRegion) ||
2871                           ParentRegion == OMPD_master ||
2872                           ParentRegion == OMPD_critical ||
2873                           ParentRegion == OMPD_ordered;
2874     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
2875                !isOpenMPParallelDirective(CurrentRegion)) {
2876       // OpenMP [2.16, Nesting of Regions]
2877       // A worksharing region may not be closely nested inside a worksharing,
2878       // explicit task, critical, ordered, atomic, or master region.
2879       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2880                           isOpenMPTaskingDirective(ParentRegion) ||
2881                           ParentRegion == OMPD_master ||
2882                           ParentRegion == OMPD_critical ||
2883                           ParentRegion == OMPD_ordered;
2884       Recommend = ShouldBeInParallelRegion;
2885     } else if (CurrentRegion == OMPD_ordered) {
2886       // OpenMP [2.16, Nesting of Regions]
2887       // An ordered region may not be closely nested inside a critical,
2888       // atomic, or explicit task region.
2889       // An ordered region must be closely nested inside a loop region (or
2890       // parallel loop region) with an ordered clause.
2891       // OpenMP [2.8.1,simd Construct, Restrictions]
2892       // An ordered construct with the simd clause is the only OpenMP construct
2893       // that can appear in the simd region.
2894       NestingProhibited = ParentRegion == OMPD_critical ||
2895                           isOpenMPTaskingDirective(ParentRegion) ||
2896                           !(isOpenMPSimdDirective(ParentRegion) ||
2897                             Stack->isParentOrderedRegion());
2898       Recommend = ShouldBeInOrderedRegion;
2899     } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2900       // OpenMP [2.16, Nesting of Regions]
2901       // If specified, a teams construct must be contained within a target
2902       // construct.
2903       NestingProhibited = ParentRegion != OMPD_target;
2904       Recommend = ShouldBeInTargetRegion;
2905       Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2906     }
2907     if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2908       // OpenMP [2.16, Nesting of Regions]
2909       // distribute, parallel, parallel sections, parallel workshare, and the
2910       // parallel loop and parallel loop SIMD constructs are the only OpenMP
2911       // constructs that can be closely nested in the teams region.
2912       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2913                           !isOpenMPDistributeDirective(CurrentRegion);
2914       Recommend = ShouldBeInParallelRegion;
2915     }
2916     if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2917       // OpenMP 4.5 [2.17 Nesting of Regions]
2918       // The region associated with the distribute construct must be strictly
2919       // nested inside a teams region
2920       NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2921       Recommend = ShouldBeInTeamsRegion;
2922     }
2923     if (!NestingProhibited &&
2924         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2925          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2926       // OpenMP 4.5 [2.17 Nesting of Regions]
2927       // If a target, target update, target data, target enter data, or
2928       // target exit data construct is encountered during execution of a
2929       // target region, the behavior is unspecified.
2930       NestingProhibited = Stack->hasDirective(
2931           [&OffendingRegion](OpenMPDirectiveKind K,
2932                              const DeclarationNameInfo &DNI,
2933                              SourceLocation Loc) -> bool {
2934             if (isOpenMPTargetExecutionDirective(K)) {
2935               OffendingRegion = K;
2936               return true;
2937             } else
2938               return false;
2939           },
2940           false /* don't skip top directive */);
2941       CloseNesting = false;
2942     }
2943     if (NestingProhibited) {
2944       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2945           << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2946           << Recommend << getOpenMPDirectiveName(CurrentRegion);
2947       return true;
2948     }
2949   }
2950   return false;
2951 }
2952 
2953 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2954                            ArrayRef<OMPClause *> Clauses,
2955                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2956   bool ErrorFound = false;
2957   unsigned NamedModifiersNumber = 0;
2958   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2959       OMPD_unknown + 1);
2960   SmallVector<SourceLocation, 4> NameModifierLoc;
2961   for (const auto *C : Clauses) {
2962     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2963       // At most one if clause without a directive-name-modifier can appear on
2964       // the directive.
2965       OpenMPDirectiveKind CurNM = IC->getNameModifier();
2966       if (FoundNameModifiers[CurNM]) {
2967         S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2968             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2969             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2970         ErrorFound = true;
2971       } else if (CurNM != OMPD_unknown) {
2972         NameModifierLoc.push_back(IC->getNameModifierLoc());
2973         ++NamedModifiersNumber;
2974       }
2975       FoundNameModifiers[CurNM] = IC;
2976       if (CurNM == OMPD_unknown)
2977         continue;
2978       // Check if the specified name modifier is allowed for the current
2979       // directive.
2980       // At most one if clause with the particular directive-name-modifier can
2981       // appear on the directive.
2982       bool MatchFound = false;
2983       for (auto NM : AllowedNameModifiers) {
2984         if (CurNM == NM) {
2985           MatchFound = true;
2986           break;
2987         }
2988       }
2989       if (!MatchFound) {
2990         S.Diag(IC->getNameModifierLoc(),
2991                diag::err_omp_wrong_if_directive_name_modifier)
2992             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2993         ErrorFound = true;
2994       }
2995     }
2996   }
2997   // If any if clause on the directive includes a directive-name-modifier then
2998   // all if clauses on the directive must include a directive-name-modifier.
2999   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3000     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3001       S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3002              diag::err_omp_no_more_if_clause);
3003     } else {
3004       std::string Values;
3005       std::string Sep(", ");
3006       unsigned AllowedCnt = 0;
3007       unsigned TotalAllowedNum =
3008           AllowedNameModifiers.size() - NamedModifiersNumber;
3009       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3010            ++Cnt) {
3011         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3012         if (!FoundNameModifiers[NM]) {
3013           Values += "'";
3014           Values += getOpenMPDirectiveName(NM);
3015           Values += "'";
3016           if (AllowedCnt + 2 == TotalAllowedNum)
3017             Values += " or ";
3018           else if (AllowedCnt + 1 != TotalAllowedNum)
3019             Values += Sep;
3020           ++AllowedCnt;
3021         }
3022       }
3023       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3024              diag::err_omp_unnamed_if_clause)
3025           << (TotalAllowedNum > 1) << Values;
3026     }
3027     for (auto Loc : NameModifierLoc) {
3028       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3029     }
3030     ErrorFound = true;
3031   }
3032   return ErrorFound;
3033 }
3034 
3035 StmtResult Sema::ActOnOpenMPExecutableDirective(
3036     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3037     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3038     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3039   StmtResult Res = StmtError();
3040   if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3041                             StartLoc))
3042     return StmtError();
3043 
3044   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3045   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
3046   bool ErrorFound = false;
3047   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3048   if (AStmt) {
3049     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3050 
3051     // Check default data sharing attributes for referenced variables.
3052     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3053     DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3054     if (DSAChecker.isErrorFound())
3055       return StmtError();
3056     // Generate list of implicitly defined firstprivate variables.
3057     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3058 
3059     if (!DSAChecker.getImplicitFirstprivate().empty()) {
3060       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3061               DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3062               SourceLocation(), SourceLocation())) {
3063         ClausesWithImplicit.push_back(Implicit);
3064         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3065                      DSAChecker.getImplicitFirstprivate().size();
3066       } else
3067         ErrorFound = true;
3068     }
3069   }
3070 
3071   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3072   switch (Kind) {
3073   case OMPD_parallel:
3074     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3075                                        EndLoc);
3076     AllowedNameModifiers.push_back(OMPD_parallel);
3077     break;
3078   case OMPD_simd:
3079     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3080                                    VarsWithInheritedDSA);
3081     break;
3082   case OMPD_for:
3083     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3084                                   VarsWithInheritedDSA);
3085     break;
3086   case OMPD_for_simd:
3087     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3088                                       EndLoc, VarsWithInheritedDSA);
3089     break;
3090   case OMPD_sections:
3091     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3092                                        EndLoc);
3093     break;
3094   case OMPD_section:
3095     assert(ClausesWithImplicit.empty() &&
3096            "No clauses are allowed for 'omp section' directive");
3097     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3098     break;
3099   case OMPD_single:
3100     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3101                                      EndLoc);
3102     break;
3103   case OMPD_master:
3104     assert(ClausesWithImplicit.empty() &&
3105            "No clauses are allowed for 'omp master' directive");
3106     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3107     break;
3108   case OMPD_critical:
3109     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3110                                        StartLoc, EndLoc);
3111     break;
3112   case OMPD_parallel_for:
3113     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3114                                           EndLoc, VarsWithInheritedDSA);
3115     AllowedNameModifiers.push_back(OMPD_parallel);
3116     break;
3117   case OMPD_parallel_for_simd:
3118     Res = ActOnOpenMPParallelForSimdDirective(
3119         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3120     AllowedNameModifiers.push_back(OMPD_parallel);
3121     break;
3122   case OMPD_parallel_sections:
3123     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3124                                                StartLoc, EndLoc);
3125     AllowedNameModifiers.push_back(OMPD_parallel);
3126     break;
3127   case OMPD_task:
3128     Res =
3129         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3130     AllowedNameModifiers.push_back(OMPD_task);
3131     break;
3132   case OMPD_taskyield:
3133     assert(ClausesWithImplicit.empty() &&
3134            "No clauses are allowed for 'omp taskyield' directive");
3135     assert(AStmt == nullptr &&
3136            "No associated statement allowed for 'omp taskyield' directive");
3137     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3138     break;
3139   case OMPD_barrier:
3140     assert(ClausesWithImplicit.empty() &&
3141            "No clauses are allowed for 'omp barrier' directive");
3142     assert(AStmt == nullptr &&
3143            "No associated statement allowed for 'omp barrier' directive");
3144     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3145     break;
3146   case OMPD_taskwait:
3147     assert(ClausesWithImplicit.empty() &&
3148            "No clauses are allowed for 'omp taskwait' directive");
3149     assert(AStmt == nullptr &&
3150            "No associated statement allowed for 'omp taskwait' directive");
3151     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3152     break;
3153   case OMPD_taskgroup:
3154     assert(ClausesWithImplicit.empty() &&
3155            "No clauses are allowed for 'omp taskgroup' directive");
3156     Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3157     break;
3158   case OMPD_flush:
3159     assert(AStmt == nullptr &&
3160            "No associated statement allowed for 'omp flush' directive");
3161     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3162     break;
3163   case OMPD_ordered:
3164     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3165                                       EndLoc);
3166     break;
3167   case OMPD_atomic:
3168     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3169                                      EndLoc);
3170     break;
3171   case OMPD_teams:
3172     Res =
3173         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3174     break;
3175   case OMPD_target:
3176     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3177                                      EndLoc);
3178     AllowedNameModifiers.push_back(OMPD_target);
3179     break;
3180   case OMPD_target_parallel:
3181     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3182                                              StartLoc, EndLoc);
3183     AllowedNameModifiers.push_back(OMPD_target);
3184     AllowedNameModifiers.push_back(OMPD_parallel);
3185     break;
3186   case OMPD_target_parallel_for:
3187     Res = ActOnOpenMPTargetParallelForDirective(
3188         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3189     AllowedNameModifiers.push_back(OMPD_target);
3190     AllowedNameModifiers.push_back(OMPD_parallel);
3191     break;
3192   case OMPD_cancellation_point:
3193     assert(ClausesWithImplicit.empty() &&
3194            "No clauses are allowed for 'omp cancellation point' directive");
3195     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3196                                "cancellation point' directive");
3197     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3198     break;
3199   case OMPD_cancel:
3200     assert(AStmt == nullptr &&
3201            "No associated statement allowed for 'omp cancel' directive");
3202     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3203                                      CancelRegion);
3204     AllowedNameModifiers.push_back(OMPD_cancel);
3205     break;
3206   case OMPD_target_data:
3207     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3208                                          EndLoc);
3209     AllowedNameModifiers.push_back(OMPD_target_data);
3210     break;
3211   case OMPD_target_enter_data:
3212     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3213                                               EndLoc);
3214     AllowedNameModifiers.push_back(OMPD_target_enter_data);
3215     break;
3216   case OMPD_target_exit_data:
3217     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3218                                              EndLoc);
3219     AllowedNameModifiers.push_back(OMPD_target_exit_data);
3220     break;
3221   case OMPD_taskloop:
3222     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3223                                        EndLoc, VarsWithInheritedDSA);
3224     AllowedNameModifiers.push_back(OMPD_taskloop);
3225     break;
3226   case OMPD_taskloop_simd:
3227     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3228                                            EndLoc, VarsWithInheritedDSA);
3229     AllowedNameModifiers.push_back(OMPD_taskloop);
3230     break;
3231   case OMPD_distribute:
3232     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3233                                          EndLoc, VarsWithInheritedDSA);
3234     break;
3235   case OMPD_declare_target:
3236   case OMPD_end_declare_target:
3237   case OMPD_threadprivate:
3238   case OMPD_declare_reduction:
3239   case OMPD_declare_simd:
3240     llvm_unreachable("OpenMP Directive is not allowed");
3241   case OMPD_unknown:
3242     llvm_unreachable("Unknown OpenMP directive");
3243   }
3244 
3245   for (auto P : VarsWithInheritedDSA) {
3246     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3247         << P.first << P.second->getSourceRange();
3248   }
3249   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3250 
3251   if (!AllowedNameModifiers.empty())
3252     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3253                  ErrorFound;
3254 
3255   if (ErrorFound)
3256     return StmtError();
3257   return Res;
3258 }
3259 
3260 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3261     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3262     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3263     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3264     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3265   assert(Aligneds.size() == Alignments.size());
3266   assert(Linears.size() == LinModifiers.size());
3267   assert(Linears.size() == Steps.size());
3268   if (!DG || DG.get().isNull())
3269     return DeclGroupPtrTy();
3270 
3271   if (!DG.get().isSingleDecl()) {
3272     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3273     return DG;
3274   }
3275   auto *ADecl = DG.get().getSingleDecl();
3276   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3277     ADecl = FTD->getTemplatedDecl();
3278 
3279   auto *FD = dyn_cast<FunctionDecl>(ADecl);
3280   if (!FD) {
3281     Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3282     return DeclGroupPtrTy();
3283   }
3284 
3285   // OpenMP [2.8.2, declare simd construct, Description]
3286   // The parameter of the simdlen clause must be a constant positive integer
3287   // expression.
3288   ExprResult SL;
3289   if (Simdlen)
3290     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3291   // OpenMP [2.8.2, declare simd construct, Description]
3292   // The special this pointer can be used as if was one of the arguments to the
3293   // function in any of the linear, aligned, or uniform clauses.
3294   // The uniform clause declares one or more arguments to have an invariant
3295   // value for all concurrent invocations of the function in the execution of a
3296   // single SIMD loop.
3297   llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3298   Expr *UniformedLinearThis = nullptr;
3299   for (auto *E : Uniforms) {
3300     E = E->IgnoreParenImpCasts();
3301     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3302       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3303         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3304             FD->getParamDecl(PVD->getFunctionScopeIndex())
3305                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3306           UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
3307           continue;
3308         }
3309     if (isa<CXXThisExpr>(E)) {
3310       UniformedLinearThis = E;
3311       continue;
3312     }
3313     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3314         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3315   }
3316   // OpenMP [2.8.2, declare simd construct, Description]
3317   // The aligned clause declares that the object to which each list item points
3318   // is aligned to the number of bytes expressed in the optional parameter of
3319   // the aligned clause.
3320   // The special this pointer can be used as if was one of the arguments to the
3321   // function in any of the linear, aligned, or uniform clauses.
3322   // The type of list items appearing in the aligned clause must be array,
3323   // pointer, reference to array, or reference to pointer.
3324   llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3325   Expr *AlignedThis = nullptr;
3326   for (auto *E : Aligneds) {
3327     E = E->IgnoreParenImpCasts();
3328     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3329       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3330         auto *CanonPVD = PVD->getCanonicalDecl();
3331         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3332             FD->getParamDecl(PVD->getFunctionScopeIndex())
3333                     ->getCanonicalDecl() == CanonPVD) {
3334           // OpenMP  [2.8.1, simd construct, Restrictions]
3335           // A list-item cannot appear in more than one aligned clause.
3336           if (AlignedArgs.count(CanonPVD) > 0) {
3337             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3338                 << 1 << E->getSourceRange();
3339             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3340                  diag::note_omp_explicit_dsa)
3341                 << getOpenMPClauseName(OMPC_aligned);
3342             continue;
3343           }
3344           AlignedArgs[CanonPVD] = E;
3345           QualType QTy = PVD->getType()
3346                              .getNonReferenceType()
3347                              .getUnqualifiedType()
3348                              .getCanonicalType();
3349           const Type *Ty = QTy.getTypePtrOrNull();
3350           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3351             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3352                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3353             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3354           }
3355           continue;
3356         }
3357       }
3358     if (isa<CXXThisExpr>(E)) {
3359       if (AlignedThis) {
3360         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3361             << 2 << E->getSourceRange();
3362         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3363             << getOpenMPClauseName(OMPC_aligned);
3364       }
3365       AlignedThis = E;
3366       continue;
3367     }
3368     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3369         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3370   }
3371   // The optional parameter of the aligned clause, alignment, must be a constant
3372   // positive integer expression. If no optional parameter is specified,
3373   // implementation-defined default alignments for SIMD instructions on the
3374   // target platforms are assumed.
3375   SmallVector<Expr *, 4> NewAligns;
3376   for (auto *E : Alignments) {
3377     ExprResult Align;
3378     if (E)
3379       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3380     NewAligns.push_back(Align.get());
3381   }
3382   // OpenMP [2.8.2, declare simd construct, Description]
3383   // The linear clause declares one or more list items to be private to a SIMD
3384   // lane and to have a linear relationship with respect to the iteration space
3385   // of a loop.
3386   // The special this pointer can be used as if was one of the arguments to the
3387   // function in any of the linear, aligned, or uniform clauses.
3388   // When a linear-step expression is specified in a linear clause it must be
3389   // either a constant integer expression or an integer-typed parameter that is
3390   // specified in a uniform clause on the directive.
3391   llvm::DenseMap<Decl *, Expr *> LinearArgs;
3392   const bool IsUniformedThis = UniformedLinearThis != nullptr;
3393   auto MI = LinModifiers.begin();
3394   for (auto *E : Linears) {
3395     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3396     ++MI;
3397     E = E->IgnoreParenImpCasts();
3398     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3399       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3400         auto *CanonPVD = PVD->getCanonicalDecl();
3401         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3402             FD->getParamDecl(PVD->getFunctionScopeIndex())
3403                     ->getCanonicalDecl() == CanonPVD) {
3404           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
3405           // A list-item cannot appear in more than one linear clause.
3406           if (LinearArgs.count(CanonPVD) > 0) {
3407             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3408                 << getOpenMPClauseName(OMPC_linear)
3409                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3410             Diag(LinearArgs[CanonPVD]->getExprLoc(),
3411                  diag::note_omp_explicit_dsa)
3412                 << getOpenMPClauseName(OMPC_linear);
3413             continue;
3414           }
3415           // Each argument can appear in at most one uniform or linear clause.
3416           if (UniformedArgs.count(CanonPVD) > 0) {
3417             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3418                 << getOpenMPClauseName(OMPC_linear)
3419                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3420             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3421                  diag::note_omp_explicit_dsa)
3422                 << getOpenMPClauseName(OMPC_uniform);
3423             continue;
3424           }
3425           LinearArgs[CanonPVD] = E;
3426           if (E->isValueDependent() || E->isTypeDependent() ||
3427               E->isInstantiationDependent() ||
3428               E->containsUnexpandedParameterPack())
3429             continue;
3430           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3431                                       PVD->getOriginalType());
3432           continue;
3433         }
3434       }
3435     if (isa<CXXThisExpr>(E)) {
3436       if (UniformedLinearThis) {
3437         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3438             << getOpenMPClauseName(OMPC_linear)
3439             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3440             << E->getSourceRange();
3441         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3442             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3443                                                    : OMPC_linear);
3444         continue;
3445       }
3446       UniformedLinearThis = E;
3447       if (E->isValueDependent() || E->isTypeDependent() ||
3448           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3449         continue;
3450       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3451                                   E->getType());
3452       continue;
3453     }
3454     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3455         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3456   }
3457   Expr *Step = nullptr;
3458   Expr *NewStep = nullptr;
3459   SmallVector<Expr *, 4> NewSteps;
3460   for (auto *E : Steps) {
3461     // Skip the same step expression, it was checked already.
3462     if (Step == E || !E) {
3463       NewSteps.push_back(E ? NewStep : nullptr);
3464       continue;
3465     }
3466     Step = E;
3467     if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3468       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3469         auto *CanonPVD = PVD->getCanonicalDecl();
3470         if (UniformedArgs.count(CanonPVD) == 0) {
3471           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3472               << Step->getSourceRange();
3473         } else if (E->isValueDependent() || E->isTypeDependent() ||
3474                    E->isInstantiationDependent() ||
3475                    E->containsUnexpandedParameterPack() ||
3476                    CanonPVD->getType()->hasIntegerRepresentation())
3477           NewSteps.push_back(Step);
3478         else {
3479           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3480               << Step->getSourceRange();
3481         }
3482         continue;
3483       }
3484     NewStep = Step;
3485     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3486         !Step->isInstantiationDependent() &&
3487         !Step->containsUnexpandedParameterPack()) {
3488       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3489                     .get();
3490       if (NewStep)
3491         NewStep = VerifyIntegerConstantExpression(NewStep).get();
3492     }
3493     NewSteps.push_back(NewStep);
3494   }
3495   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3496       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
3497       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3498       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3499       const_cast<Expr **>(Linears.data()), Linears.size(),
3500       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3501       NewSteps.data(), NewSteps.size(), SR);
3502   ADecl->addAttr(NewAttr);
3503   return ConvertDeclToDeclGroup(ADecl);
3504 }
3505 
3506 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3507                                               Stmt *AStmt,
3508                                               SourceLocation StartLoc,
3509                                               SourceLocation EndLoc) {
3510   if (!AStmt)
3511     return StmtError();
3512 
3513   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3514   // 1.2.2 OpenMP Language Terminology
3515   // Structured block - An executable statement with a single entry at the
3516   // top and a single exit at the bottom.
3517   // The point of exit cannot be a branch out of the structured block.
3518   // longjmp() and throw() must not violate the entry/exit criteria.
3519   CS->getCapturedDecl()->setNothrow();
3520 
3521   getCurFunction()->setHasBranchProtectedScope();
3522 
3523   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3524                                       DSAStack->isCancelRegion());
3525 }
3526 
3527 namespace {
3528 /// \brief Helper class for checking canonical form of the OpenMP loops and
3529 /// extracting iteration space of each loop in the loop nest, that will be used
3530 /// for IR generation.
3531 class OpenMPIterationSpaceChecker {
3532   /// \brief Reference to Sema.
3533   Sema &SemaRef;
3534   /// \brief A location for diagnostics (when there is no some better location).
3535   SourceLocation DefaultLoc;
3536   /// \brief A location for diagnostics (when increment is not compatible).
3537   SourceLocation ConditionLoc;
3538   /// \brief A source location for referring to loop init later.
3539   SourceRange InitSrcRange;
3540   /// \brief A source location for referring to condition later.
3541   SourceRange ConditionSrcRange;
3542   /// \brief A source location for referring to increment later.
3543   SourceRange IncrementSrcRange;
3544   /// \brief Loop variable.
3545   ValueDecl *LCDecl = nullptr;
3546   /// \brief Reference to loop variable.
3547   Expr *LCRef = nullptr;
3548   /// \brief Lower bound (initializer for the var).
3549   Expr *LB = nullptr;
3550   /// \brief Upper bound.
3551   Expr *UB = nullptr;
3552   /// \brief Loop step (increment).
3553   Expr *Step = nullptr;
3554   /// \brief This flag is true when condition is one of:
3555   ///   Var <  UB
3556   ///   Var <= UB
3557   ///   UB  >  Var
3558   ///   UB  >= Var
3559   bool TestIsLessOp = false;
3560   /// \brief This flag is true when condition is strict ( < or > ).
3561   bool TestIsStrictOp = false;
3562   /// \brief This flag is true when step is subtracted on each iteration.
3563   bool SubtractStep = false;
3564 
3565 public:
3566   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3567       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
3568   /// \brief Check init-expr for canonical loop form and save loop counter
3569   /// variable - #Var and its initialization value - #LB.
3570   bool CheckInit(Stmt *S, bool EmitDiags = true);
3571   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3572   /// for less/greater and for strict/non-strict comparison.
3573   bool CheckCond(Expr *S);
3574   /// \brief Check incr-expr for canonical loop form and return true if it
3575   /// does not conform, otherwise save loop step (#Step).
3576   bool CheckInc(Expr *S);
3577   /// \brief Return the loop counter variable.
3578   ValueDecl *GetLoopDecl() const { return LCDecl; }
3579   /// \brief Return the reference expression to loop counter variable.
3580   Expr *GetLoopDeclRefExpr() const { return LCRef; }
3581   /// \brief Source range of the loop init.
3582   SourceRange GetInitSrcRange() const { return InitSrcRange; }
3583   /// \brief Source range of the loop condition.
3584   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3585   /// \brief Source range of the loop increment.
3586   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3587   /// \brief True if the step should be subtracted.
3588   bool ShouldSubtractStep() const { return SubtractStep; }
3589   /// \brief Build the expression to calculate the number of iterations.
3590   Expr *
3591   BuildNumIterations(Scope *S, const bool LimitedType,
3592                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3593   /// \brief Build the precondition expression for the loops.
3594   Expr *BuildPreCond(Scope *S, Expr *Cond,
3595                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3596   /// \brief Build reference expression to the counter be used for codegen.
3597   DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3598                                DSAStackTy &DSA) const;
3599   /// \brief Build reference expression to the private counter be used for
3600   /// codegen.
3601   Expr *BuildPrivateCounterVar() const;
3602   /// \brief Build initization of the counter be used for codegen.
3603   Expr *BuildCounterInit() const;
3604   /// \brief Build step of the counter be used for codegen.
3605   Expr *BuildCounterStep() const;
3606   /// \brief Return true if any expression is dependent.
3607   bool Dependent() const;
3608 
3609 private:
3610   /// \brief Check the right-hand side of an assignment in the increment
3611   /// expression.
3612   bool CheckIncRHS(Expr *RHS);
3613   /// \brief Helper to set loop counter variable and its initializer.
3614   bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3615   /// \brief Helper to set upper bound.
3616   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
3617              SourceLocation SL);
3618   /// \brief Helper to set loop increment.
3619   bool SetStep(Expr *NewStep, bool Subtract);
3620 };
3621 
3622 bool OpenMPIterationSpaceChecker::Dependent() const {
3623   if (!LCDecl) {
3624     assert(!LB && !UB && !Step);
3625     return false;
3626   }
3627   return LCDecl->getType()->isDependentType() ||
3628          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3629          (Step && Step->isValueDependent());
3630 }
3631 
3632 static Expr *getExprAsWritten(Expr *E) {
3633   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3634     E = ExprTemp->getSubExpr();
3635 
3636   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3637     E = MTE->GetTemporaryExpr();
3638 
3639   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3640     E = Binder->getSubExpr();
3641 
3642   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3643     E = ICE->getSubExprAsWritten();
3644   return E->IgnoreParens();
3645 }
3646 
3647 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3648                                                  Expr *NewLCRefExpr,
3649                                                  Expr *NewLB) {
3650   // State consistency checking to ensure correct usage.
3651   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
3652          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3653   if (!NewLCDecl || !NewLB)
3654     return true;
3655   LCDecl = getCanonicalDecl(NewLCDecl);
3656   LCRef = NewLCRefExpr;
3657   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3658     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3659       if ((Ctor->isCopyOrMoveConstructor() ||
3660            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3661           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3662         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
3663   LB = NewLB;
3664   return false;
3665 }
3666 
3667 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
3668                                         SourceRange SR, SourceLocation SL) {
3669   // State consistency checking to ensure correct usage.
3670   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3671          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3672   if (!NewUB)
3673     return true;
3674   UB = NewUB;
3675   TestIsLessOp = LessOp;
3676   TestIsStrictOp = StrictOp;
3677   ConditionSrcRange = SR;
3678   ConditionLoc = SL;
3679   return false;
3680 }
3681 
3682 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3683   // State consistency checking to ensure correct usage.
3684   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
3685   if (!NewStep)
3686     return true;
3687   if (!NewStep->isValueDependent()) {
3688     // Check that the step is integer expression.
3689     SourceLocation StepLoc = NewStep->getLocStart();
3690     ExprResult Val =
3691         SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3692     if (Val.isInvalid())
3693       return true;
3694     NewStep = Val.get();
3695 
3696     // OpenMP [2.6, Canonical Loop Form, Restrictions]
3697     //  If test-expr is of form var relational-op b and relational-op is < or
3698     //  <= then incr-expr must cause var to increase on each iteration of the
3699     //  loop. If test-expr is of form var relational-op b and relational-op is
3700     //  > or >= then incr-expr must cause var to decrease on each iteration of
3701     //  the loop.
3702     //  If test-expr is of form b relational-op var and relational-op is < or
3703     //  <= then incr-expr must cause var to decrease on each iteration of the
3704     //  loop. If test-expr is of form b relational-op var and relational-op is
3705     //  > or >= then incr-expr must cause var to increase on each iteration of
3706     //  the loop.
3707     llvm::APSInt Result;
3708     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3709     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3710     bool IsConstNeg =
3711         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
3712     bool IsConstPos =
3713         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
3714     bool IsConstZero = IsConstant && !Result.getBoolValue();
3715     if (UB && (IsConstZero ||
3716                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
3717                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
3718       SemaRef.Diag(NewStep->getExprLoc(),
3719                    diag::err_omp_loop_incr_not_compatible)
3720           << LCDecl << TestIsLessOp << NewStep->getSourceRange();
3721       SemaRef.Diag(ConditionLoc,
3722                    diag::note_omp_loop_cond_requres_compatible_incr)
3723           << TestIsLessOp << ConditionSrcRange;
3724       return true;
3725     }
3726     if (TestIsLessOp == Subtract) {
3727       NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3728                                              NewStep).get();
3729       Subtract = !Subtract;
3730     }
3731   }
3732 
3733   Step = NewStep;
3734   SubtractStep = Subtract;
3735   return false;
3736 }
3737 
3738 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
3739   // Check init-expr for canonical loop form and save loop counter
3740   // variable - #Var and its initialization value - #LB.
3741   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3742   //   var = lb
3743   //   integer-type var = lb
3744   //   random-access-iterator-type var = lb
3745   //   pointer-type var = lb
3746   //
3747   if (!S) {
3748     if (EmitDiags) {
3749       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3750     }
3751     return true;
3752   }
3753   InitSrcRange = S->getSourceRange();
3754   if (Expr *E = dyn_cast<Expr>(S))
3755     S = E->IgnoreParens();
3756   if (auto BO = dyn_cast<BinaryOperator>(S)) {
3757     if (BO->getOpcode() == BO_Assign) {
3758       auto *LHS = BO->getLHS()->IgnoreParens();
3759       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3760         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3761           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3762             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3763         return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3764       }
3765       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3766         if (ME->isArrow() &&
3767             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3768           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3769       }
3770     }
3771   } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3772     if (DS->isSingleDecl()) {
3773       if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
3774         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
3775           // Accept non-canonical init form here but emit ext. warning.
3776           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
3777             SemaRef.Diag(S->getLocStart(),
3778                          diag::ext_omp_loop_not_canonical_init)
3779                 << S->getSourceRange();
3780           return SetLCDeclAndLB(Var, nullptr, Var->getInit());
3781         }
3782       }
3783     }
3784   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3785     if (CE->getOperator() == OO_Equal) {
3786       auto *LHS = CE->getArg(0);
3787       if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3788         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3789           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3790             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3791         return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3792       }
3793       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3794         if (ME->isArrow() &&
3795             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3796           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3797       }
3798     }
3799   }
3800 
3801   if (Dependent() || SemaRef.CurContext->isDependentContext())
3802     return false;
3803   if (EmitDiags) {
3804     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3805         << S->getSourceRange();
3806   }
3807   return true;
3808 }
3809 
3810 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
3811 /// variable (which may be the loop variable) if possible.
3812 static const ValueDecl *GetInitLCDecl(Expr *E) {
3813   if (!E)
3814     return nullptr;
3815   E = getExprAsWritten(E);
3816   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3817     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3818       if ((Ctor->isCopyOrMoveConstructor() ||
3819            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3820           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3821         E = CE->getArg(0)->IgnoreParenImpCasts();
3822   if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3823     if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3824       if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3825         if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3826           return getCanonicalDecl(ME->getMemberDecl());
3827       return getCanonicalDecl(VD);
3828     }
3829   }
3830   if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3831     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3832       return getCanonicalDecl(ME->getMemberDecl());
3833   return nullptr;
3834 }
3835 
3836 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3837   // Check test-expr for canonical form, save upper-bound UB, flags for
3838   // less/greater and for strict/non-strict comparison.
3839   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3840   //   var relational-op b
3841   //   b relational-op var
3842   //
3843   if (!S) {
3844     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
3845     return true;
3846   }
3847   S = getExprAsWritten(S);
3848   SourceLocation CondLoc = S->getLocStart();
3849   if (auto BO = dyn_cast<BinaryOperator>(S)) {
3850     if (BO->isRelationalOp()) {
3851       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3852         return SetUB(BO->getRHS(),
3853                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3854                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3855                      BO->getSourceRange(), BO->getOperatorLoc());
3856       if (GetInitLCDecl(BO->getRHS()) == LCDecl)
3857         return SetUB(BO->getLHS(),
3858                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3859                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3860                      BO->getSourceRange(), BO->getOperatorLoc());
3861     }
3862   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3863     if (CE->getNumArgs() == 2) {
3864       auto Op = CE->getOperator();
3865       switch (Op) {
3866       case OO_Greater:
3867       case OO_GreaterEqual:
3868       case OO_Less:
3869       case OO_LessEqual:
3870         if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3871           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3872                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3873                        CE->getOperatorLoc());
3874         if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
3875           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3876                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3877                        CE->getOperatorLoc());
3878         break;
3879       default:
3880         break;
3881       }
3882     }
3883   }
3884   if (Dependent() || SemaRef.CurContext->isDependentContext())
3885     return false;
3886   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3887       << S->getSourceRange() << LCDecl;
3888   return true;
3889 }
3890 
3891 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3892   // RHS of canonical loop form increment can be:
3893   //   var + incr
3894   //   incr + var
3895   //   var - incr
3896   //
3897   RHS = RHS->IgnoreParenImpCasts();
3898   if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3899     if (BO->isAdditiveOp()) {
3900       bool IsAdd = BO->getOpcode() == BO_Add;
3901       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3902         return SetStep(BO->getRHS(), !IsAdd);
3903       if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
3904         return SetStep(BO->getLHS(), false);
3905     }
3906   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3907     bool IsAdd = CE->getOperator() == OO_Plus;
3908     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3909       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3910         return SetStep(CE->getArg(1), !IsAdd);
3911       if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
3912         return SetStep(CE->getArg(0), false);
3913     }
3914   }
3915   if (Dependent() || SemaRef.CurContext->isDependentContext())
3916     return false;
3917   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3918       << RHS->getSourceRange() << LCDecl;
3919   return true;
3920 }
3921 
3922 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3923   // Check incr-expr for canonical loop form and return true if it
3924   // does not conform.
3925   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3926   //   ++var
3927   //   var++
3928   //   --var
3929   //   var--
3930   //   var += incr
3931   //   var -= incr
3932   //   var = var + incr
3933   //   var = incr + var
3934   //   var = var - incr
3935   //
3936   if (!S) {
3937     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
3938     return true;
3939   }
3940   IncrementSrcRange = S->getSourceRange();
3941   S = S->IgnoreParens();
3942   if (auto UO = dyn_cast<UnaryOperator>(S)) {
3943     if (UO->isIncrementDecrementOp() &&
3944         GetInitLCDecl(UO->getSubExpr()) == LCDecl)
3945       return SetStep(
3946           SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3947                                        (UO->isDecrementOp() ? -1 : 1)).get(),
3948           false);
3949   } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3950     switch (BO->getOpcode()) {
3951     case BO_AddAssign:
3952     case BO_SubAssign:
3953       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3954         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3955       break;
3956     case BO_Assign:
3957       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3958         return CheckIncRHS(BO->getRHS());
3959       break;
3960     default:
3961       break;
3962     }
3963   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3964     switch (CE->getOperator()) {
3965     case OO_PlusPlus:
3966     case OO_MinusMinus:
3967       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3968         return SetStep(
3969             SemaRef.ActOnIntegerConstant(
3970                         CE->getLocStart(),
3971                         ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3972             false);
3973       break;
3974     case OO_PlusEqual:
3975     case OO_MinusEqual:
3976       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3977         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3978       break;
3979     case OO_Equal:
3980       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3981         return CheckIncRHS(CE->getArg(1));
3982       break;
3983     default:
3984       break;
3985     }
3986   }
3987   if (Dependent() || SemaRef.CurContext->isDependentContext())
3988     return false;
3989   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3990       << S->getSourceRange() << LCDecl;
3991   return true;
3992 }
3993 
3994 static ExprResult
3995 tryBuildCapture(Sema &SemaRef, Expr *Capture,
3996                 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3997   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3998     return SemaRef.PerformImplicitConversion(
3999         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4000         /*AllowExplicit=*/true);
4001   auto I = Captures.find(Capture);
4002   if (I != Captures.end())
4003     return buildCapture(SemaRef, Capture, I->second);
4004   DeclRefExpr *Ref = nullptr;
4005   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4006   Captures[Capture] = Ref;
4007   return Res;
4008 }
4009 
4010 /// \brief Build the expression to calculate the number of iterations.
4011 Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4012     Scope *S, const bool LimitedType,
4013     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4014   ExprResult Diff;
4015   auto VarType = LCDecl->getType().getNonReferenceType();
4016   if (VarType->isIntegerType() || VarType->isPointerType() ||
4017       SemaRef.getLangOpts().CPlusPlus) {
4018     // Upper - Lower
4019     auto *UBExpr = TestIsLessOp ? UB : LB;
4020     auto *LBExpr = TestIsLessOp ? LB : UB;
4021     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4022     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
4023     if (!Upper || !Lower)
4024       return nullptr;
4025 
4026     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4027 
4028     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4029       // BuildBinOp already emitted error, this one is to point user to upper
4030       // and lower bound, and to tell what is passed to 'operator-'.
4031       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4032           << Upper->getSourceRange() << Lower->getSourceRange();
4033       return nullptr;
4034     }
4035   }
4036 
4037   if (!Diff.isUsable())
4038     return nullptr;
4039 
4040   // Upper - Lower [- 1]
4041   if (TestIsStrictOp)
4042     Diff = SemaRef.BuildBinOp(
4043         S, DefaultLoc, BO_Sub, Diff.get(),
4044         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4045   if (!Diff.isUsable())
4046     return nullptr;
4047 
4048   // Upper - Lower [- 1] + Step
4049   auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4050   if (!NewStep.isUsable())
4051     return nullptr;
4052   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
4053   if (!Diff.isUsable())
4054     return nullptr;
4055 
4056   // Parentheses (for dumping/debugging purposes only).
4057   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4058   if (!Diff.isUsable())
4059     return nullptr;
4060 
4061   // (Upper - Lower [- 1] + Step) / Step
4062   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4063   if (!Diff.isUsable())
4064     return nullptr;
4065 
4066   // OpenMP runtime requires 32-bit or 64-bit loop variables.
4067   QualType Type = Diff.get()->getType();
4068   auto &C = SemaRef.Context;
4069   bool UseVarType = VarType->hasIntegerRepresentation() &&
4070                     C.getTypeSize(Type) > C.getTypeSize(VarType);
4071   if (!Type->isIntegerType() || UseVarType) {
4072     unsigned NewSize =
4073         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4074     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4075                                : Type->hasSignedIntegerRepresentation();
4076     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
4077     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4078       Diff = SemaRef.PerformImplicitConversion(
4079           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4080       if (!Diff.isUsable())
4081         return nullptr;
4082     }
4083   }
4084   if (LimitedType) {
4085     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4086     if (NewSize != C.getTypeSize(Type)) {
4087       if (NewSize < C.getTypeSize(Type)) {
4088         assert(NewSize == 64 && "incorrect loop var size");
4089         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4090             << InitSrcRange << ConditionSrcRange;
4091       }
4092       QualType NewType = C.getIntTypeForBitwidth(
4093           NewSize, Type->hasSignedIntegerRepresentation() ||
4094                        C.getTypeSize(Type) < NewSize);
4095       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4096         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4097                                                  Sema::AA_Converting, true);
4098         if (!Diff.isUsable())
4099           return nullptr;
4100       }
4101     }
4102   }
4103 
4104   return Diff.get();
4105 }
4106 
4107 Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4108     Scope *S, Expr *Cond,
4109     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4110   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4111   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4112   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4113 
4114   auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4115   auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4116   if (!NewLB.isUsable() || !NewUB.isUsable())
4117     return nullptr;
4118 
4119   auto CondExpr = SemaRef.BuildBinOp(
4120       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4121                                   : (TestIsStrictOp ? BO_GT : BO_GE),
4122       NewLB.get(), NewUB.get());
4123   if (CondExpr.isUsable()) {
4124     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4125                                                 SemaRef.Context.BoolTy))
4126       CondExpr = SemaRef.PerformImplicitConversion(
4127           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4128           /*AllowExplicit=*/true);
4129   }
4130   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4131   // Otherwise use original loop conditon and evaluate it in runtime.
4132   return CondExpr.isUsable() ? CondExpr.get() : Cond;
4133 }
4134 
4135 /// \brief Build reference expression to the counter be used for codegen.
4136 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
4137     llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
4138   auto *VD = dyn_cast<VarDecl>(LCDecl);
4139   if (!VD) {
4140     VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4141     auto *Ref = buildDeclRefExpr(
4142         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4143     DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4144     // If the loop control decl is explicitly marked as private, do not mark it
4145     // as captured again.
4146     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4147       Captures.insert(std::make_pair(LCRef, Ref));
4148     return Ref;
4149   }
4150   return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4151                           DefaultLoc);
4152 }
4153 
4154 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
4155   if (LCDecl && !LCDecl->isInvalidDecl()) {
4156     auto Type = LCDecl->getType().getNonReferenceType();
4157     auto *PrivateVar =
4158         buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4159                      LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
4160     if (PrivateVar->isInvalidDecl())
4161       return nullptr;
4162     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4163   }
4164   return nullptr;
4165 }
4166 
4167 /// \brief Build initization of the counter be used for codegen.
4168 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4169 
4170 /// \brief Build step of the counter be used for codegen.
4171 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4172 
4173 /// \brief Iteration space of a single for loop.
4174 struct LoopIterationSpace {
4175   /// \brief Condition of the loop.
4176   Expr *PreCond;
4177   /// \brief This expression calculates the number of iterations in the loop.
4178   /// It is always possible to calculate it before starting the loop.
4179   Expr *NumIterations;
4180   /// \brief The loop counter variable.
4181   Expr *CounterVar;
4182   /// \brief Private loop counter variable.
4183   Expr *PrivateCounterVar;
4184   /// \brief This is initializer for the initial value of #CounterVar.
4185   Expr *CounterInit;
4186   /// \brief This is step for the #CounterVar used to generate its update:
4187   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4188   Expr *CounterStep;
4189   /// \brief Should step be subtracted?
4190   bool Subtract;
4191   /// \brief Source range of the loop init.
4192   SourceRange InitSrcRange;
4193   /// \brief Source range of the loop condition.
4194   SourceRange CondSrcRange;
4195   /// \brief Source range of the loop increment.
4196   SourceRange IncSrcRange;
4197 };
4198 
4199 } // namespace
4200 
4201 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4202   assert(getLangOpts().OpenMP && "OpenMP is not active.");
4203   assert(Init && "Expected loop in canonical form.");
4204   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4205   if (AssociatedLoops > 0 &&
4206       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4207     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4208     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4209       if (auto *D = ISC.GetLoopDecl()) {
4210         auto *VD = dyn_cast<VarDecl>(D);
4211         if (!VD) {
4212           if (auto *Private = IsOpenMPCapturedDecl(D))
4213             VD = Private;
4214           else {
4215             auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4216                                      /*WithInit=*/false);
4217             VD = cast<VarDecl>(Ref->getDecl());
4218           }
4219         }
4220         DSAStack->addLoopControlVariable(D, VD);
4221       }
4222     }
4223     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4224   }
4225 }
4226 
4227 /// \brief Called on a for stmt to check and extract its iteration space
4228 /// for further processing (such as collapsing).
4229 static bool CheckOpenMPIterationSpace(
4230     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4231     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4232     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
4233     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4234     LoopIterationSpace &ResultIterSpace,
4235     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4236   // OpenMP [2.6, Canonical Loop Form]
4237   //   for (init-expr; test-expr; incr-expr) structured-block
4238   auto For = dyn_cast_or_null<ForStmt>(S);
4239   if (!For) {
4240     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
4241         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4242         << getOpenMPDirectiveName(DKind) << NestedLoopCount
4243         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4244     if (NestedLoopCount > 1) {
4245       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4246         SemaRef.Diag(DSA.getConstructLoc(),
4247                      diag::note_omp_collapse_ordered_expr)
4248             << 2 << CollapseLoopCountExpr->getSourceRange()
4249             << OrderedLoopCountExpr->getSourceRange();
4250       else if (CollapseLoopCountExpr)
4251         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4252                      diag::note_omp_collapse_ordered_expr)
4253             << 0 << CollapseLoopCountExpr->getSourceRange();
4254       else
4255         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4256                      diag::note_omp_collapse_ordered_expr)
4257             << 1 << OrderedLoopCountExpr->getSourceRange();
4258     }
4259     return true;
4260   }
4261   assert(For->getBody());
4262 
4263   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4264 
4265   // Check init.
4266   auto Init = For->getInit();
4267   if (ISC.CheckInit(Init))
4268     return true;
4269 
4270   bool HasErrors = false;
4271 
4272   // Check loop variable's type.
4273   if (auto *LCDecl = ISC.GetLoopDecl()) {
4274     auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
4275 
4276     // OpenMP [2.6, Canonical Loop Form]
4277     // Var is one of the following:
4278     //   A variable of signed or unsigned integer type.
4279     //   For C++, a variable of a random access iterator type.
4280     //   For C, a variable of a pointer type.
4281     auto VarType = LCDecl->getType().getNonReferenceType();
4282     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4283         !VarType->isPointerType() &&
4284         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4285       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4286           << SemaRef.getLangOpts().CPlusPlus;
4287       HasErrors = true;
4288     }
4289 
4290     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4291     // a Construct
4292     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4293     // parallel for construct is (are) private.
4294     // The loop iteration variable in the associated for-loop of a simd
4295     // construct with just one associated for-loop is linear with a
4296     // constant-linear-step that is the increment of the associated for-loop.
4297     // Exclude loop var from the list of variables with implicitly defined data
4298     // sharing attributes.
4299     VarsWithImplicitDSA.erase(LCDecl);
4300 
4301     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4302     // in a Construct, C/C++].
4303     // The loop iteration variable in the associated for-loop of a simd
4304     // construct with just one associated for-loop may be listed in a linear
4305     // clause with a constant-linear-step that is the increment of the
4306     // associated for-loop.
4307     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4308     // parallel for construct may be listed in a private or lastprivate clause.
4309     DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4310     // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4311     // declared in the loop and it is predetermined as a private.
4312     auto PredeterminedCKind =
4313         isOpenMPSimdDirective(DKind)
4314             ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4315             : OMPC_private;
4316     if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4317           DVar.CKind != PredeterminedCKind) ||
4318          ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4319            isOpenMPDistributeDirective(DKind)) &&
4320           !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4321           DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4322         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4323       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4324           << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4325           << getOpenMPClauseName(PredeterminedCKind);
4326       if (DVar.RefExpr == nullptr)
4327         DVar.CKind = PredeterminedCKind;
4328       ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4329       HasErrors = true;
4330     } else if (LoopDeclRefExpr != nullptr) {
4331       // Make the loop iteration variable private (for worksharing constructs),
4332       // linear (for simd directives with the only one associated loop) or
4333       // lastprivate (for simd directives with several collapsed or ordered
4334       // loops).
4335       if (DVar.CKind == OMPC_unknown)
4336         DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4337                           /*FromParent=*/false);
4338       DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4339     }
4340 
4341     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4342 
4343     // Check test-expr.
4344     HasErrors |= ISC.CheckCond(For->getCond());
4345 
4346     // Check incr-expr.
4347     HasErrors |= ISC.CheckInc(For->getInc());
4348   }
4349 
4350   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4351     return HasErrors;
4352 
4353   // Build the loop's iteration space representation.
4354   ResultIterSpace.PreCond =
4355       ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4356   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
4357       DSA.getCurScope(),
4358       (isOpenMPWorksharingDirective(DKind) ||
4359        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4360       Captures);
4361   ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
4362   ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
4363   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4364   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4365   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4366   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4367   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4368   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4369 
4370   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4371                 ResultIterSpace.NumIterations == nullptr ||
4372                 ResultIterSpace.CounterVar == nullptr ||
4373                 ResultIterSpace.PrivateCounterVar == nullptr ||
4374                 ResultIterSpace.CounterInit == nullptr ||
4375                 ResultIterSpace.CounterStep == nullptr);
4376 
4377   return HasErrors;
4378 }
4379 
4380 /// \brief Build 'VarRef = Start.
4381 static ExprResult
4382 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4383                  ExprResult Start,
4384                  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4385   // Build 'VarRef = Start.
4386   auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4387   if (!NewStart.isUsable())
4388     return ExprError();
4389   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4390                                    VarRef.get()->getType())) {
4391     NewStart = SemaRef.PerformImplicitConversion(
4392         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4393         /*AllowExplicit=*/true);
4394     if (!NewStart.isUsable())
4395       return ExprError();
4396   }
4397 
4398   auto Init =
4399       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4400   return Init;
4401 }
4402 
4403 /// \brief Build 'VarRef = Start + Iter * Step'.
4404 static ExprResult
4405 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4406                    ExprResult VarRef, ExprResult Start, ExprResult Iter,
4407                    ExprResult Step, bool Subtract,
4408                    llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
4409   // Add parentheses (for debugging purposes only).
4410   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4411   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4412       !Step.isUsable())
4413     return ExprError();
4414 
4415   ExprResult NewStep = Step;
4416   if (Captures)
4417     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4418   if (NewStep.isInvalid())
4419     return ExprError();
4420   ExprResult Update =
4421       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4422   if (!Update.isUsable())
4423     return ExprError();
4424 
4425   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4426   // 'VarRef = Start (+|-) Iter * Step'.
4427   ExprResult NewStart = Start;
4428   if (Captures)
4429     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4430   if (NewStart.isInvalid())
4431     return ExprError();
4432 
4433   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4434   ExprResult SavedUpdate = Update;
4435   ExprResult UpdateVal;
4436   if (VarRef.get()->getType()->isOverloadableType() ||
4437       NewStart.get()->getType()->isOverloadableType() ||
4438       Update.get()->getType()->isOverloadableType()) {
4439     bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4440     SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4441     Update =
4442         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4443     if (Update.isUsable()) {
4444       UpdateVal =
4445           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4446                              VarRef.get(), SavedUpdate.get());
4447       if (UpdateVal.isUsable()) {
4448         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4449                                             UpdateVal.get());
4450       }
4451     }
4452     SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4453   }
4454 
4455   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4456   if (!Update.isUsable() || !UpdateVal.isUsable()) {
4457     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4458                                 NewStart.get(), SavedUpdate.get());
4459     if (!Update.isUsable())
4460       return ExprError();
4461 
4462     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4463                                      VarRef.get()->getType())) {
4464       Update = SemaRef.PerformImplicitConversion(
4465           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4466       if (!Update.isUsable())
4467         return ExprError();
4468     }
4469 
4470     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4471   }
4472   return Update;
4473 }
4474 
4475 /// \brief Convert integer expression \a E to make it have at least \a Bits
4476 /// bits.
4477 static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4478                                       Sema &SemaRef) {
4479   if (E == nullptr)
4480     return ExprError();
4481   auto &C = SemaRef.Context;
4482   QualType OldType = E->getType();
4483   unsigned HasBits = C.getTypeSize(OldType);
4484   if (HasBits >= Bits)
4485     return ExprResult(E);
4486   // OK to convert to signed, because new type has more bits than old.
4487   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4488   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4489                                            true);
4490 }
4491 
4492 /// \brief Check if the given expression \a E is a constant integer that fits
4493 /// into \a Bits bits.
4494 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4495   if (E == nullptr)
4496     return false;
4497   llvm::APSInt Result;
4498   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4499     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4500   return false;
4501 }
4502 
4503 /// Build preinits statement for the given declarations.
4504 static Stmt *buildPreInits(ASTContext &Context,
4505                            SmallVectorImpl<Decl *> &PreInits) {
4506   if (!PreInits.empty()) {
4507     return new (Context) DeclStmt(
4508         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4509         SourceLocation(), SourceLocation());
4510   }
4511   return nullptr;
4512 }
4513 
4514 /// Build preinits statement for the given declarations.
4515 static Stmt *buildPreInits(ASTContext &Context,
4516                            llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4517   if (!Captures.empty()) {
4518     SmallVector<Decl *, 16> PreInits;
4519     for (auto &Pair : Captures)
4520       PreInits.push_back(Pair.second->getDecl());
4521     return buildPreInits(Context, PreInits);
4522   }
4523   return nullptr;
4524 }
4525 
4526 /// Build postupdate expression for the given list of postupdates expressions.
4527 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4528   Expr *PostUpdate = nullptr;
4529   if (!PostUpdates.empty()) {
4530     for (auto *E : PostUpdates) {
4531       Expr *ConvE = S.BuildCStyleCastExpr(
4532                          E->getExprLoc(),
4533                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4534                          E->getExprLoc(), E)
4535                         .get();
4536       PostUpdate = PostUpdate
4537                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4538                                               PostUpdate, ConvE)
4539                              .get()
4540                        : ConvE;
4541     }
4542   }
4543   return PostUpdate;
4544 }
4545 
4546 /// \brief Called on a for stmt to check itself and nested loops (if any).
4547 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4548 /// number of collapsed loops otherwise.
4549 static unsigned
4550 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4551                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4552                 DSAStackTy &DSA,
4553                 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4554                 OMPLoopDirective::HelperExprs &Built) {
4555   unsigned NestedLoopCount = 1;
4556   if (CollapseLoopCountExpr) {
4557     // Found 'collapse' clause - calculate collapse number.
4558     llvm::APSInt Result;
4559     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
4560       NestedLoopCount = Result.getLimitedValue();
4561   }
4562   if (OrderedLoopCountExpr) {
4563     // Found 'ordered' clause - calculate collapse number.
4564     llvm::APSInt Result;
4565     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4566       if (Result.getLimitedValue() < NestedLoopCount) {
4567         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4568                      diag::err_omp_wrong_ordered_loop_count)
4569             << OrderedLoopCountExpr->getSourceRange();
4570         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4571                      diag::note_collapse_loop_count)
4572             << CollapseLoopCountExpr->getSourceRange();
4573       }
4574       NestedLoopCount = Result.getLimitedValue();
4575     }
4576   }
4577   // This is helper routine for loop directives (e.g., 'for', 'simd',
4578   // 'for simd', etc.).
4579   llvm::MapVector<Expr *, DeclRefExpr *> Captures;
4580   SmallVector<LoopIterationSpace, 4> IterSpaces;
4581   IterSpaces.resize(NestedLoopCount);
4582   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
4583   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
4584     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
4585                                   NestedLoopCount, CollapseLoopCountExpr,
4586                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
4587                                   IterSpaces[Cnt], Captures))
4588       return 0;
4589     // Move on to the next nested for loop, or to the loop body.
4590     // OpenMP [2.8.1, simd construct, Restrictions]
4591     // All loops associated with the construct must be perfectly nested; that
4592     // is, there must be no intervening code nor any OpenMP directive between
4593     // any two loops.
4594     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4595   }
4596 
4597   Built.clear(/* size */ NestedLoopCount);
4598 
4599   if (SemaRef.CurContext->isDependentContext())
4600     return NestedLoopCount;
4601 
4602   // An example of what is generated for the following code:
4603   //
4604   //   #pragma omp simd collapse(2) ordered(2)
4605   //   for (i = 0; i < NI; ++i)
4606   //     for (k = 0; k < NK; ++k)
4607   //       for (j = J0; j < NJ; j+=2) {
4608   //         <loop body>
4609   //       }
4610   //
4611   // We generate the code below.
4612   // Note: the loop body may be outlined in CodeGen.
4613   // Note: some counters may be C++ classes, operator- is used to find number of
4614   // iterations and operator+= to calculate counter value.
4615   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4616   // or i64 is currently supported).
4617   //
4618   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4619   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4620   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4621   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4622   //     // similar updates for vars in clauses (e.g. 'linear')
4623   //     <loop body (using local i and j)>
4624   //   }
4625   //   i = NI; // assign final values of counters
4626   //   j = NJ;
4627   //
4628 
4629   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4630   // the iteration counts of the collapsed for loops.
4631   // Precondition tests if there is at least one iteration (all conditions are
4632   // true).
4633   auto PreCond = ExprResult(IterSpaces[0].PreCond);
4634   auto N0 = IterSpaces[0].NumIterations;
4635   ExprResult LastIteration32 = WidenIterationCount(
4636       32 /* Bits */, SemaRef.PerformImplicitConversion(
4637                                 N0->IgnoreImpCasts(), N0->getType(),
4638                                 Sema::AA_Converting, /*AllowExplicit=*/true)
4639                          .get(),
4640       SemaRef);
4641   ExprResult LastIteration64 = WidenIterationCount(
4642       64 /* Bits */, SemaRef.PerformImplicitConversion(
4643                                 N0->IgnoreImpCasts(), N0->getType(),
4644                                 Sema::AA_Converting, /*AllowExplicit=*/true)
4645                          .get(),
4646       SemaRef);
4647 
4648   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4649     return NestedLoopCount;
4650 
4651   auto &C = SemaRef.Context;
4652   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4653 
4654   Scope *CurScope = DSA.getCurScope();
4655   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
4656     if (PreCond.isUsable()) {
4657       PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4658                                    PreCond.get(), IterSpaces[Cnt].PreCond);
4659     }
4660     auto N = IterSpaces[Cnt].NumIterations;
4661     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4662     if (LastIteration32.isUsable())
4663       LastIteration32 = SemaRef.BuildBinOp(
4664           CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4665           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4666                                             Sema::AA_Converting,
4667                                             /*AllowExplicit=*/true)
4668               .get());
4669     if (LastIteration64.isUsable())
4670       LastIteration64 = SemaRef.BuildBinOp(
4671           CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4672           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4673                                             Sema::AA_Converting,
4674                                             /*AllowExplicit=*/true)
4675               .get());
4676   }
4677 
4678   // Choose either the 32-bit or 64-bit version.
4679   ExprResult LastIteration = LastIteration64;
4680   if (LastIteration32.isUsable() &&
4681       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4682       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4683        FitsInto(
4684            32 /* Bits */,
4685            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4686            LastIteration64.get(), SemaRef)))
4687     LastIteration = LastIteration32;
4688   QualType VType = LastIteration.get()->getType();
4689   QualType RealVType = VType;
4690   QualType StrideVType = VType;
4691   if (isOpenMPTaskLoopDirective(DKind)) {
4692     VType =
4693         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4694     StrideVType =
4695         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4696   }
4697 
4698   if (!LastIteration.isUsable())
4699     return 0;
4700 
4701   // Save the number of iterations.
4702   ExprResult NumIterations = LastIteration;
4703   {
4704     LastIteration = SemaRef.BuildBinOp(
4705         CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4706         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4707     if (!LastIteration.isUsable())
4708       return 0;
4709   }
4710 
4711   // Calculate the last iteration number beforehand instead of doing this on
4712   // each iteration. Do not do this if the number of iterations may be kfold-ed.
4713   llvm::APSInt Result;
4714   bool IsConstant =
4715       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4716   ExprResult CalcLastIteration;
4717   if (!IsConstant) {
4718     ExprResult SaveRef =
4719         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
4720     LastIteration = SaveRef;
4721 
4722     // Prepare SaveRef + 1.
4723     NumIterations = SemaRef.BuildBinOp(
4724         CurScope, SourceLocation(), BO_Add, SaveRef.get(),
4725         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4726     if (!NumIterations.isUsable())
4727       return 0;
4728   }
4729 
4730   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4731 
4732   // Build variables passed into runtime, nesessary for worksharing directives.
4733   ExprResult LB, UB, IL, ST, EUB;
4734   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4735       isOpenMPDistributeDirective(DKind)) {
4736     // Lower bound variable, initialized with zero.
4737     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4738     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
4739     SemaRef.AddInitializerToDecl(
4740         LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4741         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4742 
4743     // Upper bound variable, initialized with last iteration number.
4744     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4745     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
4746     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4747                                  /*DirectInit*/ false,
4748                                  /*TypeMayContainAuto*/ false);
4749 
4750     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4751     // This will be used to implement clause 'lastprivate'.
4752     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
4753     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4754     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
4755     SemaRef.AddInitializerToDecl(
4756         ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4757         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4758 
4759     // Stride variable returned by runtime (we initialize it to 1 by default).
4760     VarDecl *STDecl =
4761         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4762     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
4763     SemaRef.AddInitializerToDecl(
4764         STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4765         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4766 
4767     // Build expression: UB = min(UB, LastIteration)
4768     // It is nesessary for CodeGen of directives with static scheduling.
4769     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4770                                                 UB.get(), LastIteration.get());
4771     ExprResult CondOp = SemaRef.ActOnConditionalOp(
4772         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4773     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4774                              CondOp.get());
4775     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4776   }
4777 
4778   // Build the iteration variable and its initialization before loop.
4779   ExprResult IV;
4780   ExprResult Init;
4781   {
4782     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4783     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
4784     Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
4785                  isOpenMPTaskLoopDirective(DKind) ||
4786                  isOpenMPDistributeDirective(DKind))
4787                     ? LB.get()
4788                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4789     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4790     Init = SemaRef.ActOnFinishFullExpr(Init.get());
4791   }
4792 
4793   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
4794   SourceLocation CondLoc;
4795   ExprResult Cond =
4796       (isOpenMPWorksharingDirective(DKind) ||
4797        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4798           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4799           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4800                                NumIterations.get());
4801 
4802   // Loop increment (IV = IV + 1)
4803   SourceLocation IncLoc;
4804   ExprResult Inc =
4805       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4806                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4807   if (!Inc.isUsable())
4808     return 0;
4809   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
4810   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4811   if (!Inc.isUsable())
4812     return 0;
4813 
4814   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4815   // Used for directives with static scheduling.
4816   ExprResult NextLB, NextUB;
4817   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4818       isOpenMPDistributeDirective(DKind)) {
4819     // LB + ST
4820     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4821     if (!NextLB.isUsable())
4822       return 0;
4823     // LB = LB + ST
4824     NextLB =
4825         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4826     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4827     if (!NextLB.isUsable())
4828       return 0;
4829     // UB + ST
4830     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4831     if (!NextUB.isUsable())
4832       return 0;
4833     // UB = UB + ST
4834     NextUB =
4835         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4836     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4837     if (!NextUB.isUsable())
4838       return 0;
4839   }
4840 
4841   // Build updates and final values of the loop counters.
4842   bool HasErrors = false;
4843   Built.Counters.resize(NestedLoopCount);
4844   Built.Inits.resize(NestedLoopCount);
4845   Built.Updates.resize(NestedLoopCount);
4846   Built.Finals.resize(NestedLoopCount);
4847   {
4848     ExprResult Div;
4849     // Go from inner nested loop to outer.
4850     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4851       LoopIterationSpace &IS = IterSpaces[Cnt];
4852       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4853       // Build: Iter = (IV / Div) % IS.NumIters
4854       // where Div is product of previous iterations' IS.NumIters.
4855       ExprResult Iter;
4856       if (Div.isUsable()) {
4857         Iter =
4858             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4859       } else {
4860         Iter = IV;
4861         assert((Cnt == (int)NestedLoopCount - 1) &&
4862                "unusable div expected on first iteration only");
4863       }
4864 
4865       if (Cnt != 0 && Iter.isUsable())
4866         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4867                                   IS.NumIterations);
4868       if (!Iter.isUsable()) {
4869         HasErrors = true;
4870         break;
4871       }
4872 
4873       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4874       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4875       auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4876                                           IS.CounterVar->getExprLoc(),
4877                                           /*RefersToCapture=*/true);
4878       ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4879                                          IS.CounterInit, Captures);
4880       if (!Init.isUsable()) {
4881         HasErrors = true;
4882         break;
4883       }
4884       ExprResult Update = BuildCounterUpdate(
4885           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4886           IS.CounterStep, IS.Subtract, &Captures);
4887       if (!Update.isUsable()) {
4888         HasErrors = true;
4889         break;
4890       }
4891 
4892       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4893       ExprResult Final = BuildCounterUpdate(
4894           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
4895           IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
4896       if (!Final.isUsable()) {
4897         HasErrors = true;
4898         break;
4899       }
4900 
4901       // Build Div for the next iteration: Div <- Div * IS.NumIters
4902       if (Cnt != 0) {
4903         if (Div.isUnset())
4904           Div = IS.NumIterations;
4905         else
4906           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4907                                    IS.NumIterations);
4908 
4909         // Add parentheses (for debugging purposes only).
4910         if (Div.isUsable())
4911           Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4912         if (!Div.isUsable()) {
4913           HasErrors = true;
4914           break;
4915         }
4916       }
4917       if (!Update.isUsable() || !Final.isUsable()) {
4918         HasErrors = true;
4919         break;
4920       }
4921       // Save results
4922       Built.Counters[Cnt] = IS.CounterVar;
4923       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
4924       Built.Inits[Cnt] = Init.get();
4925       Built.Updates[Cnt] = Update.get();
4926       Built.Finals[Cnt] = Final.get();
4927     }
4928   }
4929 
4930   if (HasErrors)
4931     return 0;
4932 
4933   // Save results
4934   Built.IterationVarRef = IV.get();
4935   Built.LastIteration = LastIteration.get();
4936   Built.NumIterations = NumIterations.get();
4937   Built.CalcLastIteration =
4938       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
4939   Built.PreCond = PreCond.get();
4940   Built.PreInits = buildPreInits(C, Captures);
4941   Built.Cond = Cond.get();
4942   Built.Init = Init.get();
4943   Built.Inc = Inc.get();
4944   Built.LB = LB.get();
4945   Built.UB = UB.get();
4946   Built.IL = IL.get();
4947   Built.ST = ST.get();
4948   Built.EUB = EUB.get();
4949   Built.NLB = NextLB.get();
4950   Built.NUB = NextUB.get();
4951 
4952   return NestedLoopCount;
4953 }
4954 
4955 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
4956   auto CollapseClauses =
4957       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4958   if (CollapseClauses.begin() != CollapseClauses.end())
4959     return (*CollapseClauses.begin())->getNumForLoops();
4960   return nullptr;
4961 }
4962 
4963 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
4964   auto OrderedClauses =
4965       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4966   if (OrderedClauses.begin() != OrderedClauses.end())
4967     return (*OrderedClauses.begin())->getNumForLoops();
4968   return nullptr;
4969 }
4970 
4971 static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4972                                       const Expr *Safelen) {
4973   llvm::APSInt SimdlenRes, SafelenRes;
4974   if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4975       Simdlen->isInstantiationDependent() ||
4976       Simdlen->containsUnexpandedParameterPack())
4977     return false;
4978   if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4979       Safelen->isInstantiationDependent() ||
4980       Safelen->containsUnexpandedParameterPack())
4981     return false;
4982   Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4983   Safelen->EvaluateAsInt(SafelenRes, S.Context);
4984   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4985   // If both simdlen and safelen clauses are specified, the value of the simdlen
4986   // parameter must be less than or equal to the value of the safelen parameter.
4987   if (SimdlenRes > SafelenRes) {
4988     S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4989         << Simdlen->getSourceRange() << Safelen->getSourceRange();
4990     return true;
4991   }
4992   return false;
4993 }
4994 
4995 StmtResult Sema::ActOnOpenMPSimdDirective(
4996     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4997     SourceLocation EndLoc,
4998     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4999   if (!AStmt)
5000     return StmtError();
5001 
5002   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5003   OMPLoopDirective::HelperExprs B;
5004   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5005   // define the nested loops number.
5006   unsigned NestedLoopCount = CheckOpenMPLoop(
5007       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5008       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5009   if (NestedLoopCount == 0)
5010     return StmtError();
5011 
5012   assert((CurContext->isDependentContext() || B.builtAll()) &&
5013          "omp simd loop exprs were not built");
5014 
5015   if (!CurContext->isDependentContext()) {
5016     // Finalize the clauses that need pre-built expressions for CodeGen.
5017     for (auto C : Clauses) {
5018       if (auto LC = dyn_cast<OMPLinearClause>(C))
5019         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5020                                      B.NumIterations, *this, CurScope,
5021                                      DSAStack))
5022           return StmtError();
5023     }
5024   }
5025 
5026   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5027   // If both simdlen and safelen clauses are specified, the value of the simdlen
5028   // parameter must be less than or equal to the value of the safelen parameter.
5029   OMPSafelenClause *Safelen = nullptr;
5030   OMPSimdlenClause *Simdlen = nullptr;
5031   for (auto *Clause : Clauses) {
5032     if (Clause->getClauseKind() == OMPC_safelen)
5033       Safelen = cast<OMPSafelenClause>(Clause);
5034     else if (Clause->getClauseKind() == OMPC_simdlen)
5035       Simdlen = cast<OMPSimdlenClause>(Clause);
5036     if (Safelen && Simdlen)
5037       break;
5038   }
5039   if (Simdlen && Safelen &&
5040       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5041                                 Safelen->getSafelen()))
5042     return StmtError();
5043 
5044   getCurFunction()->setHasBranchProtectedScope();
5045   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5046                                   Clauses, AStmt, B);
5047 }
5048 
5049 StmtResult Sema::ActOnOpenMPForDirective(
5050     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5051     SourceLocation EndLoc,
5052     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5053   if (!AStmt)
5054     return StmtError();
5055 
5056   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5057   OMPLoopDirective::HelperExprs B;
5058   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5059   // define the nested loops number.
5060   unsigned NestedLoopCount = CheckOpenMPLoop(
5061       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5062       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5063   if (NestedLoopCount == 0)
5064     return StmtError();
5065 
5066   assert((CurContext->isDependentContext() || B.builtAll()) &&
5067          "omp for loop exprs were not built");
5068 
5069   if (!CurContext->isDependentContext()) {
5070     // Finalize the clauses that need pre-built expressions for CodeGen.
5071     for (auto C : Clauses) {
5072       if (auto LC = dyn_cast<OMPLinearClause>(C))
5073         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5074                                      B.NumIterations, *this, CurScope,
5075                                      DSAStack))
5076           return StmtError();
5077     }
5078   }
5079 
5080   getCurFunction()->setHasBranchProtectedScope();
5081   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5082                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
5083 }
5084 
5085 StmtResult Sema::ActOnOpenMPForSimdDirective(
5086     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5087     SourceLocation EndLoc,
5088     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5089   if (!AStmt)
5090     return StmtError();
5091 
5092   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5093   OMPLoopDirective::HelperExprs B;
5094   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5095   // define the nested loops number.
5096   unsigned NestedLoopCount =
5097       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5098                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5099                       VarsWithImplicitDSA, B);
5100   if (NestedLoopCount == 0)
5101     return StmtError();
5102 
5103   assert((CurContext->isDependentContext() || B.builtAll()) &&
5104          "omp for simd loop exprs were not built");
5105 
5106   if (!CurContext->isDependentContext()) {
5107     // Finalize the clauses that need pre-built expressions for CodeGen.
5108     for (auto C : Clauses) {
5109       if (auto LC = dyn_cast<OMPLinearClause>(C))
5110         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5111                                      B.NumIterations, *this, CurScope,
5112                                      DSAStack))
5113           return StmtError();
5114     }
5115   }
5116 
5117   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5118   // If both simdlen and safelen clauses are specified, the value of the simdlen
5119   // parameter must be less than or equal to the value of the safelen parameter.
5120   OMPSafelenClause *Safelen = nullptr;
5121   OMPSimdlenClause *Simdlen = nullptr;
5122   for (auto *Clause : Clauses) {
5123     if (Clause->getClauseKind() == OMPC_safelen)
5124       Safelen = cast<OMPSafelenClause>(Clause);
5125     else if (Clause->getClauseKind() == OMPC_simdlen)
5126       Simdlen = cast<OMPSimdlenClause>(Clause);
5127     if (Safelen && Simdlen)
5128       break;
5129   }
5130   if (Simdlen && Safelen &&
5131       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5132                                 Safelen->getSafelen()))
5133     return StmtError();
5134 
5135   getCurFunction()->setHasBranchProtectedScope();
5136   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5137                                      Clauses, AStmt, B);
5138 }
5139 
5140 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5141                                               Stmt *AStmt,
5142                                               SourceLocation StartLoc,
5143                                               SourceLocation EndLoc) {
5144   if (!AStmt)
5145     return StmtError();
5146 
5147   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5148   auto BaseStmt = AStmt;
5149   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5150     BaseStmt = CS->getCapturedStmt();
5151   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5152     auto S = C->children();
5153     if (S.begin() == S.end())
5154       return StmtError();
5155     // All associated statements must be '#pragma omp section' except for
5156     // the first one.
5157     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5158       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5159         if (SectionStmt)
5160           Diag(SectionStmt->getLocStart(),
5161                diag::err_omp_sections_substmt_not_section);
5162         return StmtError();
5163       }
5164       cast<OMPSectionDirective>(SectionStmt)
5165           ->setHasCancel(DSAStack->isCancelRegion());
5166     }
5167   } else {
5168     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5169     return StmtError();
5170   }
5171 
5172   getCurFunction()->setHasBranchProtectedScope();
5173 
5174   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5175                                       DSAStack->isCancelRegion());
5176 }
5177 
5178 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5179                                              SourceLocation StartLoc,
5180                                              SourceLocation EndLoc) {
5181   if (!AStmt)
5182     return StmtError();
5183 
5184   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5185 
5186   getCurFunction()->setHasBranchProtectedScope();
5187   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5188 
5189   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5190                                      DSAStack->isCancelRegion());
5191 }
5192 
5193 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5194                                             Stmt *AStmt,
5195                                             SourceLocation StartLoc,
5196                                             SourceLocation EndLoc) {
5197   if (!AStmt)
5198     return StmtError();
5199 
5200   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5201 
5202   getCurFunction()->setHasBranchProtectedScope();
5203 
5204   // OpenMP [2.7.3, single Construct, Restrictions]
5205   // The copyprivate clause must not be used with the nowait clause.
5206   OMPClause *Nowait = nullptr;
5207   OMPClause *Copyprivate = nullptr;
5208   for (auto *Clause : Clauses) {
5209     if (Clause->getClauseKind() == OMPC_nowait)
5210       Nowait = Clause;
5211     else if (Clause->getClauseKind() == OMPC_copyprivate)
5212       Copyprivate = Clause;
5213     if (Copyprivate && Nowait) {
5214       Diag(Copyprivate->getLocStart(),
5215            diag::err_omp_single_copyprivate_with_nowait);
5216       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5217       return StmtError();
5218     }
5219   }
5220 
5221   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5222 }
5223 
5224 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5225                                             SourceLocation StartLoc,
5226                                             SourceLocation EndLoc) {
5227   if (!AStmt)
5228     return StmtError();
5229 
5230   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5231 
5232   getCurFunction()->setHasBranchProtectedScope();
5233 
5234   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5235 }
5236 
5237 StmtResult Sema::ActOnOpenMPCriticalDirective(
5238     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5239     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5240   if (!AStmt)
5241     return StmtError();
5242 
5243   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5244 
5245   bool ErrorFound = false;
5246   llvm::APSInt Hint;
5247   SourceLocation HintLoc;
5248   bool DependentHint = false;
5249   for (auto *C : Clauses) {
5250     if (C->getClauseKind() == OMPC_hint) {
5251       if (!DirName.getName()) {
5252         Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5253         ErrorFound = true;
5254       }
5255       Expr *E = cast<OMPHintClause>(C)->getHint();
5256       if (E->isTypeDependent() || E->isValueDependent() ||
5257           E->isInstantiationDependent())
5258         DependentHint = true;
5259       else {
5260         Hint = E->EvaluateKnownConstInt(Context);
5261         HintLoc = C->getLocStart();
5262       }
5263     }
5264   }
5265   if (ErrorFound)
5266     return StmtError();
5267   auto Pair = DSAStack->getCriticalWithHint(DirName);
5268   if (Pair.first && DirName.getName() && !DependentHint) {
5269     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5270       Diag(StartLoc, diag::err_omp_critical_with_hint);
5271       if (HintLoc.isValid()) {
5272         Diag(HintLoc, diag::note_omp_critical_hint_here)
5273             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5274       } else
5275         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5276       if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5277         Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5278             << 1
5279             << C->getHint()->EvaluateKnownConstInt(Context).toString(
5280                    /*Radix=*/10, /*Signed=*/false);
5281       } else
5282         Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5283     }
5284   }
5285 
5286   getCurFunction()->setHasBranchProtectedScope();
5287 
5288   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5289                                            Clauses, AStmt);
5290   if (!Pair.first && DirName.getName() && !DependentHint)
5291     DSAStack->addCriticalWithHint(Dir, Hint);
5292   return Dir;
5293 }
5294 
5295 StmtResult Sema::ActOnOpenMPParallelForDirective(
5296     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5297     SourceLocation EndLoc,
5298     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5299   if (!AStmt)
5300     return StmtError();
5301 
5302   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5303   // 1.2.2 OpenMP Language Terminology
5304   // Structured block - An executable statement with a single entry at the
5305   // top and a single exit at the bottom.
5306   // The point of exit cannot be a branch out of the structured block.
5307   // longjmp() and throw() must not violate the entry/exit criteria.
5308   CS->getCapturedDecl()->setNothrow();
5309 
5310   OMPLoopDirective::HelperExprs B;
5311   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5312   // define the nested loops number.
5313   unsigned NestedLoopCount =
5314       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5315                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5316                       VarsWithImplicitDSA, B);
5317   if (NestedLoopCount == 0)
5318     return StmtError();
5319 
5320   assert((CurContext->isDependentContext() || B.builtAll()) &&
5321          "omp parallel for loop exprs were not built");
5322 
5323   if (!CurContext->isDependentContext()) {
5324     // Finalize the clauses that need pre-built expressions for CodeGen.
5325     for (auto C : Clauses) {
5326       if (auto LC = dyn_cast<OMPLinearClause>(C))
5327         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5328                                      B.NumIterations, *this, CurScope,
5329                                      DSAStack))
5330           return StmtError();
5331     }
5332   }
5333 
5334   getCurFunction()->setHasBranchProtectedScope();
5335   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5336                                          NestedLoopCount, Clauses, AStmt, B,
5337                                          DSAStack->isCancelRegion());
5338 }
5339 
5340 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5341     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5342     SourceLocation EndLoc,
5343     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5344   if (!AStmt)
5345     return StmtError();
5346 
5347   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5348   // 1.2.2 OpenMP Language Terminology
5349   // Structured block - An executable statement with a single entry at the
5350   // top and a single exit at the bottom.
5351   // The point of exit cannot be a branch out of the structured block.
5352   // longjmp() and throw() must not violate the entry/exit criteria.
5353   CS->getCapturedDecl()->setNothrow();
5354 
5355   OMPLoopDirective::HelperExprs B;
5356   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5357   // define the nested loops number.
5358   unsigned NestedLoopCount =
5359       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5360                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5361                       VarsWithImplicitDSA, B);
5362   if (NestedLoopCount == 0)
5363     return StmtError();
5364 
5365   if (!CurContext->isDependentContext()) {
5366     // Finalize the clauses that need pre-built expressions for CodeGen.
5367     for (auto C : Clauses) {
5368       if (auto LC = dyn_cast<OMPLinearClause>(C))
5369         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5370                                      B.NumIterations, *this, CurScope,
5371                                      DSAStack))
5372           return StmtError();
5373     }
5374   }
5375 
5376   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5377   // If both simdlen and safelen clauses are specified, the value of the simdlen
5378   // parameter must be less than or equal to the value of the safelen parameter.
5379   OMPSafelenClause *Safelen = nullptr;
5380   OMPSimdlenClause *Simdlen = nullptr;
5381   for (auto *Clause : Clauses) {
5382     if (Clause->getClauseKind() == OMPC_safelen)
5383       Safelen = cast<OMPSafelenClause>(Clause);
5384     else if (Clause->getClauseKind() == OMPC_simdlen)
5385       Simdlen = cast<OMPSimdlenClause>(Clause);
5386     if (Safelen && Simdlen)
5387       break;
5388   }
5389   if (Simdlen && Safelen &&
5390       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5391                                 Safelen->getSafelen()))
5392     return StmtError();
5393 
5394   getCurFunction()->setHasBranchProtectedScope();
5395   return OMPParallelForSimdDirective::Create(
5396       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5397 }
5398 
5399 StmtResult
5400 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5401                                            Stmt *AStmt, SourceLocation StartLoc,
5402                                            SourceLocation EndLoc) {
5403   if (!AStmt)
5404     return StmtError();
5405 
5406   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5407   auto BaseStmt = AStmt;
5408   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5409     BaseStmt = CS->getCapturedStmt();
5410   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5411     auto S = C->children();
5412     if (S.begin() == S.end())
5413       return StmtError();
5414     // All associated statements must be '#pragma omp section' except for
5415     // the first one.
5416     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5417       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5418         if (SectionStmt)
5419           Diag(SectionStmt->getLocStart(),
5420                diag::err_omp_parallel_sections_substmt_not_section);
5421         return StmtError();
5422       }
5423       cast<OMPSectionDirective>(SectionStmt)
5424           ->setHasCancel(DSAStack->isCancelRegion());
5425     }
5426   } else {
5427     Diag(AStmt->getLocStart(),
5428          diag::err_omp_parallel_sections_not_compound_stmt);
5429     return StmtError();
5430   }
5431 
5432   getCurFunction()->setHasBranchProtectedScope();
5433 
5434   return OMPParallelSectionsDirective::Create(
5435       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
5436 }
5437 
5438 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5439                                           Stmt *AStmt, SourceLocation StartLoc,
5440                                           SourceLocation EndLoc) {
5441   if (!AStmt)
5442     return StmtError();
5443 
5444   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5445   // 1.2.2 OpenMP Language Terminology
5446   // Structured block - An executable statement with a single entry at the
5447   // top and a single exit at the bottom.
5448   // The point of exit cannot be a branch out of the structured block.
5449   // longjmp() and throw() must not violate the entry/exit criteria.
5450   CS->getCapturedDecl()->setNothrow();
5451 
5452   getCurFunction()->setHasBranchProtectedScope();
5453 
5454   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5455                                   DSAStack->isCancelRegion());
5456 }
5457 
5458 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5459                                                SourceLocation EndLoc) {
5460   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5461 }
5462 
5463 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5464                                              SourceLocation EndLoc) {
5465   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5466 }
5467 
5468 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5469                                               SourceLocation EndLoc) {
5470   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5471 }
5472 
5473 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5474                                                SourceLocation StartLoc,
5475                                                SourceLocation EndLoc) {
5476   if (!AStmt)
5477     return StmtError();
5478 
5479   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5480 
5481   getCurFunction()->setHasBranchProtectedScope();
5482 
5483   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5484 }
5485 
5486 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5487                                            SourceLocation StartLoc,
5488                                            SourceLocation EndLoc) {
5489   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5490   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5491 }
5492 
5493 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5494                                              Stmt *AStmt,
5495                                              SourceLocation StartLoc,
5496                                              SourceLocation EndLoc) {
5497   OMPClause *DependFound = nullptr;
5498   OMPClause *DependSourceClause = nullptr;
5499   OMPClause *DependSinkClause = nullptr;
5500   bool ErrorFound = false;
5501   OMPThreadsClause *TC = nullptr;
5502   OMPSIMDClause *SC = nullptr;
5503   for (auto *C : Clauses) {
5504     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5505       DependFound = C;
5506       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5507         if (DependSourceClause) {
5508           Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5509               << getOpenMPDirectiveName(OMPD_ordered)
5510               << getOpenMPClauseName(OMPC_depend) << 2;
5511           ErrorFound = true;
5512         } else
5513           DependSourceClause = C;
5514         if (DependSinkClause) {
5515           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5516               << 0;
5517           ErrorFound = true;
5518         }
5519       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5520         if (DependSourceClause) {
5521           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5522               << 1;
5523           ErrorFound = true;
5524         }
5525         DependSinkClause = C;
5526       }
5527     } else if (C->getClauseKind() == OMPC_threads)
5528       TC = cast<OMPThreadsClause>(C);
5529     else if (C->getClauseKind() == OMPC_simd)
5530       SC = cast<OMPSIMDClause>(C);
5531   }
5532   if (!ErrorFound && !SC &&
5533       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
5534     // OpenMP [2.8.1,simd Construct, Restrictions]
5535     // An ordered construct with the simd clause is the only OpenMP construct
5536     // that can appear in the simd region.
5537     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5538     ErrorFound = true;
5539   } else if (DependFound && (TC || SC)) {
5540     Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5541         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5542     ErrorFound = true;
5543   } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5544     Diag(DependFound->getLocStart(),
5545          diag::err_omp_ordered_directive_without_param);
5546     ErrorFound = true;
5547   } else if (TC || Clauses.empty()) {
5548     if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5549       SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5550       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5551           << (TC != nullptr);
5552       Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5553       ErrorFound = true;
5554     }
5555   }
5556   if ((!AStmt && !DependFound) || ErrorFound)
5557     return StmtError();
5558 
5559   if (AStmt) {
5560     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5561 
5562     getCurFunction()->setHasBranchProtectedScope();
5563   }
5564 
5565   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5566 }
5567 
5568 namespace {
5569 /// \brief Helper class for checking expression in 'omp atomic [update]'
5570 /// construct.
5571 class OpenMPAtomicUpdateChecker {
5572   /// \brief Error results for atomic update expressions.
5573   enum ExprAnalysisErrorCode {
5574     /// \brief A statement is not an expression statement.
5575     NotAnExpression,
5576     /// \brief Expression is not builtin binary or unary operation.
5577     NotABinaryOrUnaryExpression,
5578     /// \brief Unary operation is not post-/pre- increment/decrement operation.
5579     NotAnUnaryIncDecExpression,
5580     /// \brief An expression is not of scalar type.
5581     NotAScalarType,
5582     /// \brief A binary operation is not an assignment operation.
5583     NotAnAssignmentOp,
5584     /// \brief RHS part of the binary operation is not a binary expression.
5585     NotABinaryExpression,
5586     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5587     /// expression.
5588     NotABinaryOperator,
5589     /// \brief RHS binary operation does not have reference to the updated LHS
5590     /// part.
5591     NotAnUpdateExpression,
5592     /// \brief No errors is found.
5593     NoError
5594   };
5595   /// \brief Reference to Sema.
5596   Sema &SemaRef;
5597   /// \brief A location for note diagnostics (when error is found).
5598   SourceLocation NoteLoc;
5599   /// \brief 'x' lvalue part of the source atomic expression.
5600   Expr *X;
5601   /// \brief 'expr' rvalue part of the source atomic expression.
5602   Expr *E;
5603   /// \brief Helper expression of the form
5604   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5605   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5606   Expr *UpdateExpr;
5607   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5608   /// important for non-associative operations.
5609   bool IsXLHSInRHSPart;
5610   BinaryOperatorKind Op;
5611   SourceLocation OpLoc;
5612   /// \brief true if the source expression is a postfix unary operation, false
5613   /// if it is a prefix unary operation.
5614   bool IsPostfixUpdate;
5615 
5616 public:
5617   OpenMPAtomicUpdateChecker(Sema &SemaRef)
5618       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
5619         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
5620   /// \brief Check specified statement that it is suitable for 'atomic update'
5621   /// constructs and extract 'x', 'expr' and Operation from the original
5622   /// expression. If DiagId and NoteId == 0, then only check is performed
5623   /// without error notification.
5624   /// \param DiagId Diagnostic which should be emitted if error is found.
5625   /// \param NoteId Diagnostic note for the main error message.
5626   /// \return true if statement is not an update expression, false otherwise.
5627   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
5628   /// \brief Return the 'x' lvalue part of the source atomic expression.
5629   Expr *getX() const { return X; }
5630   /// \brief Return the 'expr' rvalue part of the source atomic expression.
5631   Expr *getExpr() const { return E; }
5632   /// \brief Return the update expression used in calculation of the updated
5633   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5634   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5635   Expr *getUpdateExpr() const { return UpdateExpr; }
5636   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5637   /// false otherwise.
5638   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5639 
5640   /// \brief true if the source expression is a postfix unary operation, false
5641   /// if it is a prefix unary operation.
5642   bool isPostfixUpdate() const { return IsPostfixUpdate; }
5643 
5644 private:
5645   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5646                             unsigned NoteId = 0);
5647 };
5648 } // namespace
5649 
5650 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5651     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5652   ExprAnalysisErrorCode ErrorFound = NoError;
5653   SourceLocation ErrorLoc, NoteLoc;
5654   SourceRange ErrorRange, NoteRange;
5655   // Allowed constructs are:
5656   //  x = x binop expr;
5657   //  x = expr binop x;
5658   if (AtomicBinOp->getOpcode() == BO_Assign) {
5659     X = AtomicBinOp->getLHS();
5660     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5661             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5662       if (AtomicInnerBinOp->isMultiplicativeOp() ||
5663           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5664           AtomicInnerBinOp->isBitwiseOp()) {
5665         Op = AtomicInnerBinOp->getOpcode();
5666         OpLoc = AtomicInnerBinOp->getOperatorLoc();
5667         auto *LHS = AtomicInnerBinOp->getLHS();
5668         auto *RHS = AtomicInnerBinOp->getRHS();
5669         llvm::FoldingSetNodeID XId, LHSId, RHSId;
5670         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5671                                           /*Canonical=*/true);
5672         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5673                                             /*Canonical=*/true);
5674         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5675                                             /*Canonical=*/true);
5676         if (XId == LHSId) {
5677           E = RHS;
5678           IsXLHSInRHSPart = true;
5679         } else if (XId == RHSId) {
5680           E = LHS;
5681           IsXLHSInRHSPart = false;
5682         } else {
5683           ErrorLoc = AtomicInnerBinOp->getExprLoc();
5684           ErrorRange = AtomicInnerBinOp->getSourceRange();
5685           NoteLoc = X->getExprLoc();
5686           NoteRange = X->getSourceRange();
5687           ErrorFound = NotAnUpdateExpression;
5688         }
5689       } else {
5690         ErrorLoc = AtomicInnerBinOp->getExprLoc();
5691         ErrorRange = AtomicInnerBinOp->getSourceRange();
5692         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5693         NoteRange = SourceRange(NoteLoc, NoteLoc);
5694         ErrorFound = NotABinaryOperator;
5695       }
5696     } else {
5697       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5698       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5699       ErrorFound = NotABinaryExpression;
5700     }
5701   } else {
5702     ErrorLoc = AtomicBinOp->getExprLoc();
5703     ErrorRange = AtomicBinOp->getSourceRange();
5704     NoteLoc = AtomicBinOp->getOperatorLoc();
5705     NoteRange = SourceRange(NoteLoc, NoteLoc);
5706     ErrorFound = NotAnAssignmentOp;
5707   }
5708   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5709     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5710     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5711     return true;
5712   } else if (SemaRef.CurContext->isDependentContext())
5713     E = X = UpdateExpr = nullptr;
5714   return ErrorFound != NoError;
5715 }
5716 
5717 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5718                                                unsigned NoteId) {
5719   ExprAnalysisErrorCode ErrorFound = NoError;
5720   SourceLocation ErrorLoc, NoteLoc;
5721   SourceRange ErrorRange, NoteRange;
5722   // Allowed constructs are:
5723   //  x++;
5724   //  x--;
5725   //  ++x;
5726   //  --x;
5727   //  x binop= expr;
5728   //  x = x binop expr;
5729   //  x = expr binop x;
5730   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5731     AtomicBody = AtomicBody->IgnoreParenImpCasts();
5732     if (AtomicBody->getType()->isScalarType() ||
5733         AtomicBody->isInstantiationDependent()) {
5734       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5735               AtomicBody->IgnoreParenImpCasts())) {
5736         // Check for Compound Assignment Operation
5737         Op = BinaryOperator::getOpForCompoundAssignment(
5738             AtomicCompAssignOp->getOpcode());
5739         OpLoc = AtomicCompAssignOp->getOperatorLoc();
5740         E = AtomicCompAssignOp->getRHS();
5741         X = AtomicCompAssignOp->getLHS();
5742         IsXLHSInRHSPart = true;
5743       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5744                      AtomicBody->IgnoreParenImpCasts())) {
5745         // Check for Binary Operation
5746         if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5747           return true;
5748       } else if (auto *AtomicUnaryOp =
5749                  dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5750         // Check for Unary Operation
5751         if (AtomicUnaryOp->isIncrementDecrementOp()) {
5752           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
5753           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5754           OpLoc = AtomicUnaryOp->getOperatorLoc();
5755           X = AtomicUnaryOp->getSubExpr();
5756           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5757           IsXLHSInRHSPart = true;
5758         } else {
5759           ErrorFound = NotAnUnaryIncDecExpression;
5760           ErrorLoc = AtomicUnaryOp->getExprLoc();
5761           ErrorRange = AtomicUnaryOp->getSourceRange();
5762           NoteLoc = AtomicUnaryOp->getOperatorLoc();
5763           NoteRange = SourceRange(NoteLoc, NoteLoc);
5764         }
5765       } else if (!AtomicBody->isInstantiationDependent()) {
5766         ErrorFound = NotABinaryOrUnaryExpression;
5767         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5768         NoteRange = ErrorRange = AtomicBody->getSourceRange();
5769       }
5770     } else {
5771       ErrorFound = NotAScalarType;
5772       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5773       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5774     }
5775   } else {
5776     ErrorFound = NotAnExpression;
5777     NoteLoc = ErrorLoc = S->getLocStart();
5778     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5779   }
5780   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5781     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5782     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5783     return true;
5784   } else if (SemaRef.CurContext->isDependentContext())
5785     E = X = UpdateExpr = nullptr;
5786   if (ErrorFound == NoError && E && X) {
5787     // Build an update expression of form 'OpaqueValueExpr(x) binop
5788     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5789     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5790     auto *OVEX = new (SemaRef.getASTContext())
5791         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5792     auto *OVEExpr = new (SemaRef.getASTContext())
5793         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5794     auto Update =
5795         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5796                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
5797     if (Update.isInvalid())
5798       return true;
5799     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5800                                                Sema::AA_Casting);
5801     if (Update.isInvalid())
5802       return true;
5803     UpdateExpr = Update.get();
5804   }
5805   return ErrorFound != NoError;
5806 }
5807 
5808 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5809                                             Stmt *AStmt,
5810                                             SourceLocation StartLoc,
5811                                             SourceLocation EndLoc) {
5812   if (!AStmt)
5813     return StmtError();
5814 
5815   auto CS = cast<CapturedStmt>(AStmt);
5816   // 1.2.2 OpenMP Language Terminology
5817   // Structured block - An executable statement with a single entry at the
5818   // top and a single exit at the bottom.
5819   // The point of exit cannot be a branch out of the structured block.
5820   // longjmp() and throw() must not violate the entry/exit criteria.
5821   OpenMPClauseKind AtomicKind = OMPC_unknown;
5822   SourceLocation AtomicKindLoc;
5823   for (auto *C : Clauses) {
5824     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
5825         C->getClauseKind() == OMPC_update ||
5826         C->getClauseKind() == OMPC_capture) {
5827       if (AtomicKind != OMPC_unknown) {
5828         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5829             << SourceRange(C->getLocStart(), C->getLocEnd());
5830         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5831             << getOpenMPClauseName(AtomicKind);
5832       } else {
5833         AtomicKind = C->getClauseKind();
5834         AtomicKindLoc = C->getLocStart();
5835       }
5836     }
5837   }
5838 
5839   auto Body = CS->getCapturedStmt();
5840   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5841     Body = EWC->getSubExpr();
5842 
5843   Expr *X = nullptr;
5844   Expr *V = nullptr;
5845   Expr *E = nullptr;
5846   Expr *UE = nullptr;
5847   bool IsXLHSInRHSPart = false;
5848   bool IsPostfixUpdate = false;
5849   // OpenMP [2.12.6, atomic Construct]
5850   // In the next expressions:
5851   // * x and v (as applicable) are both l-value expressions with scalar type.
5852   // * During the execution of an atomic region, multiple syntactic
5853   // occurrences of x must designate the same storage location.
5854   // * Neither of v and expr (as applicable) may access the storage location
5855   // designated by x.
5856   // * Neither of x and expr (as applicable) may access the storage location
5857   // designated by v.
5858   // * expr is an expression with scalar type.
5859   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5860   // * binop, binop=, ++, and -- are not overloaded operators.
5861   // * The expression x binop expr must be numerically equivalent to x binop
5862   // (expr). This requirement is satisfied if the operators in expr have
5863   // precedence greater than binop, or by using parentheses around expr or
5864   // subexpressions of expr.
5865   // * The expression expr binop x must be numerically equivalent to (expr)
5866   // binop x. This requirement is satisfied if the operators in expr have
5867   // precedence equal to or greater than binop, or by using parentheses around
5868   // expr or subexpressions of expr.
5869   // * For forms that allow multiple occurrences of x, the number of times
5870   // that x is evaluated is unspecified.
5871   if (AtomicKind == OMPC_read) {
5872     enum {
5873       NotAnExpression,
5874       NotAnAssignmentOp,
5875       NotAScalarType,
5876       NotAnLValue,
5877       NoError
5878     } ErrorFound = NoError;
5879     SourceLocation ErrorLoc, NoteLoc;
5880     SourceRange ErrorRange, NoteRange;
5881     // If clause is read:
5882     //  v = x;
5883     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5884       auto AtomicBinOp =
5885           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5886       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5887         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5888         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5889         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5890             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5891           if (!X->isLValue() || !V->isLValue()) {
5892             auto NotLValueExpr = X->isLValue() ? V : X;
5893             ErrorFound = NotAnLValue;
5894             ErrorLoc = AtomicBinOp->getExprLoc();
5895             ErrorRange = AtomicBinOp->getSourceRange();
5896             NoteLoc = NotLValueExpr->getExprLoc();
5897             NoteRange = NotLValueExpr->getSourceRange();
5898           }
5899         } else if (!X->isInstantiationDependent() ||
5900                    !V->isInstantiationDependent()) {
5901           auto NotScalarExpr =
5902               (X->isInstantiationDependent() || X->getType()->isScalarType())
5903                   ? V
5904                   : X;
5905           ErrorFound = NotAScalarType;
5906           ErrorLoc = AtomicBinOp->getExprLoc();
5907           ErrorRange = AtomicBinOp->getSourceRange();
5908           NoteLoc = NotScalarExpr->getExprLoc();
5909           NoteRange = NotScalarExpr->getSourceRange();
5910         }
5911       } else if (!AtomicBody->isInstantiationDependent()) {
5912         ErrorFound = NotAnAssignmentOp;
5913         ErrorLoc = AtomicBody->getExprLoc();
5914         ErrorRange = AtomicBody->getSourceRange();
5915         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5916                               : AtomicBody->getExprLoc();
5917         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5918                                 : AtomicBody->getSourceRange();
5919       }
5920     } else {
5921       ErrorFound = NotAnExpression;
5922       NoteLoc = ErrorLoc = Body->getLocStart();
5923       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5924     }
5925     if (ErrorFound != NoError) {
5926       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5927           << ErrorRange;
5928       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5929                                                       << NoteRange;
5930       return StmtError();
5931     } else if (CurContext->isDependentContext())
5932       V = X = nullptr;
5933   } else if (AtomicKind == OMPC_write) {
5934     enum {
5935       NotAnExpression,
5936       NotAnAssignmentOp,
5937       NotAScalarType,
5938       NotAnLValue,
5939       NoError
5940     } ErrorFound = NoError;
5941     SourceLocation ErrorLoc, NoteLoc;
5942     SourceRange ErrorRange, NoteRange;
5943     // If clause is write:
5944     //  x = expr;
5945     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5946       auto AtomicBinOp =
5947           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5948       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5949         X = AtomicBinOp->getLHS();
5950         E = AtomicBinOp->getRHS();
5951         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5952             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5953           if (!X->isLValue()) {
5954             ErrorFound = NotAnLValue;
5955             ErrorLoc = AtomicBinOp->getExprLoc();
5956             ErrorRange = AtomicBinOp->getSourceRange();
5957             NoteLoc = X->getExprLoc();
5958             NoteRange = X->getSourceRange();
5959           }
5960         } else if (!X->isInstantiationDependent() ||
5961                    !E->isInstantiationDependent()) {
5962           auto NotScalarExpr =
5963               (X->isInstantiationDependent() || X->getType()->isScalarType())
5964                   ? E
5965                   : X;
5966           ErrorFound = NotAScalarType;
5967           ErrorLoc = AtomicBinOp->getExprLoc();
5968           ErrorRange = AtomicBinOp->getSourceRange();
5969           NoteLoc = NotScalarExpr->getExprLoc();
5970           NoteRange = NotScalarExpr->getSourceRange();
5971         }
5972       } else if (!AtomicBody->isInstantiationDependent()) {
5973         ErrorFound = NotAnAssignmentOp;
5974         ErrorLoc = AtomicBody->getExprLoc();
5975         ErrorRange = AtomicBody->getSourceRange();
5976         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5977                               : AtomicBody->getExprLoc();
5978         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5979                                 : AtomicBody->getSourceRange();
5980       }
5981     } else {
5982       ErrorFound = NotAnExpression;
5983       NoteLoc = ErrorLoc = Body->getLocStart();
5984       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5985     }
5986     if (ErrorFound != NoError) {
5987       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5988           << ErrorRange;
5989       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5990                                                       << NoteRange;
5991       return StmtError();
5992     } else if (CurContext->isDependentContext())
5993       E = X = nullptr;
5994   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
5995     // If clause is update:
5996     //  x++;
5997     //  x--;
5998     //  ++x;
5999     //  --x;
6000     //  x binop= expr;
6001     //  x = x binop expr;
6002     //  x = expr binop x;
6003     OpenMPAtomicUpdateChecker Checker(*this);
6004     if (Checker.checkStatement(
6005             Body, (AtomicKind == OMPC_update)
6006                       ? diag::err_omp_atomic_update_not_expression_statement
6007                       : diag::err_omp_atomic_not_expression_statement,
6008             diag::note_omp_atomic_update))
6009       return StmtError();
6010     if (!CurContext->isDependentContext()) {
6011       E = Checker.getExpr();
6012       X = Checker.getX();
6013       UE = Checker.getUpdateExpr();
6014       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6015     }
6016   } else if (AtomicKind == OMPC_capture) {
6017     enum {
6018       NotAnAssignmentOp,
6019       NotACompoundStatement,
6020       NotTwoSubstatements,
6021       NotASpecificExpression,
6022       NoError
6023     } ErrorFound = NoError;
6024     SourceLocation ErrorLoc, NoteLoc;
6025     SourceRange ErrorRange, NoteRange;
6026     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6027       // If clause is a capture:
6028       //  v = x++;
6029       //  v = x--;
6030       //  v = ++x;
6031       //  v = --x;
6032       //  v = x binop= expr;
6033       //  v = x = x binop expr;
6034       //  v = x = expr binop x;
6035       auto *AtomicBinOp =
6036           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6037       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6038         V = AtomicBinOp->getLHS();
6039         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6040         OpenMPAtomicUpdateChecker Checker(*this);
6041         if (Checker.checkStatement(
6042                 Body, diag::err_omp_atomic_capture_not_expression_statement,
6043                 diag::note_omp_atomic_update))
6044           return StmtError();
6045         E = Checker.getExpr();
6046         X = Checker.getX();
6047         UE = Checker.getUpdateExpr();
6048         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6049         IsPostfixUpdate = Checker.isPostfixUpdate();
6050       } else if (!AtomicBody->isInstantiationDependent()) {
6051         ErrorLoc = AtomicBody->getExprLoc();
6052         ErrorRange = AtomicBody->getSourceRange();
6053         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6054                               : AtomicBody->getExprLoc();
6055         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6056                                 : AtomicBody->getSourceRange();
6057         ErrorFound = NotAnAssignmentOp;
6058       }
6059       if (ErrorFound != NoError) {
6060         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6061             << ErrorRange;
6062         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6063         return StmtError();
6064       } else if (CurContext->isDependentContext()) {
6065         UE = V = E = X = nullptr;
6066       }
6067     } else {
6068       // If clause is a capture:
6069       //  { v = x; x = expr; }
6070       //  { v = x; x++; }
6071       //  { v = x; x--; }
6072       //  { v = x; ++x; }
6073       //  { v = x; --x; }
6074       //  { v = x; x binop= expr; }
6075       //  { v = x; x = x binop expr; }
6076       //  { v = x; x = expr binop x; }
6077       //  { x++; v = x; }
6078       //  { x--; v = x; }
6079       //  { ++x; v = x; }
6080       //  { --x; v = x; }
6081       //  { x binop= expr; v = x; }
6082       //  { x = x binop expr; v = x; }
6083       //  { x = expr binop x; v = x; }
6084       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6085         // Check that this is { expr1; expr2; }
6086         if (CS->size() == 2) {
6087           auto *First = CS->body_front();
6088           auto *Second = CS->body_back();
6089           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6090             First = EWC->getSubExpr()->IgnoreParenImpCasts();
6091           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6092             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6093           // Need to find what subexpression is 'v' and what is 'x'.
6094           OpenMPAtomicUpdateChecker Checker(*this);
6095           bool IsUpdateExprFound = !Checker.checkStatement(Second);
6096           BinaryOperator *BinOp = nullptr;
6097           if (IsUpdateExprFound) {
6098             BinOp = dyn_cast<BinaryOperator>(First);
6099             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6100           }
6101           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6102             //  { v = x; x++; }
6103             //  { v = x; x--; }
6104             //  { v = x; ++x; }
6105             //  { v = x; --x; }
6106             //  { v = x; x binop= expr; }
6107             //  { v = x; x = x binop expr; }
6108             //  { v = x; x = expr binop x; }
6109             // Check that the first expression has form v = x.
6110             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6111             llvm::FoldingSetNodeID XId, PossibleXId;
6112             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6113             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6114             IsUpdateExprFound = XId == PossibleXId;
6115             if (IsUpdateExprFound) {
6116               V = BinOp->getLHS();
6117               X = Checker.getX();
6118               E = Checker.getExpr();
6119               UE = Checker.getUpdateExpr();
6120               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6121               IsPostfixUpdate = true;
6122             }
6123           }
6124           if (!IsUpdateExprFound) {
6125             IsUpdateExprFound = !Checker.checkStatement(First);
6126             BinOp = nullptr;
6127             if (IsUpdateExprFound) {
6128               BinOp = dyn_cast<BinaryOperator>(Second);
6129               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6130             }
6131             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6132               //  { x++; v = x; }
6133               //  { x--; v = x; }
6134               //  { ++x; v = x; }
6135               //  { --x; v = x; }
6136               //  { x binop= expr; v = x; }
6137               //  { x = x binop expr; v = x; }
6138               //  { x = expr binop x; v = x; }
6139               // Check that the second expression has form v = x.
6140               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6141               llvm::FoldingSetNodeID XId, PossibleXId;
6142               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6143               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6144               IsUpdateExprFound = XId == PossibleXId;
6145               if (IsUpdateExprFound) {
6146                 V = BinOp->getLHS();
6147                 X = Checker.getX();
6148                 E = Checker.getExpr();
6149                 UE = Checker.getUpdateExpr();
6150                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6151                 IsPostfixUpdate = false;
6152               }
6153             }
6154           }
6155           if (!IsUpdateExprFound) {
6156             //  { v = x; x = expr; }
6157             auto *FirstExpr = dyn_cast<Expr>(First);
6158             auto *SecondExpr = dyn_cast<Expr>(Second);
6159             if (!FirstExpr || !SecondExpr ||
6160                 !(FirstExpr->isInstantiationDependent() ||
6161                   SecondExpr->isInstantiationDependent())) {
6162               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6163               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
6164                 ErrorFound = NotAnAssignmentOp;
6165                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6166                                                 : First->getLocStart();
6167                 NoteRange = ErrorRange = FirstBinOp
6168                                              ? FirstBinOp->getSourceRange()
6169                                              : SourceRange(ErrorLoc, ErrorLoc);
6170               } else {
6171                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6172                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6173                   ErrorFound = NotAnAssignmentOp;
6174                   NoteLoc = ErrorLoc = SecondBinOp
6175                                            ? SecondBinOp->getOperatorLoc()
6176                                            : Second->getLocStart();
6177                   NoteRange = ErrorRange =
6178                       SecondBinOp ? SecondBinOp->getSourceRange()
6179                                   : SourceRange(ErrorLoc, ErrorLoc);
6180                 } else {
6181                   auto *PossibleXRHSInFirst =
6182                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
6183                   auto *PossibleXLHSInSecond =
6184                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
6185                   llvm::FoldingSetNodeID X1Id, X2Id;
6186                   PossibleXRHSInFirst->Profile(X1Id, Context,
6187                                                /*Canonical=*/true);
6188                   PossibleXLHSInSecond->Profile(X2Id, Context,
6189                                                 /*Canonical=*/true);
6190                   IsUpdateExprFound = X1Id == X2Id;
6191                   if (IsUpdateExprFound) {
6192                     V = FirstBinOp->getLHS();
6193                     X = SecondBinOp->getLHS();
6194                     E = SecondBinOp->getRHS();
6195                     UE = nullptr;
6196                     IsXLHSInRHSPart = false;
6197                     IsPostfixUpdate = true;
6198                   } else {
6199                     ErrorFound = NotASpecificExpression;
6200                     ErrorLoc = FirstBinOp->getExprLoc();
6201                     ErrorRange = FirstBinOp->getSourceRange();
6202                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6203                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
6204                   }
6205                 }
6206               }
6207             }
6208           }
6209         } else {
6210           NoteLoc = ErrorLoc = Body->getLocStart();
6211           NoteRange = ErrorRange =
6212               SourceRange(Body->getLocStart(), Body->getLocStart());
6213           ErrorFound = NotTwoSubstatements;
6214         }
6215       } else {
6216         NoteLoc = ErrorLoc = Body->getLocStart();
6217         NoteRange = ErrorRange =
6218             SourceRange(Body->getLocStart(), Body->getLocStart());
6219         ErrorFound = NotACompoundStatement;
6220       }
6221       if (ErrorFound != NoError) {
6222         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6223             << ErrorRange;
6224         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6225         return StmtError();
6226       } else if (CurContext->isDependentContext()) {
6227         UE = V = E = X = nullptr;
6228       }
6229     }
6230   }
6231 
6232   getCurFunction()->setHasBranchProtectedScope();
6233 
6234   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6235                                     X, V, E, UE, IsXLHSInRHSPart,
6236                                     IsPostfixUpdate);
6237 }
6238 
6239 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6240                                             Stmt *AStmt,
6241                                             SourceLocation StartLoc,
6242                                             SourceLocation EndLoc) {
6243   if (!AStmt)
6244     return StmtError();
6245 
6246   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6247   // 1.2.2 OpenMP Language Terminology
6248   // Structured block - An executable statement with a single entry at the
6249   // top and a single exit at the bottom.
6250   // The point of exit cannot be a branch out of the structured block.
6251   // longjmp() and throw() must not violate the entry/exit criteria.
6252   CS->getCapturedDecl()->setNothrow();
6253 
6254   // OpenMP [2.16, Nesting of Regions]
6255   // If specified, a teams construct must be contained within a target
6256   // construct. That target construct must contain no statements or directives
6257   // outside of the teams construct.
6258   if (DSAStack->hasInnerTeamsRegion()) {
6259     auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6260     bool OMPTeamsFound = true;
6261     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6262       auto I = CS->body_begin();
6263       while (I != CS->body_end()) {
6264         auto OED = dyn_cast<OMPExecutableDirective>(*I);
6265         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6266           OMPTeamsFound = false;
6267           break;
6268         }
6269         ++I;
6270       }
6271       assert(I != CS->body_end() && "Not found statement");
6272       S = *I;
6273     }
6274     if (!OMPTeamsFound) {
6275       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6276       Diag(DSAStack->getInnerTeamsRegionLoc(),
6277            diag::note_omp_nested_teams_construct_here);
6278       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6279           << isa<OMPExecutableDirective>(S);
6280       return StmtError();
6281     }
6282   }
6283 
6284   getCurFunction()->setHasBranchProtectedScope();
6285 
6286   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6287 }
6288 
6289 StmtResult
6290 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6291                                          Stmt *AStmt, SourceLocation StartLoc,
6292                                          SourceLocation EndLoc) {
6293   if (!AStmt)
6294     return StmtError();
6295 
6296   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6297   // 1.2.2 OpenMP Language Terminology
6298   // Structured block - An executable statement with a single entry at the
6299   // top and a single exit at the bottom.
6300   // The point of exit cannot be a branch out of the structured block.
6301   // longjmp() and throw() must not violate the entry/exit criteria.
6302   CS->getCapturedDecl()->setNothrow();
6303 
6304   getCurFunction()->setHasBranchProtectedScope();
6305 
6306   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6307                                             AStmt);
6308 }
6309 
6310 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6311     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6312     SourceLocation EndLoc,
6313     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6314   if (!AStmt)
6315     return StmtError();
6316 
6317   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6318   // 1.2.2 OpenMP Language Terminology
6319   // Structured block - An executable statement with a single entry at the
6320   // top and a single exit at the bottom.
6321   // The point of exit cannot be a branch out of the structured block.
6322   // longjmp() and throw() must not violate the entry/exit criteria.
6323   CS->getCapturedDecl()->setNothrow();
6324 
6325   OMPLoopDirective::HelperExprs B;
6326   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6327   // define the nested loops number.
6328   unsigned NestedLoopCount =
6329       CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6330                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6331                       VarsWithImplicitDSA, B);
6332   if (NestedLoopCount == 0)
6333     return StmtError();
6334 
6335   assert((CurContext->isDependentContext() || B.builtAll()) &&
6336          "omp target parallel for loop exprs were not built");
6337 
6338   if (!CurContext->isDependentContext()) {
6339     // Finalize the clauses that need pre-built expressions for CodeGen.
6340     for (auto C : Clauses) {
6341       if (auto LC = dyn_cast<OMPLinearClause>(C))
6342         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6343                                      B.NumIterations, *this, CurScope,
6344                                      DSAStack))
6345           return StmtError();
6346     }
6347   }
6348 
6349   getCurFunction()->setHasBranchProtectedScope();
6350   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6351                                                NestedLoopCount, Clauses, AStmt,
6352                                                B, DSAStack->isCancelRegion());
6353 }
6354 
6355 /// \brief Check for existence of a map clause in the list of clauses.
6356 static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6357   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6358        I != E; ++I) {
6359     if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6360       return true;
6361     }
6362   }
6363 
6364   return false;
6365 }
6366 
6367 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6368                                                 Stmt *AStmt,
6369                                                 SourceLocation StartLoc,
6370                                                 SourceLocation EndLoc) {
6371   if (!AStmt)
6372     return StmtError();
6373 
6374   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6375 
6376   // OpenMP [2.10.1, Restrictions, p. 97]
6377   // At least one map clause must appear on the directive.
6378   if (!HasMapClause(Clauses)) {
6379     Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6380         getOpenMPDirectiveName(OMPD_target_data);
6381     return StmtError();
6382   }
6383 
6384   getCurFunction()->setHasBranchProtectedScope();
6385 
6386   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6387                                         AStmt);
6388 }
6389 
6390 StmtResult
6391 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6392                                           SourceLocation StartLoc,
6393                                           SourceLocation EndLoc) {
6394   // OpenMP [2.10.2, Restrictions, p. 99]
6395   // At least one map clause must appear on the directive.
6396   if (!HasMapClause(Clauses)) {
6397     Diag(StartLoc, diag::err_omp_no_map_for_directive)
6398         << getOpenMPDirectiveName(OMPD_target_enter_data);
6399     return StmtError();
6400   }
6401 
6402   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6403                                              Clauses);
6404 }
6405 
6406 StmtResult
6407 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6408                                          SourceLocation StartLoc,
6409                                          SourceLocation EndLoc) {
6410   // OpenMP [2.10.3, Restrictions, p. 102]
6411   // At least one map clause must appear on the directive.
6412   if (!HasMapClause(Clauses)) {
6413     Diag(StartLoc, diag::err_omp_no_map_for_directive)
6414         << getOpenMPDirectiveName(OMPD_target_exit_data);
6415     return StmtError();
6416   }
6417 
6418   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6419 }
6420 
6421 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6422                                            Stmt *AStmt, SourceLocation StartLoc,
6423                                            SourceLocation EndLoc) {
6424   if (!AStmt)
6425     return StmtError();
6426 
6427   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6428   // 1.2.2 OpenMP Language Terminology
6429   // Structured block - An executable statement with a single entry at the
6430   // top and a single exit at the bottom.
6431   // The point of exit cannot be a branch out of the structured block.
6432   // longjmp() and throw() must not violate the entry/exit criteria.
6433   CS->getCapturedDecl()->setNothrow();
6434 
6435   getCurFunction()->setHasBranchProtectedScope();
6436 
6437   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6438 }
6439 
6440 StmtResult
6441 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6442                                             SourceLocation EndLoc,
6443                                             OpenMPDirectiveKind CancelRegion) {
6444   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6445       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6446     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6447         << getOpenMPDirectiveName(CancelRegion);
6448     return StmtError();
6449   }
6450   if (DSAStack->isParentNowaitRegion()) {
6451     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6452     return StmtError();
6453   }
6454   if (DSAStack->isParentOrderedRegion()) {
6455     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6456     return StmtError();
6457   }
6458   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6459                                                CancelRegion);
6460 }
6461 
6462 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6463                                             SourceLocation StartLoc,
6464                                             SourceLocation EndLoc,
6465                                             OpenMPDirectiveKind CancelRegion) {
6466   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6467       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6468     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6469         << getOpenMPDirectiveName(CancelRegion);
6470     return StmtError();
6471   }
6472   if (DSAStack->isParentNowaitRegion()) {
6473     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6474     return StmtError();
6475   }
6476   if (DSAStack->isParentOrderedRegion()) {
6477     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6478     return StmtError();
6479   }
6480   DSAStack->setParentCancelRegion(/*Cancel=*/true);
6481   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6482                                     CancelRegion);
6483 }
6484 
6485 static bool checkGrainsizeNumTasksClauses(Sema &S,
6486                                           ArrayRef<OMPClause *> Clauses) {
6487   OMPClause *PrevClause = nullptr;
6488   bool ErrorFound = false;
6489   for (auto *C : Clauses) {
6490     if (C->getClauseKind() == OMPC_grainsize ||
6491         C->getClauseKind() == OMPC_num_tasks) {
6492       if (!PrevClause)
6493         PrevClause = C;
6494       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6495         S.Diag(C->getLocStart(),
6496                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6497             << getOpenMPClauseName(C->getClauseKind())
6498             << getOpenMPClauseName(PrevClause->getClauseKind());
6499         S.Diag(PrevClause->getLocStart(),
6500                diag::note_omp_previous_grainsize_num_tasks)
6501             << getOpenMPClauseName(PrevClause->getClauseKind());
6502         ErrorFound = true;
6503       }
6504     }
6505   }
6506   return ErrorFound;
6507 }
6508 
6509 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6510     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6511     SourceLocation EndLoc,
6512     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6513   if (!AStmt)
6514     return StmtError();
6515 
6516   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6517   OMPLoopDirective::HelperExprs B;
6518   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6519   // define the nested loops number.
6520   unsigned NestedLoopCount =
6521       CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
6522                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6523                       VarsWithImplicitDSA, B);
6524   if (NestedLoopCount == 0)
6525     return StmtError();
6526 
6527   assert((CurContext->isDependentContext() || B.builtAll()) &&
6528          "omp for loop exprs were not built");
6529 
6530   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6531   // The grainsize clause and num_tasks clause are mutually exclusive and may
6532   // not appear on the same taskloop directive.
6533   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6534     return StmtError();
6535 
6536   getCurFunction()->setHasBranchProtectedScope();
6537   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6538                                       NestedLoopCount, Clauses, AStmt, B);
6539 }
6540 
6541 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6542     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6543     SourceLocation EndLoc,
6544     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6545   if (!AStmt)
6546     return StmtError();
6547 
6548   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6549   OMPLoopDirective::HelperExprs B;
6550   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6551   // define the nested loops number.
6552   unsigned NestedLoopCount =
6553       CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6554                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6555                       VarsWithImplicitDSA, B);
6556   if (NestedLoopCount == 0)
6557     return StmtError();
6558 
6559   assert((CurContext->isDependentContext() || B.builtAll()) &&
6560          "omp for loop exprs were not built");
6561 
6562   if (!CurContext->isDependentContext()) {
6563     // Finalize the clauses that need pre-built expressions for CodeGen.
6564     for (auto C : Clauses) {
6565       if (auto LC = dyn_cast<OMPLinearClause>(C))
6566         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6567                                      B.NumIterations, *this, CurScope,
6568                                      DSAStack))
6569           return StmtError();
6570     }
6571   }
6572 
6573   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6574   // The grainsize clause and num_tasks clause are mutually exclusive and may
6575   // not appear on the same taskloop directive.
6576   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6577     return StmtError();
6578 
6579   getCurFunction()->setHasBranchProtectedScope();
6580   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6581                                           NestedLoopCount, Clauses, AStmt, B);
6582 }
6583 
6584 StmtResult Sema::ActOnOpenMPDistributeDirective(
6585     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6586     SourceLocation EndLoc,
6587     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6588   if (!AStmt)
6589     return StmtError();
6590 
6591   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6592   OMPLoopDirective::HelperExprs B;
6593   // In presence of clause 'collapse' with number of loops, it will
6594   // define the nested loops number.
6595   unsigned NestedLoopCount =
6596       CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6597                       nullptr /*ordered not a clause on distribute*/, AStmt,
6598                       *this, *DSAStack, VarsWithImplicitDSA, B);
6599   if (NestedLoopCount == 0)
6600     return StmtError();
6601 
6602   assert((CurContext->isDependentContext() || B.builtAll()) &&
6603          "omp for loop exprs were not built");
6604 
6605   getCurFunction()->setHasBranchProtectedScope();
6606   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6607                                         NestedLoopCount, Clauses, AStmt, B);
6608 }
6609 
6610 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
6611                                              SourceLocation StartLoc,
6612                                              SourceLocation LParenLoc,
6613                                              SourceLocation EndLoc) {
6614   OMPClause *Res = nullptr;
6615   switch (Kind) {
6616   case OMPC_final:
6617     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6618     break;
6619   case OMPC_num_threads:
6620     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6621     break;
6622   case OMPC_safelen:
6623     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6624     break;
6625   case OMPC_simdlen:
6626     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6627     break;
6628   case OMPC_collapse:
6629     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6630     break;
6631   case OMPC_ordered:
6632     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6633     break;
6634   case OMPC_device:
6635     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6636     break;
6637   case OMPC_num_teams:
6638     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6639     break;
6640   case OMPC_thread_limit:
6641     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6642     break;
6643   case OMPC_priority:
6644     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6645     break;
6646   case OMPC_grainsize:
6647     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6648     break;
6649   case OMPC_num_tasks:
6650     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6651     break;
6652   case OMPC_hint:
6653     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6654     break;
6655   case OMPC_if:
6656   case OMPC_default:
6657   case OMPC_proc_bind:
6658   case OMPC_schedule:
6659   case OMPC_private:
6660   case OMPC_firstprivate:
6661   case OMPC_lastprivate:
6662   case OMPC_shared:
6663   case OMPC_reduction:
6664   case OMPC_linear:
6665   case OMPC_aligned:
6666   case OMPC_copyin:
6667   case OMPC_copyprivate:
6668   case OMPC_nowait:
6669   case OMPC_untied:
6670   case OMPC_mergeable:
6671   case OMPC_threadprivate:
6672   case OMPC_flush:
6673   case OMPC_read:
6674   case OMPC_write:
6675   case OMPC_update:
6676   case OMPC_capture:
6677   case OMPC_seq_cst:
6678   case OMPC_depend:
6679   case OMPC_threads:
6680   case OMPC_simd:
6681   case OMPC_map:
6682   case OMPC_nogroup:
6683   case OMPC_dist_schedule:
6684   case OMPC_defaultmap:
6685   case OMPC_unknown:
6686   case OMPC_uniform:
6687     llvm_unreachable("Clause is not allowed.");
6688   }
6689   return Res;
6690 }
6691 
6692 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6693                                      Expr *Condition, SourceLocation StartLoc,
6694                                      SourceLocation LParenLoc,
6695                                      SourceLocation NameModifierLoc,
6696                                      SourceLocation ColonLoc,
6697                                      SourceLocation EndLoc) {
6698   Expr *ValExpr = Condition;
6699   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6700       !Condition->isInstantiationDependent() &&
6701       !Condition->containsUnexpandedParameterPack()) {
6702     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6703                                            Condition->getExprLoc(), Condition);
6704     if (Val.isInvalid())
6705       return nullptr;
6706 
6707     ValExpr = Val.get();
6708   }
6709 
6710   return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6711                                    NameModifierLoc, ColonLoc, EndLoc);
6712 }
6713 
6714 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6715                                         SourceLocation StartLoc,
6716                                         SourceLocation LParenLoc,
6717                                         SourceLocation EndLoc) {
6718   Expr *ValExpr = Condition;
6719   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6720       !Condition->isInstantiationDependent() &&
6721       !Condition->containsUnexpandedParameterPack()) {
6722     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6723                                            Condition->getExprLoc(), Condition);
6724     if (Val.isInvalid())
6725       return nullptr;
6726 
6727     ValExpr = Val.get();
6728   }
6729 
6730   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6731 }
6732 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6733                                                         Expr *Op) {
6734   if (!Op)
6735     return ExprError();
6736 
6737   class IntConvertDiagnoser : public ICEConvertDiagnoser {
6738   public:
6739     IntConvertDiagnoser()
6740         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
6741     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6742                                          QualType T) override {
6743       return S.Diag(Loc, diag::err_omp_not_integral) << T;
6744     }
6745     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6746                                              QualType T) override {
6747       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6748     }
6749     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6750                                                QualType T,
6751                                                QualType ConvTy) override {
6752       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6753     }
6754     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6755                                            QualType ConvTy) override {
6756       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
6757              << ConvTy->isEnumeralType() << ConvTy;
6758     }
6759     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6760                                             QualType T) override {
6761       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6762     }
6763     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6764                                         QualType ConvTy) override {
6765       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
6766              << ConvTy->isEnumeralType() << ConvTy;
6767     }
6768     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6769                                              QualType) override {
6770       llvm_unreachable("conversion functions are permitted");
6771     }
6772   } ConvertDiagnoser;
6773   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6774 }
6775 
6776 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
6777                                       OpenMPClauseKind CKind,
6778                                       bool StrictlyPositive) {
6779   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6780       !ValExpr->isInstantiationDependent()) {
6781     SourceLocation Loc = ValExpr->getExprLoc();
6782     ExprResult Value =
6783         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6784     if (Value.isInvalid())
6785       return false;
6786 
6787     ValExpr = Value.get();
6788     // The expression must evaluate to a non-negative integer value.
6789     llvm::APSInt Result;
6790     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
6791         Result.isSigned() &&
6792         !((!StrictlyPositive && Result.isNonNegative()) ||
6793           (StrictlyPositive && Result.isStrictlyPositive()))) {
6794       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
6795           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6796           << ValExpr->getSourceRange();
6797       return false;
6798     }
6799   }
6800   return true;
6801 }
6802 
6803 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6804                                              SourceLocation StartLoc,
6805                                              SourceLocation LParenLoc,
6806                                              SourceLocation EndLoc) {
6807   Expr *ValExpr = NumThreads;
6808 
6809   // OpenMP [2.5, Restrictions]
6810   //  The num_threads expression must evaluate to a positive integer value.
6811   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6812                                  /*StrictlyPositive=*/true))
6813     return nullptr;
6814 
6815   return new (Context)
6816       OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6817 }
6818 
6819 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
6820                                                        OpenMPClauseKind CKind,
6821                                                        bool StrictlyPositive) {
6822   if (!E)
6823     return ExprError();
6824   if (E->isValueDependent() || E->isTypeDependent() ||
6825       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
6826     return E;
6827   llvm::APSInt Result;
6828   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6829   if (ICE.isInvalid())
6830     return ExprError();
6831   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6832       (!StrictlyPositive && !Result.isNonNegative())) {
6833     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
6834         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6835         << E->getSourceRange();
6836     return ExprError();
6837   }
6838   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6839     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6840         << E->getSourceRange();
6841     return ExprError();
6842   }
6843   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6844     DSAStack->setAssociatedLoops(Result.getExtValue());
6845   else if (CKind == OMPC_ordered)
6846     DSAStack->setAssociatedLoops(Result.getExtValue());
6847   return ICE;
6848 }
6849 
6850 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6851                                           SourceLocation LParenLoc,
6852                                           SourceLocation EndLoc) {
6853   // OpenMP [2.8.1, simd construct, Description]
6854   // The parameter of the safelen clause must be a constant
6855   // positive integer expression.
6856   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6857   if (Safelen.isInvalid())
6858     return nullptr;
6859   return new (Context)
6860       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
6861 }
6862 
6863 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6864                                           SourceLocation LParenLoc,
6865                                           SourceLocation EndLoc) {
6866   // OpenMP [2.8.1, simd construct, Description]
6867   // The parameter of the simdlen clause must be a constant
6868   // positive integer expression.
6869   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6870   if (Simdlen.isInvalid())
6871     return nullptr;
6872   return new (Context)
6873       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6874 }
6875 
6876 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6877                                            SourceLocation StartLoc,
6878                                            SourceLocation LParenLoc,
6879                                            SourceLocation EndLoc) {
6880   // OpenMP [2.7.1, loop construct, Description]
6881   // OpenMP [2.8.1, simd construct, Description]
6882   // OpenMP [2.9.6, distribute construct, Description]
6883   // The parameter of the collapse clause must be a constant
6884   // positive integer expression.
6885   ExprResult NumForLoopsResult =
6886       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6887   if (NumForLoopsResult.isInvalid())
6888     return nullptr;
6889   return new (Context)
6890       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
6891 }
6892 
6893 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6894                                           SourceLocation EndLoc,
6895                                           SourceLocation LParenLoc,
6896                                           Expr *NumForLoops) {
6897   // OpenMP [2.7.1, loop construct, Description]
6898   // OpenMP [2.8.1, simd construct, Description]
6899   // OpenMP [2.9.6, distribute construct, Description]
6900   // The parameter of the ordered clause must be a constant
6901   // positive integer expression if any.
6902   if (NumForLoops && LParenLoc.isValid()) {
6903     ExprResult NumForLoopsResult =
6904         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6905     if (NumForLoopsResult.isInvalid())
6906       return nullptr;
6907     NumForLoops = NumForLoopsResult.get();
6908   } else
6909     NumForLoops = nullptr;
6910   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
6911   return new (Context)
6912       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6913 }
6914 
6915 OMPClause *Sema::ActOnOpenMPSimpleClause(
6916     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6917     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
6918   OMPClause *Res = nullptr;
6919   switch (Kind) {
6920   case OMPC_default:
6921     Res =
6922         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6923                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
6924     break;
6925   case OMPC_proc_bind:
6926     Res = ActOnOpenMPProcBindClause(
6927         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6928         LParenLoc, EndLoc);
6929     break;
6930   case OMPC_if:
6931   case OMPC_final:
6932   case OMPC_num_threads:
6933   case OMPC_safelen:
6934   case OMPC_simdlen:
6935   case OMPC_collapse:
6936   case OMPC_schedule:
6937   case OMPC_private:
6938   case OMPC_firstprivate:
6939   case OMPC_lastprivate:
6940   case OMPC_shared:
6941   case OMPC_reduction:
6942   case OMPC_linear:
6943   case OMPC_aligned:
6944   case OMPC_copyin:
6945   case OMPC_copyprivate:
6946   case OMPC_ordered:
6947   case OMPC_nowait:
6948   case OMPC_untied:
6949   case OMPC_mergeable:
6950   case OMPC_threadprivate:
6951   case OMPC_flush:
6952   case OMPC_read:
6953   case OMPC_write:
6954   case OMPC_update:
6955   case OMPC_capture:
6956   case OMPC_seq_cst:
6957   case OMPC_depend:
6958   case OMPC_device:
6959   case OMPC_threads:
6960   case OMPC_simd:
6961   case OMPC_map:
6962   case OMPC_num_teams:
6963   case OMPC_thread_limit:
6964   case OMPC_priority:
6965   case OMPC_grainsize:
6966   case OMPC_nogroup:
6967   case OMPC_num_tasks:
6968   case OMPC_hint:
6969   case OMPC_dist_schedule:
6970   case OMPC_defaultmap:
6971   case OMPC_unknown:
6972   case OMPC_uniform:
6973     llvm_unreachable("Clause is not allowed.");
6974   }
6975   return Res;
6976 }
6977 
6978 static std::string
6979 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6980                         ArrayRef<unsigned> Exclude = llvm::None) {
6981   std::string Values;
6982   unsigned Bound = Last >= 2 ? Last - 2 : 0;
6983   unsigned Skipped = Exclude.size();
6984   auto S = Exclude.begin(), E = Exclude.end();
6985   for (unsigned i = First; i < Last; ++i) {
6986     if (std::find(S, E, i) != E) {
6987       --Skipped;
6988       continue;
6989     }
6990     Values += "'";
6991     Values += getOpenMPSimpleClauseTypeName(K, i);
6992     Values += "'";
6993     if (i == Bound - Skipped)
6994       Values += " or ";
6995     else if (i != Bound + 1 - Skipped)
6996       Values += ", ";
6997   }
6998   return Values;
6999 }
7000 
7001 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7002                                           SourceLocation KindKwLoc,
7003                                           SourceLocation StartLoc,
7004                                           SourceLocation LParenLoc,
7005                                           SourceLocation EndLoc) {
7006   if (Kind == OMPC_DEFAULT_unknown) {
7007     static_assert(OMPC_DEFAULT_unknown > 0,
7008                   "OMPC_DEFAULT_unknown not greater than 0");
7009     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7010         << getListOfPossibleValues(OMPC_default, /*First=*/0,
7011                                    /*Last=*/OMPC_DEFAULT_unknown)
7012         << getOpenMPClauseName(OMPC_default);
7013     return nullptr;
7014   }
7015   switch (Kind) {
7016   case OMPC_DEFAULT_none:
7017     DSAStack->setDefaultDSANone(KindKwLoc);
7018     break;
7019   case OMPC_DEFAULT_shared:
7020     DSAStack->setDefaultDSAShared(KindKwLoc);
7021     break;
7022   case OMPC_DEFAULT_unknown:
7023     llvm_unreachable("Clause kind is not allowed.");
7024     break;
7025   }
7026   return new (Context)
7027       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7028 }
7029 
7030 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7031                                            SourceLocation KindKwLoc,
7032                                            SourceLocation StartLoc,
7033                                            SourceLocation LParenLoc,
7034                                            SourceLocation EndLoc) {
7035   if (Kind == OMPC_PROC_BIND_unknown) {
7036     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7037         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7038                                    /*Last=*/OMPC_PROC_BIND_unknown)
7039         << getOpenMPClauseName(OMPC_proc_bind);
7040     return nullptr;
7041   }
7042   return new (Context)
7043       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7044 }
7045 
7046 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
7047     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
7048     SourceLocation StartLoc, SourceLocation LParenLoc,
7049     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
7050     SourceLocation EndLoc) {
7051   OMPClause *Res = nullptr;
7052   switch (Kind) {
7053   case OMPC_schedule:
7054     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7055     assert(Argument.size() == NumberOfElements &&
7056            ArgumentLoc.size() == NumberOfElements);
7057     Res = ActOnOpenMPScheduleClause(
7058         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7059         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7060         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7061         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7062         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
7063     break;
7064   case OMPC_if:
7065     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7066     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7067                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7068                               DelimLoc, EndLoc);
7069     break;
7070   case OMPC_dist_schedule:
7071     Res = ActOnOpenMPDistScheduleClause(
7072         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7073         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7074     break;
7075   case OMPC_defaultmap:
7076     enum { Modifier, DefaultmapKind };
7077     Res = ActOnOpenMPDefaultmapClause(
7078         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7079         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7080         StartLoc, LParenLoc, ArgumentLoc[Modifier],
7081         ArgumentLoc[DefaultmapKind], EndLoc);
7082     break;
7083   case OMPC_final:
7084   case OMPC_num_threads:
7085   case OMPC_safelen:
7086   case OMPC_simdlen:
7087   case OMPC_collapse:
7088   case OMPC_default:
7089   case OMPC_proc_bind:
7090   case OMPC_private:
7091   case OMPC_firstprivate:
7092   case OMPC_lastprivate:
7093   case OMPC_shared:
7094   case OMPC_reduction:
7095   case OMPC_linear:
7096   case OMPC_aligned:
7097   case OMPC_copyin:
7098   case OMPC_copyprivate:
7099   case OMPC_ordered:
7100   case OMPC_nowait:
7101   case OMPC_untied:
7102   case OMPC_mergeable:
7103   case OMPC_threadprivate:
7104   case OMPC_flush:
7105   case OMPC_read:
7106   case OMPC_write:
7107   case OMPC_update:
7108   case OMPC_capture:
7109   case OMPC_seq_cst:
7110   case OMPC_depend:
7111   case OMPC_device:
7112   case OMPC_threads:
7113   case OMPC_simd:
7114   case OMPC_map:
7115   case OMPC_num_teams:
7116   case OMPC_thread_limit:
7117   case OMPC_priority:
7118   case OMPC_grainsize:
7119   case OMPC_nogroup:
7120   case OMPC_num_tasks:
7121   case OMPC_hint:
7122   case OMPC_unknown:
7123   case OMPC_uniform:
7124     llvm_unreachable("Clause is not allowed.");
7125   }
7126   return Res;
7127 }
7128 
7129 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7130                                    OpenMPScheduleClauseModifier M2,
7131                                    SourceLocation M1Loc, SourceLocation M2Loc) {
7132   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7133     SmallVector<unsigned, 2> Excluded;
7134     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7135       Excluded.push_back(M2);
7136     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7137       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7138     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7139       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7140     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7141         << getListOfPossibleValues(OMPC_schedule,
7142                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7143                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7144                                    Excluded)
7145         << getOpenMPClauseName(OMPC_schedule);
7146     return true;
7147   }
7148   return false;
7149 }
7150 
7151 OMPClause *Sema::ActOnOpenMPScheduleClause(
7152     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
7153     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
7154     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7155     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7156   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7157       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7158     return nullptr;
7159   // OpenMP, 2.7.1, Loop Construct, Restrictions
7160   // Either the monotonic modifier or the nonmonotonic modifier can be specified
7161   // but not both.
7162   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7163       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7164        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7165       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7166        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7167     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7168         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7169         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7170     return nullptr;
7171   }
7172   if (Kind == OMPC_SCHEDULE_unknown) {
7173     std::string Values;
7174     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7175       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7176       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7177                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7178                                        Exclude);
7179     } else {
7180       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7181                                        /*Last=*/OMPC_SCHEDULE_unknown);
7182     }
7183     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7184         << Values << getOpenMPClauseName(OMPC_schedule);
7185     return nullptr;
7186   }
7187   // OpenMP, 2.7.1, Loop Construct, Restrictions
7188   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7189   // schedule(guided).
7190   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7191        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7192       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7193     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7194          diag::err_omp_schedule_nonmonotonic_static);
7195     return nullptr;
7196   }
7197   Expr *ValExpr = ChunkSize;
7198   Stmt *HelperValStmt = nullptr;
7199   if (ChunkSize) {
7200     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7201         !ChunkSize->isInstantiationDependent() &&
7202         !ChunkSize->containsUnexpandedParameterPack()) {
7203       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7204       ExprResult Val =
7205           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7206       if (Val.isInvalid())
7207         return nullptr;
7208 
7209       ValExpr = Val.get();
7210 
7211       // OpenMP [2.7.1, Restrictions]
7212       //  chunk_size must be a loop invariant integer expression with a positive
7213       //  value.
7214       llvm::APSInt Result;
7215       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7216         if (Result.isSigned() && !Result.isStrictlyPositive()) {
7217           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
7218               << "schedule" << 1 << ChunkSize->getSourceRange();
7219           return nullptr;
7220         }
7221       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
7222         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7223         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7224         HelperValStmt = buildPreInits(Context, Captures);
7225       }
7226     }
7227   }
7228 
7229   return new (Context)
7230       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
7231                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
7232 }
7233 
7234 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7235                                    SourceLocation StartLoc,
7236                                    SourceLocation EndLoc) {
7237   OMPClause *Res = nullptr;
7238   switch (Kind) {
7239   case OMPC_ordered:
7240     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7241     break;
7242   case OMPC_nowait:
7243     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7244     break;
7245   case OMPC_untied:
7246     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7247     break;
7248   case OMPC_mergeable:
7249     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7250     break;
7251   case OMPC_read:
7252     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7253     break;
7254   case OMPC_write:
7255     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7256     break;
7257   case OMPC_update:
7258     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7259     break;
7260   case OMPC_capture:
7261     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7262     break;
7263   case OMPC_seq_cst:
7264     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7265     break;
7266   case OMPC_threads:
7267     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7268     break;
7269   case OMPC_simd:
7270     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7271     break;
7272   case OMPC_nogroup:
7273     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7274     break;
7275   case OMPC_if:
7276   case OMPC_final:
7277   case OMPC_num_threads:
7278   case OMPC_safelen:
7279   case OMPC_simdlen:
7280   case OMPC_collapse:
7281   case OMPC_schedule:
7282   case OMPC_private:
7283   case OMPC_firstprivate:
7284   case OMPC_lastprivate:
7285   case OMPC_shared:
7286   case OMPC_reduction:
7287   case OMPC_linear:
7288   case OMPC_aligned:
7289   case OMPC_copyin:
7290   case OMPC_copyprivate:
7291   case OMPC_default:
7292   case OMPC_proc_bind:
7293   case OMPC_threadprivate:
7294   case OMPC_flush:
7295   case OMPC_depend:
7296   case OMPC_device:
7297   case OMPC_map:
7298   case OMPC_num_teams:
7299   case OMPC_thread_limit:
7300   case OMPC_priority:
7301   case OMPC_grainsize:
7302   case OMPC_num_tasks:
7303   case OMPC_hint:
7304   case OMPC_dist_schedule:
7305   case OMPC_defaultmap:
7306   case OMPC_unknown:
7307   case OMPC_uniform:
7308     llvm_unreachable("Clause is not allowed.");
7309   }
7310   return Res;
7311 }
7312 
7313 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7314                                          SourceLocation EndLoc) {
7315   DSAStack->setNowaitRegion();
7316   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7317 }
7318 
7319 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7320                                          SourceLocation EndLoc) {
7321   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7322 }
7323 
7324 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7325                                             SourceLocation EndLoc) {
7326   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7327 }
7328 
7329 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7330                                        SourceLocation EndLoc) {
7331   return new (Context) OMPReadClause(StartLoc, EndLoc);
7332 }
7333 
7334 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7335                                         SourceLocation EndLoc) {
7336   return new (Context) OMPWriteClause(StartLoc, EndLoc);
7337 }
7338 
7339 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7340                                          SourceLocation EndLoc) {
7341   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7342 }
7343 
7344 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7345                                           SourceLocation EndLoc) {
7346   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7347 }
7348 
7349 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7350                                          SourceLocation EndLoc) {
7351   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7352 }
7353 
7354 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7355                                           SourceLocation EndLoc) {
7356   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7357 }
7358 
7359 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7360                                        SourceLocation EndLoc) {
7361   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7362 }
7363 
7364 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7365                                           SourceLocation EndLoc) {
7366   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7367 }
7368 
7369 OMPClause *Sema::ActOnOpenMPVarListClause(
7370     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7371     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7372     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
7373     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
7374     OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7375     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7376     SourceLocation DepLinMapLoc) {
7377   OMPClause *Res = nullptr;
7378   switch (Kind) {
7379   case OMPC_private:
7380     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7381     break;
7382   case OMPC_firstprivate:
7383     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7384     break;
7385   case OMPC_lastprivate:
7386     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7387     break;
7388   case OMPC_shared:
7389     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7390     break;
7391   case OMPC_reduction:
7392     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7393                                      EndLoc, ReductionIdScopeSpec, ReductionId);
7394     break;
7395   case OMPC_linear:
7396     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
7397                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
7398     break;
7399   case OMPC_aligned:
7400     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7401                                    ColonLoc, EndLoc);
7402     break;
7403   case OMPC_copyin:
7404     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7405     break;
7406   case OMPC_copyprivate:
7407     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7408     break;
7409   case OMPC_flush:
7410     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7411     break;
7412   case OMPC_depend:
7413     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7414                                   StartLoc, LParenLoc, EndLoc);
7415     break;
7416   case OMPC_map:
7417     Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7418                                DepLinMapLoc, ColonLoc, VarList, StartLoc,
7419                                LParenLoc, EndLoc);
7420     break;
7421   case OMPC_if:
7422   case OMPC_final:
7423   case OMPC_num_threads:
7424   case OMPC_safelen:
7425   case OMPC_simdlen:
7426   case OMPC_collapse:
7427   case OMPC_default:
7428   case OMPC_proc_bind:
7429   case OMPC_schedule:
7430   case OMPC_ordered:
7431   case OMPC_nowait:
7432   case OMPC_untied:
7433   case OMPC_mergeable:
7434   case OMPC_threadprivate:
7435   case OMPC_read:
7436   case OMPC_write:
7437   case OMPC_update:
7438   case OMPC_capture:
7439   case OMPC_seq_cst:
7440   case OMPC_device:
7441   case OMPC_threads:
7442   case OMPC_simd:
7443   case OMPC_num_teams:
7444   case OMPC_thread_limit:
7445   case OMPC_priority:
7446   case OMPC_grainsize:
7447   case OMPC_nogroup:
7448   case OMPC_num_tasks:
7449   case OMPC_hint:
7450   case OMPC_dist_schedule:
7451   case OMPC_defaultmap:
7452   case OMPC_unknown:
7453   case OMPC_uniform:
7454     llvm_unreachable("Clause is not allowed.");
7455   }
7456   return Res;
7457 }
7458 
7459 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
7460                                        ExprObjectKind OK, SourceLocation Loc) {
7461   ExprResult Res = BuildDeclRefExpr(
7462       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7463   if (!Res.isUsable())
7464     return ExprError();
7465   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7466     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7467     if (!Res.isUsable())
7468       return ExprError();
7469   }
7470   if (VK != VK_LValue && Res.get()->isGLValue()) {
7471     Res = DefaultLvalueConversion(Res.get());
7472     if (!Res.isUsable())
7473       return ExprError();
7474   }
7475   return Res;
7476 }
7477 
7478 static std::pair<ValueDecl *, bool>
7479 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7480                SourceRange &ERange, bool AllowArraySection = false) {
7481   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7482       RefExpr->containsUnexpandedParameterPack())
7483     return std::make_pair(nullptr, true);
7484 
7485   // OpenMP [3.1, C/C++]
7486   //  A list item is a variable name.
7487   // OpenMP  [2.9.3.3, Restrictions, p.1]
7488   //  A variable that is part of another variable (as an array or
7489   //  structure element) cannot appear in a private clause.
7490   RefExpr = RefExpr->IgnoreParens();
7491   enum {
7492     NoArrayExpr = -1,
7493     ArraySubscript = 0,
7494     OMPArraySection = 1
7495   } IsArrayExpr = NoArrayExpr;
7496   if (AllowArraySection) {
7497     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7498       auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7499       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7500         Base = TempASE->getBase()->IgnoreParenImpCasts();
7501       RefExpr = Base;
7502       IsArrayExpr = ArraySubscript;
7503     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7504       auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7505       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7506         Base = TempOASE->getBase()->IgnoreParenImpCasts();
7507       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7508         Base = TempASE->getBase()->IgnoreParenImpCasts();
7509       RefExpr = Base;
7510       IsArrayExpr = OMPArraySection;
7511     }
7512   }
7513   ELoc = RefExpr->getExprLoc();
7514   ERange = RefExpr->getSourceRange();
7515   RefExpr = RefExpr->IgnoreParenImpCasts();
7516   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7517   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7518   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7519       (S.getCurrentThisType().isNull() || !ME ||
7520        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7521        !isa<FieldDecl>(ME->getMemberDecl()))) {
7522     if (IsArrayExpr != NoArrayExpr)
7523       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7524                                                          << ERange;
7525     else {
7526       S.Diag(ELoc,
7527              AllowArraySection
7528                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
7529                  : diag::err_omp_expected_var_name_member_expr)
7530           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7531     }
7532     return std::make_pair(nullptr, false);
7533   }
7534   return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7535 }
7536 
7537 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7538                                           SourceLocation StartLoc,
7539                                           SourceLocation LParenLoc,
7540                                           SourceLocation EndLoc) {
7541   SmallVector<Expr *, 8> Vars;
7542   SmallVector<Expr *, 8> PrivateCopies;
7543   for (auto &RefExpr : VarList) {
7544     assert(RefExpr && "NULL expr in OpenMP private clause.");
7545     SourceLocation ELoc;
7546     SourceRange ERange;
7547     Expr *SimpleRefExpr = RefExpr;
7548     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
7549     if (Res.second) {
7550       // It will be analyzed later.
7551       Vars.push_back(RefExpr);
7552       PrivateCopies.push_back(nullptr);
7553     }
7554     ValueDecl *D = Res.first;
7555     if (!D)
7556       continue;
7557 
7558     QualType Type = D->getType();
7559     auto *VD = dyn_cast<VarDecl>(D);
7560 
7561     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7562     //  A variable that appears in a private clause must not have an incomplete
7563     //  type or a reference type.
7564     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
7565       continue;
7566     Type = Type.getNonReferenceType();
7567 
7568     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7569     // in a Construct]
7570     //  Variables with the predetermined data-sharing attributes may not be
7571     //  listed in data-sharing attributes clauses, except for the cases
7572     //  listed below. For these exceptions only, listing a predetermined
7573     //  variable in a data-sharing attribute clause is allowed and overrides
7574     //  the variable's predetermined data-sharing attributes.
7575     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7576     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
7577       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7578                                           << getOpenMPClauseName(OMPC_private);
7579       ReportOriginalDSA(*this, DSAStack, D, DVar);
7580       continue;
7581     }
7582 
7583     // Variably modified types are not supported for tasks.
7584     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
7585         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
7586       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7587           << getOpenMPClauseName(OMPC_private) << Type
7588           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7589       bool IsDecl =
7590           !VD ||
7591           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7592       Diag(D->getLocation(),
7593            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7594           << D;
7595       continue;
7596     }
7597 
7598     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7599     // A list item cannot appear in both a map clause and a data-sharing
7600     // attribute clause on the same construct
7601     if (DSAStack->getCurrentDirective() == OMPD_target) {
7602       if (DSAStack->checkMappableExprComponentListsForDecl(
7603               VD, /* CurrentRegionOnly = */ true,
7604               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7605                   -> bool { return true; })) {
7606         Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7607             << getOpenMPClauseName(OMPC_private)
7608             << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7609         ReportOriginalDSA(*this, DSAStack, D, DVar);
7610         continue;
7611       }
7612     }
7613 
7614     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7615     //  A variable of class type (or array thereof) that appears in a private
7616     //  clause requires an accessible, unambiguous default constructor for the
7617     //  class type.
7618     // Generate helper private variable and initialize it with the default
7619     // value. The address of the original variable is replaced by the address of
7620     // the new private variable in CodeGen. This new variable is not added to
7621     // IdResolver, so the code in the OpenMP region uses original variable for
7622     // proper diagnostics.
7623     Type = Type.getUnqualifiedType();
7624     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7625                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
7626     ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
7627     if (VDPrivate->isInvalidDecl())
7628       continue;
7629     auto VDPrivateRefExpr = buildDeclRefExpr(
7630         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
7631 
7632     DeclRefExpr *Ref = nullptr;
7633     if (!VD)
7634       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
7635     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7636     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
7637     PrivateCopies.push_back(VDPrivateRefExpr);
7638   }
7639 
7640   if (Vars.empty())
7641     return nullptr;
7642 
7643   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7644                                   PrivateCopies);
7645 }
7646 
7647 namespace {
7648 class DiagsUninitializedSeveretyRAII {
7649 private:
7650   DiagnosticsEngine &Diags;
7651   SourceLocation SavedLoc;
7652   bool IsIgnored;
7653 
7654 public:
7655   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7656                                  bool IsIgnored)
7657       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7658     if (!IsIgnored) {
7659       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7660                         /*Map*/ diag::Severity::Ignored, Loc);
7661     }
7662   }
7663   ~DiagsUninitializedSeveretyRAII() {
7664     if (!IsIgnored)
7665       Diags.popMappings(SavedLoc);
7666   }
7667 };
7668 }
7669 
7670 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7671                                                SourceLocation StartLoc,
7672                                                SourceLocation LParenLoc,
7673                                                SourceLocation EndLoc) {
7674   SmallVector<Expr *, 8> Vars;
7675   SmallVector<Expr *, 8> PrivateCopies;
7676   SmallVector<Expr *, 8> Inits;
7677   SmallVector<Decl *, 4> ExprCaptures;
7678   bool IsImplicitClause =
7679       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7680   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7681 
7682   for (auto &RefExpr : VarList) {
7683     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
7684     SourceLocation ELoc;
7685     SourceRange ERange;
7686     Expr *SimpleRefExpr = RefExpr;
7687     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
7688     if (Res.second) {
7689       // It will be analyzed later.
7690       Vars.push_back(RefExpr);
7691       PrivateCopies.push_back(nullptr);
7692       Inits.push_back(nullptr);
7693     }
7694     ValueDecl *D = Res.first;
7695     if (!D)
7696       continue;
7697 
7698     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
7699     QualType Type = D->getType();
7700     auto *VD = dyn_cast<VarDecl>(D);
7701 
7702     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7703     //  A variable that appears in a private clause must not have an incomplete
7704     //  type or a reference type.
7705     if (RequireCompleteType(ELoc, Type,
7706                             diag::err_omp_firstprivate_incomplete_type))
7707       continue;
7708     Type = Type.getNonReferenceType();
7709 
7710     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7711     //  A variable of class type (or array thereof) that appears in a private
7712     //  clause requires an accessible, unambiguous copy constructor for the
7713     //  class type.
7714     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
7715 
7716     // If an implicit firstprivate variable found it was checked already.
7717     DSAStackTy::DSAVarData TopDVar;
7718     if (!IsImplicitClause) {
7719       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7720       TopDVar = DVar;
7721       bool IsConstant = ElemType.isConstant(Context);
7722       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7723       //  A list item that specifies a given variable may not appear in more
7724       // than one clause on the same directive, except that a variable may be
7725       //  specified in both firstprivate and lastprivate clauses.
7726       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
7727           DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
7728         Diag(ELoc, diag::err_omp_wrong_dsa)
7729             << getOpenMPClauseName(DVar.CKind)
7730             << getOpenMPClauseName(OMPC_firstprivate);
7731         ReportOriginalDSA(*this, DSAStack, D, DVar);
7732         continue;
7733       }
7734 
7735       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7736       // in a Construct]
7737       //  Variables with the predetermined data-sharing attributes may not be
7738       //  listed in data-sharing attributes clauses, except for the cases
7739       //  listed below. For these exceptions only, listing a predetermined
7740       //  variable in a data-sharing attribute clause is allowed and overrides
7741       //  the variable's predetermined data-sharing attributes.
7742       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7743       // in a Construct, C/C++, p.2]
7744       //  Variables with const-qualified type having no mutable member may be
7745       //  listed in a firstprivate clause, even if they are static data members.
7746       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
7747           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7748         Diag(ELoc, diag::err_omp_wrong_dsa)
7749             << getOpenMPClauseName(DVar.CKind)
7750             << getOpenMPClauseName(OMPC_firstprivate);
7751         ReportOriginalDSA(*this, DSAStack, D, DVar);
7752         continue;
7753       }
7754 
7755       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7756       // OpenMP [2.9.3.4, Restrictions, p.2]
7757       //  A list item that is private within a parallel region must not appear
7758       //  in a firstprivate clause on a worksharing construct if any of the
7759       //  worksharing regions arising from the worksharing construct ever bind
7760       //  to any of the parallel regions arising from the parallel construct.
7761       if (isOpenMPWorksharingDirective(CurrDir) &&
7762           !isOpenMPParallelDirective(CurrDir)) {
7763         DVar = DSAStack->getImplicitDSA(D, true);
7764         if (DVar.CKind != OMPC_shared &&
7765             (isOpenMPParallelDirective(DVar.DKind) ||
7766              DVar.DKind == OMPD_unknown)) {
7767           Diag(ELoc, diag::err_omp_required_access)
7768               << getOpenMPClauseName(OMPC_firstprivate)
7769               << getOpenMPClauseName(OMPC_shared);
7770           ReportOriginalDSA(*this, DSAStack, D, DVar);
7771           continue;
7772         }
7773       }
7774       // OpenMP [2.9.3.4, Restrictions, p.3]
7775       //  A list item that appears in a reduction clause of a parallel construct
7776       //  must not appear in a firstprivate clause on a worksharing or task
7777       //  construct if any of the worksharing or task regions arising from the
7778       //  worksharing or task construct ever bind to any of the parallel regions
7779       //  arising from the parallel construct.
7780       // OpenMP [2.9.3.4, Restrictions, p.4]
7781       //  A list item that appears in a reduction clause in worksharing
7782       //  construct must not appear in a firstprivate clause in a task construct
7783       //  encountered during execution of any of the worksharing regions arising
7784       //  from the worksharing construct.
7785       if (isOpenMPTaskingDirective(CurrDir)) {
7786         DVar =
7787             DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
7788                                       [](OpenMPDirectiveKind K) -> bool {
7789                                         return isOpenMPParallelDirective(K) ||
7790                                                isOpenMPWorksharingDirective(K);
7791                                       },
7792                                       false);
7793         if (DVar.CKind == OMPC_reduction &&
7794             (isOpenMPParallelDirective(DVar.DKind) ||
7795              isOpenMPWorksharingDirective(DVar.DKind))) {
7796           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7797               << getOpenMPDirectiveName(DVar.DKind);
7798           ReportOriginalDSA(*this, DSAStack, D, DVar);
7799           continue;
7800         }
7801       }
7802 
7803       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7804       // A list item that is private within a teams region must not appear in a
7805       // firstprivate clause on a distribute construct if any of the distribute
7806       // regions arising from the distribute construct ever bind to any of the
7807       // teams regions arising from the teams construct.
7808       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7809       // A list item that appears in a reduction clause of a teams construct
7810       // must not appear in a firstprivate clause on a distribute construct if
7811       // any of the distribute regions arising from the distribute construct
7812       // ever bind to any of the teams regions arising from the teams construct.
7813       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7814       // A list item may appear in a firstprivate or lastprivate clause but not
7815       // both.
7816       if (CurrDir == OMPD_distribute) {
7817         DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
7818                                          [](OpenMPDirectiveKind K) -> bool {
7819                                            return isOpenMPTeamsDirective(K);
7820                                          },
7821                                          false);
7822         if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7823           Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7824           ReportOriginalDSA(*this, DSAStack, D, DVar);
7825           continue;
7826         }
7827         DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
7828                                          [](OpenMPDirectiveKind K) -> bool {
7829                                            return isOpenMPTeamsDirective(K);
7830                                          },
7831                                          false);
7832         if (DVar.CKind == OMPC_reduction &&
7833             isOpenMPTeamsDirective(DVar.DKind)) {
7834           Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7835           ReportOriginalDSA(*this, DSAStack, D, DVar);
7836           continue;
7837         }
7838         DVar = DSAStack->getTopDSA(D, false);
7839         if (DVar.CKind == OMPC_lastprivate) {
7840           Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7841           ReportOriginalDSA(*this, DSAStack, D, DVar);
7842           continue;
7843         }
7844       }
7845       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7846       // A list item cannot appear in both a map clause and a data-sharing
7847       // attribute clause on the same construct
7848       if (CurrDir == OMPD_target) {
7849         if (DSAStack->checkMappableExprComponentListsForDecl(
7850                 VD, /* CurrentRegionOnly = */ true,
7851                 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7852                     -> bool { return true; })) {
7853           Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7854               << getOpenMPClauseName(OMPC_firstprivate)
7855               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7856           ReportOriginalDSA(*this, DSAStack, D, DVar);
7857           continue;
7858         }
7859       }
7860     }
7861 
7862     // Variably modified types are not supported for tasks.
7863     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
7864         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
7865       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7866           << getOpenMPClauseName(OMPC_firstprivate) << Type
7867           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7868       bool IsDecl =
7869           !VD ||
7870           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7871       Diag(D->getLocation(),
7872            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7873           << D;
7874       continue;
7875     }
7876 
7877     Type = Type.getUnqualifiedType();
7878     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7879                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
7880     // Generate helper private variable and initialize it with the value of the
7881     // original variable. The address of the original variable is replaced by
7882     // the address of the new private variable in the CodeGen. This new variable
7883     // is not added to IdResolver, so the code in the OpenMP region uses
7884     // original variable for proper diagnostics and variable capturing.
7885     Expr *VDInitRefExpr = nullptr;
7886     // For arrays generate initializer for single element and replace it by the
7887     // original array element in CodeGen.
7888     if (Type->isArrayType()) {
7889       auto VDInit =
7890           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
7891       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
7892       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
7893       ElemType = ElemType.getUnqualifiedType();
7894       auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
7895                                       ".firstprivate.temp");
7896       InitializedEntity Entity =
7897           InitializedEntity::InitializeVariable(VDInitTemp);
7898       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7899 
7900       InitializationSequence InitSeq(*this, Entity, Kind, Init);
7901       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7902       if (Result.isInvalid())
7903         VDPrivate->setInvalidDecl();
7904       else
7905         VDPrivate->setInit(Result.getAs<Expr>());
7906       // Remove temp variable declaration.
7907       Context.Deallocate(VDInitTemp);
7908     } else {
7909       auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7910                                   ".firstprivate.temp");
7911       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7912                                        RefExpr->getExprLoc());
7913       AddInitializerToDecl(VDPrivate,
7914                            DefaultLvalueConversion(VDInitRefExpr).get(),
7915                            /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
7916     }
7917     if (VDPrivate->isInvalidDecl()) {
7918       if (IsImplicitClause) {
7919         Diag(RefExpr->getExprLoc(),
7920              diag::note_omp_task_predetermined_firstprivate_here);
7921       }
7922       continue;
7923     }
7924     CurContext->addDecl(VDPrivate);
7925     auto VDPrivateRefExpr = buildDeclRefExpr(
7926         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7927         RefExpr->getExprLoc());
7928     DeclRefExpr *Ref = nullptr;
7929     if (!VD) {
7930       if (TopDVar.CKind == OMPC_lastprivate)
7931         Ref = TopDVar.PrivateCopy;
7932       else {
7933         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
7934         if (!IsOpenMPCapturedDecl(D))
7935           ExprCaptures.push_back(Ref->getDecl());
7936       }
7937     }
7938     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7939     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
7940     PrivateCopies.push_back(VDPrivateRefExpr);
7941     Inits.push_back(VDInitRefExpr);
7942   }
7943 
7944   if (Vars.empty())
7945     return nullptr;
7946 
7947   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7948                                        Vars, PrivateCopies, Inits,
7949                                        buildPreInits(Context, ExprCaptures));
7950 }
7951 
7952 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7953                                               SourceLocation StartLoc,
7954                                               SourceLocation LParenLoc,
7955                                               SourceLocation EndLoc) {
7956   SmallVector<Expr *, 8> Vars;
7957   SmallVector<Expr *, 8> SrcExprs;
7958   SmallVector<Expr *, 8> DstExprs;
7959   SmallVector<Expr *, 8> AssignmentOps;
7960   SmallVector<Decl *, 4> ExprCaptures;
7961   SmallVector<Expr *, 4> ExprPostUpdates;
7962   for (auto &RefExpr : VarList) {
7963     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7964     SourceLocation ELoc;
7965     SourceRange ERange;
7966     Expr *SimpleRefExpr = RefExpr;
7967     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
7968     if (Res.second) {
7969       // It will be analyzed later.
7970       Vars.push_back(RefExpr);
7971       SrcExprs.push_back(nullptr);
7972       DstExprs.push_back(nullptr);
7973       AssignmentOps.push_back(nullptr);
7974     }
7975     ValueDecl *D = Res.first;
7976     if (!D)
7977       continue;
7978 
7979     QualType Type = D->getType();
7980     auto *VD = dyn_cast<VarDecl>(D);
7981 
7982     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7983     //  A variable that appears in a lastprivate clause must not have an
7984     //  incomplete type or a reference type.
7985     if (RequireCompleteType(ELoc, Type,
7986                             diag::err_omp_lastprivate_incomplete_type))
7987       continue;
7988     Type = Type.getNonReferenceType();
7989 
7990     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7991     // in a Construct]
7992     //  Variables with the predetermined data-sharing attributes may not be
7993     //  listed in data-sharing attributes clauses, except for the cases
7994     //  listed below.
7995     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7996     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7997         DVar.CKind != OMPC_firstprivate &&
7998         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7999       Diag(ELoc, diag::err_omp_wrong_dsa)
8000           << getOpenMPClauseName(DVar.CKind)
8001           << getOpenMPClauseName(OMPC_lastprivate);
8002       ReportOriginalDSA(*this, DSAStack, D, DVar);
8003       continue;
8004     }
8005 
8006     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8007     // OpenMP [2.14.3.5, Restrictions, p.2]
8008     // A list item that is private within a parallel region, or that appears in
8009     // the reduction clause of a parallel construct, must not appear in a
8010     // lastprivate clause on a worksharing construct if any of the corresponding
8011     // worksharing regions ever binds to any of the corresponding parallel
8012     // regions.
8013     DSAStackTy::DSAVarData TopDVar = DVar;
8014     if (isOpenMPWorksharingDirective(CurrDir) &&
8015         !isOpenMPParallelDirective(CurrDir)) {
8016       DVar = DSAStack->getImplicitDSA(D, true);
8017       if (DVar.CKind != OMPC_shared) {
8018         Diag(ELoc, diag::err_omp_required_access)
8019             << getOpenMPClauseName(OMPC_lastprivate)
8020             << getOpenMPClauseName(OMPC_shared);
8021         ReportOriginalDSA(*this, DSAStack, D, DVar);
8022         continue;
8023       }
8024     }
8025 
8026     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8027     // A list item may appear in a firstprivate or lastprivate clause but not
8028     // both.
8029     if (CurrDir == OMPD_distribute) {
8030       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8031       if (DVar.CKind == OMPC_firstprivate) {
8032         Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8033         ReportOriginalDSA(*this, DSAStack, D, DVar);
8034         continue;
8035       }
8036     }
8037 
8038     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
8039     //  A variable of class type (or array thereof) that appears in a
8040     //  lastprivate clause requires an accessible, unambiguous default
8041     //  constructor for the class type, unless the list item is also specified
8042     //  in a firstprivate clause.
8043     //  A variable of class type (or array thereof) that appears in a
8044     //  lastprivate clause requires an accessible, unambiguous copy assignment
8045     //  operator for the class type.
8046     Type = Context.getBaseElementType(Type).getNonReferenceType();
8047     auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
8048                                Type.getUnqualifiedType(), ".lastprivate.src",
8049                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8050     auto *PseudoSrcExpr =
8051         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
8052     auto *DstVD =
8053         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
8054                      D->hasAttrs() ? &D->getAttrs() : nullptr);
8055     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
8056     // For arrays generate assignment operation for single element and replace
8057     // it by the original array element in CodeGen.
8058     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
8059                                    PseudoDstExpr, PseudoSrcExpr);
8060     if (AssignmentOp.isInvalid())
8061       continue;
8062     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
8063                                        /*DiscardedValue=*/true);
8064     if (AssignmentOp.isInvalid())
8065       continue;
8066 
8067     DeclRefExpr *Ref = nullptr;
8068     if (!VD) {
8069       if (TopDVar.CKind == OMPC_firstprivate)
8070         Ref = TopDVar.PrivateCopy;
8071       else {
8072         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8073         if (!IsOpenMPCapturedDecl(D))
8074           ExprCaptures.push_back(Ref->getDecl());
8075       }
8076       if (TopDVar.CKind == OMPC_firstprivate ||
8077           (!IsOpenMPCapturedDecl(D) &&
8078            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
8079         ExprResult RefRes = DefaultLvalueConversion(Ref);
8080         if (!RefRes.isUsable())
8081           continue;
8082         ExprResult PostUpdateRes =
8083             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8084                        RefRes.get());
8085         if (!PostUpdateRes.isUsable())
8086           continue;
8087         ExprPostUpdates.push_back(
8088             IgnoredValueConversions(PostUpdateRes.get()).get());
8089       }
8090     }
8091     if (TopDVar.CKind != OMPC_firstprivate)
8092       DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8093     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
8094     SrcExprs.push_back(PseudoSrcExpr);
8095     DstExprs.push_back(PseudoDstExpr);
8096     AssignmentOps.push_back(AssignmentOp.get());
8097   }
8098 
8099   if (Vars.empty())
8100     return nullptr;
8101 
8102   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8103                                       Vars, SrcExprs, DstExprs, AssignmentOps,
8104                                       buildPreInits(Context, ExprCaptures),
8105                                       buildPostUpdate(*this, ExprPostUpdates));
8106 }
8107 
8108 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8109                                          SourceLocation StartLoc,
8110                                          SourceLocation LParenLoc,
8111                                          SourceLocation EndLoc) {
8112   SmallVector<Expr *, 8> Vars;
8113   for (auto &RefExpr : VarList) {
8114     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
8115     SourceLocation ELoc;
8116     SourceRange ERange;
8117     Expr *SimpleRefExpr = RefExpr;
8118     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8119     if (Res.second) {
8120       // It will be analyzed later.
8121       Vars.push_back(RefExpr);
8122     }
8123     ValueDecl *D = Res.first;
8124     if (!D)
8125       continue;
8126 
8127     auto *VD = dyn_cast<VarDecl>(D);
8128     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8129     // in a Construct]
8130     //  Variables with the predetermined data-sharing attributes may not be
8131     //  listed in data-sharing attributes clauses, except for the cases
8132     //  listed below. For these exceptions only, listing a predetermined
8133     //  variable in a data-sharing attribute clause is allowed and overrides
8134     //  the variable's predetermined data-sharing attributes.
8135     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8136     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8137         DVar.RefExpr) {
8138       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8139                                           << getOpenMPClauseName(OMPC_shared);
8140       ReportOriginalDSA(*this, DSAStack, D, DVar);
8141       continue;
8142     }
8143 
8144     DeclRefExpr *Ref = nullptr;
8145     if (!VD && IsOpenMPCapturedDecl(D))
8146       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8147     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
8148     Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
8149   }
8150 
8151   if (Vars.empty())
8152     return nullptr;
8153 
8154   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8155 }
8156 
8157 namespace {
8158 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8159   DSAStackTy *Stack;
8160 
8161 public:
8162   bool VisitDeclRefExpr(DeclRefExpr *E) {
8163     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
8164       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
8165       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8166         return false;
8167       if (DVar.CKind != OMPC_unknown)
8168         return true;
8169       DSAStackTy::DSAVarData DVarPrivate =
8170           Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
8171       if (DVarPrivate.CKind != OMPC_unknown)
8172         return true;
8173       return false;
8174     }
8175     return false;
8176   }
8177   bool VisitStmt(Stmt *S) {
8178     for (auto Child : S->children()) {
8179       if (Child && Visit(Child))
8180         return true;
8181     }
8182     return false;
8183   }
8184   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
8185 };
8186 } // namespace
8187 
8188 namespace {
8189 // Transform MemberExpression for specified FieldDecl of current class to
8190 // DeclRefExpr to specified OMPCapturedExprDecl.
8191 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8192   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8193   ValueDecl *Field;
8194   DeclRefExpr *CapturedExpr;
8195 
8196 public:
8197   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8198       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8199 
8200   ExprResult TransformMemberExpr(MemberExpr *E) {
8201     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8202         E->getMemberDecl() == Field) {
8203       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
8204       return CapturedExpr;
8205     }
8206     return BaseTransform::TransformMemberExpr(E);
8207   }
8208   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8209 };
8210 } // namespace
8211 
8212 template <typename T>
8213 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8214                             const llvm::function_ref<T(ValueDecl *)> &Gen) {
8215   for (auto &Set : Lookups) {
8216     for (auto *D : Set) {
8217       if (auto Res = Gen(cast<ValueDecl>(D)))
8218         return Res;
8219     }
8220   }
8221   return T();
8222 }
8223 
8224 static ExprResult
8225 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8226                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8227                          const DeclarationNameInfo &ReductionId, QualType Ty,
8228                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8229   if (ReductionIdScopeSpec.isInvalid())
8230     return ExprError();
8231   SmallVector<UnresolvedSet<8>, 4> Lookups;
8232   if (S) {
8233     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8234     Lookup.suppressDiagnostics();
8235     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8236       auto *D = Lookup.getRepresentativeDecl();
8237       do {
8238         S = S->getParent();
8239       } while (S && !S->isDeclScope(D));
8240       if (S)
8241         S = S->getParent();
8242       Lookups.push_back(UnresolvedSet<8>());
8243       Lookups.back().append(Lookup.begin(), Lookup.end());
8244       Lookup.clear();
8245     }
8246   } else if (auto *ULE =
8247                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8248     Lookups.push_back(UnresolvedSet<8>());
8249     Decl *PrevD = nullptr;
8250     for(auto *D : ULE->decls()) {
8251       if (D == PrevD)
8252         Lookups.push_back(UnresolvedSet<8>());
8253       else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8254         Lookups.back().addDecl(DRD);
8255       PrevD = D;
8256     }
8257   }
8258   if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8259       Ty->containsUnexpandedParameterPack() ||
8260       filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8261         return !D->isInvalidDecl() &&
8262                (D->getType()->isDependentType() ||
8263                 D->getType()->isInstantiationDependentType() ||
8264                 D->getType()->containsUnexpandedParameterPack());
8265       })) {
8266     UnresolvedSet<8> ResSet;
8267     for (auto &Set : Lookups) {
8268       ResSet.append(Set.begin(), Set.end());
8269       // The last item marks the end of all declarations at the specified scope.
8270       ResSet.addDecl(Set[Set.size() - 1]);
8271     }
8272     return UnresolvedLookupExpr::Create(
8273         SemaRef.Context, /*NamingClass=*/nullptr,
8274         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8275         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8276   }
8277   if (auto *VD = filterLookupForUDR<ValueDecl *>(
8278           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8279             if (!D->isInvalidDecl() &&
8280                 SemaRef.Context.hasSameType(D->getType(), Ty))
8281               return D;
8282             return nullptr;
8283           }))
8284     return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8285   if (auto *VD = filterLookupForUDR<ValueDecl *>(
8286           Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8287             if (!D->isInvalidDecl() &&
8288                 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8289                 !Ty.isMoreQualifiedThan(D->getType()))
8290               return D;
8291             return nullptr;
8292           })) {
8293     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8294                        /*DetectVirtual=*/false);
8295     if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8296       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8297               VD->getType().getUnqualifiedType()))) {
8298         if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8299                                          /*DiagID=*/0) !=
8300             Sema::AR_inaccessible) {
8301           SemaRef.BuildBasePathArray(Paths, BasePath);
8302           return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8303         }
8304       }
8305     }
8306   }
8307   if (ReductionIdScopeSpec.isSet()) {
8308     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8309     return ExprError();
8310   }
8311   return ExprEmpty();
8312 }
8313 
8314 OMPClause *Sema::ActOnOpenMPReductionClause(
8315     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8316     SourceLocation ColonLoc, SourceLocation EndLoc,
8317     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8318     ArrayRef<Expr *> UnresolvedReductions) {
8319   auto DN = ReductionId.getName();
8320   auto OOK = DN.getCXXOverloadedOperator();
8321   BinaryOperatorKind BOK = BO_Comma;
8322 
8323   // OpenMP [2.14.3.6, reduction clause]
8324   // C
8325   // reduction-identifier is either an identifier or one of the following
8326   // operators: +, -, *,  &, |, ^, && and ||
8327   // C++
8328   // reduction-identifier is either an id-expression or one of the following
8329   // operators: +, -, *, &, |, ^, && and ||
8330   // FIXME: Only 'min' and 'max' identifiers are supported for now.
8331   switch (OOK) {
8332   case OO_Plus:
8333   case OO_Minus:
8334     BOK = BO_Add;
8335     break;
8336   case OO_Star:
8337     BOK = BO_Mul;
8338     break;
8339   case OO_Amp:
8340     BOK = BO_And;
8341     break;
8342   case OO_Pipe:
8343     BOK = BO_Or;
8344     break;
8345   case OO_Caret:
8346     BOK = BO_Xor;
8347     break;
8348   case OO_AmpAmp:
8349     BOK = BO_LAnd;
8350     break;
8351   case OO_PipePipe:
8352     BOK = BO_LOr;
8353     break;
8354   case OO_New:
8355   case OO_Delete:
8356   case OO_Array_New:
8357   case OO_Array_Delete:
8358   case OO_Slash:
8359   case OO_Percent:
8360   case OO_Tilde:
8361   case OO_Exclaim:
8362   case OO_Equal:
8363   case OO_Less:
8364   case OO_Greater:
8365   case OO_LessEqual:
8366   case OO_GreaterEqual:
8367   case OO_PlusEqual:
8368   case OO_MinusEqual:
8369   case OO_StarEqual:
8370   case OO_SlashEqual:
8371   case OO_PercentEqual:
8372   case OO_CaretEqual:
8373   case OO_AmpEqual:
8374   case OO_PipeEqual:
8375   case OO_LessLess:
8376   case OO_GreaterGreater:
8377   case OO_LessLessEqual:
8378   case OO_GreaterGreaterEqual:
8379   case OO_EqualEqual:
8380   case OO_ExclaimEqual:
8381   case OO_PlusPlus:
8382   case OO_MinusMinus:
8383   case OO_Comma:
8384   case OO_ArrowStar:
8385   case OO_Arrow:
8386   case OO_Call:
8387   case OO_Subscript:
8388   case OO_Conditional:
8389   case OO_Coawait:
8390   case NUM_OVERLOADED_OPERATORS:
8391     llvm_unreachable("Unexpected reduction identifier");
8392   case OO_None:
8393     if (auto II = DN.getAsIdentifierInfo()) {
8394       if (II->isStr("max"))
8395         BOK = BO_GT;
8396       else if (II->isStr("min"))
8397         BOK = BO_LT;
8398     }
8399     break;
8400   }
8401   SourceRange ReductionIdRange;
8402   if (ReductionIdScopeSpec.isValid())
8403     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
8404   ReductionIdRange.setEnd(ReductionId.getEndLoc());
8405 
8406   SmallVector<Expr *, 8> Vars;
8407   SmallVector<Expr *, 8> Privates;
8408   SmallVector<Expr *, 8> LHSs;
8409   SmallVector<Expr *, 8> RHSs;
8410   SmallVector<Expr *, 8> ReductionOps;
8411   SmallVector<Decl *, 4> ExprCaptures;
8412   SmallVector<Expr *, 4> ExprPostUpdates;
8413   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8414   bool FirstIter = true;
8415   for (auto RefExpr : VarList) {
8416     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
8417     // OpenMP [2.1, C/C++]
8418     //  A list item is a variable or array section, subject to the restrictions
8419     //  specified in Section 2.4 on page 42 and in each of the sections
8420     // describing clauses and directives for which a list appears.
8421     // OpenMP  [2.14.3.3, Restrictions, p.1]
8422     //  A variable that is part of another variable (as an array or
8423     //  structure element) cannot appear in a private clause.
8424     if (!FirstIter && IR != ER)
8425       ++IR;
8426     FirstIter = false;
8427     SourceLocation ELoc;
8428     SourceRange ERange;
8429     Expr *SimpleRefExpr = RefExpr;
8430     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8431                               /*AllowArraySection=*/true);
8432     if (Res.second) {
8433       // It will be analyzed later.
8434       Vars.push_back(RefExpr);
8435       Privates.push_back(nullptr);
8436       LHSs.push_back(nullptr);
8437       RHSs.push_back(nullptr);
8438       // Try to find 'declare reduction' corresponding construct before using
8439       // builtin/overloaded operators.
8440       QualType Type = Context.DependentTy;
8441       CXXCastPath BasePath;
8442       ExprResult DeclareReductionRef = buildDeclareReductionRef(
8443           *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8444           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8445       if (CurContext->isDependentContext() &&
8446           (DeclareReductionRef.isUnset() ||
8447            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8448         ReductionOps.push_back(DeclareReductionRef.get());
8449       else
8450         ReductionOps.push_back(nullptr);
8451     }
8452     ValueDecl *D = Res.first;
8453     if (!D)
8454       continue;
8455 
8456     QualType Type;
8457     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8458     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8459     if (ASE)
8460       Type = ASE->getType().getNonReferenceType();
8461     else if (OASE) {
8462       auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8463       if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8464         Type = ATy->getElementType();
8465       else
8466         Type = BaseType->getPointeeType();
8467       Type = Type.getNonReferenceType();
8468     } else
8469       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8470     auto *VD = dyn_cast<VarDecl>(D);
8471 
8472     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8473     //  A variable that appears in a private clause must not have an incomplete
8474     //  type or a reference type.
8475     if (RequireCompleteType(ELoc, Type,
8476                             diag::err_omp_reduction_incomplete_type))
8477       continue;
8478     // OpenMP [2.14.3.6, reduction clause, Restrictions]
8479     // A list item that appears in a reduction clause must not be
8480     // const-qualified.
8481     if (Type.getNonReferenceType().isConstant(Context)) {
8482       Diag(ELoc, diag::err_omp_const_reduction_list_item)
8483           << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
8484       if (!ASE && !OASE) {
8485         bool IsDecl = !VD ||
8486                       VD->isThisDeclarationADefinition(Context) ==
8487                           VarDecl::DeclarationOnly;
8488         Diag(D->getLocation(),
8489              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8490             << D;
8491       }
8492       continue;
8493     }
8494     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8495     //  If a list-item is a reference type then it must bind to the same object
8496     //  for all threads of the team.
8497     if (!ASE && !OASE && VD) {
8498       VarDecl *VDDef = VD->getDefinition();
8499       if (VD->getType()->isReferenceType() && VDDef) {
8500         DSARefChecker Check(DSAStack);
8501         if (Check.Visit(VDDef->getInit())) {
8502           Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8503           Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8504           continue;
8505         }
8506       }
8507     }
8508 
8509     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8510     // in a Construct]
8511     //  Variables with the predetermined data-sharing attributes may not be
8512     //  listed in data-sharing attributes clauses, except for the cases
8513     //  listed below. For these exceptions only, listing a predetermined
8514     //  variable in a data-sharing attribute clause is allowed and overrides
8515     //  the variable's predetermined data-sharing attributes.
8516     // OpenMP [2.14.3.6, Restrictions, p.3]
8517     //  Any number of reduction clauses can be specified on the directive,
8518     //  but a list item can appear only once in the reduction clauses for that
8519     //  directive.
8520     DSAStackTy::DSAVarData DVar;
8521     DVar = DSAStack->getTopDSA(D, false);
8522     if (DVar.CKind == OMPC_reduction) {
8523       Diag(ELoc, diag::err_omp_once_referenced)
8524           << getOpenMPClauseName(OMPC_reduction);
8525       if (DVar.RefExpr)
8526         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
8527     } else if (DVar.CKind != OMPC_unknown) {
8528       Diag(ELoc, diag::err_omp_wrong_dsa)
8529           << getOpenMPClauseName(DVar.CKind)
8530           << getOpenMPClauseName(OMPC_reduction);
8531       ReportOriginalDSA(*this, DSAStack, D, DVar);
8532       continue;
8533     }
8534 
8535     // OpenMP [2.14.3.6, Restrictions, p.1]
8536     //  A list item that appears in a reduction clause of a worksharing
8537     //  construct must be shared in the parallel regions to which any of the
8538     //  worksharing regions arising from the worksharing construct bind.
8539     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8540     if (isOpenMPWorksharingDirective(CurrDir) &&
8541         !isOpenMPParallelDirective(CurrDir)) {
8542       DVar = DSAStack->getImplicitDSA(D, true);
8543       if (DVar.CKind != OMPC_shared) {
8544         Diag(ELoc, diag::err_omp_required_access)
8545             << getOpenMPClauseName(OMPC_reduction)
8546             << getOpenMPClauseName(OMPC_shared);
8547         ReportOriginalDSA(*this, DSAStack, D, DVar);
8548         continue;
8549       }
8550     }
8551 
8552     // Try to find 'declare reduction' corresponding construct before using
8553     // builtin/overloaded operators.
8554     CXXCastPath BasePath;
8555     ExprResult DeclareReductionRef = buildDeclareReductionRef(
8556         *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8557         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8558     if (DeclareReductionRef.isInvalid())
8559       continue;
8560     if (CurContext->isDependentContext() &&
8561         (DeclareReductionRef.isUnset() ||
8562          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8563       Vars.push_back(RefExpr);
8564       Privates.push_back(nullptr);
8565       LHSs.push_back(nullptr);
8566       RHSs.push_back(nullptr);
8567       ReductionOps.push_back(DeclareReductionRef.get());
8568       continue;
8569     }
8570     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8571       // Not allowed reduction identifier is found.
8572       Diag(ReductionId.getLocStart(),
8573            diag::err_omp_unknown_reduction_identifier)
8574           << Type << ReductionIdRange;
8575       continue;
8576     }
8577 
8578     // OpenMP [2.14.3.6, reduction clause, Restrictions]
8579     // The type of a list item that appears in a reduction clause must be valid
8580     // for the reduction-identifier. For a max or min reduction in C, the type
8581     // of the list item must be an allowed arithmetic data type: char, int,
8582     // float, double, or _Bool, possibly modified with long, short, signed, or
8583     // unsigned. For a max or min reduction in C++, the type of the list item
8584     // must be an allowed arithmetic data type: char, wchar_t, int, float,
8585     // double, or bool, possibly modified with long, short, signed, or unsigned.
8586     if (DeclareReductionRef.isUnset()) {
8587       if ((BOK == BO_GT || BOK == BO_LT) &&
8588           !(Type->isScalarType() ||
8589             (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8590         Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8591             << getLangOpts().CPlusPlus;
8592         if (!ASE && !OASE) {
8593           bool IsDecl = !VD ||
8594                         VD->isThisDeclarationADefinition(Context) ==
8595                             VarDecl::DeclarationOnly;
8596           Diag(D->getLocation(),
8597                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8598               << D;
8599         }
8600         continue;
8601       }
8602       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8603           !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8604         Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8605         if (!ASE && !OASE) {
8606           bool IsDecl = !VD ||
8607                         VD->isThisDeclarationADefinition(Context) ==
8608                             VarDecl::DeclarationOnly;
8609           Diag(D->getLocation(),
8610                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8611               << D;
8612         }
8613         continue;
8614       }
8615     }
8616 
8617     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
8618     auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
8619                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8620     auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8621                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8622     auto PrivateTy = Type;
8623     if (OASE ||
8624         (!ASE &&
8625          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
8626       // For arays/array sections only:
8627       // Create pseudo array type for private copy. The size for this array will
8628       // be generated during codegen.
8629       // For array subscripts or single variables Private Ty is the same as Type
8630       // (type of the variable or single array element).
8631       PrivateTy = Context.getVariableArrayType(
8632           Type, new (Context) OpaqueValueExpr(SourceLocation(),
8633                                               Context.getSizeType(), VK_RValue),
8634           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
8635     } else if (!ASE && !OASE &&
8636                Context.getAsArrayType(D->getType().getNonReferenceType()))
8637       PrivateTy = D->getType().getNonReferenceType();
8638     // Private copy.
8639     auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8640                                    D->hasAttrs() ? &D->getAttrs() : nullptr);
8641     // Add initializer for private variable.
8642     Expr *Init = nullptr;
8643     auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8644     auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8645     if (DeclareReductionRef.isUsable()) {
8646       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8647       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8648       if (DRD->getInitializer()) {
8649         Init = DRDRef;
8650         RHSVD->setInit(DRDRef);
8651         RHSVD->setInitStyle(VarDecl::CallInit);
8652       }
8653     } else {
8654       switch (BOK) {
8655       case BO_Add:
8656       case BO_Xor:
8657       case BO_Or:
8658       case BO_LOr:
8659         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8660         if (Type->isScalarType() || Type->isAnyComplexType())
8661           Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8662         break;
8663       case BO_Mul:
8664       case BO_LAnd:
8665         if (Type->isScalarType() || Type->isAnyComplexType()) {
8666           // '*' and '&&' reduction ops - initializer is '1'.
8667           Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8668         }
8669         break;
8670       case BO_And: {
8671         // '&' reduction op - initializer is '~0'.
8672         QualType OrigType = Type;
8673         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8674           Type = ComplexTy->getElementType();
8675         if (Type->isRealFloatingType()) {
8676           llvm::APFloat InitValue =
8677               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8678                                              /*isIEEE=*/true);
8679           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8680                                          Type, ELoc);
8681         } else if (Type->isScalarType()) {
8682           auto Size = Context.getTypeSize(Type);
8683           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8684           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8685           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8686         }
8687         if (Init && OrigType->isAnyComplexType()) {
8688           // Init = 0xFFFF + 0xFFFFi;
8689           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8690           Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8691         }
8692         Type = OrigType;
8693         break;
8694       }
8695       case BO_LT:
8696       case BO_GT: {
8697         // 'min' reduction op - initializer is 'Largest representable number in
8698         // the reduction list item type'.
8699         // 'max' reduction op - initializer is 'Least representable number in
8700         // the reduction list item type'.
8701         if (Type->isIntegerType() || Type->isPointerType()) {
8702           bool IsSigned = Type->hasSignedIntegerRepresentation();
8703           auto Size = Context.getTypeSize(Type);
8704           QualType IntTy =
8705               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8706           llvm::APInt InitValue =
8707               (BOK != BO_LT)
8708                   ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8709                              : llvm::APInt::getMinValue(Size)
8710                   : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8711                              : llvm::APInt::getMaxValue(Size);
8712           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8713           if (Type->isPointerType()) {
8714             // Cast to pointer type.
8715             auto CastExpr = BuildCStyleCastExpr(
8716                 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8717                 SourceLocation(), Init);
8718             if (CastExpr.isInvalid())
8719               continue;
8720             Init = CastExpr.get();
8721           }
8722         } else if (Type->isRealFloatingType()) {
8723           llvm::APFloat InitValue = llvm::APFloat::getLargest(
8724               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8725           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8726                                          Type, ELoc);
8727         }
8728         break;
8729       }
8730       case BO_PtrMemD:
8731       case BO_PtrMemI:
8732       case BO_MulAssign:
8733       case BO_Div:
8734       case BO_Rem:
8735       case BO_Sub:
8736       case BO_Shl:
8737       case BO_Shr:
8738       case BO_LE:
8739       case BO_GE:
8740       case BO_EQ:
8741       case BO_NE:
8742       case BO_AndAssign:
8743       case BO_XorAssign:
8744       case BO_OrAssign:
8745       case BO_Assign:
8746       case BO_AddAssign:
8747       case BO_SubAssign:
8748       case BO_DivAssign:
8749       case BO_RemAssign:
8750       case BO_ShlAssign:
8751       case BO_ShrAssign:
8752       case BO_Comma:
8753         llvm_unreachable("Unexpected reduction operation");
8754       }
8755     }
8756     if (Init && DeclareReductionRef.isUnset()) {
8757       AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8758                            /*TypeMayContainAuto=*/false);
8759     } else if (!Init)
8760       ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
8761     if (RHSVD->isInvalidDecl())
8762       continue;
8763     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
8764       Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8765                                                             << ReductionIdRange;
8766       bool IsDecl =
8767           !VD ||
8768           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8769       Diag(D->getLocation(),
8770            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8771           << D;
8772       continue;
8773     }
8774     // Store initializer for single element in private copy. Will be used during
8775     // codegen.
8776     PrivateVD->setInit(RHSVD->getInit());
8777     PrivateVD->setInitStyle(RHSVD->getInitStyle());
8778     auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
8779     ExprResult ReductionOp;
8780     if (DeclareReductionRef.isUsable()) {
8781       QualType RedTy = DeclareReductionRef.get()->getType();
8782       QualType PtrRedTy = Context.getPointerType(RedTy);
8783       ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8784       ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8785       if (!BasePath.empty()) {
8786         LHS = DefaultLvalueConversion(LHS.get());
8787         RHS = DefaultLvalueConversion(RHS.get());
8788         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8789                                        CK_UncheckedDerivedToBase, LHS.get(),
8790                                        &BasePath, LHS.get()->getValueKind());
8791         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8792                                        CK_UncheckedDerivedToBase, RHS.get(),
8793                                        &BasePath, RHS.get()->getValueKind());
8794       }
8795       FunctionProtoType::ExtProtoInfo EPI;
8796       QualType Params[] = {PtrRedTy, PtrRedTy};
8797       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8798       auto *OVE = new (Context) OpaqueValueExpr(
8799           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8800           DefaultLvalueConversion(DeclareReductionRef.get()).get());
8801       Expr *Args[] = {LHS.get(), RHS.get()};
8802       ReductionOp = new (Context)
8803           CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8804     } else {
8805       ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8806                                ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8807       if (ReductionOp.isUsable()) {
8808         if (BOK != BO_LT && BOK != BO_GT) {
8809           ReductionOp =
8810               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8811                          BO_Assign, LHSDRE, ReductionOp.get());
8812         } else {
8813           auto *ConditionalOp = new (Context) ConditionalOperator(
8814               ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8815               RHSDRE, Type, VK_LValue, OK_Ordinary);
8816           ReductionOp =
8817               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8818                          BO_Assign, LHSDRE, ConditionalOp);
8819         }
8820         ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8821       }
8822       if (ReductionOp.isInvalid())
8823         continue;
8824     }
8825 
8826     DeclRefExpr *Ref = nullptr;
8827     Expr *VarsExpr = RefExpr->IgnoreParens();
8828     if (!VD) {
8829       if (ASE || OASE) {
8830         TransformExprToCaptures RebuildToCapture(*this, D);
8831         VarsExpr =
8832             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8833         Ref = RebuildToCapture.getCapturedExpr();
8834       } else {
8835         VarsExpr = Ref =
8836             buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8837       }
8838       if (!IsOpenMPCapturedDecl(D)) {
8839         ExprCaptures.push_back(Ref->getDecl());
8840         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8841           ExprResult RefRes = DefaultLvalueConversion(Ref);
8842           if (!RefRes.isUsable())
8843             continue;
8844           ExprResult PostUpdateRes =
8845               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8846                          SimpleRefExpr, RefRes.get());
8847           if (!PostUpdateRes.isUsable())
8848             continue;
8849           ExprPostUpdates.push_back(
8850               IgnoredValueConversions(PostUpdateRes.get()).get());
8851         }
8852       }
8853     }
8854     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8855     Vars.push_back(VarsExpr);
8856     Privates.push_back(PrivateDRE);
8857     LHSs.push_back(LHSDRE);
8858     RHSs.push_back(RHSDRE);
8859     ReductionOps.push_back(ReductionOp.get());
8860   }
8861 
8862   if (Vars.empty())
8863     return nullptr;
8864 
8865   return OMPReductionClause::Create(
8866       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
8867       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8868       LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8869       buildPostUpdate(*this, ExprPostUpdates));
8870 }
8871 
8872 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8873                                      SourceLocation LinLoc) {
8874   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8875       LinKind == OMPC_LINEAR_unknown) {
8876     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8877     return true;
8878   }
8879   return false;
8880 }
8881 
8882 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8883                                  OpenMPLinearClauseKind LinKind,
8884                                  QualType Type) {
8885   auto *VD = dyn_cast_or_null<VarDecl>(D);
8886   // A variable must not have an incomplete type or a reference type.
8887   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8888     return true;
8889   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8890       !Type->isReferenceType()) {
8891     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8892         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8893     return true;
8894   }
8895   Type = Type.getNonReferenceType();
8896 
8897   // A list item must not be const-qualified.
8898   if (Type.isConstant(Context)) {
8899     Diag(ELoc, diag::err_omp_const_variable)
8900         << getOpenMPClauseName(OMPC_linear);
8901     if (D) {
8902       bool IsDecl =
8903           !VD ||
8904           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8905       Diag(D->getLocation(),
8906            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8907           << D;
8908     }
8909     return true;
8910   }
8911 
8912   // A list item must be of integral or pointer type.
8913   Type = Type.getUnqualifiedType().getCanonicalType();
8914   const auto *Ty = Type.getTypePtrOrNull();
8915   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8916               !Ty->isPointerType())) {
8917     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8918     if (D) {
8919       bool IsDecl =
8920           !VD ||
8921           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8922       Diag(D->getLocation(),
8923            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8924           << D;
8925     }
8926     return true;
8927   }
8928   return false;
8929 }
8930 
8931 OMPClause *Sema::ActOnOpenMPLinearClause(
8932     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8933     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8934     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8935   SmallVector<Expr *, 8> Vars;
8936   SmallVector<Expr *, 8> Privates;
8937   SmallVector<Expr *, 8> Inits;
8938   SmallVector<Decl *, 4> ExprCaptures;
8939   SmallVector<Expr *, 4> ExprPostUpdates;
8940   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
8941     LinKind = OMPC_LINEAR_val;
8942   for (auto &RefExpr : VarList) {
8943     assert(RefExpr && "NULL expr in OpenMP linear clause.");
8944     SourceLocation ELoc;
8945     SourceRange ERange;
8946     Expr *SimpleRefExpr = RefExpr;
8947     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8948                               /*AllowArraySection=*/false);
8949     if (Res.second) {
8950       // It will be analyzed later.
8951       Vars.push_back(RefExpr);
8952       Privates.push_back(nullptr);
8953       Inits.push_back(nullptr);
8954     }
8955     ValueDecl *D = Res.first;
8956     if (!D)
8957       continue;
8958 
8959     QualType Type = D->getType();
8960     auto *VD = dyn_cast<VarDecl>(D);
8961 
8962     // OpenMP [2.14.3.7, linear clause]
8963     //  A list-item cannot appear in more than one linear clause.
8964     //  A list-item that appears in a linear clause cannot appear in any
8965     //  other data-sharing attribute clause.
8966     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8967     if (DVar.RefExpr) {
8968       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8969                                           << getOpenMPClauseName(OMPC_linear);
8970       ReportOriginalDSA(*this, DSAStack, D, DVar);
8971       continue;
8972     }
8973 
8974     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
8975       continue;
8976     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
8977 
8978     // Build private copy of original var.
8979     auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8980                                  D->hasAttrs() ? &D->getAttrs() : nullptr);
8981     auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
8982     // Build var to save initial value.
8983     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
8984     Expr *InitExpr;
8985     DeclRefExpr *Ref = nullptr;
8986     if (!VD) {
8987       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8988       if (!IsOpenMPCapturedDecl(D)) {
8989         ExprCaptures.push_back(Ref->getDecl());
8990         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8991           ExprResult RefRes = DefaultLvalueConversion(Ref);
8992           if (!RefRes.isUsable())
8993             continue;
8994           ExprResult PostUpdateRes =
8995               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8996                          SimpleRefExpr, RefRes.get());
8997           if (!PostUpdateRes.isUsable())
8998             continue;
8999           ExprPostUpdates.push_back(
9000               IgnoredValueConversions(PostUpdateRes.get()).get());
9001         }
9002       }
9003     }
9004     if (LinKind == OMPC_LINEAR_uval)
9005       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
9006     else
9007       InitExpr = VD ? SimpleRefExpr : Ref;
9008     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
9009                          /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9010     auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9011 
9012     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9013     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
9014     Privates.push_back(PrivateRef);
9015     Inits.push_back(InitRef);
9016   }
9017 
9018   if (Vars.empty())
9019     return nullptr;
9020 
9021   Expr *StepExpr = Step;
9022   Expr *CalcStepExpr = nullptr;
9023   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9024       !Step->isInstantiationDependent() &&
9025       !Step->containsUnexpandedParameterPack()) {
9026     SourceLocation StepLoc = Step->getLocStart();
9027     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
9028     if (Val.isInvalid())
9029       return nullptr;
9030     StepExpr = Val.get();
9031 
9032     // Build var to save the step value.
9033     VarDecl *SaveVar =
9034         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
9035     ExprResult SaveRef =
9036         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
9037     ExprResult CalcStep =
9038         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
9039     CalcStep = ActOnFinishFullExpr(CalcStep.get());
9040 
9041     // Warn about zero linear step (it would be probably better specified as
9042     // making corresponding variables 'const').
9043     llvm::APSInt Result;
9044     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9045     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
9046       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9047                                                      << (Vars.size() > 1);
9048     if (!IsConstant && CalcStep.isUsable()) {
9049       // Calculate the step beforehand instead of doing this on each iteration.
9050       // (This is not used if the number of iterations may be kfold-ed).
9051       CalcStepExpr = CalcStep.get();
9052     }
9053   }
9054 
9055   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9056                                  ColonLoc, EndLoc, Vars, Privates, Inits,
9057                                  StepExpr, CalcStepExpr,
9058                                  buildPreInits(Context, ExprCaptures),
9059                                  buildPostUpdate(*this, ExprPostUpdates));
9060 }
9061 
9062 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9063                                      Expr *NumIterations, Sema &SemaRef,
9064                                      Scope *S, DSAStackTy *Stack) {
9065   // Walk the vars and build update/final expressions for the CodeGen.
9066   SmallVector<Expr *, 8> Updates;
9067   SmallVector<Expr *, 8> Finals;
9068   Expr *Step = Clause.getStep();
9069   Expr *CalcStep = Clause.getCalcStep();
9070   // OpenMP [2.14.3.7, linear clause]
9071   // If linear-step is not specified it is assumed to be 1.
9072   if (Step == nullptr)
9073     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
9074   else if (CalcStep) {
9075     Step = cast<BinaryOperator>(CalcStep)->getLHS();
9076   }
9077   bool HasErrors = false;
9078   auto CurInit = Clause.inits().begin();
9079   auto CurPrivate = Clause.privates().begin();
9080   auto LinKind = Clause.getModifier();
9081   for (auto &RefExpr : Clause.varlists()) {
9082     SourceLocation ELoc;
9083     SourceRange ERange;
9084     Expr *SimpleRefExpr = RefExpr;
9085     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9086                               /*AllowArraySection=*/false);
9087     ValueDecl *D = Res.first;
9088     if (Res.second || !D) {
9089       Updates.push_back(nullptr);
9090       Finals.push_back(nullptr);
9091       HasErrors = true;
9092       continue;
9093     }
9094     if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9095       D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9096               ->getMemberDecl();
9097     }
9098     auto &&Info = Stack->isLoopControlVariable(D);
9099     Expr *InitExpr = *CurInit;
9100 
9101     // Build privatized reference to the current linear var.
9102     auto DE = cast<DeclRefExpr>(SimpleRefExpr);
9103     Expr *CapturedRef;
9104     if (LinKind == OMPC_LINEAR_uval)
9105       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9106     else
9107       CapturedRef =
9108           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9109                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9110                            /*RefersToCapture=*/true);
9111 
9112     // Build update: Var = InitExpr + IV * Step
9113     ExprResult Update;
9114     if (!Info.first) {
9115       Update =
9116           BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9117                              InitExpr, IV, Step, /* Subtract */ false);
9118     } else
9119       Update = *CurPrivate;
9120     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9121                                          /*DiscardedValue=*/true);
9122 
9123     // Build final: Var = InitExpr + NumIterations * Step
9124     ExprResult Final;
9125     if (!Info.first) {
9126       Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9127                                  InitExpr, NumIterations, Step,
9128                                  /* Subtract */ false);
9129     } else
9130       Final = *CurPrivate;
9131     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9132                                         /*DiscardedValue=*/true);
9133 
9134     if (!Update.isUsable() || !Final.isUsable()) {
9135       Updates.push_back(nullptr);
9136       Finals.push_back(nullptr);
9137       HasErrors = true;
9138     } else {
9139       Updates.push_back(Update.get());
9140       Finals.push_back(Final.get());
9141     }
9142     ++CurInit;
9143     ++CurPrivate;
9144   }
9145   Clause.setUpdates(Updates);
9146   Clause.setFinals(Finals);
9147   return HasErrors;
9148 }
9149 
9150 OMPClause *Sema::ActOnOpenMPAlignedClause(
9151     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9152     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9153 
9154   SmallVector<Expr *, 8> Vars;
9155   for (auto &RefExpr : VarList) {
9156     assert(RefExpr && "NULL expr in OpenMP linear clause.");
9157     SourceLocation ELoc;
9158     SourceRange ERange;
9159     Expr *SimpleRefExpr = RefExpr;
9160     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9161                               /*AllowArraySection=*/false);
9162     if (Res.second) {
9163       // It will be analyzed later.
9164       Vars.push_back(RefExpr);
9165     }
9166     ValueDecl *D = Res.first;
9167     if (!D)
9168       continue;
9169 
9170     QualType QType = D->getType();
9171     auto *VD = dyn_cast<VarDecl>(D);
9172 
9173     // OpenMP  [2.8.1, simd construct, Restrictions]
9174     // The type of list items appearing in the aligned clause must be
9175     // array, pointer, reference to array, or reference to pointer.
9176     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
9177     const Type *Ty = QType.getTypePtrOrNull();
9178     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
9179       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
9180           << QType << getLangOpts().CPlusPlus << ERange;
9181       bool IsDecl =
9182           !VD ||
9183           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9184       Diag(D->getLocation(),
9185            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9186           << D;
9187       continue;
9188     }
9189 
9190     // OpenMP  [2.8.1, simd construct, Restrictions]
9191     // A list-item cannot appear in more than one aligned clause.
9192     if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
9193       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
9194       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9195           << getOpenMPClauseName(OMPC_aligned);
9196       continue;
9197     }
9198 
9199     DeclRefExpr *Ref = nullptr;
9200     if (!VD && IsOpenMPCapturedDecl(D))
9201       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9202     Vars.push_back(DefaultFunctionArrayConversion(
9203                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9204                        .get());
9205   }
9206 
9207   // OpenMP [2.8.1, simd construct, Description]
9208   // The parameter of the aligned clause, alignment, must be a constant
9209   // positive integer expression.
9210   // If no optional parameter is specified, implementation-defined default
9211   // alignments for SIMD instructions on the target platforms are assumed.
9212   if (Alignment != nullptr) {
9213     ExprResult AlignResult =
9214         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9215     if (AlignResult.isInvalid())
9216       return nullptr;
9217     Alignment = AlignResult.get();
9218   }
9219   if (Vars.empty())
9220     return nullptr;
9221 
9222   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9223                                   EndLoc, Vars, Alignment);
9224 }
9225 
9226 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9227                                          SourceLocation StartLoc,
9228                                          SourceLocation LParenLoc,
9229                                          SourceLocation EndLoc) {
9230   SmallVector<Expr *, 8> Vars;
9231   SmallVector<Expr *, 8> SrcExprs;
9232   SmallVector<Expr *, 8> DstExprs;
9233   SmallVector<Expr *, 8> AssignmentOps;
9234   for (auto &RefExpr : VarList) {
9235     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9236     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
9237       // It will be analyzed later.
9238       Vars.push_back(RefExpr);
9239       SrcExprs.push_back(nullptr);
9240       DstExprs.push_back(nullptr);
9241       AssignmentOps.push_back(nullptr);
9242       continue;
9243     }
9244 
9245     SourceLocation ELoc = RefExpr->getExprLoc();
9246     // OpenMP [2.1, C/C++]
9247     //  A list item is a variable name.
9248     // OpenMP  [2.14.4.1, Restrictions, p.1]
9249     //  A list item that appears in a copyin clause must be threadprivate.
9250     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
9251     if (!DE || !isa<VarDecl>(DE->getDecl())) {
9252       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9253           << 0 << RefExpr->getSourceRange();
9254       continue;
9255     }
9256 
9257     Decl *D = DE->getDecl();
9258     VarDecl *VD = cast<VarDecl>(D);
9259 
9260     QualType Type = VD->getType();
9261     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9262       // It will be analyzed later.
9263       Vars.push_back(DE);
9264       SrcExprs.push_back(nullptr);
9265       DstExprs.push_back(nullptr);
9266       AssignmentOps.push_back(nullptr);
9267       continue;
9268     }
9269 
9270     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9271     //  A list item that appears in a copyin clause must be threadprivate.
9272     if (!DSAStack->isThreadPrivate(VD)) {
9273       Diag(ELoc, diag::err_omp_required_access)
9274           << getOpenMPClauseName(OMPC_copyin)
9275           << getOpenMPDirectiveName(OMPD_threadprivate);
9276       continue;
9277     }
9278 
9279     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9280     //  A variable of class type (or array thereof) that appears in a
9281     //  copyin clause requires an accessible, unambiguous copy assignment
9282     //  operator for the class type.
9283     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
9284     auto *SrcVD =
9285         buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9286                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9287     auto *PseudoSrcExpr = buildDeclRefExpr(
9288         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9289     auto *DstVD =
9290         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9291                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9292     auto *PseudoDstExpr =
9293         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
9294     // For arrays generate assignment operation for single element and replace
9295     // it by the original array element in CodeGen.
9296     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9297                                    PseudoDstExpr, PseudoSrcExpr);
9298     if (AssignmentOp.isInvalid())
9299       continue;
9300     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9301                                        /*DiscardedValue=*/true);
9302     if (AssignmentOp.isInvalid())
9303       continue;
9304 
9305     DSAStack->addDSA(VD, DE, OMPC_copyin);
9306     Vars.push_back(DE);
9307     SrcExprs.push_back(PseudoSrcExpr);
9308     DstExprs.push_back(PseudoDstExpr);
9309     AssignmentOps.push_back(AssignmentOp.get());
9310   }
9311 
9312   if (Vars.empty())
9313     return nullptr;
9314 
9315   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9316                                  SrcExprs, DstExprs, AssignmentOps);
9317 }
9318 
9319 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9320                                               SourceLocation StartLoc,
9321                                               SourceLocation LParenLoc,
9322                                               SourceLocation EndLoc) {
9323   SmallVector<Expr *, 8> Vars;
9324   SmallVector<Expr *, 8> SrcExprs;
9325   SmallVector<Expr *, 8> DstExprs;
9326   SmallVector<Expr *, 8> AssignmentOps;
9327   for (auto &RefExpr : VarList) {
9328     assert(RefExpr && "NULL expr in OpenMP linear clause.");
9329     SourceLocation ELoc;
9330     SourceRange ERange;
9331     Expr *SimpleRefExpr = RefExpr;
9332     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9333                               /*AllowArraySection=*/false);
9334     if (Res.second) {
9335       // It will be analyzed later.
9336       Vars.push_back(RefExpr);
9337       SrcExprs.push_back(nullptr);
9338       DstExprs.push_back(nullptr);
9339       AssignmentOps.push_back(nullptr);
9340     }
9341     ValueDecl *D = Res.first;
9342     if (!D)
9343       continue;
9344 
9345     QualType Type = D->getType();
9346     auto *VD = dyn_cast<VarDecl>(D);
9347 
9348     // OpenMP [2.14.4.2, Restrictions, p.2]
9349     //  A list item that appears in a copyprivate clause may not appear in a
9350     //  private or firstprivate clause on the single construct.
9351     if (!VD || !DSAStack->isThreadPrivate(VD)) {
9352       auto DVar = DSAStack->getTopDSA(D, false);
9353       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9354           DVar.RefExpr) {
9355         Diag(ELoc, diag::err_omp_wrong_dsa)
9356             << getOpenMPClauseName(DVar.CKind)
9357             << getOpenMPClauseName(OMPC_copyprivate);
9358         ReportOriginalDSA(*this, DSAStack, D, DVar);
9359         continue;
9360       }
9361 
9362       // OpenMP [2.11.4.2, Restrictions, p.1]
9363       //  All list items that appear in a copyprivate clause must be either
9364       //  threadprivate or private in the enclosing context.
9365       if (DVar.CKind == OMPC_unknown) {
9366         DVar = DSAStack->getImplicitDSA(D, false);
9367         if (DVar.CKind == OMPC_shared) {
9368           Diag(ELoc, diag::err_omp_required_access)
9369               << getOpenMPClauseName(OMPC_copyprivate)
9370               << "threadprivate or private in the enclosing context";
9371           ReportOriginalDSA(*this, DSAStack, D, DVar);
9372           continue;
9373         }
9374       }
9375     }
9376 
9377     // Variably modified types are not supported.
9378     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
9379       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9380           << getOpenMPClauseName(OMPC_copyprivate) << Type
9381           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9382       bool IsDecl =
9383           !VD ||
9384           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9385       Diag(D->getLocation(),
9386            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9387           << D;
9388       continue;
9389     }
9390 
9391     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9392     //  A variable of class type (or array thereof) that appears in a
9393     //  copyin clause requires an accessible, unambiguous copy assignment
9394     //  operator for the class type.
9395     Type = Context.getBaseElementType(Type.getNonReferenceType())
9396                .getUnqualifiedType();
9397     auto *SrcVD =
9398         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9399                      D->hasAttrs() ? &D->getAttrs() : nullptr);
9400     auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
9401     auto *DstVD =
9402         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9403                      D->hasAttrs() ? &D->getAttrs() : nullptr);
9404     auto *PseudoDstExpr =
9405         buildDeclRefExpr(*this, DstVD, Type, ELoc);
9406     auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9407                                    PseudoDstExpr, PseudoSrcExpr);
9408     if (AssignmentOp.isInvalid())
9409       continue;
9410     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
9411                                        /*DiscardedValue=*/true);
9412     if (AssignmentOp.isInvalid())
9413       continue;
9414 
9415     // No need to mark vars as copyprivate, they are already threadprivate or
9416     // implicitly private.
9417     assert(VD || IsOpenMPCapturedDecl(D));
9418     Vars.push_back(
9419         VD ? RefExpr->IgnoreParens()
9420            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
9421     SrcExprs.push_back(PseudoSrcExpr);
9422     DstExprs.push_back(PseudoDstExpr);
9423     AssignmentOps.push_back(AssignmentOp.get());
9424   }
9425 
9426   if (Vars.empty())
9427     return nullptr;
9428 
9429   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9430                                       Vars, SrcExprs, DstExprs, AssignmentOps);
9431 }
9432 
9433 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9434                                         SourceLocation StartLoc,
9435                                         SourceLocation LParenLoc,
9436                                         SourceLocation EndLoc) {
9437   if (VarList.empty())
9438     return nullptr;
9439 
9440   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9441 }
9442 
9443 OMPClause *
9444 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9445                               SourceLocation DepLoc, SourceLocation ColonLoc,
9446                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9447                               SourceLocation LParenLoc, SourceLocation EndLoc) {
9448   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
9449       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
9450     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
9451         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
9452     return nullptr;
9453   }
9454   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
9455       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9456        DepKind == OMPC_DEPEND_sink)) {
9457     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
9458     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
9459         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9460                                    /*Last=*/OMPC_DEPEND_unknown, Except)
9461         << getOpenMPClauseName(OMPC_depend);
9462     return nullptr;
9463   }
9464   SmallVector<Expr *, 8> Vars;
9465   llvm::APSInt DepCounter(/*BitWidth=*/32);
9466   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9467   if (DepKind == OMPC_DEPEND_sink) {
9468     if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9469       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9470       TotalDepCount.setIsUnsigned(/*Val=*/true);
9471     }
9472   }
9473   if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9474       DSAStack->getParentOrderedRegionParam()) {
9475     for (auto &RefExpr : VarList) {
9476       assert(RefExpr && "NULL expr in OpenMP shared clause.");
9477       if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9478           (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9479         // It will be analyzed later.
9480         Vars.push_back(RefExpr);
9481         continue;
9482       }
9483 
9484       SourceLocation ELoc = RefExpr->getExprLoc();
9485       auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9486       if (DepKind == OMPC_DEPEND_sink) {
9487         if (DepCounter >= TotalDepCount) {
9488           Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9489           continue;
9490         }
9491         ++DepCounter;
9492         // OpenMP  [2.13.9, Summary]
9493         // depend(dependence-type : vec), where dependence-type is:
9494         // 'sink' and where vec is the iteration vector, which has the form:
9495         //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9496         // where n is the value specified by the ordered clause in the loop
9497         // directive, xi denotes the loop iteration variable of the i-th nested
9498         // loop associated with the loop directive, and di is a constant
9499         // non-negative integer.
9500         SimpleExpr = SimpleExpr->IgnoreImplicit();
9501         auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9502         if (!DE) {
9503           OverloadedOperatorKind OOK = OO_None;
9504           SourceLocation OOLoc;
9505           Expr *LHS, *RHS;
9506           if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9507             OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9508             OOLoc = BO->getOperatorLoc();
9509             LHS = BO->getLHS()->IgnoreParenImpCasts();
9510             RHS = BO->getRHS()->IgnoreParenImpCasts();
9511           } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9512             OOK = OCE->getOperator();
9513             OOLoc = OCE->getOperatorLoc();
9514             LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9515             RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9516           } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9517             OOK = MCE->getMethodDecl()
9518                       ->getNameInfo()
9519                       .getName()
9520                       .getCXXOverloadedOperator();
9521             OOLoc = MCE->getCallee()->getExprLoc();
9522             LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9523             RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9524           } else {
9525             Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9526             continue;
9527           }
9528           DE = dyn_cast<DeclRefExpr>(LHS);
9529           if (!DE) {
9530             Diag(LHS->getExprLoc(),
9531                  diag::err_omp_depend_sink_expected_loop_iteration)
9532                 << DSAStack->getParentLoopControlVariable(
9533                     DepCounter.getZExtValue());
9534             continue;
9535           }
9536           if (OOK != OO_Plus && OOK != OO_Minus) {
9537             Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9538             continue;
9539           }
9540           ExprResult Res = VerifyPositiveIntegerConstantInClause(
9541               RHS, OMPC_depend, /*StrictlyPositive=*/false);
9542           if (Res.isInvalid())
9543             continue;
9544         }
9545         auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9546         if (!CurContext->isDependentContext() &&
9547             DSAStack->getParentOrderedRegionParam() &&
9548             (!VD ||
9549              DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
9550           Diag(DE->getExprLoc(),
9551                diag::err_omp_depend_sink_expected_loop_iteration)
9552               << DSAStack->getParentLoopControlVariable(
9553                   DepCounter.getZExtValue());
9554           continue;
9555         }
9556       } else {
9557         // OpenMP  [2.11.1.1, Restrictions, p.3]
9558         //  A variable that is part of another variable (such as a field of a
9559         //  structure) but is not an array element or an array section cannot
9560         //  appear  in a depend clause.
9561         auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9562         auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9563         auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9564         if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9565             (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
9566             (ASE &&
9567              !ASE->getBase()
9568                   ->getType()
9569                   .getNonReferenceType()
9570                   ->isPointerType() &&
9571              !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
9572           Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9573               << 0 << RefExpr->getSourceRange();
9574           continue;
9575         }
9576       }
9577 
9578       Vars.push_back(RefExpr->IgnoreParenImpCasts());
9579     }
9580 
9581     if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9582         TotalDepCount > VarList.size() &&
9583         DSAStack->getParentOrderedRegionParam()) {
9584       Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9585           << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9586     }
9587     if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9588         Vars.empty())
9589       return nullptr;
9590   }
9591 
9592   return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9593                                  DepLoc, ColonLoc, Vars);
9594 }
9595 
9596 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9597                                          SourceLocation LParenLoc,
9598                                          SourceLocation EndLoc) {
9599   Expr *ValExpr = Device;
9600 
9601   // OpenMP [2.9.1, Restrictions]
9602   // The device expression must evaluate to a non-negative integer value.
9603   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9604                                  /*StrictlyPositive=*/false))
9605     return nullptr;
9606 
9607   return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9608 }
9609 
9610 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9611                                    DSAStackTy *Stack, CXXRecordDecl *RD) {
9612   if (!RD || RD->isInvalidDecl())
9613     return true;
9614 
9615   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9616     if (auto *CTD = CTSD->getSpecializedTemplate())
9617       RD = CTD->getTemplatedDecl();
9618   auto QTy = SemaRef.Context.getRecordType(RD);
9619   if (RD->isDynamicClass()) {
9620     SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9621     SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9622     return false;
9623   }
9624   auto *DC = RD;
9625   bool IsCorrect = true;
9626   for (auto *I : DC->decls()) {
9627     if (I) {
9628       if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9629         if (MD->isStatic()) {
9630           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9631           SemaRef.Diag(MD->getLocation(),
9632                        diag::note_omp_static_member_in_target);
9633           IsCorrect = false;
9634         }
9635       } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9636         if (VD->isStaticDataMember()) {
9637           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9638           SemaRef.Diag(VD->getLocation(),
9639                        diag::note_omp_static_member_in_target);
9640           IsCorrect = false;
9641         }
9642       }
9643     }
9644   }
9645 
9646   for (auto &I : RD->bases()) {
9647     if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9648                                 I.getType()->getAsCXXRecordDecl()))
9649       IsCorrect = false;
9650   }
9651   return IsCorrect;
9652 }
9653 
9654 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9655                               DSAStackTy *Stack, QualType QTy) {
9656   NamedDecl *ND;
9657   if (QTy->isIncompleteType(&ND)) {
9658     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9659     return false;
9660   } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9661     if (!RD->isInvalidDecl() &&
9662         !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9663       return false;
9664   }
9665   return true;
9666 }
9667 
9668 /// \brief Return true if it can be proven that the provided array expression
9669 /// (array section or array subscript) does NOT specify the whole size of the
9670 /// array whose base type is \a BaseQTy.
9671 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9672                                                         const Expr *E,
9673                                                         QualType BaseQTy) {
9674   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9675 
9676   // If this is an array subscript, it refers to the whole size if the size of
9677   // the dimension is constant and equals 1. Also, an array section assumes the
9678   // format of an array subscript if no colon is used.
9679   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9680     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9681       return ATy->getSize().getSExtValue() != 1;
9682     // Size can't be evaluated statically.
9683     return false;
9684   }
9685 
9686   assert(OASE && "Expecting array section if not an array subscript.");
9687   auto *LowerBound = OASE->getLowerBound();
9688   auto *Length = OASE->getLength();
9689 
9690   // If there is a lower bound that does not evaluates to zero, we are not
9691   // convering the whole dimension.
9692   if (LowerBound) {
9693     llvm::APSInt ConstLowerBound;
9694     if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9695       return false; // Can't get the integer value as a constant.
9696     if (ConstLowerBound.getSExtValue())
9697       return true;
9698   }
9699 
9700   // If we don't have a length we covering the whole dimension.
9701   if (!Length)
9702     return false;
9703 
9704   // If the base is a pointer, we don't have a way to get the size of the
9705   // pointee.
9706   if (BaseQTy->isPointerType())
9707     return false;
9708 
9709   // We can only check if the length is the same as the size of the dimension
9710   // if we have a constant array.
9711   auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9712   if (!CATy)
9713     return false;
9714 
9715   llvm::APSInt ConstLength;
9716   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9717     return false; // Can't get the integer value as a constant.
9718 
9719   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9720 }
9721 
9722 // Return true if it can be proven that the provided array expression (array
9723 // section or array subscript) does NOT specify a single element of the array
9724 // whose base type is \a BaseQTy.
9725 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9726                                                        const Expr *E,
9727                                                        QualType BaseQTy) {
9728   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9729 
9730   // An array subscript always refer to a single element. Also, an array section
9731   // assumes the format of an array subscript if no colon is used.
9732   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9733     return false;
9734 
9735   assert(OASE && "Expecting array section if not an array subscript.");
9736   auto *Length = OASE->getLength();
9737 
9738   // If we don't have a length we have to check if the array has unitary size
9739   // for this dimension. Also, we should always expect a length if the base type
9740   // is pointer.
9741   if (!Length) {
9742     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9743       return ATy->getSize().getSExtValue() != 1;
9744     // We cannot assume anything.
9745     return false;
9746   }
9747 
9748   // Check if the length evaluates to 1.
9749   llvm::APSInt ConstLength;
9750   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9751     return false; // Can't get the integer value as a constant.
9752 
9753   return ConstLength.getSExtValue() != 1;
9754 }
9755 
9756 // Return the expression of the base of the map clause or null if it cannot
9757 // be determined and do all the necessary checks to see if the expression is
9758 // valid as a standalone map clause expression. In the process, record all the
9759 // components of the expression.
9760 static Expr *CheckMapClauseExpressionBase(
9761     Sema &SemaRef, Expr *E,
9762     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
9763   SourceLocation ELoc = E->getExprLoc();
9764   SourceRange ERange = E->getSourceRange();
9765 
9766   // The base of elements of list in a map clause have to be either:
9767   //  - a reference to variable or field.
9768   //  - a member expression.
9769   //  - an array expression.
9770   //
9771   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9772   // reference to 'r'.
9773   //
9774   // If we have:
9775   //
9776   // struct SS {
9777   //   Bla S;
9778   //   foo() {
9779   //     #pragma omp target map (S.Arr[:12]);
9780   //   }
9781   // }
9782   //
9783   // We want to retrieve the member expression 'this->S';
9784 
9785   Expr *RelevantExpr = nullptr;
9786 
9787   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9788   //  If a list item is an array section, it must specify contiguous storage.
9789   //
9790   // For this restriction it is sufficient that we make sure only references
9791   // to variables or fields and array expressions, and that no array sections
9792   // exist except in the rightmost expression (unless they cover the whole
9793   // dimension of the array). E.g. these would be invalid:
9794   //
9795   //   r.ArrS[3:5].Arr[6:7]
9796   //
9797   //   r.ArrS[3:5].x
9798   //
9799   // but these would be valid:
9800   //   r.ArrS[3].Arr[6:7]
9801   //
9802   //   r.ArrS[3].x
9803 
9804   bool AllowUnitySizeArraySection = true;
9805   bool AllowWholeSizeArraySection = true;
9806 
9807   while (!RelevantExpr) {
9808     E = E->IgnoreParenImpCasts();
9809 
9810     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9811       if (!isa<VarDecl>(CurE->getDecl()))
9812         break;
9813 
9814       RelevantExpr = CurE;
9815 
9816       // If we got a reference to a declaration, we should not expect any array
9817       // section before that.
9818       AllowUnitySizeArraySection = false;
9819       AllowWholeSizeArraySection = false;
9820 
9821       // Record the component.
9822       CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9823           CurE, CurE->getDecl()));
9824       continue;
9825     }
9826 
9827     if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9828       auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9829 
9830       if (isa<CXXThisExpr>(BaseE))
9831         // We found a base expression: this->Val.
9832         RelevantExpr = CurE;
9833       else
9834         E = BaseE;
9835 
9836       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9837         SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9838             << CurE->getSourceRange();
9839         break;
9840       }
9841 
9842       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9843 
9844       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9845       //  A bit-field cannot appear in a map clause.
9846       //
9847       if (FD->isBitField()) {
9848         SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9849             << CurE->getSourceRange();
9850         break;
9851       }
9852 
9853       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9854       //  If the type of a list item is a reference to a type T then the type
9855       //  will be considered to be T for all purposes of this clause.
9856       QualType CurType = BaseE->getType().getNonReferenceType();
9857 
9858       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9859       //  A list item cannot be a variable that is a member of a structure with
9860       //  a union type.
9861       //
9862       if (auto *RT = CurType->getAs<RecordType>())
9863         if (RT->isUnionType()) {
9864           SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9865               << CurE->getSourceRange();
9866           break;
9867         }
9868 
9869       // If we got a member expression, we should not expect any array section
9870       // before that:
9871       //
9872       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9873       //  If a list item is an element of a structure, only the rightmost symbol
9874       //  of the variable reference can be an array section.
9875       //
9876       AllowUnitySizeArraySection = false;
9877       AllowWholeSizeArraySection = false;
9878 
9879       // Record the component.
9880       CurComponents.push_back(
9881           OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
9882       continue;
9883     }
9884 
9885     if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9886       E = CurE->getBase()->IgnoreParenImpCasts();
9887 
9888       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9889         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9890             << 0 << CurE->getSourceRange();
9891         break;
9892       }
9893 
9894       // If we got an array subscript that express the whole dimension we
9895       // can have any array expressions before. If it only expressing part of
9896       // the dimension, we can only have unitary-size array expressions.
9897       if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9898                                                       E->getType()))
9899         AllowWholeSizeArraySection = false;
9900 
9901       // Record the component - we don't have any declaration associated.
9902       CurComponents.push_back(
9903           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
9904       continue;
9905     }
9906 
9907     if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9908       E = CurE->getBase()->IgnoreParenImpCasts();
9909 
9910       auto CurType =
9911           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9912 
9913       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9914       //  If the type of a list item is a reference to a type T then the type
9915       //  will be considered to be T for all purposes of this clause.
9916       if (CurType->isReferenceType())
9917         CurType = CurType->getPointeeType();
9918 
9919       bool IsPointer = CurType->isAnyPointerType();
9920 
9921       if (!IsPointer && !CurType->isArrayType()) {
9922         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9923             << 0 << CurE->getSourceRange();
9924         break;
9925       }
9926 
9927       bool NotWhole =
9928           CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9929       bool NotUnity =
9930           CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9931 
9932       if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9933         // Any array section is currently allowed.
9934         //
9935         // If this array section refers to the whole dimension we can still
9936         // accept other array sections before this one, except if the base is a
9937         // pointer. Otherwise, only unitary sections are accepted.
9938         if (NotWhole || IsPointer)
9939           AllowWholeSizeArraySection = false;
9940       } else if ((AllowUnitySizeArraySection && NotUnity) ||
9941                  (AllowWholeSizeArraySection && NotWhole)) {
9942         // A unity or whole array section is not allowed and that is not
9943         // compatible with the properties of the current array section.
9944         SemaRef.Diag(
9945             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9946             << CurE->getSourceRange();
9947         break;
9948       }
9949 
9950       // Record the component - we don't have any declaration associated.
9951       CurComponents.push_back(
9952           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
9953       continue;
9954     }
9955 
9956     // If nothing else worked, this is not a valid map clause expression.
9957     SemaRef.Diag(ELoc,
9958                  diag::err_omp_expected_named_var_member_or_array_expression)
9959         << ERange;
9960     break;
9961   }
9962 
9963   return RelevantExpr;
9964 }
9965 
9966 // Return true if expression E associated with value VD has conflicts with other
9967 // map information.
9968 static bool CheckMapConflicts(
9969     Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
9970     bool CurrentRegionOnly,
9971     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
9972   assert(VD && E);
9973   SourceLocation ELoc = E->getExprLoc();
9974   SourceRange ERange = E->getSourceRange();
9975 
9976   // In order to easily check the conflicts we need to match each component of
9977   // the expression under test with the components of the expressions that are
9978   // already in the stack.
9979 
9980   assert(!CurComponents.empty() && "Map clause expression with no components!");
9981   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
9982          "Map clause expression with unexpected base!");
9983 
9984   // Variables to help detecting enclosing problems in data environment nests.
9985   bool IsEnclosedByDataEnvironmentExpr = false;
9986   const Expr *EnclosingExpr = nullptr;
9987 
9988   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
9989       VD, CurrentRegionOnly,
9990       [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
9991               StackComponents) -> bool {
9992 
9993         assert(!StackComponents.empty() &&
9994                "Map clause expression with no components!");
9995         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
9996                "Map clause expression with unexpected base!");
9997 
9998         // The whole expression in the stack.
9999         auto *RE = StackComponents.front().getAssociatedExpression();
10000 
10001         // Expressions must start from the same base. Here we detect at which
10002         // point both expressions diverge from each other and see if we can
10003         // detect if the memory referred to both expressions is contiguous and
10004         // do not overlap.
10005         auto CI = CurComponents.rbegin();
10006         auto CE = CurComponents.rend();
10007         auto SI = StackComponents.rbegin();
10008         auto SE = StackComponents.rend();
10009         for (; CI != CE && SI != SE; ++CI, ++SI) {
10010 
10011           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10012           //  At most one list item can be an array item derived from a given
10013           //  variable in map clauses of the same construct.
10014           if (CurrentRegionOnly &&
10015               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10016                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10017               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10018                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10019             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
10020                          diag::err_omp_multiple_array_items_in_map_clause)
10021                 << CI->getAssociatedExpression()->getSourceRange();
10022             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10023                          diag::note_used_here)
10024                 << SI->getAssociatedExpression()->getSourceRange();
10025             return true;
10026           }
10027 
10028           // Do both expressions have the same kind?
10029           if (CI->getAssociatedExpression()->getStmtClass() !=
10030               SI->getAssociatedExpression()->getStmtClass())
10031             break;
10032 
10033           // Are we dealing with different variables/fields?
10034           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
10035             break;
10036         }
10037 
10038         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10039         //  List items of map clauses in the same construct must not share
10040         //  original storage.
10041         //
10042         // If the expressions are exactly the same or one is a subset of the
10043         // other, it means they are sharing storage.
10044         if (CI == CE && SI == SE) {
10045           if (CurrentRegionOnly) {
10046             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10047             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10048                 << RE->getSourceRange();
10049             return true;
10050           } else {
10051             // If we find the same expression in the enclosing data environment,
10052             // that is legal.
10053             IsEnclosedByDataEnvironmentExpr = true;
10054             return false;
10055           }
10056         }
10057 
10058         QualType DerivedType =
10059             std::prev(CI)->getAssociatedDeclaration()->getType();
10060         SourceLocation DerivedLoc =
10061             std::prev(CI)->getAssociatedExpression()->getExprLoc();
10062 
10063         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10064         //  If the type of a list item is a reference to a type T then the type
10065         //  will be considered to be T for all purposes of this clause.
10066         DerivedType = DerivedType.getNonReferenceType();
10067 
10068         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10069         //  A variable for which the type is pointer and an array section
10070         //  derived from that variable must not appear as list items of map
10071         //  clauses of the same construct.
10072         //
10073         // Also, cover one of the cases in:
10074         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10075         //  If any part of the original storage of a list item has corresponding
10076         //  storage in the device data environment, all of the original storage
10077         //  must have corresponding storage in the device data environment.
10078         //
10079         if (DerivedType->isAnyPointerType()) {
10080           if (CI == CE || SI == SE) {
10081             SemaRef.Diag(
10082                 DerivedLoc,
10083                 diag::err_omp_pointer_mapped_along_with_derived_section)
10084                 << DerivedLoc;
10085           } else {
10086             assert(CI != CE && SI != SE);
10087             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10088                 << DerivedLoc;
10089           }
10090           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10091               << RE->getSourceRange();
10092           return true;
10093         }
10094 
10095         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10096         //  List items of map clauses in the same construct must not share
10097         //  original storage.
10098         //
10099         // An expression is a subset of the other.
10100         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10101           SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10102           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10103               << RE->getSourceRange();
10104           return true;
10105         }
10106 
10107         // The current expression uses the same base as other expression in the
10108         // data environment but does not contain it completely.
10109         if (!CurrentRegionOnly && SI != SE)
10110           EnclosingExpr = RE;
10111 
10112         // The current expression is a subset of the expression in the data
10113         // environment.
10114         IsEnclosedByDataEnvironmentExpr |=
10115             (!CurrentRegionOnly && CI != CE && SI == SE);
10116 
10117         return false;
10118       });
10119 
10120   if (CurrentRegionOnly)
10121     return FoundError;
10122 
10123   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10124   //  If any part of the original storage of a list item has corresponding
10125   //  storage in the device data environment, all of the original storage must
10126   //  have corresponding storage in the device data environment.
10127   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10128   //  If a list item is an element of a structure, and a different element of
10129   //  the structure has a corresponding list item in the device data environment
10130   //  prior to a task encountering the construct associated with the map clause,
10131   //  then the list item must also have a corresponding list item in the device
10132   //  data environment prior to the task encountering the construct.
10133   //
10134   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10135     SemaRef.Diag(ELoc,
10136                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
10137         << ERange;
10138     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10139         << EnclosingExpr->getSourceRange();
10140     return true;
10141   }
10142 
10143   return FoundError;
10144 }
10145 
10146 OMPClause *
10147 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10148                            OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10149                            SourceLocation MapLoc, SourceLocation ColonLoc,
10150                            ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10151                            SourceLocation LParenLoc, SourceLocation EndLoc) {
10152   SmallVector<Expr *, 4> Vars;
10153 
10154   // Keep track of the mappable components and base declarations in this clause.
10155   // Each entry in the list is going to have a list of components associated. We
10156   // record each set of the components so that we can build the clause later on.
10157   // In the end we should have the same amount of declarations and component
10158   // lists.
10159   OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10160   SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10161 
10162   ClauseComponents.reserve(VarList.size());
10163   ClauseBaseDeclarations.reserve(VarList.size());
10164 
10165   for (auto &RE : VarList) {
10166     assert(RE && "Null expr in omp map");
10167     if (isa<DependentScopeDeclRefExpr>(RE)) {
10168       // It will be analyzed later.
10169       Vars.push_back(RE);
10170       continue;
10171     }
10172     SourceLocation ELoc = RE->getExprLoc();
10173 
10174     auto *VE = RE->IgnoreParenLValueCasts();
10175 
10176     if (VE->isValueDependent() || VE->isTypeDependent() ||
10177         VE->isInstantiationDependent() ||
10178         VE->containsUnexpandedParameterPack()) {
10179       // We can only analyze this information once the missing information is
10180       // resolved.
10181       Vars.push_back(RE);
10182       continue;
10183     }
10184 
10185     auto *SimpleExpr = RE->IgnoreParenCasts();
10186 
10187     if (!RE->IgnoreParenImpCasts()->isLValue()) {
10188       Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10189           << RE->getSourceRange();
10190       continue;
10191     }
10192 
10193     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10194     ValueDecl *CurDeclaration = nullptr;
10195 
10196     // Obtain the array or member expression bases if required. Also, fill the
10197     // components array with all the components identified in the process.
10198     auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
10199     if (!BE)
10200       continue;
10201 
10202     assert(!CurComponents.empty() &&
10203            "Invalid mappable expression information.");
10204 
10205     // For the following checks, we rely on the base declaration which is
10206     // expected to be associated with the last component. The declaration is
10207     // expected to be a variable or a field (if 'this' is being mapped).
10208     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10209     assert(CurDeclaration && "Null decl on map clause.");
10210     assert(
10211         CurDeclaration->isCanonicalDecl() &&
10212         "Expecting components to have associated only canonical declarations.");
10213 
10214     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10215     auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
10216 
10217     assert((VD || FD) && "Only variables or fields are expected here!");
10218     (void)FD;
10219 
10220     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10221     //  threadprivate variables cannot appear in a map clause.
10222     if (VD && DSAStack->isThreadPrivate(VD)) {
10223       auto DVar = DSAStack->getTopDSA(VD, false);
10224       Diag(ELoc, diag::err_omp_threadprivate_in_map);
10225       ReportOriginalDSA(*this, DSAStack, VD, DVar);
10226       continue;
10227     }
10228 
10229     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10230     //  A list item cannot appear in both a map clause and a data-sharing
10231     //  attribute clause on the same construct.
10232     //
10233     // TODO: Implement this check - it cannot currently be tested because of
10234     // missing implementation of the other data sharing clauses in target
10235     // directives.
10236 
10237     // Check conflicts with other map clause expressions. We check the conflicts
10238     // with the current construct separately from the enclosing data
10239     // environment, because the restrictions are different.
10240     if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10241                           /*CurrentRegionOnly=*/true, CurComponents))
10242       break;
10243     if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10244                           /*CurrentRegionOnly=*/false, CurComponents))
10245       break;
10246 
10247     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10248     //  If the type of a list item is a reference to a type T then the type will
10249     //  be considered to be T for all purposes of this clause.
10250     QualType Type = CurDeclaration->getType().getNonReferenceType();
10251 
10252     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10253     //  A list item must have a mappable type.
10254     if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10255                            DSAStack, Type))
10256       continue;
10257 
10258     // target enter data
10259     // OpenMP [2.10.2, Restrictions, p. 99]
10260     // A map-type must be specified in all map clauses and must be either
10261     // to or alloc.
10262     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10263     if (DKind == OMPD_target_enter_data &&
10264         !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10265       Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10266           << (IsMapTypeImplicit ? 1 : 0)
10267           << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10268           << getOpenMPDirectiveName(DKind);
10269       continue;
10270     }
10271 
10272     // target exit_data
10273     // OpenMP [2.10.3, Restrictions, p. 102]
10274     // A map-type must be specified in all map clauses and must be either
10275     // from, release, or delete.
10276     DKind = DSAStack->getCurrentDirective();
10277     if (DKind == OMPD_target_exit_data &&
10278         !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10279           MapType == OMPC_MAP_delete)) {
10280       Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10281           << (IsMapTypeImplicit ? 1 : 0)
10282           << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10283           << getOpenMPDirectiveName(DKind);
10284       continue;
10285     }
10286 
10287     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10288     // A list item cannot appear in both a map clause and a data-sharing
10289     // attribute clause on the same construct
10290     if (DKind == OMPD_target && VD) {
10291       auto DVar = DSAStack->getTopDSA(VD, false);
10292       if (isOpenMPPrivate(DVar.CKind)) {
10293         Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10294             << getOpenMPClauseName(DVar.CKind)
10295             << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10296         ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
10297         continue;
10298       }
10299     }
10300 
10301     // Save the current expression.
10302     Vars.push_back(RE);
10303 
10304     // Store the components in the stack so that they can be used to check
10305     // against other clauses later on.
10306     DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10307 
10308     // Save the components and declaration to create the clause. For purposes of
10309     // the clause creation, any component list that has has base 'this' uses
10310     // null has
10311     ClauseComponents.resize(ClauseComponents.size() + 1);
10312     ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10313     ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10314                                                          : CurDeclaration);
10315   }
10316 
10317   // We need to produce a map clause even if we don't have variables so that
10318   // other diagnostics related with non-existing map clauses are accurate.
10319   return OMPMapClause::Create(
10320       Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10321       ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
10322 }
10323 
10324 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10325                                                TypeResult ParsedType) {
10326   assert(ParsedType.isUsable());
10327 
10328   QualType ReductionType = GetTypeFromParser(ParsedType.get());
10329   if (ReductionType.isNull())
10330     return QualType();
10331 
10332   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10333   // A type name in a declare reduction directive cannot be a function type, an
10334   // array type, a reference type, or a type qualified with const, volatile or
10335   // restrict.
10336   if (ReductionType.hasQualifiers()) {
10337     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10338     return QualType();
10339   }
10340 
10341   if (ReductionType->isFunctionType()) {
10342     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10343     return QualType();
10344   }
10345   if (ReductionType->isReferenceType()) {
10346     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10347     return QualType();
10348   }
10349   if (ReductionType->isArrayType()) {
10350     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10351     return QualType();
10352   }
10353   return ReductionType;
10354 }
10355 
10356 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10357     Scope *S, DeclContext *DC, DeclarationName Name,
10358     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10359     AccessSpecifier AS, Decl *PrevDeclInScope) {
10360   SmallVector<Decl *, 8> Decls;
10361   Decls.reserve(ReductionTypes.size());
10362 
10363   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10364                       ForRedeclaration);
10365   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10366   // A reduction-identifier may not be re-declared in the current scope for the
10367   // same type or for a type that is compatible according to the base language
10368   // rules.
10369   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10370   OMPDeclareReductionDecl *PrevDRD = nullptr;
10371   bool InCompoundScope = true;
10372   if (S != nullptr) {
10373     // Find previous declaration with the same name not referenced in other
10374     // declarations.
10375     FunctionScopeInfo *ParentFn = getEnclosingFunction();
10376     InCompoundScope =
10377         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10378     LookupName(Lookup, S);
10379     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10380                          /*AllowInlineNamespace=*/false);
10381     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10382     auto Filter = Lookup.makeFilter();
10383     while (Filter.hasNext()) {
10384       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10385       if (InCompoundScope) {
10386         auto I = UsedAsPrevious.find(PrevDecl);
10387         if (I == UsedAsPrevious.end())
10388           UsedAsPrevious[PrevDecl] = false;
10389         if (auto *D = PrevDecl->getPrevDeclInScope())
10390           UsedAsPrevious[D] = true;
10391       }
10392       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10393           PrevDecl->getLocation();
10394     }
10395     Filter.done();
10396     if (InCompoundScope) {
10397       for (auto &PrevData : UsedAsPrevious) {
10398         if (!PrevData.second) {
10399           PrevDRD = PrevData.first;
10400           break;
10401         }
10402       }
10403     }
10404   } else if (PrevDeclInScope != nullptr) {
10405     auto *PrevDRDInScope = PrevDRD =
10406         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10407     do {
10408       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10409           PrevDRDInScope->getLocation();
10410       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10411     } while (PrevDRDInScope != nullptr);
10412   }
10413   for (auto &TyData : ReductionTypes) {
10414     auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10415     bool Invalid = false;
10416     if (I != PreviousRedeclTypes.end()) {
10417       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10418           << TyData.first;
10419       Diag(I->second, diag::note_previous_definition);
10420       Invalid = true;
10421     }
10422     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10423     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10424                                                 Name, TyData.first, PrevDRD);
10425     DC->addDecl(DRD);
10426     DRD->setAccess(AS);
10427     Decls.push_back(DRD);
10428     if (Invalid)
10429       DRD->setInvalidDecl();
10430     else
10431       PrevDRD = DRD;
10432   }
10433 
10434   return DeclGroupPtrTy::make(
10435       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10436 }
10437 
10438 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10439   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10440 
10441   // Enter new function scope.
10442   PushFunctionScope();
10443   getCurFunction()->setHasBranchProtectedScope();
10444   getCurFunction()->setHasOMPDeclareReductionCombiner();
10445 
10446   if (S != nullptr)
10447     PushDeclContext(S, DRD);
10448   else
10449     CurContext = DRD;
10450 
10451   PushExpressionEvaluationContext(PotentiallyEvaluated);
10452 
10453   QualType ReductionType = DRD->getType();
10454   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10455   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10456   // uses semantics of argument handles by value, but it should be passed by
10457   // reference. C lang does not support references, so pass all parameters as
10458   // pointers.
10459   // Create 'T omp_in;' variable.
10460   auto *OmpInParm =
10461       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
10462   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10463   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10464   // uses semantics of argument handles by value, but it should be passed by
10465   // reference. C lang does not support references, so pass all parameters as
10466   // pointers.
10467   // Create 'T omp_out;' variable.
10468   auto *OmpOutParm =
10469       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10470   if (S != nullptr) {
10471     PushOnScopeChains(OmpInParm, S);
10472     PushOnScopeChains(OmpOutParm, S);
10473   } else {
10474     DRD->addDecl(OmpInParm);
10475     DRD->addDecl(OmpOutParm);
10476   }
10477 }
10478 
10479 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10480   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10481   DiscardCleanupsInEvaluationContext();
10482   PopExpressionEvaluationContext();
10483 
10484   PopDeclContext();
10485   PopFunctionScopeInfo();
10486 
10487   if (Combiner != nullptr)
10488     DRD->setCombiner(Combiner);
10489   else
10490     DRD->setInvalidDecl();
10491 }
10492 
10493 void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10494   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10495 
10496   // Enter new function scope.
10497   PushFunctionScope();
10498   getCurFunction()->setHasBranchProtectedScope();
10499 
10500   if (S != nullptr)
10501     PushDeclContext(S, DRD);
10502   else
10503     CurContext = DRD;
10504 
10505   PushExpressionEvaluationContext(PotentiallyEvaluated);
10506 
10507   QualType ReductionType = DRD->getType();
10508   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10509   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10510   // uses semantics of argument handles by value, but it should be passed by
10511   // reference. C lang does not support references, so pass all parameters as
10512   // pointers.
10513   // Create 'T omp_priv;' variable.
10514   auto *OmpPrivParm =
10515       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
10516   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10517   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10518   // uses semantics of argument handles by value, but it should be passed by
10519   // reference. C lang does not support references, so pass all parameters as
10520   // pointers.
10521   // Create 'T omp_orig;' variable.
10522   auto *OmpOrigParm =
10523       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
10524   if (S != nullptr) {
10525     PushOnScopeChains(OmpPrivParm, S);
10526     PushOnScopeChains(OmpOrigParm, S);
10527   } else {
10528     DRD->addDecl(OmpPrivParm);
10529     DRD->addDecl(OmpOrigParm);
10530   }
10531 }
10532 
10533 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10534                                                      Expr *Initializer) {
10535   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10536   DiscardCleanupsInEvaluationContext();
10537   PopExpressionEvaluationContext();
10538 
10539   PopDeclContext();
10540   PopFunctionScopeInfo();
10541 
10542   if (Initializer != nullptr)
10543     DRD->setInitializer(Initializer);
10544   else
10545     DRD->setInvalidDecl();
10546 }
10547 
10548 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10549     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10550   for (auto *D : DeclReductions.get()) {
10551     if (IsValid) {
10552       auto *DRD = cast<OMPDeclareReductionDecl>(D);
10553       if (S != nullptr)
10554         PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10555     } else
10556       D->setInvalidDecl();
10557   }
10558   return DeclReductions;
10559 }
10560 
10561 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10562                                            SourceLocation StartLoc,
10563                                            SourceLocation LParenLoc,
10564                                            SourceLocation EndLoc) {
10565   Expr *ValExpr = NumTeams;
10566 
10567   // OpenMP [teams Constrcut, Restrictions]
10568   // The num_teams expression must evaluate to a positive integer value.
10569   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10570                                  /*StrictlyPositive=*/true))
10571     return nullptr;
10572 
10573   return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10574 }
10575 
10576 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10577                                               SourceLocation StartLoc,
10578                                               SourceLocation LParenLoc,
10579                                               SourceLocation EndLoc) {
10580   Expr *ValExpr = ThreadLimit;
10581 
10582   // OpenMP [teams Constrcut, Restrictions]
10583   // The thread_limit expression must evaluate to a positive integer value.
10584   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10585                                  /*StrictlyPositive=*/true))
10586     return nullptr;
10587 
10588   return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10589                                             EndLoc);
10590 }
10591 
10592 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10593                                            SourceLocation StartLoc,
10594                                            SourceLocation LParenLoc,
10595                                            SourceLocation EndLoc) {
10596   Expr *ValExpr = Priority;
10597 
10598   // OpenMP [2.9.1, task Constrcut]
10599   // The priority-value is a non-negative numerical scalar expression.
10600   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10601                                  /*StrictlyPositive=*/false))
10602     return nullptr;
10603 
10604   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10605 }
10606 
10607 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10608                                             SourceLocation StartLoc,
10609                                             SourceLocation LParenLoc,
10610                                             SourceLocation EndLoc) {
10611   Expr *ValExpr = Grainsize;
10612 
10613   // OpenMP [2.9.2, taskloop Constrcut]
10614   // The parameter of the grainsize clause must be a positive integer
10615   // expression.
10616   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10617                                  /*StrictlyPositive=*/true))
10618     return nullptr;
10619 
10620   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10621 }
10622 
10623 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10624                                            SourceLocation StartLoc,
10625                                            SourceLocation LParenLoc,
10626                                            SourceLocation EndLoc) {
10627   Expr *ValExpr = NumTasks;
10628 
10629   // OpenMP [2.9.2, taskloop Constrcut]
10630   // The parameter of the num_tasks clause must be a positive integer
10631   // expression.
10632   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10633                                  /*StrictlyPositive=*/true))
10634     return nullptr;
10635 
10636   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10637 }
10638 
10639 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10640                                        SourceLocation LParenLoc,
10641                                        SourceLocation EndLoc) {
10642   // OpenMP [2.13.2, critical construct, Description]
10643   // ... where hint-expression is an integer constant expression that evaluates
10644   // to a valid lock hint.
10645   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10646   if (HintExpr.isInvalid())
10647     return nullptr;
10648   return new (Context)
10649       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10650 }
10651 
10652 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10653     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10654     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10655     SourceLocation EndLoc) {
10656   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10657     std::string Values;
10658     Values += "'";
10659     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10660     Values += "'";
10661     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10662         << Values << getOpenMPClauseName(OMPC_dist_schedule);
10663     return nullptr;
10664   }
10665   Expr *ValExpr = ChunkSize;
10666   Stmt *HelperValStmt = nullptr;
10667   if (ChunkSize) {
10668     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10669         !ChunkSize->isInstantiationDependent() &&
10670         !ChunkSize->containsUnexpandedParameterPack()) {
10671       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10672       ExprResult Val =
10673           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10674       if (Val.isInvalid())
10675         return nullptr;
10676 
10677       ValExpr = Val.get();
10678 
10679       // OpenMP [2.7.1, Restrictions]
10680       //  chunk_size must be a loop invariant integer expression with a positive
10681       //  value.
10682       llvm::APSInt Result;
10683       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10684         if (Result.isSigned() && !Result.isStrictlyPositive()) {
10685           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10686               << "dist_schedule" << ChunkSize->getSourceRange();
10687           return nullptr;
10688         }
10689       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
10690         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10691         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10692         HelperValStmt = buildPreInits(Context, Captures);
10693       }
10694     }
10695   }
10696 
10697   return new (Context)
10698       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
10699                             Kind, ValExpr, HelperValStmt);
10700 }
10701 
10702 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10703     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10704     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10705     SourceLocation KindLoc, SourceLocation EndLoc) {
10706   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10707   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10708       Kind != OMPC_DEFAULTMAP_scalar) {
10709     std::string Value;
10710     SourceLocation Loc;
10711     Value += "'";
10712     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10713       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10714                  OMPC_DEFAULTMAP_MODIFIER_tofrom);
10715       Loc = MLoc;
10716     } else {
10717       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10718                  OMPC_DEFAULTMAP_scalar);
10719       Loc = KindLoc;
10720     }
10721     Value += "'";
10722     Diag(Loc, diag::err_omp_unexpected_clause_value)
10723         << Value << getOpenMPClauseName(OMPC_defaultmap);
10724     return nullptr;
10725   }
10726 
10727   return new (Context)
10728       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10729 }
10730 
10731 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10732   DeclContext *CurLexicalContext = getCurLexicalContext();
10733   if (!CurLexicalContext->isFileContext() &&
10734       !CurLexicalContext->isExternCContext() &&
10735       !CurLexicalContext->isExternCXXContext()) {
10736     Diag(Loc, diag::err_omp_region_not_file_context);
10737     return false;
10738   }
10739   if (IsInOpenMPDeclareTargetContext) {
10740     Diag(Loc, diag::err_omp_enclosed_declare_target);
10741     return false;
10742   }
10743 
10744   IsInOpenMPDeclareTargetContext = true;
10745   return true;
10746 }
10747 
10748 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10749   assert(IsInOpenMPDeclareTargetContext &&
10750          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10751 
10752   IsInOpenMPDeclareTargetContext = false;
10753 }
10754 
10755 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10756                                      Sema &SemaRef, Decl *D) {
10757   if (!D)
10758     return;
10759   Decl *LD = nullptr;
10760   if (isa<TagDecl>(D)) {
10761     LD = cast<TagDecl>(D)->getDefinition();
10762   } else if (isa<VarDecl>(D)) {
10763     LD = cast<VarDecl>(D)->getDefinition();
10764 
10765     // If this is an implicit variable that is legal and we do not need to do
10766     // anything.
10767     if (cast<VarDecl>(D)->isImplicit()) {
10768       D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10769       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10770         ML->DeclarationMarkedOpenMPDeclareTarget(D);
10771       return;
10772     }
10773 
10774   } else if (isa<FunctionDecl>(D)) {
10775     const FunctionDecl *FD = nullptr;
10776     if (cast<FunctionDecl>(D)->hasBody(FD))
10777       LD = const_cast<FunctionDecl *>(FD);
10778 
10779     // If the definition is associated with the current declaration in the
10780     // target region (it can be e.g. a lambda) that is legal and we do not need
10781     // to do anything else.
10782     if (LD == D) {
10783       D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10784       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10785         ML->DeclarationMarkedOpenMPDeclareTarget(D);
10786       return;
10787     }
10788   }
10789   if (!LD)
10790     LD = D;
10791   if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10792       (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10793     // Outlined declaration is not declared target.
10794     if (LD->isOutOfLine()) {
10795       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10796       SemaRef.Diag(SL, diag::note_used_here) << SR;
10797     } else {
10798       DeclContext *DC = LD->getDeclContext();
10799       while (DC) {
10800         if (isa<FunctionDecl>(DC) &&
10801             cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10802           break;
10803         DC = DC->getParent();
10804       }
10805       if (DC)
10806         return;
10807 
10808       // Is not declared in target context.
10809       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10810       SemaRef.Diag(SL, diag::note_used_here) << SR;
10811     }
10812     // Mark decl as declared target to prevent further diagnostic.
10813     D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10814     if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10815       ML->DeclarationMarkedOpenMPDeclareTarget(D);
10816   }
10817 }
10818 
10819 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10820                                    Sema &SemaRef, DSAStackTy *Stack,
10821                                    ValueDecl *VD) {
10822   if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10823     return true;
10824   if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10825     return false;
10826   return true;
10827 }
10828 
10829 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10830   if (!D || D->isInvalidDecl())
10831     return;
10832   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10833   SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10834   // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10835   if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10836     if (DSAStack->isThreadPrivate(VD)) {
10837       Diag(SL, diag::err_omp_threadprivate_in_target);
10838       ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10839       return;
10840     }
10841   }
10842   if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10843     // Problem if any with var declared with incomplete type will be reported
10844     // as normal, so no need to check it here.
10845     if ((E || !VD->getType()->isIncompleteType()) &&
10846         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10847       // Mark decl as declared target to prevent further diagnostic.
10848       if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10849         VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10850         if (ASTMutationListener *ML = Context.getASTMutationListener())
10851           ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10852       }
10853       return;
10854     }
10855   }
10856   if (!E) {
10857     // Checking declaration inside declare target region.
10858     if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10859         (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10860       D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10861       if (ASTMutationListener *ML = Context.getASTMutationListener())
10862         ML->DeclarationMarkedOpenMPDeclareTarget(D);
10863     }
10864     return;
10865   }
10866   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10867 }
10868