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 (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1420       // Skip internally declared variables.
1421       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1422         return;
1423 
1424       auto DVar = Stack->getTopDSA(VD, false);
1425       // Check if the variable has explicit DSA set and stop analysis if it so.
1426       if (DVar.RefExpr) return;
1427 
1428       auto ELoc = E->getExprLoc();
1429       auto DKind = Stack->getCurrentDirective();
1430       // The default(none) clause requires that each variable that is referenced
1431       // in the construct, and does not have a predetermined data-sharing
1432       // attribute, must have its data-sharing attribute explicitly determined
1433       // by being listed in a data-sharing attribute clause.
1434       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1435           isParallelOrTaskRegion(DKind) &&
1436           VarsWithInheritedDSA.count(VD) == 0) {
1437         VarsWithInheritedDSA[VD] = E;
1438         return;
1439       }
1440 
1441       // OpenMP [2.9.3.6, Restrictions, p.2]
1442       //  A list item that appears in a reduction clause of the innermost
1443       //  enclosing worksharing or parallel construct may not be accessed in an
1444       //  explicit task.
1445       DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
1446                                     [](OpenMPDirectiveKind K) -> bool {
1447                                       return isOpenMPParallelDirective(K) ||
1448                                              isOpenMPWorksharingDirective(K) ||
1449                                              isOpenMPTeamsDirective(K);
1450                                     },
1451                                     false);
1452       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1453         ErrorFound = true;
1454         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1455         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1456         return;
1457       }
1458 
1459       // Define implicit data-sharing attributes for task.
1460       DVar = Stack->getImplicitDSA(VD, false);
1461       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1462           !Stack->isLoopControlVariable(VD).first)
1463         ImplicitFirstprivate.push_back(E);
1464     }
1465   }
1466   void VisitMemberExpr(MemberExpr *E) {
1467     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1468       if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1469         auto DVar = Stack->getTopDSA(FD, false);
1470         // Check if the variable has explicit DSA set and stop analysis if it
1471         // so.
1472         if (DVar.RefExpr)
1473           return;
1474 
1475         auto ELoc = E->getExprLoc();
1476         auto DKind = Stack->getCurrentDirective();
1477         // OpenMP [2.9.3.6, Restrictions, p.2]
1478         //  A list item that appears in a reduction clause of the innermost
1479         //  enclosing worksharing or parallel construct may not be accessed in
1480         //  an  explicit task.
1481         DVar =
1482             Stack->hasInnermostDSA(FD, MatchesAnyClause(OMPC_reduction),
1483                                    [](OpenMPDirectiveKind K) -> bool {
1484                                      return isOpenMPParallelDirective(K) ||
1485                                             isOpenMPWorksharingDirective(K) ||
1486                                             isOpenMPTeamsDirective(K);
1487                                    },
1488                                    false);
1489         if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1490           ErrorFound = true;
1491           SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1492           ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1493           return;
1494         }
1495 
1496         // Define implicit data-sharing attributes for task.
1497         DVar = Stack->getImplicitDSA(FD, false);
1498         if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1499             !Stack->isLoopControlVariable(FD).first)
1500           ImplicitFirstprivate.push_back(E);
1501       }
1502     }
1503   }
1504   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1505     for (auto *C : S->clauses()) {
1506       // Skip analysis of arguments of implicitly defined firstprivate clause
1507       // for task directives.
1508       if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1509         for (auto *CC : C->children()) {
1510           if (CC)
1511             Visit(CC);
1512         }
1513     }
1514   }
1515   void VisitStmt(Stmt *S) {
1516     for (auto *C : S->children()) {
1517       if (C && !isa<OMPExecutableDirective>(C))
1518         Visit(C);
1519     }
1520   }
1521 
1522   bool isErrorFound() { return ErrorFound; }
1523   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
1524   llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
1525     return VarsWithInheritedDSA;
1526   }
1527 
1528   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1529       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1530 };
1531 } // namespace
1532 
1533 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1534   switch (DKind) {
1535   case OMPD_parallel: {
1536     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1537     QualType KmpInt32PtrTy =
1538         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1539     Sema::CapturedParamNameType Params[] = {
1540         std::make_pair(".global_tid.", KmpInt32PtrTy),
1541         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1542         std::make_pair(StringRef(), QualType()) // __context with shared vars
1543     };
1544     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1545                              Params);
1546     break;
1547   }
1548   case OMPD_simd: {
1549     Sema::CapturedParamNameType Params[] = {
1550         std::make_pair(StringRef(), QualType()) // __context with shared vars
1551     };
1552     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1553                              Params);
1554     break;
1555   }
1556   case OMPD_for: {
1557     Sema::CapturedParamNameType Params[] = {
1558         std::make_pair(StringRef(), QualType()) // __context with shared vars
1559     };
1560     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1561                              Params);
1562     break;
1563   }
1564   case OMPD_for_simd: {
1565     Sema::CapturedParamNameType Params[] = {
1566         std::make_pair(StringRef(), QualType()) // __context with shared vars
1567     };
1568     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1569                              Params);
1570     break;
1571   }
1572   case OMPD_sections: {
1573     Sema::CapturedParamNameType Params[] = {
1574         std::make_pair(StringRef(), QualType()) // __context with shared vars
1575     };
1576     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1577                              Params);
1578     break;
1579   }
1580   case OMPD_section: {
1581     Sema::CapturedParamNameType Params[] = {
1582         std::make_pair(StringRef(), QualType()) // __context with shared vars
1583     };
1584     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1585                              Params);
1586     break;
1587   }
1588   case OMPD_single: {
1589     Sema::CapturedParamNameType Params[] = {
1590         std::make_pair(StringRef(), QualType()) // __context with shared vars
1591     };
1592     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1593                              Params);
1594     break;
1595   }
1596   case OMPD_master: {
1597     Sema::CapturedParamNameType Params[] = {
1598         std::make_pair(StringRef(), QualType()) // __context with shared vars
1599     };
1600     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1601                              Params);
1602     break;
1603   }
1604   case OMPD_critical: {
1605     Sema::CapturedParamNameType Params[] = {
1606         std::make_pair(StringRef(), QualType()) // __context with shared vars
1607     };
1608     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1609                              Params);
1610     break;
1611   }
1612   case OMPD_parallel_for: {
1613     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1614     QualType KmpInt32PtrTy =
1615         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1616     Sema::CapturedParamNameType Params[] = {
1617         std::make_pair(".global_tid.", KmpInt32PtrTy),
1618         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1619         std::make_pair(StringRef(), QualType()) // __context with shared vars
1620     };
1621     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1622                              Params);
1623     break;
1624   }
1625   case OMPD_parallel_for_simd: {
1626     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1627     QualType KmpInt32PtrTy =
1628         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1629     Sema::CapturedParamNameType Params[] = {
1630         std::make_pair(".global_tid.", KmpInt32PtrTy),
1631         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1632         std::make_pair(StringRef(), QualType()) // __context with shared vars
1633     };
1634     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1635                              Params);
1636     break;
1637   }
1638   case OMPD_parallel_sections: {
1639     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1640     QualType KmpInt32PtrTy =
1641         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1642     Sema::CapturedParamNameType Params[] = {
1643         std::make_pair(".global_tid.", KmpInt32PtrTy),
1644         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1645         std::make_pair(StringRef(), QualType()) // __context with shared vars
1646     };
1647     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1648                              Params);
1649     break;
1650   }
1651   case OMPD_task: {
1652     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1653     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1654     FunctionProtoType::ExtProtoInfo EPI;
1655     EPI.Variadic = true;
1656     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1657     Sema::CapturedParamNameType Params[] = {
1658         std::make_pair(".global_tid.", KmpInt32Ty),
1659         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1660         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1661         std::make_pair(".copy_fn.",
1662                        Context.getPointerType(CopyFnType).withConst()),
1663         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1664         std::make_pair(StringRef(), QualType()) // __context with shared vars
1665     };
1666     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1667                              Params);
1668     // Mark this captured region as inlined, because we don't use outlined
1669     // function directly.
1670     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1671         AlwaysInlineAttr::CreateImplicit(
1672             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1673     break;
1674   }
1675   case OMPD_ordered: {
1676     Sema::CapturedParamNameType Params[] = {
1677         std::make_pair(StringRef(), QualType()) // __context with shared vars
1678     };
1679     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1680                              Params);
1681     break;
1682   }
1683   case OMPD_atomic: {
1684     Sema::CapturedParamNameType Params[] = {
1685         std::make_pair(StringRef(), QualType()) // __context with shared vars
1686     };
1687     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1688                              Params);
1689     break;
1690   }
1691   case OMPD_target_data:
1692   case OMPD_target:
1693   case OMPD_target_parallel:
1694   case OMPD_target_parallel_for: {
1695     Sema::CapturedParamNameType Params[] = {
1696         std::make_pair(StringRef(), QualType()) // __context with shared vars
1697     };
1698     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1699                              Params);
1700     break;
1701   }
1702   case OMPD_teams: {
1703     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1704     QualType KmpInt32PtrTy =
1705         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1706     Sema::CapturedParamNameType Params[] = {
1707         std::make_pair(".global_tid.", KmpInt32PtrTy),
1708         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1709         std::make_pair(StringRef(), QualType()) // __context with shared vars
1710     };
1711     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1712                              Params);
1713     break;
1714   }
1715   case OMPD_taskgroup: {
1716     Sema::CapturedParamNameType Params[] = {
1717         std::make_pair(StringRef(), QualType()) // __context with shared vars
1718     };
1719     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1720                              Params);
1721     break;
1722   }
1723   case OMPD_taskloop:
1724   case OMPD_taskloop_simd: {
1725     QualType KmpInt32Ty =
1726         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1727     QualType KmpUInt64Ty =
1728         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1729     QualType KmpInt64Ty =
1730         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1731     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1732     FunctionProtoType::ExtProtoInfo EPI;
1733     EPI.Variadic = true;
1734     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1735     Sema::CapturedParamNameType Params[] = {
1736         std::make_pair(".global_tid.", KmpInt32Ty),
1737         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1738         std::make_pair(".privates.",
1739                        Context.VoidPtrTy.withConst().withRestrict()),
1740         std::make_pair(
1741             ".copy_fn.",
1742             Context.getPointerType(CopyFnType).withConst().withRestrict()),
1743         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1744         std::make_pair(".lb.", KmpUInt64Ty),
1745         std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1746         std::make_pair(".liter.", KmpInt32Ty),
1747         std::make_pair(StringRef(), QualType()) // __context with shared vars
1748     };
1749     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1750                              Params);
1751     // Mark this captured region as inlined, because we don't use outlined
1752     // function directly.
1753     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1754         AlwaysInlineAttr::CreateImplicit(
1755             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1756     break;
1757   }
1758   case OMPD_distribute: {
1759     Sema::CapturedParamNameType Params[] = {
1760         std::make_pair(StringRef(), QualType()) // __context with shared vars
1761     };
1762     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1763                              Params);
1764     break;
1765   }
1766   case OMPD_threadprivate:
1767   case OMPD_taskyield:
1768   case OMPD_barrier:
1769   case OMPD_taskwait:
1770   case OMPD_cancellation_point:
1771   case OMPD_cancel:
1772   case OMPD_flush:
1773   case OMPD_target_enter_data:
1774   case OMPD_target_exit_data:
1775   case OMPD_declare_reduction:
1776   case OMPD_declare_simd:
1777   case OMPD_declare_target:
1778   case OMPD_end_declare_target:
1779     llvm_unreachable("OpenMP Directive is not allowed");
1780   case OMPD_unknown:
1781     llvm_unreachable("Unknown OpenMP directive");
1782   }
1783 }
1784 
1785 static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1786                                              Expr *CaptureExpr, bool WithInit,
1787                                              bool AsExpression) {
1788   assert(CaptureExpr);
1789   ASTContext &C = S.getASTContext();
1790   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
1791   QualType Ty = Init->getType();
1792   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1793     if (S.getLangOpts().CPlusPlus)
1794       Ty = C.getLValueReferenceType(Ty);
1795     else {
1796       Ty = C.getPointerType(Ty);
1797       ExprResult Res =
1798           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1799       if (!Res.isUsable())
1800         return nullptr;
1801       Init = Res.get();
1802     }
1803     WithInit = true;
1804   }
1805   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1806   if (!WithInit)
1807     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
1808   S.CurContext->addHiddenDecl(CED);
1809   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1810                          /*TypeMayContainAuto=*/true);
1811   return CED;
1812 }
1813 
1814 static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1815                                  bool WithInit) {
1816   OMPCapturedExprDecl *CD;
1817   if (auto *VD = S.IsOpenMPCapturedDecl(D))
1818     CD = cast<OMPCapturedExprDecl>(VD);
1819   else
1820     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1821                           /*AsExpression=*/false);
1822   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1823                           CaptureExpr->getExprLoc());
1824 }
1825 
1826 static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1827   if (!Ref) {
1828     auto *CD =
1829         buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1830                          CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1831     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1832                            CaptureExpr->getExprLoc());
1833   }
1834   ExprResult Res = Ref;
1835   if (!S.getLangOpts().CPlusPlus &&
1836       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1837       Ref->getType()->isPointerType())
1838     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1839   if (!Res.isUsable())
1840     return ExprError();
1841   return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
1842 }
1843 
1844 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1845                                       ArrayRef<OMPClause *> Clauses) {
1846   if (!S.isUsable()) {
1847     ActOnCapturedRegionError();
1848     return StmtError();
1849   }
1850 
1851   OMPOrderedClause *OC = nullptr;
1852   OMPScheduleClause *SC = nullptr;
1853   SmallVector<OMPLinearClause *, 4> LCs;
1854   // This is required for proper codegen.
1855   for (auto *Clause : Clauses) {
1856     if (isOpenMPPrivate(Clause->getClauseKind()) ||
1857         Clause->getClauseKind() == OMPC_copyprivate ||
1858         (getLangOpts().OpenMPUseTLS &&
1859          getASTContext().getTargetInfo().isTLSSupported() &&
1860          Clause->getClauseKind() == OMPC_copyin)) {
1861       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
1862       // Mark all variables in private list clauses as used in inner region.
1863       for (auto *VarRef : Clause->children()) {
1864         if (auto *E = cast_or_null<Expr>(VarRef)) {
1865           MarkDeclarationsReferencedInExpr(E);
1866         }
1867       }
1868       DSAStack->setForceVarCapturing(/*V=*/false);
1869     } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
1870       // Mark all variables in private list clauses as used in inner region.
1871       // Required for proper codegen of combined directives.
1872       // TODO: add processing for other clauses.
1873       if (auto *C = OMPClauseWithPreInit::get(Clause)) {
1874         if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1875           for (auto *D : DS->decls())
1876             MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1877         }
1878       }
1879       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1880         if (auto *E = C->getPostUpdateExpr())
1881           MarkDeclarationsReferencedInExpr(E);
1882       }
1883     }
1884     if (Clause->getClauseKind() == OMPC_schedule)
1885       SC = cast<OMPScheduleClause>(Clause);
1886     else if (Clause->getClauseKind() == OMPC_ordered)
1887       OC = cast<OMPOrderedClause>(Clause);
1888     else if (Clause->getClauseKind() == OMPC_linear)
1889       LCs.push_back(cast<OMPLinearClause>(Clause));
1890   }
1891   bool ErrorFound = false;
1892   // OpenMP, 2.7.1 Loop Construct, Restrictions
1893   // The nonmonotonic modifier cannot be specified if an ordered clause is
1894   // specified.
1895   if (SC &&
1896       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1897        SC->getSecondScheduleModifier() ==
1898            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1899       OC) {
1900     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1901              ? SC->getFirstScheduleModifierLoc()
1902              : SC->getSecondScheduleModifierLoc(),
1903          diag::err_omp_schedule_nonmonotonic_ordered)
1904         << SourceRange(OC->getLocStart(), OC->getLocEnd());
1905     ErrorFound = true;
1906   }
1907   if (!LCs.empty() && OC && OC->getNumForLoops()) {
1908     for (auto *C : LCs) {
1909       Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1910           << SourceRange(OC->getLocStart(), OC->getLocEnd());
1911     }
1912     ErrorFound = true;
1913   }
1914   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1915       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1916       OC->getNumForLoops()) {
1917     Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1918         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1919     ErrorFound = true;
1920   }
1921   if (ErrorFound) {
1922     ActOnCapturedRegionError();
1923     return StmtError();
1924   }
1925   return ActOnCapturedRegionEnd(S.get());
1926 }
1927 
1928 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1929                                   OpenMPDirectiveKind CurrentRegion,
1930                                   const DeclarationNameInfo &CurrentName,
1931                                   OpenMPDirectiveKind CancelRegion,
1932                                   SourceLocation StartLoc) {
1933   // Allowed nesting of constructs
1934   // +------------------+-----------------+------------------------------------+
1935   // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1936   // +------------------+-----------------+------------------------------------+
1937   // | parallel         | parallel        | *                                  |
1938   // | parallel         | for             | *                                  |
1939   // | parallel         | for simd        | *                                  |
1940   // | parallel         | master          | *                                  |
1941   // | parallel         | critical        | *                                  |
1942   // | parallel         | simd            | *                                  |
1943   // | parallel         | sections        | *                                  |
1944   // | parallel         | section         | +                                  |
1945   // | parallel         | single          | *                                  |
1946   // | parallel         | parallel for    | *                                  |
1947   // | parallel         |parallel for simd| *                                  |
1948   // | parallel         |parallel sections| *                                  |
1949   // | parallel         | task            | *                                  |
1950   // | parallel         | taskyield       | *                                  |
1951   // | parallel         | barrier         | *                                  |
1952   // | parallel         | taskwait        | *                                  |
1953   // | parallel         | taskgroup       | *                                  |
1954   // | parallel         | flush           | *                                  |
1955   // | parallel         | ordered         | +                                  |
1956   // | parallel         | atomic          | *                                  |
1957   // | parallel         | target          | *                                  |
1958   // | parallel         | target parallel | *                                  |
1959   // | parallel         | target parallel | *                                  |
1960   // |                  | for             |                                    |
1961   // | parallel         | target enter    | *                                  |
1962   // |                  | data            |                                    |
1963   // | parallel         | target exit     | *                                  |
1964   // |                  | data            |                                    |
1965   // | parallel         | teams           | +                                  |
1966   // | parallel         | cancellation    |                                    |
1967   // |                  | point           | !                                  |
1968   // | parallel         | cancel          | !                                  |
1969   // | parallel         | taskloop        | *                                  |
1970   // | parallel         | taskloop simd   | *                                  |
1971   // | parallel         | distribute      |                                    |
1972   // +------------------+-----------------+------------------------------------+
1973   // | for              | parallel        | *                                  |
1974   // | for              | for             | +                                  |
1975   // | for              | for simd        | +                                  |
1976   // | for              | master          | +                                  |
1977   // | for              | critical        | *                                  |
1978   // | for              | simd            | *                                  |
1979   // | for              | sections        | +                                  |
1980   // | for              | section         | +                                  |
1981   // | for              | single          | +                                  |
1982   // | for              | parallel for    | *                                  |
1983   // | for              |parallel for simd| *                                  |
1984   // | for              |parallel sections| *                                  |
1985   // | for              | task            | *                                  |
1986   // | for              | taskyield       | *                                  |
1987   // | for              | barrier         | +                                  |
1988   // | for              | taskwait        | *                                  |
1989   // | for              | taskgroup       | *                                  |
1990   // | for              | flush           | *                                  |
1991   // | for              | ordered         | * (if construct is ordered)        |
1992   // | for              | atomic          | *                                  |
1993   // | for              | target          | *                                  |
1994   // | for              | target parallel | *                                  |
1995   // | for              | target parallel | *                                  |
1996   // |                  | for             |                                    |
1997   // | for              | target enter    | *                                  |
1998   // |                  | data            |                                    |
1999   // | for              | target exit     | *                                  |
2000   // |                  | data            |                                    |
2001   // | for              | teams           | +                                  |
2002   // | for              | cancellation    |                                    |
2003   // |                  | point           | !                                  |
2004   // | for              | cancel          | !                                  |
2005   // | for              | taskloop        | *                                  |
2006   // | for              | taskloop simd   | *                                  |
2007   // | for              | distribute      |                                    |
2008   // +------------------+-----------------+------------------------------------+
2009   // | master           | parallel        | *                                  |
2010   // | master           | for             | +                                  |
2011   // | master           | for simd        | +                                  |
2012   // | master           | master          | *                                  |
2013   // | master           | critical        | *                                  |
2014   // | master           | simd            | *                                  |
2015   // | master           | sections        | +                                  |
2016   // | master           | section         | +                                  |
2017   // | master           | single          | +                                  |
2018   // | master           | parallel for    | *                                  |
2019   // | master           |parallel for simd| *                                  |
2020   // | master           |parallel sections| *                                  |
2021   // | master           | task            | *                                  |
2022   // | master           | taskyield       | *                                  |
2023   // | master           | barrier         | +                                  |
2024   // | master           | taskwait        | *                                  |
2025   // | master           | taskgroup       | *                                  |
2026   // | master           | flush           | *                                  |
2027   // | master           | ordered         | +                                  |
2028   // | master           | atomic          | *                                  |
2029   // | master           | target          | *                                  |
2030   // | master           | target parallel | *                                  |
2031   // | master           | target parallel | *                                  |
2032   // |                  | for             |                                    |
2033   // | master           | target enter    | *                                  |
2034   // |                  | data            |                                    |
2035   // | master           | target exit     | *                                  |
2036   // |                  | data            |                                    |
2037   // | master           | teams           | +                                  |
2038   // | master           | cancellation    |                                    |
2039   // |                  | point           |                                    |
2040   // | master           | cancel          |                                    |
2041   // | master           | taskloop        | *                                  |
2042   // | master           | taskloop simd   | *                                  |
2043   // | master           | distribute      |                                    |
2044   // +------------------+-----------------+------------------------------------+
2045   // | critical         | parallel        | *                                  |
2046   // | critical         | for             | +                                  |
2047   // | critical         | for simd        | +                                  |
2048   // | critical         | master          | *                                  |
2049   // | critical         | critical        | * (should have different names)    |
2050   // | critical         | simd            | *                                  |
2051   // | critical         | sections        | +                                  |
2052   // | critical         | section         | +                                  |
2053   // | critical         | single          | +                                  |
2054   // | critical         | parallel for    | *                                  |
2055   // | critical         |parallel for simd| *                                  |
2056   // | critical         |parallel sections| *                                  |
2057   // | critical         | task            | *                                  |
2058   // | critical         | taskyield       | *                                  |
2059   // | critical         | barrier         | +                                  |
2060   // | critical         | taskwait        | *                                  |
2061   // | critical         | taskgroup       | *                                  |
2062   // | critical         | ordered         | +                                  |
2063   // | critical         | atomic          | *                                  |
2064   // | critical         | target          | *                                  |
2065   // | critical         | target parallel | *                                  |
2066   // | critical         | target parallel | *                                  |
2067   // |                  | for             |                                    |
2068   // | critical         | target enter    | *                                  |
2069   // |                  | data            |                                    |
2070   // | critical         | target exit     | *                                  |
2071   // |                  | data            |                                    |
2072   // | critical         | teams           | +                                  |
2073   // | critical         | cancellation    |                                    |
2074   // |                  | point           |                                    |
2075   // | critical         | cancel          |                                    |
2076   // | critical         | taskloop        | *                                  |
2077   // | critical         | taskloop simd   | *                                  |
2078   // | critical         | distribute      |                                    |
2079   // +------------------+-----------------+------------------------------------+
2080   // | simd             | parallel        |                                    |
2081   // | simd             | for             |                                    |
2082   // | simd             | for simd        |                                    |
2083   // | simd             | master          |                                    |
2084   // | simd             | critical        |                                    |
2085   // | simd             | simd            | *                                  |
2086   // | simd             | sections        |                                    |
2087   // | simd             | section         |                                    |
2088   // | simd             | single          |                                    |
2089   // | simd             | parallel for    |                                    |
2090   // | simd             |parallel for simd|                                    |
2091   // | simd             |parallel sections|                                    |
2092   // | simd             | task            |                                    |
2093   // | simd             | taskyield       |                                    |
2094   // | simd             | barrier         |                                    |
2095   // | simd             | taskwait        |                                    |
2096   // | simd             | taskgroup       |                                    |
2097   // | simd             | flush           |                                    |
2098   // | simd             | ordered         | + (with simd clause)               |
2099   // | simd             | atomic          |                                    |
2100   // | simd             | target          |                                    |
2101   // | simd             | target parallel |                                    |
2102   // | simd             | target parallel |                                    |
2103   // |                  | for             |                                    |
2104   // | simd             | target enter    |                                    |
2105   // |                  | data            |                                    |
2106   // | simd             | target exit     |                                    |
2107   // |                  | data            |                                    |
2108   // | simd             | teams           |                                    |
2109   // | simd             | cancellation    |                                    |
2110   // |                  | point           |                                    |
2111   // | simd             | cancel          |                                    |
2112   // | simd             | taskloop        |                                    |
2113   // | simd             | taskloop simd   |                                    |
2114   // | simd             | distribute      |                                    |
2115   // +------------------+-----------------+------------------------------------+
2116   // | for simd         | parallel        |                                    |
2117   // | for simd         | for             |                                    |
2118   // | for simd         | for simd        |                                    |
2119   // | for simd         | master          |                                    |
2120   // | for simd         | critical        |                                    |
2121   // | for simd         | simd            | *                                  |
2122   // | for simd         | sections        |                                    |
2123   // | for simd         | section         |                                    |
2124   // | for simd         | single          |                                    |
2125   // | for simd         | parallel for    |                                    |
2126   // | for simd         |parallel for simd|                                    |
2127   // | for simd         |parallel sections|                                    |
2128   // | for simd         | task            |                                    |
2129   // | for simd         | taskyield       |                                    |
2130   // | for simd         | barrier         |                                    |
2131   // | for simd         | taskwait        |                                    |
2132   // | for simd         | taskgroup       |                                    |
2133   // | for simd         | flush           |                                    |
2134   // | for simd         | ordered         | + (with simd clause)               |
2135   // | for simd         | atomic          |                                    |
2136   // | for simd         | target          |                                    |
2137   // | for simd         | target parallel |                                    |
2138   // | for simd         | target parallel |                                    |
2139   // |                  | for             |                                    |
2140   // | for simd         | target enter    |                                    |
2141   // |                  | data            |                                    |
2142   // | for simd         | target exit     |                                    |
2143   // |                  | data            |                                    |
2144   // | for simd         | teams           |                                    |
2145   // | for simd         | cancellation    |                                    |
2146   // |                  | point           |                                    |
2147   // | for simd         | cancel          |                                    |
2148   // | for simd         | taskloop        |                                    |
2149   // | for simd         | taskloop simd   |                                    |
2150   // | for simd         | distribute      |                                    |
2151   // +------------------+-----------------+------------------------------------+
2152   // | parallel for simd| parallel        |                                    |
2153   // | parallel for simd| for             |                                    |
2154   // | parallel for simd| for simd        |                                    |
2155   // | parallel for simd| master          |                                    |
2156   // | parallel for simd| critical        |                                    |
2157   // | parallel for simd| simd            | *                                  |
2158   // | parallel for simd| sections        |                                    |
2159   // | parallel for simd| section         |                                    |
2160   // | parallel for simd| single          |                                    |
2161   // | parallel for simd| parallel for    |                                    |
2162   // | parallel for simd|parallel for simd|                                    |
2163   // | parallel for simd|parallel sections|                                    |
2164   // | parallel for simd| task            |                                    |
2165   // | parallel for simd| taskyield       |                                    |
2166   // | parallel for simd| barrier         |                                    |
2167   // | parallel for simd| taskwait        |                                    |
2168   // | parallel for simd| taskgroup       |                                    |
2169   // | parallel for simd| flush           |                                    |
2170   // | parallel for simd| ordered         | + (with simd clause)               |
2171   // | parallel for simd| atomic          |                                    |
2172   // | parallel for simd| target          |                                    |
2173   // | parallel for simd| target parallel |                                    |
2174   // | parallel for simd| target parallel |                                    |
2175   // |                  | for             |                                    |
2176   // | parallel for simd| target enter    |                                    |
2177   // |                  | data            |                                    |
2178   // | parallel for simd| target exit     |                                    |
2179   // |                  | data            |                                    |
2180   // | parallel for simd| teams           |                                    |
2181   // | parallel for simd| cancellation    |                                    |
2182   // |                  | point           |                                    |
2183   // | parallel for simd| cancel          |                                    |
2184   // | parallel for simd| taskloop        |                                    |
2185   // | parallel for simd| taskloop simd   |                                    |
2186   // | parallel for simd| distribute      |                                    |
2187   // +------------------+-----------------+------------------------------------+
2188   // | sections         | parallel        | *                                  |
2189   // | sections         | for             | +                                  |
2190   // | sections         | for simd        | +                                  |
2191   // | sections         | master          | +                                  |
2192   // | sections         | critical        | *                                  |
2193   // | sections         | simd            | *                                  |
2194   // | sections         | sections        | +                                  |
2195   // | sections         | section         | *                                  |
2196   // | sections         | single          | +                                  |
2197   // | sections         | parallel for    | *                                  |
2198   // | sections         |parallel for simd| *                                  |
2199   // | sections         |parallel sections| *                                  |
2200   // | sections         | task            | *                                  |
2201   // | sections         | taskyield       | *                                  |
2202   // | sections         | barrier         | +                                  |
2203   // | sections         | taskwait        | *                                  |
2204   // | sections         | taskgroup       | *                                  |
2205   // | sections         | flush           | *                                  |
2206   // | sections         | ordered         | +                                  |
2207   // | sections         | atomic          | *                                  |
2208   // | sections         | target          | *                                  |
2209   // | sections         | target parallel | *                                  |
2210   // | sections         | target parallel | *                                  |
2211   // |                  | for             |                                    |
2212   // | sections         | target enter    | *                                  |
2213   // |                  | data            |                                    |
2214   // | sections         | target exit     | *                                  |
2215   // |                  | data            |                                    |
2216   // | sections         | teams           | +                                  |
2217   // | sections         | cancellation    |                                    |
2218   // |                  | point           | !                                  |
2219   // | sections         | cancel          | !                                  |
2220   // | sections         | taskloop        | *                                  |
2221   // | sections         | taskloop simd   | *                                  |
2222   // | sections         | distribute      |                                    |
2223   // +------------------+-----------------+------------------------------------+
2224   // | section          | parallel        | *                                  |
2225   // | section          | for             | +                                  |
2226   // | section          | for simd        | +                                  |
2227   // | section          | master          | +                                  |
2228   // | section          | critical        | *                                  |
2229   // | section          | simd            | *                                  |
2230   // | section          | sections        | +                                  |
2231   // | section          | section         | +                                  |
2232   // | section          | single          | +                                  |
2233   // | section          | parallel for    | *                                  |
2234   // | section          |parallel for simd| *                                  |
2235   // | section          |parallel sections| *                                  |
2236   // | section          | task            | *                                  |
2237   // | section          | taskyield       | *                                  |
2238   // | section          | barrier         | +                                  |
2239   // | section          | taskwait        | *                                  |
2240   // | section          | taskgroup       | *                                  |
2241   // | section          | flush           | *                                  |
2242   // | section          | ordered         | +                                  |
2243   // | section          | atomic          | *                                  |
2244   // | section          | target          | *                                  |
2245   // | section          | target parallel | *                                  |
2246   // | section          | target parallel | *                                  |
2247   // |                  | for             |                                    |
2248   // | section          | target enter    | *                                  |
2249   // |                  | data            |                                    |
2250   // | section          | target exit     | *                                  |
2251   // |                  | data            |                                    |
2252   // | section          | teams           | +                                  |
2253   // | section          | cancellation    |                                    |
2254   // |                  | point           | !                                  |
2255   // | section          | cancel          | !                                  |
2256   // | section          | taskloop        | *                                  |
2257   // | section          | taskloop simd   | *                                  |
2258   // | section          | distribute      |                                    |
2259   // +------------------+-----------------+------------------------------------+
2260   // | single           | parallel        | *                                  |
2261   // | single           | for             | +                                  |
2262   // | single           | for simd        | +                                  |
2263   // | single           | master          | +                                  |
2264   // | single           | critical        | *                                  |
2265   // | single           | simd            | *                                  |
2266   // | single           | sections        | +                                  |
2267   // | single           | section         | +                                  |
2268   // | single           | single          | +                                  |
2269   // | single           | parallel for    | *                                  |
2270   // | single           |parallel for simd| *                                  |
2271   // | single           |parallel sections| *                                  |
2272   // | single           | task            | *                                  |
2273   // | single           | taskyield       | *                                  |
2274   // | single           | barrier         | +                                  |
2275   // | single           | taskwait        | *                                  |
2276   // | single           | taskgroup       | *                                  |
2277   // | single           | flush           | *                                  |
2278   // | single           | ordered         | +                                  |
2279   // | single           | atomic          | *                                  |
2280   // | single           | target          | *                                  |
2281   // | single           | target parallel | *                                  |
2282   // | single           | target parallel | *                                  |
2283   // |                  | for             |                                    |
2284   // | single           | target enter    | *                                  |
2285   // |                  | data            |                                    |
2286   // | single           | target exit     | *                                  |
2287   // |                  | data            |                                    |
2288   // | single           | teams           | +                                  |
2289   // | single           | cancellation    |                                    |
2290   // |                  | point           |                                    |
2291   // | single           | cancel          |                                    |
2292   // | single           | taskloop        | *                                  |
2293   // | single           | taskloop simd   | *                                  |
2294   // | single           | distribute      |                                    |
2295   // +------------------+-----------------+------------------------------------+
2296   // | parallel for     | parallel        | *                                  |
2297   // | parallel for     | for             | +                                  |
2298   // | parallel for     | for simd        | +                                  |
2299   // | parallel for     | master          | +                                  |
2300   // | parallel for     | critical        | *                                  |
2301   // | parallel for     | simd            | *                                  |
2302   // | parallel for     | sections        | +                                  |
2303   // | parallel for     | section         | +                                  |
2304   // | parallel for     | single          | +                                  |
2305   // | parallel for     | parallel for    | *                                  |
2306   // | parallel for     |parallel for simd| *                                  |
2307   // | parallel for     |parallel sections| *                                  |
2308   // | parallel for     | task            | *                                  |
2309   // | parallel for     | taskyield       | *                                  |
2310   // | parallel for     | barrier         | +                                  |
2311   // | parallel for     | taskwait        | *                                  |
2312   // | parallel for     | taskgroup       | *                                  |
2313   // | parallel for     | flush           | *                                  |
2314   // | parallel for     | ordered         | * (if construct is ordered)        |
2315   // | parallel for     | atomic          | *                                  |
2316   // | parallel for     | target          | *                                  |
2317   // | parallel for     | target parallel | *                                  |
2318   // | parallel for     | target parallel | *                                  |
2319   // |                  | for             |                                    |
2320   // | parallel for     | target enter    | *                                  |
2321   // |                  | data            |                                    |
2322   // | parallel for     | target exit     | *                                  |
2323   // |                  | data            |                                    |
2324   // | parallel for     | teams           | +                                  |
2325   // | parallel for     | cancellation    |                                    |
2326   // |                  | point           | !                                  |
2327   // | parallel for     | cancel          | !                                  |
2328   // | parallel for     | taskloop        | *                                  |
2329   // | parallel for     | taskloop simd   | *                                  |
2330   // | parallel for     | distribute      |                                    |
2331   // +------------------+-----------------+------------------------------------+
2332   // | parallel sections| parallel        | *                                  |
2333   // | parallel sections| for             | +                                  |
2334   // | parallel sections| for simd        | +                                  |
2335   // | parallel sections| master          | +                                  |
2336   // | parallel sections| critical        | +                                  |
2337   // | parallel sections| simd            | *                                  |
2338   // | parallel sections| sections        | +                                  |
2339   // | parallel sections| section         | *                                  |
2340   // | parallel sections| single          | +                                  |
2341   // | parallel sections| parallel for    | *                                  |
2342   // | parallel sections|parallel for simd| *                                  |
2343   // | parallel sections|parallel sections| *                                  |
2344   // | parallel sections| task            | *                                  |
2345   // | parallel sections| taskyield       | *                                  |
2346   // | parallel sections| barrier         | +                                  |
2347   // | parallel sections| taskwait        | *                                  |
2348   // | parallel sections| taskgroup       | *                                  |
2349   // | parallel sections| flush           | *                                  |
2350   // | parallel sections| ordered         | +                                  |
2351   // | parallel sections| atomic          | *                                  |
2352   // | parallel sections| target          | *                                  |
2353   // | parallel sections| target parallel | *                                  |
2354   // | parallel sections| target parallel | *                                  |
2355   // |                  | for             |                                    |
2356   // | parallel sections| target enter    | *                                  |
2357   // |                  | data            |                                    |
2358   // | parallel sections| target exit     | *                                  |
2359   // |                  | data            |                                    |
2360   // | parallel sections| teams           | +                                  |
2361   // | parallel sections| cancellation    |                                    |
2362   // |                  | point           | !                                  |
2363   // | parallel sections| cancel          | !                                  |
2364   // | parallel sections| taskloop        | *                                  |
2365   // | parallel sections| taskloop simd   | *                                  |
2366   // | parallel sections| distribute      |                                    |
2367   // +------------------+-----------------+------------------------------------+
2368   // | task             | parallel        | *                                  |
2369   // | task             | for             | +                                  |
2370   // | task             | for simd        | +                                  |
2371   // | task             | master          | +                                  |
2372   // | task             | critical        | *                                  |
2373   // | task             | simd            | *                                  |
2374   // | task             | sections        | +                                  |
2375   // | task             | section         | +                                  |
2376   // | task             | single          | +                                  |
2377   // | task             | parallel for    | *                                  |
2378   // | task             |parallel for simd| *                                  |
2379   // | task             |parallel sections| *                                  |
2380   // | task             | task            | *                                  |
2381   // | task             | taskyield       | *                                  |
2382   // | task             | barrier         | +                                  |
2383   // | task             | taskwait        | *                                  |
2384   // | task             | taskgroup       | *                                  |
2385   // | task             | flush           | *                                  |
2386   // | task             | ordered         | +                                  |
2387   // | task             | atomic          | *                                  |
2388   // | task             | target          | *                                  |
2389   // | task             | target parallel | *                                  |
2390   // | task             | target parallel | *                                  |
2391   // |                  | for             |                                    |
2392   // | task             | target enter    | *                                  |
2393   // |                  | data            |                                    |
2394   // | task             | target exit     | *                                  |
2395   // |                  | data            |                                    |
2396   // | task             | teams           | +                                  |
2397   // | task             | cancellation    |                                    |
2398   // |                  | point           | !                                  |
2399   // | task             | cancel          | !                                  |
2400   // | task             | taskloop        | *                                  |
2401   // | task             | taskloop simd   | *                                  |
2402   // | task             | distribute      |                                    |
2403   // +------------------+-----------------+------------------------------------+
2404   // | ordered          | parallel        | *                                  |
2405   // | ordered          | for             | +                                  |
2406   // | ordered          | for simd        | +                                  |
2407   // | ordered          | master          | *                                  |
2408   // | ordered          | critical        | *                                  |
2409   // | ordered          | simd            | *                                  |
2410   // | ordered          | sections        | +                                  |
2411   // | ordered          | section         | +                                  |
2412   // | ordered          | single          | +                                  |
2413   // | ordered          | parallel for    | *                                  |
2414   // | ordered          |parallel for simd| *                                  |
2415   // | ordered          |parallel sections| *                                  |
2416   // | ordered          | task            | *                                  |
2417   // | ordered          | taskyield       | *                                  |
2418   // | ordered          | barrier         | +                                  |
2419   // | ordered          | taskwait        | *                                  |
2420   // | ordered          | taskgroup       | *                                  |
2421   // | ordered          | flush           | *                                  |
2422   // | ordered          | ordered         | +                                  |
2423   // | ordered          | atomic          | *                                  |
2424   // | ordered          | target          | *                                  |
2425   // | ordered          | target parallel | *                                  |
2426   // | ordered          | target parallel | *                                  |
2427   // |                  | for             |                                    |
2428   // | ordered          | target enter    | *                                  |
2429   // |                  | data            |                                    |
2430   // | ordered          | target exit     | *                                  |
2431   // |                  | data            |                                    |
2432   // | ordered          | teams           | +                                  |
2433   // | ordered          | cancellation    |                                    |
2434   // |                  | point           |                                    |
2435   // | ordered          | cancel          |                                    |
2436   // | ordered          | taskloop        | *                                  |
2437   // | ordered          | taskloop simd   | *                                  |
2438   // | ordered          | distribute      |                                    |
2439   // +------------------+-----------------+------------------------------------+
2440   // | atomic           | parallel        |                                    |
2441   // | atomic           | for             |                                    |
2442   // | atomic           | for simd        |                                    |
2443   // | atomic           | master          |                                    |
2444   // | atomic           | critical        |                                    |
2445   // | atomic           | simd            |                                    |
2446   // | atomic           | sections        |                                    |
2447   // | atomic           | section         |                                    |
2448   // | atomic           | single          |                                    |
2449   // | atomic           | parallel for    |                                    |
2450   // | atomic           |parallel for simd|                                    |
2451   // | atomic           |parallel sections|                                    |
2452   // | atomic           | task            |                                    |
2453   // | atomic           | taskyield       |                                    |
2454   // | atomic           | barrier         |                                    |
2455   // | atomic           | taskwait        |                                    |
2456   // | atomic           | taskgroup       |                                    |
2457   // | atomic           | flush           |                                    |
2458   // | atomic           | ordered         |                                    |
2459   // | atomic           | atomic          |                                    |
2460   // | atomic           | target          |                                    |
2461   // | atomic           | target parallel |                                    |
2462   // | atomic           | target parallel |                                    |
2463   // |                  | for             |                                    |
2464   // | atomic           | target enter    |                                    |
2465   // |                  | data            |                                    |
2466   // | atomic           | target exit     |                                    |
2467   // |                  | data            |                                    |
2468   // | atomic           | teams           |                                    |
2469   // | atomic           | cancellation    |                                    |
2470   // |                  | point           |                                    |
2471   // | atomic           | cancel          |                                    |
2472   // | atomic           | taskloop        |                                    |
2473   // | atomic           | taskloop simd   |                                    |
2474   // | atomic           | distribute      |                                    |
2475   // +------------------+-----------------+------------------------------------+
2476   // | target           | parallel        | *                                  |
2477   // | target           | for             | *                                  |
2478   // | target           | for simd        | *                                  |
2479   // | target           | master          | *                                  |
2480   // | target           | critical        | *                                  |
2481   // | target           | simd            | *                                  |
2482   // | target           | sections        | *                                  |
2483   // | target           | section         | *                                  |
2484   // | target           | single          | *                                  |
2485   // | target           | parallel for    | *                                  |
2486   // | target           |parallel for simd| *                                  |
2487   // | target           |parallel sections| *                                  |
2488   // | target           | task            | *                                  |
2489   // | target           | taskyield       | *                                  |
2490   // | target           | barrier         | *                                  |
2491   // | target           | taskwait        | *                                  |
2492   // | target           | taskgroup       | *                                  |
2493   // | target           | flush           | *                                  |
2494   // | target           | ordered         | *                                  |
2495   // | target           | atomic          | *                                  |
2496   // | target           | target          |                                    |
2497   // | target           | target parallel |                                    |
2498   // | target           | target parallel |                                    |
2499   // |                  | for             |                                    |
2500   // | target           | target enter    |                                    |
2501   // |                  | data            |                                    |
2502   // | target           | target exit     |                                    |
2503   // |                  | data            |                                    |
2504   // | target           | teams           | *                                  |
2505   // | target           | cancellation    |                                    |
2506   // |                  | point           |                                    |
2507   // | target           | cancel          |                                    |
2508   // | target           | taskloop        | *                                  |
2509   // | target           | taskloop simd   | *                                  |
2510   // | target           | distribute      |                                    |
2511   // +------------------+-----------------+------------------------------------+
2512   // | target parallel  | parallel        | *                                  |
2513   // | target parallel  | for             | *                                  |
2514   // | target parallel  | for simd        | *                                  |
2515   // | target parallel  | master          | *                                  |
2516   // | target parallel  | critical        | *                                  |
2517   // | target parallel  | simd            | *                                  |
2518   // | target parallel  | sections        | *                                  |
2519   // | target parallel  | section         | *                                  |
2520   // | target parallel  | single          | *                                  |
2521   // | target parallel  | parallel for    | *                                  |
2522   // | target parallel  |parallel for simd| *                                  |
2523   // | target parallel  |parallel sections| *                                  |
2524   // | target parallel  | task            | *                                  |
2525   // | target parallel  | taskyield       | *                                  |
2526   // | target parallel  | barrier         | *                                  |
2527   // | target parallel  | taskwait        | *                                  |
2528   // | target parallel  | taskgroup       | *                                  |
2529   // | target parallel  | flush           | *                                  |
2530   // | target parallel  | ordered         | *                                  |
2531   // | target parallel  | atomic          | *                                  |
2532   // | target parallel  | target          |                                    |
2533   // | target parallel  | target parallel |                                    |
2534   // | target parallel  | target parallel |                                    |
2535   // |                  | for             |                                    |
2536   // | target parallel  | target enter    |                                    |
2537   // |                  | data            |                                    |
2538   // | target parallel  | target exit     |                                    |
2539   // |                  | data            |                                    |
2540   // | target parallel  | teams           |                                    |
2541   // | target parallel  | cancellation    |                                    |
2542   // |                  | point           | !                                  |
2543   // | target parallel  | cancel          | !                                  |
2544   // | target parallel  | taskloop        | *                                  |
2545   // | target parallel  | taskloop simd   | *                                  |
2546   // | target parallel  | distribute      |                                    |
2547   // +------------------+-----------------+------------------------------------+
2548   // | target parallel  | parallel        | *                                  |
2549   // | for              |                 |                                    |
2550   // | target parallel  | for             | *                                  |
2551   // | for              |                 |                                    |
2552   // | target parallel  | for simd        | *                                  |
2553   // | for              |                 |                                    |
2554   // | target parallel  | master          | *                                  |
2555   // | for              |                 |                                    |
2556   // | target parallel  | critical        | *                                  |
2557   // | for              |                 |                                    |
2558   // | target parallel  | simd            | *                                  |
2559   // | for              |                 |                                    |
2560   // | target parallel  | sections        | *                                  |
2561   // | for              |                 |                                    |
2562   // | target parallel  | section         | *                                  |
2563   // | for              |                 |                                    |
2564   // | target parallel  | single          | *                                  |
2565   // | for              |                 |                                    |
2566   // | target parallel  | parallel for    | *                                  |
2567   // | for              |                 |                                    |
2568   // | target parallel  |parallel for simd| *                                  |
2569   // | for              |                 |                                    |
2570   // | target parallel  |parallel sections| *                                  |
2571   // | for              |                 |                                    |
2572   // | target parallel  | task            | *                                  |
2573   // | for              |                 |                                    |
2574   // | target parallel  | taskyield       | *                                  |
2575   // | for              |                 |                                    |
2576   // | target parallel  | barrier         | *                                  |
2577   // | for              |                 |                                    |
2578   // | target parallel  | taskwait        | *                                  |
2579   // | for              |                 |                                    |
2580   // | target parallel  | taskgroup       | *                                  |
2581   // | for              |                 |                                    |
2582   // | target parallel  | flush           | *                                  |
2583   // | for              |                 |                                    |
2584   // | target parallel  | ordered         | *                                  |
2585   // | for              |                 |                                    |
2586   // | target parallel  | atomic          | *                                  |
2587   // | for              |                 |                                    |
2588   // | target parallel  | target          |                                    |
2589   // | for              |                 |                                    |
2590   // | target parallel  | target parallel |                                    |
2591   // | for              |                 |                                    |
2592   // | target parallel  | target parallel |                                    |
2593   // | for              | for             |                                    |
2594   // | target parallel  | target enter    |                                    |
2595   // | for              | data            |                                    |
2596   // | target parallel  | target exit     |                                    |
2597   // | for              | data            |                                    |
2598   // | target parallel  | teams           |                                    |
2599   // | for              |                 |                                    |
2600   // | target parallel  | cancellation    |                                    |
2601   // | for              | point           | !                                  |
2602   // | target parallel  | cancel          | !                                  |
2603   // | for              |                 |                                    |
2604   // | target parallel  | taskloop        | *                                  |
2605   // | for              |                 |                                    |
2606   // | target parallel  | taskloop simd   | *                                  |
2607   // | for              |                 |                                    |
2608   // | target parallel  | distribute      |                                    |
2609   // | for              |                 |                                    |
2610   // +------------------+-----------------+------------------------------------+
2611   // | teams            | parallel        | *                                  |
2612   // | teams            | for             | +                                  |
2613   // | teams            | for simd        | +                                  |
2614   // | teams            | master          | +                                  |
2615   // | teams            | critical        | +                                  |
2616   // | teams            | simd            | +                                  |
2617   // | teams            | sections        | +                                  |
2618   // | teams            | section         | +                                  |
2619   // | teams            | single          | +                                  |
2620   // | teams            | parallel for    | *                                  |
2621   // | teams            |parallel for simd| *                                  |
2622   // | teams            |parallel sections| *                                  |
2623   // | teams            | task            | +                                  |
2624   // | teams            | taskyield       | +                                  |
2625   // | teams            | barrier         | +                                  |
2626   // | teams            | taskwait        | +                                  |
2627   // | teams            | taskgroup       | +                                  |
2628   // | teams            | flush           | +                                  |
2629   // | teams            | ordered         | +                                  |
2630   // | teams            | atomic          | +                                  |
2631   // | teams            | target          | +                                  |
2632   // | teams            | target parallel | +                                  |
2633   // | teams            | target parallel | +                                  |
2634   // |                  | for             |                                    |
2635   // | teams            | target enter    | +                                  |
2636   // |                  | data            |                                    |
2637   // | teams            | target exit     | +                                  |
2638   // |                  | data            |                                    |
2639   // | teams            | teams           | +                                  |
2640   // | teams            | cancellation    |                                    |
2641   // |                  | point           |                                    |
2642   // | teams            | cancel          |                                    |
2643   // | teams            | taskloop        | +                                  |
2644   // | teams            | taskloop simd   | +                                  |
2645   // | teams            | distribute      | !                                  |
2646   // +------------------+-----------------+------------------------------------+
2647   // | taskloop         | parallel        | *                                  |
2648   // | taskloop         | for             | +                                  |
2649   // | taskloop         | for simd        | +                                  |
2650   // | taskloop         | master          | +                                  |
2651   // | taskloop         | critical        | *                                  |
2652   // | taskloop         | simd            | *                                  |
2653   // | taskloop         | sections        | +                                  |
2654   // | taskloop         | section         | +                                  |
2655   // | taskloop         | single          | +                                  |
2656   // | taskloop         | parallel for    | *                                  |
2657   // | taskloop         |parallel for simd| *                                  |
2658   // | taskloop         |parallel sections| *                                  |
2659   // | taskloop         | task            | *                                  |
2660   // | taskloop         | taskyield       | *                                  |
2661   // | taskloop         | barrier         | +                                  |
2662   // | taskloop         | taskwait        | *                                  |
2663   // | taskloop         | taskgroup       | *                                  |
2664   // | taskloop         | flush           | *                                  |
2665   // | taskloop         | ordered         | +                                  |
2666   // | taskloop         | atomic          | *                                  |
2667   // | taskloop         | target          | *                                  |
2668   // | taskloop         | target parallel | *                                  |
2669   // | taskloop         | target parallel | *                                  |
2670   // |                  | for             |                                    |
2671   // | taskloop         | target enter    | *                                  |
2672   // |                  | data            |                                    |
2673   // | taskloop         | target exit     | *                                  |
2674   // |                  | data            |                                    |
2675   // | taskloop         | teams           | +                                  |
2676   // | taskloop         | cancellation    |                                    |
2677   // |                  | point           |                                    |
2678   // | taskloop         | cancel          |                                    |
2679   // | taskloop         | taskloop        | *                                  |
2680   // | taskloop         | distribute      |                                    |
2681   // +------------------+-----------------+------------------------------------+
2682   // | taskloop simd    | parallel        |                                    |
2683   // | taskloop simd    | for             |                                    |
2684   // | taskloop simd    | for simd        |                                    |
2685   // | taskloop simd    | master          |                                    |
2686   // | taskloop simd    | critical        |                                    |
2687   // | taskloop simd    | simd            | *                                  |
2688   // | taskloop simd    | sections        |                                    |
2689   // | taskloop simd    | section         |                                    |
2690   // | taskloop simd    | single          |                                    |
2691   // | taskloop simd    | parallel for    |                                    |
2692   // | taskloop simd    |parallel for simd|                                    |
2693   // | taskloop simd    |parallel sections|                                    |
2694   // | taskloop simd    | task            |                                    |
2695   // | taskloop simd    | taskyield       |                                    |
2696   // | taskloop simd    | barrier         |                                    |
2697   // | taskloop simd    | taskwait        |                                    |
2698   // | taskloop simd    | taskgroup       |                                    |
2699   // | taskloop simd    | flush           |                                    |
2700   // | taskloop simd    | ordered         | + (with simd clause)               |
2701   // | taskloop simd    | atomic          |                                    |
2702   // | taskloop simd    | target          |                                    |
2703   // | taskloop simd    | target parallel |                                    |
2704   // | taskloop simd    | target parallel |                                    |
2705   // |                  | for             |                                    |
2706   // | taskloop simd    | target enter    |                                    |
2707   // |                  | data            |                                    |
2708   // | taskloop simd    | target exit     |                                    |
2709   // |                  | data            |                                    |
2710   // | taskloop simd    | teams           |                                    |
2711   // | taskloop simd    | cancellation    |                                    |
2712   // |                  | point           |                                    |
2713   // | taskloop simd    | cancel          |                                    |
2714   // | taskloop simd    | taskloop        |                                    |
2715   // | taskloop simd    | taskloop simd   |                                    |
2716   // | taskloop simd    | distribute      |                                    |
2717   // +------------------+-----------------+------------------------------------+
2718   // | distribute       | parallel        | *                                  |
2719   // | distribute       | for             | *                                  |
2720   // | distribute       | for simd        | *                                  |
2721   // | distribute       | master          | *                                  |
2722   // | distribute       | critical        | *                                  |
2723   // | distribute       | simd            | *                                  |
2724   // | distribute       | sections        | *                                  |
2725   // | distribute       | section         | *                                  |
2726   // | distribute       | single          | *                                  |
2727   // | distribute       | parallel for    | *                                  |
2728   // | distribute       |parallel for simd| *                                  |
2729   // | distribute       |parallel sections| *                                  |
2730   // | distribute       | task            | *                                  |
2731   // | distribute       | taskyield       | *                                  |
2732   // | distribute       | barrier         | *                                  |
2733   // | distribute       | taskwait        | *                                  |
2734   // | distribute       | taskgroup       | *                                  |
2735   // | distribute       | flush           | *                                  |
2736   // | distribute       | ordered         | +                                  |
2737   // | distribute       | atomic          | *                                  |
2738   // | distribute       | target          |                                    |
2739   // | distribute       | target parallel |                                    |
2740   // | distribute       | target parallel |                                    |
2741   // |                  | for             |                                    |
2742   // | distribute       | target enter    |                                    |
2743   // |                  | data            |                                    |
2744   // | distribute       | target exit     |                                    |
2745   // |                  | data            |                                    |
2746   // | distribute       | teams           |                                    |
2747   // | distribute       | cancellation    | +                                  |
2748   // |                  | point           |                                    |
2749   // | distribute       | cancel          | +                                  |
2750   // | distribute       | taskloop        | *                                  |
2751   // | distribute       | taskloop simd   | *                                  |
2752   // | distribute       | distribute      |                                    |
2753   // +------------------+-----------------+------------------------------------+
2754   if (Stack->getCurScope()) {
2755     auto ParentRegion = Stack->getParentDirective();
2756     auto OffendingRegion = ParentRegion;
2757     bool NestingProhibited = false;
2758     bool CloseNesting = true;
2759     enum {
2760       NoRecommend,
2761       ShouldBeInParallelRegion,
2762       ShouldBeInOrderedRegion,
2763       ShouldBeInTargetRegion,
2764       ShouldBeInTeamsRegion
2765     } Recommend = NoRecommend;
2766     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered &&
2767         CurrentRegion != OMPD_simd) {
2768       // OpenMP [2.16, Nesting of Regions]
2769       // OpenMP constructs may not be nested inside a simd region.
2770       // OpenMP [2.8.1,simd Construct, Restrictions]
2771       // An ordered construct with the simd clause is the only OpenMP construct
2772       // that can appear in the simd region.
2773       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2774       return true;
2775     }
2776     if (ParentRegion == OMPD_atomic) {
2777       // OpenMP [2.16, Nesting of Regions]
2778       // OpenMP constructs may not be nested inside an atomic region.
2779       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2780       return true;
2781     }
2782     if (CurrentRegion == OMPD_section) {
2783       // OpenMP [2.7.2, sections Construct, Restrictions]
2784       // Orphaned section directives are prohibited. That is, the section
2785       // directives must appear within the sections construct and must not be
2786       // encountered elsewhere in the sections region.
2787       if (ParentRegion != OMPD_sections &&
2788           ParentRegion != OMPD_parallel_sections) {
2789         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2790             << (ParentRegion != OMPD_unknown)
2791             << getOpenMPDirectiveName(ParentRegion);
2792         return true;
2793       }
2794       return false;
2795     }
2796     // Allow some constructs to be orphaned (they could be used in functions,
2797     // called from OpenMP regions with the required preconditions).
2798     if (ParentRegion == OMPD_unknown)
2799       return false;
2800     if (CurrentRegion == OMPD_cancellation_point ||
2801         CurrentRegion == OMPD_cancel) {
2802       // OpenMP [2.16, Nesting of Regions]
2803       // A cancellation point construct for which construct-type-clause is
2804       // taskgroup must be nested inside a task construct. A cancellation
2805       // point construct for which construct-type-clause is not taskgroup must
2806       // be closely nested inside an OpenMP construct that matches the type
2807       // specified in construct-type-clause.
2808       // A cancel construct for which construct-type-clause is taskgroup must be
2809       // nested inside a task construct. A cancel construct for which
2810       // construct-type-clause is not taskgroup must be closely nested inside an
2811       // OpenMP construct that matches the type specified in
2812       // construct-type-clause.
2813       NestingProhibited =
2814           !((CancelRegion == OMPD_parallel &&
2815              (ParentRegion == OMPD_parallel ||
2816               ParentRegion == OMPD_target_parallel)) ||
2817             (CancelRegion == OMPD_for &&
2818              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
2819               ParentRegion == OMPD_target_parallel_for)) ||
2820             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2821             (CancelRegion == OMPD_sections &&
2822              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2823               ParentRegion == OMPD_parallel_sections)));
2824     } else if (CurrentRegion == OMPD_master) {
2825       // OpenMP [2.16, Nesting of Regions]
2826       // A master region may not be closely nested inside a worksharing,
2827       // atomic, or explicit task region.
2828       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2829                           isOpenMPTaskingDirective(ParentRegion);
2830     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2831       // OpenMP [2.16, Nesting of Regions]
2832       // A critical region may not be nested (closely or otherwise) inside a
2833       // critical region with the same name. Note that this restriction is not
2834       // sufficient to prevent deadlock.
2835       SourceLocation PreviousCriticalLoc;
2836       bool DeadLock =
2837           Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2838                                   OpenMPDirectiveKind K,
2839                                   const DeclarationNameInfo &DNI,
2840                                   SourceLocation Loc)
2841                                   ->bool {
2842                                 if (K == OMPD_critical &&
2843                                     DNI.getName() == CurrentName.getName()) {
2844                                   PreviousCriticalLoc = Loc;
2845                                   return true;
2846                                 } else
2847                                   return false;
2848                               },
2849                               false /* skip top directive */);
2850       if (DeadLock) {
2851         SemaRef.Diag(StartLoc,
2852                      diag::err_omp_prohibited_region_critical_same_name)
2853             << CurrentName.getName();
2854         if (PreviousCriticalLoc.isValid())
2855           SemaRef.Diag(PreviousCriticalLoc,
2856                        diag::note_omp_previous_critical_region);
2857         return true;
2858       }
2859     } else if (CurrentRegion == OMPD_barrier) {
2860       // OpenMP [2.16, Nesting of Regions]
2861       // A barrier region may not be closely nested inside a worksharing,
2862       // explicit task, critical, ordered, atomic, or master region.
2863       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2864                           isOpenMPTaskingDirective(ParentRegion) ||
2865                           ParentRegion == OMPD_master ||
2866                           ParentRegion == OMPD_critical ||
2867                           ParentRegion == OMPD_ordered;
2868     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
2869                !isOpenMPParallelDirective(CurrentRegion)) {
2870       // OpenMP [2.16, Nesting of Regions]
2871       // A worksharing region may not be closely nested inside a worksharing,
2872       // explicit task, critical, ordered, atomic, or master region.
2873       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2874                           isOpenMPTaskingDirective(ParentRegion) ||
2875                           ParentRegion == OMPD_master ||
2876                           ParentRegion == OMPD_critical ||
2877                           ParentRegion == OMPD_ordered;
2878       Recommend = ShouldBeInParallelRegion;
2879     } else if (CurrentRegion == OMPD_ordered) {
2880       // OpenMP [2.16, Nesting of Regions]
2881       // An ordered region may not be closely nested inside a critical,
2882       // atomic, or explicit task region.
2883       // An ordered region must be closely nested inside a loop region (or
2884       // parallel loop region) with an ordered clause.
2885       // OpenMP [2.8.1,simd Construct, Restrictions]
2886       // An ordered construct with the simd clause is the only OpenMP construct
2887       // that can appear in the simd region.
2888       NestingProhibited = ParentRegion == OMPD_critical ||
2889                           isOpenMPTaskingDirective(ParentRegion) ||
2890                           !(isOpenMPSimdDirective(ParentRegion) ||
2891                             Stack->isParentOrderedRegion());
2892       Recommend = ShouldBeInOrderedRegion;
2893     } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2894       // OpenMP [2.16, Nesting of Regions]
2895       // If specified, a teams construct must be contained within a target
2896       // construct.
2897       NestingProhibited = ParentRegion != OMPD_target;
2898       Recommend = ShouldBeInTargetRegion;
2899       Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2900     }
2901     if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2902       // OpenMP [2.16, Nesting of Regions]
2903       // distribute, parallel, parallel sections, parallel workshare, and the
2904       // parallel loop and parallel loop SIMD constructs are the only OpenMP
2905       // constructs that can be closely nested in the teams region.
2906       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2907                           !isOpenMPDistributeDirective(CurrentRegion);
2908       Recommend = ShouldBeInParallelRegion;
2909     }
2910     if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2911       // OpenMP 4.5 [2.17 Nesting of Regions]
2912       // The region associated with the distribute construct must be strictly
2913       // nested inside a teams region
2914       NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2915       Recommend = ShouldBeInTeamsRegion;
2916     }
2917     if (!NestingProhibited &&
2918         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
2919          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
2920       // OpenMP 4.5 [2.17 Nesting of Regions]
2921       // If a target, target update, target data, target enter data, or
2922       // target exit data construct is encountered during execution of a
2923       // target region, the behavior is unspecified.
2924       NestingProhibited = Stack->hasDirective(
2925           [&OffendingRegion](OpenMPDirectiveKind K,
2926                              const DeclarationNameInfo &DNI,
2927                              SourceLocation Loc) -> bool {
2928             if (isOpenMPTargetExecutionDirective(K)) {
2929               OffendingRegion = K;
2930               return true;
2931             } else
2932               return false;
2933           },
2934           false /* don't skip top directive */);
2935       CloseNesting = false;
2936     }
2937     if (NestingProhibited) {
2938       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
2939           << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
2940           << Recommend << getOpenMPDirectiveName(CurrentRegion);
2941       return true;
2942     }
2943   }
2944   return false;
2945 }
2946 
2947 static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2948                            ArrayRef<OMPClause *> Clauses,
2949                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2950   bool ErrorFound = false;
2951   unsigned NamedModifiersNumber = 0;
2952   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2953       OMPD_unknown + 1);
2954   SmallVector<SourceLocation, 4> NameModifierLoc;
2955   for (const auto *C : Clauses) {
2956     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2957       // At most one if clause without a directive-name-modifier can appear on
2958       // the directive.
2959       OpenMPDirectiveKind CurNM = IC->getNameModifier();
2960       if (FoundNameModifiers[CurNM]) {
2961         S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2962             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2963             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2964         ErrorFound = true;
2965       } else if (CurNM != OMPD_unknown) {
2966         NameModifierLoc.push_back(IC->getNameModifierLoc());
2967         ++NamedModifiersNumber;
2968       }
2969       FoundNameModifiers[CurNM] = IC;
2970       if (CurNM == OMPD_unknown)
2971         continue;
2972       // Check if the specified name modifier is allowed for the current
2973       // directive.
2974       // At most one if clause with the particular directive-name-modifier can
2975       // appear on the directive.
2976       bool MatchFound = false;
2977       for (auto NM : AllowedNameModifiers) {
2978         if (CurNM == NM) {
2979           MatchFound = true;
2980           break;
2981         }
2982       }
2983       if (!MatchFound) {
2984         S.Diag(IC->getNameModifierLoc(),
2985                diag::err_omp_wrong_if_directive_name_modifier)
2986             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2987         ErrorFound = true;
2988       }
2989     }
2990   }
2991   // If any if clause on the directive includes a directive-name-modifier then
2992   // all if clauses on the directive must include a directive-name-modifier.
2993   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2994     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2995       S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2996              diag::err_omp_no_more_if_clause);
2997     } else {
2998       std::string Values;
2999       std::string Sep(", ");
3000       unsigned AllowedCnt = 0;
3001       unsigned TotalAllowedNum =
3002           AllowedNameModifiers.size() - NamedModifiersNumber;
3003       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3004            ++Cnt) {
3005         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3006         if (!FoundNameModifiers[NM]) {
3007           Values += "'";
3008           Values += getOpenMPDirectiveName(NM);
3009           Values += "'";
3010           if (AllowedCnt + 2 == TotalAllowedNum)
3011             Values += " or ";
3012           else if (AllowedCnt + 1 != TotalAllowedNum)
3013             Values += Sep;
3014           ++AllowedCnt;
3015         }
3016       }
3017       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3018              diag::err_omp_unnamed_if_clause)
3019           << (TotalAllowedNum > 1) << Values;
3020     }
3021     for (auto Loc : NameModifierLoc) {
3022       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3023     }
3024     ErrorFound = true;
3025   }
3026   return ErrorFound;
3027 }
3028 
3029 StmtResult Sema::ActOnOpenMPExecutableDirective(
3030     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3031     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3032     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3033   StmtResult Res = StmtError();
3034   if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3035                             StartLoc))
3036     return StmtError();
3037 
3038   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3039   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
3040   bool ErrorFound = false;
3041   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3042   if (AStmt) {
3043     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3044 
3045     // Check default data sharing attributes for referenced variables.
3046     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3047     DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3048     if (DSAChecker.isErrorFound())
3049       return StmtError();
3050     // Generate list of implicitly defined firstprivate variables.
3051     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3052 
3053     if (!DSAChecker.getImplicitFirstprivate().empty()) {
3054       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3055               DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3056               SourceLocation(), SourceLocation())) {
3057         ClausesWithImplicit.push_back(Implicit);
3058         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3059                      DSAChecker.getImplicitFirstprivate().size();
3060       } else
3061         ErrorFound = true;
3062     }
3063   }
3064 
3065   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3066   switch (Kind) {
3067   case OMPD_parallel:
3068     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3069                                        EndLoc);
3070     AllowedNameModifiers.push_back(OMPD_parallel);
3071     break;
3072   case OMPD_simd:
3073     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3074                                    VarsWithInheritedDSA);
3075     break;
3076   case OMPD_for:
3077     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3078                                   VarsWithInheritedDSA);
3079     break;
3080   case OMPD_for_simd:
3081     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3082                                       EndLoc, VarsWithInheritedDSA);
3083     break;
3084   case OMPD_sections:
3085     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3086                                        EndLoc);
3087     break;
3088   case OMPD_section:
3089     assert(ClausesWithImplicit.empty() &&
3090            "No clauses are allowed for 'omp section' directive");
3091     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3092     break;
3093   case OMPD_single:
3094     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3095                                      EndLoc);
3096     break;
3097   case OMPD_master:
3098     assert(ClausesWithImplicit.empty() &&
3099            "No clauses are allowed for 'omp master' directive");
3100     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3101     break;
3102   case OMPD_critical:
3103     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3104                                        StartLoc, EndLoc);
3105     break;
3106   case OMPD_parallel_for:
3107     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3108                                           EndLoc, VarsWithInheritedDSA);
3109     AllowedNameModifiers.push_back(OMPD_parallel);
3110     break;
3111   case OMPD_parallel_for_simd:
3112     Res = ActOnOpenMPParallelForSimdDirective(
3113         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3114     AllowedNameModifiers.push_back(OMPD_parallel);
3115     break;
3116   case OMPD_parallel_sections:
3117     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3118                                                StartLoc, EndLoc);
3119     AllowedNameModifiers.push_back(OMPD_parallel);
3120     break;
3121   case OMPD_task:
3122     Res =
3123         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3124     AllowedNameModifiers.push_back(OMPD_task);
3125     break;
3126   case OMPD_taskyield:
3127     assert(ClausesWithImplicit.empty() &&
3128            "No clauses are allowed for 'omp taskyield' directive");
3129     assert(AStmt == nullptr &&
3130            "No associated statement allowed for 'omp taskyield' directive");
3131     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3132     break;
3133   case OMPD_barrier:
3134     assert(ClausesWithImplicit.empty() &&
3135            "No clauses are allowed for 'omp barrier' directive");
3136     assert(AStmt == nullptr &&
3137            "No associated statement allowed for 'omp barrier' directive");
3138     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3139     break;
3140   case OMPD_taskwait:
3141     assert(ClausesWithImplicit.empty() &&
3142            "No clauses are allowed for 'omp taskwait' directive");
3143     assert(AStmt == nullptr &&
3144            "No associated statement allowed for 'omp taskwait' directive");
3145     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3146     break;
3147   case OMPD_taskgroup:
3148     assert(ClausesWithImplicit.empty() &&
3149            "No clauses are allowed for 'omp taskgroup' directive");
3150     Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3151     break;
3152   case OMPD_flush:
3153     assert(AStmt == nullptr &&
3154            "No associated statement allowed for 'omp flush' directive");
3155     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3156     break;
3157   case OMPD_ordered:
3158     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3159                                       EndLoc);
3160     break;
3161   case OMPD_atomic:
3162     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3163                                      EndLoc);
3164     break;
3165   case OMPD_teams:
3166     Res =
3167         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3168     break;
3169   case OMPD_target:
3170     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3171                                      EndLoc);
3172     AllowedNameModifiers.push_back(OMPD_target);
3173     break;
3174   case OMPD_target_parallel:
3175     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3176                                              StartLoc, EndLoc);
3177     AllowedNameModifiers.push_back(OMPD_target);
3178     AllowedNameModifiers.push_back(OMPD_parallel);
3179     break;
3180   case OMPD_target_parallel_for:
3181     Res = ActOnOpenMPTargetParallelForDirective(
3182         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3183     AllowedNameModifiers.push_back(OMPD_target);
3184     AllowedNameModifiers.push_back(OMPD_parallel);
3185     break;
3186   case OMPD_cancellation_point:
3187     assert(ClausesWithImplicit.empty() &&
3188            "No clauses are allowed for 'omp cancellation point' directive");
3189     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3190                                "cancellation point' directive");
3191     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3192     break;
3193   case OMPD_cancel:
3194     assert(AStmt == nullptr &&
3195            "No associated statement allowed for 'omp cancel' directive");
3196     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3197                                      CancelRegion);
3198     AllowedNameModifiers.push_back(OMPD_cancel);
3199     break;
3200   case OMPD_target_data:
3201     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3202                                          EndLoc);
3203     AllowedNameModifiers.push_back(OMPD_target_data);
3204     break;
3205   case OMPD_target_enter_data:
3206     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3207                                               EndLoc);
3208     AllowedNameModifiers.push_back(OMPD_target_enter_data);
3209     break;
3210   case OMPD_target_exit_data:
3211     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3212                                              EndLoc);
3213     AllowedNameModifiers.push_back(OMPD_target_exit_data);
3214     break;
3215   case OMPD_taskloop:
3216     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3217                                        EndLoc, VarsWithInheritedDSA);
3218     AllowedNameModifiers.push_back(OMPD_taskloop);
3219     break;
3220   case OMPD_taskloop_simd:
3221     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3222                                            EndLoc, VarsWithInheritedDSA);
3223     AllowedNameModifiers.push_back(OMPD_taskloop);
3224     break;
3225   case OMPD_distribute:
3226     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3227                                          EndLoc, VarsWithInheritedDSA);
3228     break;
3229   case OMPD_declare_target:
3230   case OMPD_end_declare_target:
3231   case OMPD_threadprivate:
3232   case OMPD_declare_reduction:
3233   case OMPD_declare_simd:
3234     llvm_unreachable("OpenMP Directive is not allowed");
3235   case OMPD_unknown:
3236     llvm_unreachable("Unknown OpenMP directive");
3237   }
3238 
3239   for (auto P : VarsWithInheritedDSA) {
3240     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3241         << P.first << P.second->getSourceRange();
3242   }
3243   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3244 
3245   if (!AllowedNameModifiers.empty())
3246     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3247                  ErrorFound;
3248 
3249   if (ErrorFound)
3250     return StmtError();
3251   return Res;
3252 }
3253 
3254 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3255     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3256     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3257     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3258     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3259   assert(Aligneds.size() == Alignments.size());
3260   assert(Linears.size() == LinModifiers.size());
3261   assert(Linears.size() == Steps.size());
3262   if (!DG || DG.get().isNull())
3263     return DeclGroupPtrTy();
3264 
3265   if (!DG.get().isSingleDecl()) {
3266     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3267     return DG;
3268   }
3269   auto *ADecl = DG.get().getSingleDecl();
3270   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3271     ADecl = FTD->getTemplatedDecl();
3272 
3273   auto *FD = dyn_cast<FunctionDecl>(ADecl);
3274   if (!FD) {
3275     Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3276     return DeclGroupPtrTy();
3277   }
3278 
3279   // OpenMP [2.8.2, declare simd construct, Description]
3280   // The parameter of the simdlen clause must be a constant positive integer
3281   // expression.
3282   ExprResult SL;
3283   if (Simdlen)
3284     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3285   // OpenMP [2.8.2, declare simd construct, Description]
3286   // The special this pointer can be used as if was one of the arguments to the
3287   // function in any of the linear, aligned, or uniform clauses.
3288   // The uniform clause declares one or more arguments to have an invariant
3289   // value for all concurrent invocations of the function in the execution of a
3290   // single SIMD loop.
3291   llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3292   Expr *UniformedLinearThis = nullptr;
3293   for (auto *E : Uniforms) {
3294     E = E->IgnoreParenImpCasts();
3295     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3296       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3297         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3298             FD->getParamDecl(PVD->getFunctionScopeIndex())
3299                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3300           UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
3301           continue;
3302         }
3303     if (isa<CXXThisExpr>(E)) {
3304       UniformedLinearThis = E;
3305       continue;
3306     }
3307     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3308         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3309   }
3310   // OpenMP [2.8.2, declare simd construct, Description]
3311   // The aligned clause declares that the object to which each list item points
3312   // is aligned to the number of bytes expressed in the optional parameter of
3313   // the aligned clause.
3314   // The special this pointer can be used as if was one of the arguments to the
3315   // function in any of the linear, aligned, or uniform clauses.
3316   // The type of list items appearing in the aligned clause must be array,
3317   // pointer, reference to array, or reference to pointer.
3318   llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3319   Expr *AlignedThis = nullptr;
3320   for (auto *E : Aligneds) {
3321     E = E->IgnoreParenImpCasts();
3322     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3323       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3324         auto *CanonPVD = PVD->getCanonicalDecl();
3325         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3326             FD->getParamDecl(PVD->getFunctionScopeIndex())
3327                     ->getCanonicalDecl() == CanonPVD) {
3328           // OpenMP  [2.8.1, simd construct, Restrictions]
3329           // A list-item cannot appear in more than one aligned clause.
3330           if (AlignedArgs.count(CanonPVD) > 0) {
3331             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3332                 << 1 << E->getSourceRange();
3333             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3334                  diag::note_omp_explicit_dsa)
3335                 << getOpenMPClauseName(OMPC_aligned);
3336             continue;
3337           }
3338           AlignedArgs[CanonPVD] = E;
3339           QualType QTy = PVD->getType()
3340                              .getNonReferenceType()
3341                              .getUnqualifiedType()
3342                              .getCanonicalType();
3343           const Type *Ty = QTy.getTypePtrOrNull();
3344           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3345             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3346                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3347             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3348           }
3349           continue;
3350         }
3351       }
3352     if (isa<CXXThisExpr>(E)) {
3353       if (AlignedThis) {
3354         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3355             << 2 << E->getSourceRange();
3356         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3357             << getOpenMPClauseName(OMPC_aligned);
3358       }
3359       AlignedThis = E;
3360       continue;
3361     }
3362     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3363         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3364   }
3365   // The optional parameter of the aligned clause, alignment, must be a constant
3366   // positive integer expression. If no optional parameter is specified,
3367   // implementation-defined default alignments for SIMD instructions on the
3368   // target platforms are assumed.
3369   SmallVector<Expr *, 4> NewAligns;
3370   for (auto *E : Alignments) {
3371     ExprResult Align;
3372     if (E)
3373       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3374     NewAligns.push_back(Align.get());
3375   }
3376   // OpenMP [2.8.2, declare simd construct, Description]
3377   // The linear clause declares one or more list items to be private to a SIMD
3378   // lane and to have a linear relationship with respect to the iteration space
3379   // of a loop.
3380   // The special this pointer can be used as if was one of the arguments to the
3381   // function in any of the linear, aligned, or uniform clauses.
3382   // When a linear-step expression is specified in a linear clause it must be
3383   // either a constant integer expression or an integer-typed parameter that is
3384   // specified in a uniform clause on the directive.
3385   llvm::DenseMap<Decl *, Expr *> LinearArgs;
3386   const bool IsUniformedThis = UniformedLinearThis != nullptr;
3387   auto MI = LinModifiers.begin();
3388   for (auto *E : Linears) {
3389     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3390     ++MI;
3391     E = E->IgnoreParenImpCasts();
3392     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3393       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3394         auto *CanonPVD = PVD->getCanonicalDecl();
3395         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3396             FD->getParamDecl(PVD->getFunctionScopeIndex())
3397                     ->getCanonicalDecl() == CanonPVD) {
3398           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
3399           // A list-item cannot appear in more than one linear clause.
3400           if (LinearArgs.count(CanonPVD) > 0) {
3401             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3402                 << getOpenMPClauseName(OMPC_linear)
3403                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3404             Diag(LinearArgs[CanonPVD]->getExprLoc(),
3405                  diag::note_omp_explicit_dsa)
3406                 << getOpenMPClauseName(OMPC_linear);
3407             continue;
3408           }
3409           // Each argument can appear in at most one uniform or linear clause.
3410           if (UniformedArgs.count(CanonPVD) > 0) {
3411             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3412                 << getOpenMPClauseName(OMPC_linear)
3413                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3414             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3415                  diag::note_omp_explicit_dsa)
3416                 << getOpenMPClauseName(OMPC_uniform);
3417             continue;
3418           }
3419           LinearArgs[CanonPVD] = E;
3420           if (E->isValueDependent() || E->isTypeDependent() ||
3421               E->isInstantiationDependent() ||
3422               E->containsUnexpandedParameterPack())
3423             continue;
3424           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3425                                       PVD->getOriginalType());
3426           continue;
3427         }
3428       }
3429     if (isa<CXXThisExpr>(E)) {
3430       if (UniformedLinearThis) {
3431         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3432             << getOpenMPClauseName(OMPC_linear)
3433             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3434             << E->getSourceRange();
3435         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3436             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3437                                                    : OMPC_linear);
3438         continue;
3439       }
3440       UniformedLinearThis = E;
3441       if (E->isValueDependent() || E->isTypeDependent() ||
3442           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3443         continue;
3444       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3445                                   E->getType());
3446       continue;
3447     }
3448     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3449         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3450   }
3451   Expr *Step = nullptr;
3452   Expr *NewStep = nullptr;
3453   SmallVector<Expr *, 4> NewSteps;
3454   for (auto *E : Steps) {
3455     // Skip the same step expression, it was checked already.
3456     if (Step == E || !E) {
3457       NewSteps.push_back(E ? NewStep : nullptr);
3458       continue;
3459     }
3460     Step = E;
3461     if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3462       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3463         auto *CanonPVD = PVD->getCanonicalDecl();
3464         if (UniformedArgs.count(CanonPVD) == 0) {
3465           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3466               << Step->getSourceRange();
3467         } else if (E->isValueDependent() || E->isTypeDependent() ||
3468                    E->isInstantiationDependent() ||
3469                    E->containsUnexpandedParameterPack() ||
3470                    CanonPVD->getType()->hasIntegerRepresentation())
3471           NewSteps.push_back(Step);
3472         else {
3473           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3474               << Step->getSourceRange();
3475         }
3476         continue;
3477       }
3478     NewStep = Step;
3479     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3480         !Step->isInstantiationDependent() &&
3481         !Step->containsUnexpandedParameterPack()) {
3482       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3483                     .get();
3484       if (NewStep)
3485         NewStep = VerifyIntegerConstantExpression(NewStep).get();
3486     }
3487     NewSteps.push_back(NewStep);
3488   }
3489   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3490       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
3491       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3492       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3493       const_cast<Expr **>(Linears.data()), Linears.size(),
3494       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3495       NewSteps.data(), NewSteps.size(), SR);
3496   ADecl->addAttr(NewAttr);
3497   return ConvertDeclToDeclGroup(ADecl);
3498 }
3499 
3500 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3501                                               Stmt *AStmt,
3502                                               SourceLocation StartLoc,
3503                                               SourceLocation EndLoc) {
3504   if (!AStmt)
3505     return StmtError();
3506 
3507   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3508   // 1.2.2 OpenMP Language Terminology
3509   // Structured block - An executable statement with a single entry at the
3510   // top and a single exit at the bottom.
3511   // The point of exit cannot be a branch out of the structured block.
3512   // longjmp() and throw() must not violate the entry/exit criteria.
3513   CS->getCapturedDecl()->setNothrow();
3514 
3515   getCurFunction()->setHasBranchProtectedScope();
3516 
3517   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3518                                       DSAStack->isCancelRegion());
3519 }
3520 
3521 namespace {
3522 /// \brief Helper class for checking canonical form of the OpenMP loops and
3523 /// extracting iteration space of each loop in the loop nest, that will be used
3524 /// for IR generation.
3525 class OpenMPIterationSpaceChecker {
3526   /// \brief Reference to Sema.
3527   Sema &SemaRef;
3528   /// \brief A location for diagnostics (when there is no some better location).
3529   SourceLocation DefaultLoc;
3530   /// \brief A location for diagnostics (when increment is not compatible).
3531   SourceLocation ConditionLoc;
3532   /// \brief A source location for referring to loop init later.
3533   SourceRange InitSrcRange;
3534   /// \brief A source location for referring to condition later.
3535   SourceRange ConditionSrcRange;
3536   /// \brief A source location for referring to increment later.
3537   SourceRange IncrementSrcRange;
3538   /// \brief Loop variable.
3539   ValueDecl *LCDecl = nullptr;
3540   /// \brief Reference to loop variable.
3541   Expr *LCRef = nullptr;
3542   /// \brief Lower bound (initializer for the var).
3543   Expr *LB = nullptr;
3544   /// \brief Upper bound.
3545   Expr *UB = nullptr;
3546   /// \brief Loop step (increment).
3547   Expr *Step = nullptr;
3548   /// \brief This flag is true when condition is one of:
3549   ///   Var <  UB
3550   ///   Var <= UB
3551   ///   UB  >  Var
3552   ///   UB  >= Var
3553   bool TestIsLessOp = false;
3554   /// \brief This flag is true when condition is strict ( < or > ).
3555   bool TestIsStrictOp = false;
3556   /// \brief This flag is true when step is subtracted on each iteration.
3557   bool SubtractStep = false;
3558 
3559 public:
3560   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3561       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
3562   /// \brief Check init-expr for canonical loop form and save loop counter
3563   /// variable - #Var and its initialization value - #LB.
3564   bool CheckInit(Stmt *S, bool EmitDiags = true);
3565   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3566   /// for less/greater and for strict/non-strict comparison.
3567   bool CheckCond(Expr *S);
3568   /// \brief Check incr-expr for canonical loop form and return true if it
3569   /// does not conform, otherwise save loop step (#Step).
3570   bool CheckInc(Expr *S);
3571   /// \brief Return the loop counter variable.
3572   ValueDecl *GetLoopDecl() const { return LCDecl; }
3573   /// \brief Return the reference expression to loop counter variable.
3574   Expr *GetLoopDeclRefExpr() const { return LCRef; }
3575   /// \brief Source range of the loop init.
3576   SourceRange GetInitSrcRange() const { return InitSrcRange; }
3577   /// \brief Source range of the loop condition.
3578   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3579   /// \brief Source range of the loop increment.
3580   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3581   /// \brief True if the step should be subtracted.
3582   bool ShouldSubtractStep() const { return SubtractStep; }
3583   /// \brief Build the expression to calculate the number of iterations.
3584   Expr *
3585   BuildNumIterations(Scope *S, const bool LimitedType,
3586                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3587   /// \brief Build the precondition expression for the loops.
3588   Expr *BuildPreCond(Scope *S, Expr *Cond,
3589                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3590   /// \brief Build reference expression to the counter be used for codegen.
3591   DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3592                                DSAStackTy &DSA) const;
3593   /// \brief Build reference expression to the private counter be used for
3594   /// codegen.
3595   Expr *BuildPrivateCounterVar() const;
3596   /// \brief Build initization of the counter be used for codegen.
3597   Expr *BuildCounterInit() const;
3598   /// \brief Build step of the counter be used for codegen.
3599   Expr *BuildCounterStep() const;
3600   /// \brief Return true if any expression is dependent.
3601   bool Dependent() const;
3602 
3603 private:
3604   /// \brief Check the right-hand side of an assignment in the increment
3605   /// expression.
3606   bool CheckIncRHS(Expr *RHS);
3607   /// \brief Helper to set loop counter variable and its initializer.
3608   bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3609   /// \brief Helper to set upper bound.
3610   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
3611              SourceLocation SL);
3612   /// \brief Helper to set loop increment.
3613   bool SetStep(Expr *NewStep, bool Subtract);
3614 };
3615 
3616 bool OpenMPIterationSpaceChecker::Dependent() const {
3617   if (!LCDecl) {
3618     assert(!LB && !UB && !Step);
3619     return false;
3620   }
3621   return LCDecl->getType()->isDependentType() ||
3622          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3623          (Step && Step->isValueDependent());
3624 }
3625 
3626 static Expr *getExprAsWritten(Expr *E) {
3627   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
3628     E = ExprTemp->getSubExpr();
3629 
3630   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
3631     E = MTE->GetTemporaryExpr();
3632 
3633   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
3634     E = Binder->getSubExpr();
3635 
3636   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
3637     E = ICE->getSubExprAsWritten();
3638   return E->IgnoreParens();
3639 }
3640 
3641 bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
3642                                                  Expr *NewLCRefExpr,
3643                                                  Expr *NewLB) {
3644   // State consistency checking to ensure correct usage.
3645   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
3646          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3647   if (!NewLCDecl || !NewLB)
3648     return true;
3649   LCDecl = getCanonicalDecl(NewLCDecl);
3650   LCRef = NewLCRefExpr;
3651   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3652     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3653       if ((Ctor->isCopyOrMoveConstructor() ||
3654            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3655           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3656         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
3657   LB = NewLB;
3658   return false;
3659 }
3660 
3661 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
3662                                         SourceRange SR, SourceLocation SL) {
3663   // State consistency checking to ensure correct usage.
3664   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3665          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
3666   if (!NewUB)
3667     return true;
3668   UB = NewUB;
3669   TestIsLessOp = LessOp;
3670   TestIsStrictOp = StrictOp;
3671   ConditionSrcRange = SR;
3672   ConditionLoc = SL;
3673   return false;
3674 }
3675 
3676 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
3677   // State consistency checking to ensure correct usage.
3678   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
3679   if (!NewStep)
3680     return true;
3681   if (!NewStep->isValueDependent()) {
3682     // Check that the step is integer expression.
3683     SourceLocation StepLoc = NewStep->getLocStart();
3684     ExprResult Val =
3685         SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
3686     if (Val.isInvalid())
3687       return true;
3688     NewStep = Val.get();
3689 
3690     // OpenMP [2.6, Canonical Loop Form, Restrictions]
3691     //  If test-expr is of form var relational-op b and relational-op is < or
3692     //  <= then incr-expr must cause var to increase on each iteration of the
3693     //  loop. If test-expr is of form var relational-op b and relational-op is
3694     //  > or >= then incr-expr must cause var to decrease on each iteration of
3695     //  the loop.
3696     //  If test-expr is of form b relational-op var and relational-op is < or
3697     //  <= then incr-expr must cause var to decrease on each iteration of the
3698     //  loop. If test-expr is of form b relational-op var and relational-op is
3699     //  > or >= then incr-expr must cause var to increase on each iteration of
3700     //  the loop.
3701     llvm::APSInt Result;
3702     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3703     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3704     bool IsConstNeg =
3705         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
3706     bool IsConstPos =
3707         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
3708     bool IsConstZero = IsConstant && !Result.getBoolValue();
3709     if (UB && (IsConstZero ||
3710                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
3711                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
3712       SemaRef.Diag(NewStep->getExprLoc(),
3713                    diag::err_omp_loop_incr_not_compatible)
3714           << LCDecl << TestIsLessOp << NewStep->getSourceRange();
3715       SemaRef.Diag(ConditionLoc,
3716                    diag::note_omp_loop_cond_requres_compatible_incr)
3717           << TestIsLessOp << ConditionSrcRange;
3718       return true;
3719     }
3720     if (TestIsLessOp == Subtract) {
3721       NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
3722                                              NewStep).get();
3723       Subtract = !Subtract;
3724     }
3725   }
3726 
3727   Step = NewStep;
3728   SubtractStep = Subtract;
3729   return false;
3730 }
3731 
3732 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
3733   // Check init-expr for canonical loop form and save loop counter
3734   // variable - #Var and its initialization value - #LB.
3735   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3736   //   var = lb
3737   //   integer-type var = lb
3738   //   random-access-iterator-type var = lb
3739   //   pointer-type var = lb
3740   //
3741   if (!S) {
3742     if (EmitDiags) {
3743       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3744     }
3745     return true;
3746   }
3747   InitSrcRange = S->getSourceRange();
3748   if (Expr *E = dyn_cast<Expr>(S))
3749     S = E->IgnoreParens();
3750   if (auto BO = dyn_cast<BinaryOperator>(S)) {
3751     if (BO->getOpcode() == BO_Assign) {
3752       auto *LHS = BO->getLHS()->IgnoreParens();
3753       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3754         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3755           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3756             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3757         return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
3758       }
3759       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3760         if (ME->isArrow() &&
3761             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3762           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3763       }
3764     }
3765   } else if (auto DS = dyn_cast<DeclStmt>(S)) {
3766     if (DS->isSingleDecl()) {
3767       if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
3768         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
3769           // Accept non-canonical init form here but emit ext. warning.
3770           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
3771             SemaRef.Diag(S->getLocStart(),
3772                          diag::ext_omp_loop_not_canonical_init)
3773                 << S->getSourceRange();
3774           return SetLCDeclAndLB(Var, nullptr, Var->getInit());
3775         }
3776       }
3777     }
3778   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3779     if (CE->getOperator() == OO_Equal) {
3780       auto *LHS = CE->getArg(0);
3781       if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
3782         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3783           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3784             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3785         return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
3786       }
3787       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3788         if (ME->isArrow() &&
3789             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3790           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3791       }
3792     }
3793   }
3794 
3795   if (Dependent() || SemaRef.CurContext->isDependentContext())
3796     return false;
3797   if (EmitDiags) {
3798     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
3799         << S->getSourceRange();
3800   }
3801   return true;
3802 }
3803 
3804 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
3805 /// variable (which may be the loop variable) if possible.
3806 static const ValueDecl *GetInitLCDecl(Expr *E) {
3807   if (!E)
3808     return nullptr;
3809   E = getExprAsWritten(E);
3810   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
3811     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
3812       if ((Ctor->isCopyOrMoveConstructor() ||
3813            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3814           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
3815         E = CE->getArg(0)->IgnoreParenImpCasts();
3816   if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
3817     if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
3818       if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
3819         if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
3820           return getCanonicalDecl(ME->getMemberDecl());
3821       return getCanonicalDecl(VD);
3822     }
3823   }
3824   if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
3825     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
3826       return getCanonicalDecl(ME->getMemberDecl());
3827   return nullptr;
3828 }
3829 
3830 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
3831   // Check test-expr for canonical form, save upper-bound UB, flags for
3832   // less/greater and for strict/non-strict comparison.
3833   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3834   //   var relational-op b
3835   //   b relational-op var
3836   //
3837   if (!S) {
3838     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
3839     return true;
3840   }
3841   S = getExprAsWritten(S);
3842   SourceLocation CondLoc = S->getLocStart();
3843   if (auto BO = dyn_cast<BinaryOperator>(S)) {
3844     if (BO->isRelationalOp()) {
3845       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3846         return SetUB(BO->getRHS(),
3847                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
3848                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3849                      BO->getSourceRange(), BO->getOperatorLoc());
3850       if (GetInitLCDecl(BO->getRHS()) == LCDecl)
3851         return SetUB(BO->getLHS(),
3852                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
3853                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
3854                      BO->getSourceRange(), BO->getOperatorLoc());
3855     }
3856   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3857     if (CE->getNumArgs() == 2) {
3858       auto Op = CE->getOperator();
3859       switch (Op) {
3860       case OO_Greater:
3861       case OO_GreaterEqual:
3862       case OO_Less:
3863       case OO_LessEqual:
3864         if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3865           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3866                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3867                        CE->getOperatorLoc());
3868         if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
3869           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3870                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3871                        CE->getOperatorLoc());
3872         break;
3873       default:
3874         break;
3875       }
3876     }
3877   }
3878   if (Dependent() || SemaRef.CurContext->isDependentContext())
3879     return false;
3880   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3881       << S->getSourceRange() << LCDecl;
3882   return true;
3883 }
3884 
3885 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3886   // RHS of canonical loop form increment can be:
3887   //   var + incr
3888   //   incr + var
3889   //   var - incr
3890   //
3891   RHS = RHS->IgnoreParenImpCasts();
3892   if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3893     if (BO->isAdditiveOp()) {
3894       bool IsAdd = BO->getOpcode() == BO_Add;
3895       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3896         return SetStep(BO->getRHS(), !IsAdd);
3897       if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
3898         return SetStep(BO->getLHS(), false);
3899     }
3900   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3901     bool IsAdd = CE->getOperator() == OO_Plus;
3902     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3903       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3904         return SetStep(CE->getArg(1), !IsAdd);
3905       if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
3906         return SetStep(CE->getArg(0), false);
3907     }
3908   }
3909   if (Dependent() || SemaRef.CurContext->isDependentContext())
3910     return false;
3911   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3912       << RHS->getSourceRange() << LCDecl;
3913   return true;
3914 }
3915 
3916 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3917   // Check incr-expr for canonical loop form and return true if it
3918   // does not conform.
3919   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3920   //   ++var
3921   //   var++
3922   //   --var
3923   //   var--
3924   //   var += incr
3925   //   var -= incr
3926   //   var = var + incr
3927   //   var = incr + var
3928   //   var = var - incr
3929   //
3930   if (!S) {
3931     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
3932     return true;
3933   }
3934   IncrementSrcRange = S->getSourceRange();
3935   S = S->IgnoreParens();
3936   if (auto UO = dyn_cast<UnaryOperator>(S)) {
3937     if (UO->isIncrementDecrementOp() &&
3938         GetInitLCDecl(UO->getSubExpr()) == LCDecl)
3939       return SetStep(
3940           SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3941                                        (UO->isDecrementOp() ? -1 : 1)).get(),
3942           false);
3943   } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3944     switch (BO->getOpcode()) {
3945     case BO_AddAssign:
3946     case BO_SubAssign:
3947       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3948         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3949       break;
3950     case BO_Assign:
3951       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
3952         return CheckIncRHS(BO->getRHS());
3953       break;
3954     default:
3955       break;
3956     }
3957   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3958     switch (CE->getOperator()) {
3959     case OO_PlusPlus:
3960     case OO_MinusMinus:
3961       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3962         return SetStep(
3963             SemaRef.ActOnIntegerConstant(
3964                         CE->getLocStart(),
3965                         ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3966             false);
3967       break;
3968     case OO_PlusEqual:
3969     case OO_MinusEqual:
3970       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3971         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3972       break;
3973     case OO_Equal:
3974       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
3975         return CheckIncRHS(CE->getArg(1));
3976       break;
3977     default:
3978       break;
3979     }
3980   }
3981   if (Dependent() || SemaRef.CurContext->isDependentContext())
3982     return false;
3983   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3984       << S->getSourceRange() << LCDecl;
3985   return true;
3986 }
3987 
3988 static ExprResult
3989 tryBuildCapture(Sema &SemaRef, Expr *Capture,
3990                 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
3991   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
3992     return SemaRef.PerformImplicitConversion(
3993         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
3994         /*AllowExplicit=*/true);
3995   auto I = Captures.find(Capture);
3996   if (I != Captures.end())
3997     return buildCapture(SemaRef, Capture, I->second);
3998   DeclRefExpr *Ref = nullptr;
3999   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4000   Captures[Capture] = Ref;
4001   return Res;
4002 }
4003 
4004 /// \brief Build the expression to calculate the number of iterations.
4005 Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4006     Scope *S, const bool LimitedType,
4007     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4008   ExprResult Diff;
4009   auto VarType = LCDecl->getType().getNonReferenceType();
4010   if (VarType->isIntegerType() || VarType->isPointerType() ||
4011       SemaRef.getLangOpts().CPlusPlus) {
4012     // Upper - Lower
4013     auto *UBExpr = TestIsLessOp ? UB : LB;
4014     auto *LBExpr = TestIsLessOp ? LB : UB;
4015     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4016     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
4017     if (!Upper || !Lower)
4018       return nullptr;
4019 
4020     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4021 
4022     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4023       // BuildBinOp already emitted error, this one is to point user to upper
4024       // and lower bound, and to tell what is passed to 'operator-'.
4025       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4026           << Upper->getSourceRange() << Lower->getSourceRange();
4027       return nullptr;
4028     }
4029   }
4030 
4031   if (!Diff.isUsable())
4032     return nullptr;
4033 
4034   // Upper - Lower [- 1]
4035   if (TestIsStrictOp)
4036     Diff = SemaRef.BuildBinOp(
4037         S, DefaultLoc, BO_Sub, Diff.get(),
4038         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4039   if (!Diff.isUsable())
4040     return nullptr;
4041 
4042   // Upper - Lower [- 1] + Step
4043   auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4044   if (!NewStep.isUsable())
4045     return nullptr;
4046   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
4047   if (!Diff.isUsable())
4048     return nullptr;
4049 
4050   // Parentheses (for dumping/debugging purposes only).
4051   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4052   if (!Diff.isUsable())
4053     return nullptr;
4054 
4055   // (Upper - Lower [- 1] + Step) / Step
4056   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4057   if (!Diff.isUsable())
4058     return nullptr;
4059 
4060   // OpenMP runtime requires 32-bit or 64-bit loop variables.
4061   QualType Type = Diff.get()->getType();
4062   auto &C = SemaRef.Context;
4063   bool UseVarType = VarType->hasIntegerRepresentation() &&
4064                     C.getTypeSize(Type) > C.getTypeSize(VarType);
4065   if (!Type->isIntegerType() || UseVarType) {
4066     unsigned NewSize =
4067         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4068     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4069                                : Type->hasSignedIntegerRepresentation();
4070     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
4071     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4072       Diff = SemaRef.PerformImplicitConversion(
4073           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4074       if (!Diff.isUsable())
4075         return nullptr;
4076     }
4077   }
4078   if (LimitedType) {
4079     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4080     if (NewSize != C.getTypeSize(Type)) {
4081       if (NewSize < C.getTypeSize(Type)) {
4082         assert(NewSize == 64 && "incorrect loop var size");
4083         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4084             << InitSrcRange << ConditionSrcRange;
4085       }
4086       QualType NewType = C.getIntTypeForBitwidth(
4087           NewSize, Type->hasSignedIntegerRepresentation() ||
4088                        C.getTypeSize(Type) < NewSize);
4089       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4090         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4091                                                  Sema::AA_Converting, true);
4092         if (!Diff.isUsable())
4093           return nullptr;
4094       }
4095     }
4096   }
4097 
4098   return Diff.get();
4099 }
4100 
4101 Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4102     Scope *S, Expr *Cond,
4103     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4104   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4105   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4106   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4107 
4108   auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4109   auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4110   if (!NewLB.isUsable() || !NewUB.isUsable())
4111     return nullptr;
4112 
4113   auto CondExpr = SemaRef.BuildBinOp(
4114       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4115                                   : (TestIsStrictOp ? BO_GT : BO_GE),
4116       NewLB.get(), NewUB.get());
4117   if (CondExpr.isUsable()) {
4118     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4119                                                 SemaRef.Context.BoolTy))
4120       CondExpr = SemaRef.PerformImplicitConversion(
4121           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4122           /*AllowExplicit=*/true);
4123   }
4124   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4125   // Otherwise use original loop conditon and evaluate it in runtime.
4126   return CondExpr.isUsable() ? CondExpr.get() : Cond;
4127 }
4128 
4129 /// \brief Build reference expression to the counter be used for codegen.
4130 DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
4131     llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
4132   auto *VD = dyn_cast<VarDecl>(LCDecl);
4133   if (!VD) {
4134     VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4135     auto *Ref = buildDeclRefExpr(
4136         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4137     DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4138     // If the loop control decl is explicitly marked as private, do not mark it
4139     // as captured again.
4140     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4141       Captures.insert(std::make_pair(LCRef, Ref));
4142     return Ref;
4143   }
4144   return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4145                           DefaultLoc);
4146 }
4147 
4148 Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
4149   if (LCDecl && !LCDecl->isInvalidDecl()) {
4150     auto Type = LCDecl->getType().getNonReferenceType();
4151     auto *PrivateVar =
4152         buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4153                      LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
4154     if (PrivateVar->isInvalidDecl())
4155       return nullptr;
4156     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4157   }
4158   return nullptr;
4159 }
4160 
4161 /// \brief Build initization of the counter be used for codegen.
4162 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4163 
4164 /// \brief Build step of the counter be used for codegen.
4165 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4166 
4167 /// \brief Iteration space of a single for loop.
4168 struct LoopIterationSpace {
4169   /// \brief Condition of the loop.
4170   Expr *PreCond;
4171   /// \brief This expression calculates the number of iterations in the loop.
4172   /// It is always possible to calculate it before starting the loop.
4173   Expr *NumIterations;
4174   /// \brief The loop counter variable.
4175   Expr *CounterVar;
4176   /// \brief Private loop counter variable.
4177   Expr *PrivateCounterVar;
4178   /// \brief This is initializer for the initial value of #CounterVar.
4179   Expr *CounterInit;
4180   /// \brief This is step for the #CounterVar used to generate its update:
4181   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4182   Expr *CounterStep;
4183   /// \brief Should step be subtracted?
4184   bool Subtract;
4185   /// \brief Source range of the loop init.
4186   SourceRange InitSrcRange;
4187   /// \brief Source range of the loop condition.
4188   SourceRange CondSrcRange;
4189   /// \brief Source range of the loop increment.
4190   SourceRange IncSrcRange;
4191 };
4192 
4193 } // namespace
4194 
4195 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4196   assert(getLangOpts().OpenMP && "OpenMP is not active.");
4197   assert(Init && "Expected loop in canonical form.");
4198   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4199   if (AssociatedLoops > 0 &&
4200       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4201     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4202     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4203       if (auto *D = ISC.GetLoopDecl()) {
4204         auto *VD = dyn_cast<VarDecl>(D);
4205         if (!VD) {
4206           if (auto *Private = IsOpenMPCapturedDecl(D))
4207             VD = Private;
4208           else {
4209             auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4210                                      /*WithInit=*/false);
4211             VD = cast<VarDecl>(Ref->getDecl());
4212           }
4213         }
4214         DSAStack->addLoopControlVariable(D, VD);
4215       }
4216     }
4217     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4218   }
4219 }
4220 
4221 /// \brief Called on a for stmt to check and extract its iteration space
4222 /// for further processing (such as collapsing).
4223 static bool CheckOpenMPIterationSpace(
4224     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4225     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4226     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
4227     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4228     LoopIterationSpace &ResultIterSpace,
4229     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4230   // OpenMP [2.6, Canonical Loop Form]
4231   //   for (init-expr; test-expr; incr-expr) structured-block
4232   auto For = dyn_cast_or_null<ForStmt>(S);
4233   if (!For) {
4234     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
4235         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4236         << getOpenMPDirectiveName(DKind) << NestedLoopCount
4237         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4238     if (NestedLoopCount > 1) {
4239       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4240         SemaRef.Diag(DSA.getConstructLoc(),
4241                      diag::note_omp_collapse_ordered_expr)
4242             << 2 << CollapseLoopCountExpr->getSourceRange()
4243             << OrderedLoopCountExpr->getSourceRange();
4244       else if (CollapseLoopCountExpr)
4245         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4246                      diag::note_omp_collapse_ordered_expr)
4247             << 0 << CollapseLoopCountExpr->getSourceRange();
4248       else
4249         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4250                      diag::note_omp_collapse_ordered_expr)
4251             << 1 << OrderedLoopCountExpr->getSourceRange();
4252     }
4253     return true;
4254   }
4255   assert(For->getBody());
4256 
4257   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4258 
4259   // Check init.
4260   auto Init = For->getInit();
4261   if (ISC.CheckInit(Init))
4262     return true;
4263 
4264   bool HasErrors = false;
4265 
4266   // Check loop variable's type.
4267   if (auto *LCDecl = ISC.GetLoopDecl()) {
4268     auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
4269 
4270     // OpenMP [2.6, Canonical Loop Form]
4271     // Var is one of the following:
4272     //   A variable of signed or unsigned integer type.
4273     //   For C++, a variable of a random access iterator type.
4274     //   For C, a variable of a pointer type.
4275     auto VarType = LCDecl->getType().getNonReferenceType();
4276     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4277         !VarType->isPointerType() &&
4278         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4279       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4280           << SemaRef.getLangOpts().CPlusPlus;
4281       HasErrors = true;
4282     }
4283 
4284     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4285     // a Construct
4286     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4287     // parallel for construct is (are) private.
4288     // The loop iteration variable in the associated for-loop of a simd
4289     // construct with just one associated for-loop is linear with a
4290     // constant-linear-step that is the increment of the associated for-loop.
4291     // Exclude loop var from the list of variables with implicitly defined data
4292     // sharing attributes.
4293     VarsWithImplicitDSA.erase(LCDecl);
4294 
4295     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4296     // in a Construct, C/C++].
4297     // The loop iteration variable in the associated for-loop of a simd
4298     // construct with just one associated for-loop may be listed in a linear
4299     // clause with a constant-linear-step that is the increment of the
4300     // associated for-loop.
4301     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4302     // parallel for construct may be listed in a private or lastprivate clause.
4303     DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4304     // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4305     // declared in the loop and it is predetermined as a private.
4306     auto PredeterminedCKind =
4307         isOpenMPSimdDirective(DKind)
4308             ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4309             : OMPC_private;
4310     if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4311           DVar.CKind != PredeterminedCKind) ||
4312          ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4313            isOpenMPDistributeDirective(DKind)) &&
4314           !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4315           DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4316         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4317       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4318           << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4319           << getOpenMPClauseName(PredeterminedCKind);
4320       if (DVar.RefExpr == nullptr)
4321         DVar.CKind = PredeterminedCKind;
4322       ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4323       HasErrors = true;
4324     } else if (LoopDeclRefExpr != nullptr) {
4325       // Make the loop iteration variable private (for worksharing constructs),
4326       // linear (for simd directives with the only one associated loop) or
4327       // lastprivate (for simd directives with several collapsed or ordered
4328       // loops).
4329       if (DVar.CKind == OMPC_unknown)
4330         DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate, MatchesAlways(),
4331                           /*FromParent=*/false);
4332       DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4333     }
4334 
4335     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4336 
4337     // Check test-expr.
4338     HasErrors |= ISC.CheckCond(For->getCond());
4339 
4340     // Check incr-expr.
4341     HasErrors |= ISC.CheckInc(For->getInc());
4342   }
4343 
4344   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4345     return HasErrors;
4346 
4347   // Build the loop's iteration space representation.
4348   ResultIterSpace.PreCond =
4349       ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4350   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
4351       DSA.getCurScope(),
4352       (isOpenMPWorksharingDirective(DKind) ||
4353        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4354       Captures);
4355   ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
4356   ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
4357   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4358   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4359   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4360   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4361   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4362   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4363 
4364   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4365                 ResultIterSpace.NumIterations == nullptr ||
4366                 ResultIterSpace.CounterVar == nullptr ||
4367                 ResultIterSpace.PrivateCounterVar == nullptr ||
4368                 ResultIterSpace.CounterInit == nullptr ||
4369                 ResultIterSpace.CounterStep == nullptr);
4370 
4371   return HasErrors;
4372 }
4373 
4374 /// \brief Build 'VarRef = Start.
4375 static ExprResult
4376 BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4377                  ExprResult Start,
4378                  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4379   // Build 'VarRef = Start.
4380   auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4381   if (!NewStart.isUsable())
4382     return ExprError();
4383   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4384                                    VarRef.get()->getType())) {
4385     NewStart = SemaRef.PerformImplicitConversion(
4386         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4387         /*AllowExplicit=*/true);
4388     if (!NewStart.isUsable())
4389       return ExprError();
4390   }
4391 
4392   auto Init =
4393       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4394   return Init;
4395 }
4396 
4397 /// \brief Build 'VarRef = Start + Iter * Step'.
4398 static ExprResult
4399 BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4400                    ExprResult VarRef, ExprResult Start, ExprResult Iter,
4401                    ExprResult Step, bool Subtract,
4402                    llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
4403   // Add parentheses (for debugging purposes only).
4404   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4405   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4406       !Step.isUsable())
4407     return ExprError();
4408 
4409   ExprResult NewStep = Step;
4410   if (Captures)
4411     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4412   if (NewStep.isInvalid())
4413     return ExprError();
4414   ExprResult Update =
4415       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4416   if (!Update.isUsable())
4417     return ExprError();
4418 
4419   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4420   // 'VarRef = Start (+|-) Iter * Step'.
4421   ExprResult NewStart = Start;
4422   if (Captures)
4423     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4424   if (NewStart.isInvalid())
4425     return ExprError();
4426 
4427   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4428   ExprResult SavedUpdate = Update;
4429   ExprResult UpdateVal;
4430   if (VarRef.get()->getType()->isOverloadableType() ||
4431       NewStart.get()->getType()->isOverloadableType() ||
4432       Update.get()->getType()->isOverloadableType()) {
4433     bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4434     SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4435     Update =
4436         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4437     if (Update.isUsable()) {
4438       UpdateVal =
4439           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4440                              VarRef.get(), SavedUpdate.get());
4441       if (UpdateVal.isUsable()) {
4442         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4443                                             UpdateVal.get());
4444       }
4445     }
4446     SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4447   }
4448 
4449   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4450   if (!Update.isUsable() || !UpdateVal.isUsable()) {
4451     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4452                                 NewStart.get(), SavedUpdate.get());
4453     if (!Update.isUsable())
4454       return ExprError();
4455 
4456     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4457                                      VarRef.get()->getType())) {
4458       Update = SemaRef.PerformImplicitConversion(
4459           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4460       if (!Update.isUsable())
4461         return ExprError();
4462     }
4463 
4464     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4465   }
4466   return Update;
4467 }
4468 
4469 /// \brief Convert integer expression \a E to make it have at least \a Bits
4470 /// bits.
4471 static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4472                                       Sema &SemaRef) {
4473   if (E == nullptr)
4474     return ExprError();
4475   auto &C = SemaRef.Context;
4476   QualType OldType = E->getType();
4477   unsigned HasBits = C.getTypeSize(OldType);
4478   if (HasBits >= Bits)
4479     return ExprResult(E);
4480   // OK to convert to signed, because new type has more bits than old.
4481   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4482   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4483                                            true);
4484 }
4485 
4486 /// \brief Check if the given expression \a E is a constant integer that fits
4487 /// into \a Bits bits.
4488 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4489   if (E == nullptr)
4490     return false;
4491   llvm::APSInt Result;
4492   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4493     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4494   return false;
4495 }
4496 
4497 /// Build preinits statement for the given declarations.
4498 static Stmt *buildPreInits(ASTContext &Context,
4499                            SmallVectorImpl<Decl *> &PreInits) {
4500   if (!PreInits.empty()) {
4501     return new (Context) DeclStmt(
4502         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4503         SourceLocation(), SourceLocation());
4504   }
4505   return nullptr;
4506 }
4507 
4508 /// Build preinits statement for the given declarations.
4509 static Stmt *buildPreInits(ASTContext &Context,
4510                            llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4511   if (!Captures.empty()) {
4512     SmallVector<Decl *, 16> PreInits;
4513     for (auto &Pair : Captures)
4514       PreInits.push_back(Pair.second->getDecl());
4515     return buildPreInits(Context, PreInits);
4516   }
4517   return nullptr;
4518 }
4519 
4520 /// Build postupdate expression for the given list of postupdates expressions.
4521 static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4522   Expr *PostUpdate = nullptr;
4523   if (!PostUpdates.empty()) {
4524     for (auto *E : PostUpdates) {
4525       Expr *ConvE = S.BuildCStyleCastExpr(
4526                          E->getExprLoc(),
4527                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4528                          E->getExprLoc(), E)
4529                         .get();
4530       PostUpdate = PostUpdate
4531                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4532                                               PostUpdate, ConvE)
4533                              .get()
4534                        : ConvE;
4535     }
4536   }
4537   return PostUpdate;
4538 }
4539 
4540 /// \brief Called on a for stmt to check itself and nested loops (if any).
4541 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4542 /// number of collapsed loops otherwise.
4543 static unsigned
4544 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4545                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4546                 DSAStackTy &DSA,
4547                 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4548                 OMPLoopDirective::HelperExprs &Built) {
4549   unsigned NestedLoopCount = 1;
4550   if (CollapseLoopCountExpr) {
4551     // Found 'collapse' clause - calculate collapse number.
4552     llvm::APSInt Result;
4553     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
4554       NestedLoopCount = Result.getLimitedValue();
4555   }
4556   if (OrderedLoopCountExpr) {
4557     // Found 'ordered' clause - calculate collapse number.
4558     llvm::APSInt Result;
4559     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4560       if (Result.getLimitedValue() < NestedLoopCount) {
4561         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4562                      diag::err_omp_wrong_ordered_loop_count)
4563             << OrderedLoopCountExpr->getSourceRange();
4564         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4565                      diag::note_collapse_loop_count)
4566             << CollapseLoopCountExpr->getSourceRange();
4567       }
4568       NestedLoopCount = Result.getLimitedValue();
4569     }
4570   }
4571   // This is helper routine for loop directives (e.g., 'for', 'simd',
4572   // 'for simd', etc.).
4573   llvm::MapVector<Expr *, DeclRefExpr *> Captures;
4574   SmallVector<LoopIterationSpace, 4> IterSpaces;
4575   IterSpaces.resize(NestedLoopCount);
4576   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
4577   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
4578     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
4579                                   NestedLoopCount, CollapseLoopCountExpr,
4580                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
4581                                   IterSpaces[Cnt], Captures))
4582       return 0;
4583     // Move on to the next nested for loop, or to the loop body.
4584     // OpenMP [2.8.1, simd construct, Restrictions]
4585     // All loops associated with the construct must be perfectly nested; that
4586     // is, there must be no intervening code nor any OpenMP directive between
4587     // any two loops.
4588     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4589   }
4590 
4591   Built.clear(/* size */ NestedLoopCount);
4592 
4593   if (SemaRef.CurContext->isDependentContext())
4594     return NestedLoopCount;
4595 
4596   // An example of what is generated for the following code:
4597   //
4598   //   #pragma omp simd collapse(2) ordered(2)
4599   //   for (i = 0; i < NI; ++i)
4600   //     for (k = 0; k < NK; ++k)
4601   //       for (j = J0; j < NJ; j+=2) {
4602   //         <loop body>
4603   //       }
4604   //
4605   // We generate the code below.
4606   // Note: the loop body may be outlined in CodeGen.
4607   // Note: some counters may be C++ classes, operator- is used to find number of
4608   // iterations and operator+= to calculate counter value.
4609   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4610   // or i64 is currently supported).
4611   //
4612   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4613   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4614   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4615   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4616   //     // similar updates for vars in clauses (e.g. 'linear')
4617   //     <loop body (using local i and j)>
4618   //   }
4619   //   i = NI; // assign final values of counters
4620   //   j = NJ;
4621   //
4622 
4623   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4624   // the iteration counts of the collapsed for loops.
4625   // Precondition tests if there is at least one iteration (all conditions are
4626   // true).
4627   auto PreCond = ExprResult(IterSpaces[0].PreCond);
4628   auto N0 = IterSpaces[0].NumIterations;
4629   ExprResult LastIteration32 = WidenIterationCount(
4630       32 /* Bits */, SemaRef.PerformImplicitConversion(
4631                                 N0->IgnoreImpCasts(), N0->getType(),
4632                                 Sema::AA_Converting, /*AllowExplicit=*/true)
4633                          .get(),
4634       SemaRef);
4635   ExprResult LastIteration64 = WidenIterationCount(
4636       64 /* Bits */, SemaRef.PerformImplicitConversion(
4637                                 N0->IgnoreImpCasts(), N0->getType(),
4638                                 Sema::AA_Converting, /*AllowExplicit=*/true)
4639                          .get(),
4640       SemaRef);
4641 
4642   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
4643     return NestedLoopCount;
4644 
4645   auto &C = SemaRef.Context;
4646   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
4647 
4648   Scope *CurScope = DSA.getCurScope();
4649   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
4650     if (PreCond.isUsable()) {
4651       PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
4652                                    PreCond.get(), IterSpaces[Cnt].PreCond);
4653     }
4654     auto N = IterSpaces[Cnt].NumIterations;
4655     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
4656     if (LastIteration32.isUsable())
4657       LastIteration32 = SemaRef.BuildBinOp(
4658           CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
4659           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4660                                             Sema::AA_Converting,
4661                                             /*AllowExplicit=*/true)
4662               .get());
4663     if (LastIteration64.isUsable())
4664       LastIteration64 = SemaRef.BuildBinOp(
4665           CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
4666           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
4667                                             Sema::AA_Converting,
4668                                             /*AllowExplicit=*/true)
4669               .get());
4670   }
4671 
4672   // Choose either the 32-bit or 64-bit version.
4673   ExprResult LastIteration = LastIteration64;
4674   if (LastIteration32.isUsable() &&
4675       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
4676       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
4677        FitsInto(
4678            32 /* Bits */,
4679            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
4680            LastIteration64.get(), SemaRef)))
4681     LastIteration = LastIteration32;
4682   QualType VType = LastIteration.get()->getType();
4683   QualType RealVType = VType;
4684   QualType StrideVType = VType;
4685   if (isOpenMPTaskLoopDirective(DKind)) {
4686     VType =
4687         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
4688     StrideVType =
4689         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
4690   }
4691 
4692   if (!LastIteration.isUsable())
4693     return 0;
4694 
4695   // Save the number of iterations.
4696   ExprResult NumIterations = LastIteration;
4697   {
4698     LastIteration = SemaRef.BuildBinOp(
4699         CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
4700         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4701     if (!LastIteration.isUsable())
4702       return 0;
4703   }
4704 
4705   // Calculate the last iteration number beforehand instead of doing this on
4706   // each iteration. Do not do this if the number of iterations may be kfold-ed.
4707   llvm::APSInt Result;
4708   bool IsConstant =
4709       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
4710   ExprResult CalcLastIteration;
4711   if (!IsConstant) {
4712     ExprResult SaveRef =
4713         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
4714     LastIteration = SaveRef;
4715 
4716     // Prepare SaveRef + 1.
4717     NumIterations = SemaRef.BuildBinOp(
4718         CurScope, SourceLocation(), BO_Add, SaveRef.get(),
4719         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4720     if (!NumIterations.isUsable())
4721       return 0;
4722   }
4723 
4724   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
4725 
4726   // Build variables passed into runtime, nesessary for worksharing directives.
4727   ExprResult LB, UB, IL, ST, EUB;
4728   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4729       isOpenMPDistributeDirective(DKind)) {
4730     // Lower bound variable, initialized with zero.
4731     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
4732     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
4733     SemaRef.AddInitializerToDecl(
4734         LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4735         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4736 
4737     // Upper bound variable, initialized with last iteration number.
4738     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
4739     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
4740     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
4741                                  /*DirectInit*/ false,
4742                                  /*TypeMayContainAuto*/ false);
4743 
4744     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
4745     // This will be used to implement clause 'lastprivate'.
4746     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
4747     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
4748     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
4749     SemaRef.AddInitializerToDecl(
4750         ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
4751         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4752 
4753     // Stride variable returned by runtime (we initialize it to 1 by default).
4754     VarDecl *STDecl =
4755         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
4756     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
4757     SemaRef.AddInitializerToDecl(
4758         STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
4759         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4760 
4761     // Build expression: UB = min(UB, LastIteration)
4762     // It is nesessary for CodeGen of directives with static scheduling.
4763     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
4764                                                 UB.get(), LastIteration.get());
4765     ExprResult CondOp = SemaRef.ActOnConditionalOp(
4766         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
4767     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
4768                              CondOp.get());
4769     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
4770   }
4771 
4772   // Build the iteration variable and its initialization before loop.
4773   ExprResult IV;
4774   ExprResult Init;
4775   {
4776     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
4777     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
4778     Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
4779                  isOpenMPTaskLoopDirective(DKind) ||
4780                  isOpenMPDistributeDirective(DKind))
4781                     ? LB.get()
4782                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
4783     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
4784     Init = SemaRef.ActOnFinishFullExpr(Init.get());
4785   }
4786 
4787   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
4788   SourceLocation CondLoc;
4789   ExprResult Cond =
4790       (isOpenMPWorksharingDirective(DKind) ||
4791        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
4792           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
4793           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
4794                                NumIterations.get());
4795 
4796   // Loop increment (IV = IV + 1)
4797   SourceLocation IncLoc;
4798   ExprResult Inc =
4799       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
4800                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
4801   if (!Inc.isUsable())
4802     return 0;
4803   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
4804   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
4805   if (!Inc.isUsable())
4806     return 0;
4807 
4808   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
4809   // Used for directives with static scheduling.
4810   ExprResult NextLB, NextUB;
4811   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
4812       isOpenMPDistributeDirective(DKind)) {
4813     // LB + ST
4814     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
4815     if (!NextLB.isUsable())
4816       return 0;
4817     // LB = LB + ST
4818     NextLB =
4819         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
4820     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
4821     if (!NextLB.isUsable())
4822       return 0;
4823     // UB + ST
4824     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
4825     if (!NextUB.isUsable())
4826       return 0;
4827     // UB = UB + ST
4828     NextUB =
4829         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
4830     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
4831     if (!NextUB.isUsable())
4832       return 0;
4833   }
4834 
4835   // Build updates and final values of the loop counters.
4836   bool HasErrors = false;
4837   Built.Counters.resize(NestedLoopCount);
4838   Built.Inits.resize(NestedLoopCount);
4839   Built.Updates.resize(NestedLoopCount);
4840   Built.Finals.resize(NestedLoopCount);
4841   {
4842     ExprResult Div;
4843     // Go from inner nested loop to outer.
4844     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
4845       LoopIterationSpace &IS = IterSpaces[Cnt];
4846       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
4847       // Build: Iter = (IV / Div) % IS.NumIters
4848       // where Div is product of previous iterations' IS.NumIters.
4849       ExprResult Iter;
4850       if (Div.isUsable()) {
4851         Iter =
4852             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
4853       } else {
4854         Iter = IV;
4855         assert((Cnt == (int)NestedLoopCount - 1) &&
4856                "unusable div expected on first iteration only");
4857       }
4858 
4859       if (Cnt != 0 && Iter.isUsable())
4860         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
4861                                   IS.NumIterations);
4862       if (!Iter.isUsable()) {
4863         HasErrors = true;
4864         break;
4865       }
4866 
4867       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
4868       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
4869       auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
4870                                           IS.CounterVar->getExprLoc(),
4871                                           /*RefersToCapture=*/true);
4872       ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
4873                                          IS.CounterInit, Captures);
4874       if (!Init.isUsable()) {
4875         HasErrors = true;
4876         break;
4877       }
4878       ExprResult Update = BuildCounterUpdate(
4879           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
4880           IS.CounterStep, IS.Subtract, &Captures);
4881       if (!Update.isUsable()) {
4882         HasErrors = true;
4883         break;
4884       }
4885 
4886       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
4887       ExprResult Final = BuildCounterUpdate(
4888           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
4889           IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
4890       if (!Final.isUsable()) {
4891         HasErrors = true;
4892         break;
4893       }
4894 
4895       // Build Div for the next iteration: Div <- Div * IS.NumIters
4896       if (Cnt != 0) {
4897         if (Div.isUnset())
4898           Div = IS.NumIterations;
4899         else
4900           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
4901                                    IS.NumIterations);
4902 
4903         // Add parentheses (for debugging purposes only).
4904         if (Div.isUsable())
4905           Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
4906         if (!Div.isUsable()) {
4907           HasErrors = true;
4908           break;
4909         }
4910       }
4911       if (!Update.isUsable() || !Final.isUsable()) {
4912         HasErrors = true;
4913         break;
4914       }
4915       // Save results
4916       Built.Counters[Cnt] = IS.CounterVar;
4917       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
4918       Built.Inits[Cnt] = Init.get();
4919       Built.Updates[Cnt] = Update.get();
4920       Built.Finals[Cnt] = Final.get();
4921     }
4922   }
4923 
4924   if (HasErrors)
4925     return 0;
4926 
4927   // Save results
4928   Built.IterationVarRef = IV.get();
4929   Built.LastIteration = LastIteration.get();
4930   Built.NumIterations = NumIterations.get();
4931   Built.CalcLastIteration =
4932       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
4933   Built.PreCond = PreCond.get();
4934   Built.PreInits = buildPreInits(C, Captures);
4935   Built.Cond = Cond.get();
4936   Built.Init = Init.get();
4937   Built.Inc = Inc.get();
4938   Built.LB = LB.get();
4939   Built.UB = UB.get();
4940   Built.IL = IL.get();
4941   Built.ST = ST.get();
4942   Built.EUB = EUB.get();
4943   Built.NLB = NextLB.get();
4944   Built.NUB = NextUB.get();
4945 
4946   return NestedLoopCount;
4947 }
4948 
4949 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
4950   auto CollapseClauses =
4951       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4952   if (CollapseClauses.begin() != CollapseClauses.end())
4953     return (*CollapseClauses.begin())->getNumForLoops();
4954   return nullptr;
4955 }
4956 
4957 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
4958   auto OrderedClauses =
4959       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4960   if (OrderedClauses.begin() != OrderedClauses.end())
4961     return (*OrderedClauses.begin())->getNumForLoops();
4962   return nullptr;
4963 }
4964 
4965 static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4966                                       const Expr *Safelen) {
4967   llvm::APSInt SimdlenRes, SafelenRes;
4968   if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4969       Simdlen->isInstantiationDependent() ||
4970       Simdlen->containsUnexpandedParameterPack())
4971     return false;
4972   if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4973       Safelen->isInstantiationDependent() ||
4974       Safelen->containsUnexpandedParameterPack())
4975     return false;
4976   Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4977   Safelen->EvaluateAsInt(SafelenRes, S.Context);
4978   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4979   // If both simdlen and safelen clauses are specified, the value of the simdlen
4980   // parameter must be less than or equal to the value of the safelen parameter.
4981   if (SimdlenRes > SafelenRes) {
4982     S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4983         << Simdlen->getSourceRange() << Safelen->getSourceRange();
4984     return true;
4985   }
4986   return false;
4987 }
4988 
4989 StmtResult Sema::ActOnOpenMPSimdDirective(
4990     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4991     SourceLocation EndLoc,
4992     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
4993   if (!AStmt)
4994     return StmtError();
4995 
4996   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4997   OMPLoopDirective::HelperExprs B;
4998   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4999   // define the nested loops number.
5000   unsigned NestedLoopCount = CheckOpenMPLoop(
5001       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5002       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5003   if (NestedLoopCount == 0)
5004     return StmtError();
5005 
5006   assert((CurContext->isDependentContext() || B.builtAll()) &&
5007          "omp simd loop exprs were not built");
5008 
5009   if (!CurContext->isDependentContext()) {
5010     // Finalize the clauses that need pre-built expressions for CodeGen.
5011     for (auto C : Clauses) {
5012       if (auto LC = dyn_cast<OMPLinearClause>(C))
5013         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5014                                      B.NumIterations, *this, CurScope,
5015                                      DSAStack))
5016           return StmtError();
5017     }
5018   }
5019 
5020   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5021   // If both simdlen and safelen clauses are specified, the value of the simdlen
5022   // parameter must be less than or equal to the value of the safelen parameter.
5023   OMPSafelenClause *Safelen = nullptr;
5024   OMPSimdlenClause *Simdlen = nullptr;
5025   for (auto *Clause : Clauses) {
5026     if (Clause->getClauseKind() == OMPC_safelen)
5027       Safelen = cast<OMPSafelenClause>(Clause);
5028     else if (Clause->getClauseKind() == OMPC_simdlen)
5029       Simdlen = cast<OMPSimdlenClause>(Clause);
5030     if (Safelen && Simdlen)
5031       break;
5032   }
5033   if (Simdlen && Safelen &&
5034       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5035                                 Safelen->getSafelen()))
5036     return StmtError();
5037 
5038   getCurFunction()->setHasBranchProtectedScope();
5039   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5040                                   Clauses, AStmt, B);
5041 }
5042 
5043 StmtResult Sema::ActOnOpenMPForDirective(
5044     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5045     SourceLocation EndLoc,
5046     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5047   if (!AStmt)
5048     return StmtError();
5049 
5050   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5051   OMPLoopDirective::HelperExprs B;
5052   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5053   // define the nested loops number.
5054   unsigned NestedLoopCount = CheckOpenMPLoop(
5055       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5056       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5057   if (NestedLoopCount == 0)
5058     return StmtError();
5059 
5060   assert((CurContext->isDependentContext() || B.builtAll()) &&
5061          "omp for loop exprs were not built");
5062 
5063   if (!CurContext->isDependentContext()) {
5064     // Finalize the clauses that need pre-built expressions for CodeGen.
5065     for (auto C : Clauses) {
5066       if (auto LC = dyn_cast<OMPLinearClause>(C))
5067         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5068                                      B.NumIterations, *this, CurScope,
5069                                      DSAStack))
5070           return StmtError();
5071     }
5072   }
5073 
5074   getCurFunction()->setHasBranchProtectedScope();
5075   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5076                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
5077 }
5078 
5079 StmtResult Sema::ActOnOpenMPForSimdDirective(
5080     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5081     SourceLocation EndLoc,
5082     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5083   if (!AStmt)
5084     return StmtError();
5085 
5086   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5087   OMPLoopDirective::HelperExprs B;
5088   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5089   // define the nested loops number.
5090   unsigned NestedLoopCount =
5091       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5092                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5093                       VarsWithImplicitDSA, B);
5094   if (NestedLoopCount == 0)
5095     return StmtError();
5096 
5097   assert((CurContext->isDependentContext() || B.builtAll()) &&
5098          "omp for simd loop exprs were not built");
5099 
5100   if (!CurContext->isDependentContext()) {
5101     // Finalize the clauses that need pre-built expressions for CodeGen.
5102     for (auto C : Clauses) {
5103       if (auto LC = dyn_cast<OMPLinearClause>(C))
5104         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5105                                      B.NumIterations, *this, CurScope,
5106                                      DSAStack))
5107           return StmtError();
5108     }
5109   }
5110 
5111   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5112   // If both simdlen and safelen clauses are specified, the value of the simdlen
5113   // parameter must be less than or equal to the value of the safelen parameter.
5114   OMPSafelenClause *Safelen = nullptr;
5115   OMPSimdlenClause *Simdlen = nullptr;
5116   for (auto *Clause : Clauses) {
5117     if (Clause->getClauseKind() == OMPC_safelen)
5118       Safelen = cast<OMPSafelenClause>(Clause);
5119     else if (Clause->getClauseKind() == OMPC_simdlen)
5120       Simdlen = cast<OMPSimdlenClause>(Clause);
5121     if (Safelen && Simdlen)
5122       break;
5123   }
5124   if (Simdlen && Safelen &&
5125       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5126                                 Safelen->getSafelen()))
5127     return StmtError();
5128 
5129   getCurFunction()->setHasBranchProtectedScope();
5130   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5131                                      Clauses, AStmt, B);
5132 }
5133 
5134 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5135                                               Stmt *AStmt,
5136                                               SourceLocation StartLoc,
5137                                               SourceLocation EndLoc) {
5138   if (!AStmt)
5139     return StmtError();
5140 
5141   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5142   auto BaseStmt = AStmt;
5143   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5144     BaseStmt = CS->getCapturedStmt();
5145   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5146     auto S = C->children();
5147     if (S.begin() == S.end())
5148       return StmtError();
5149     // All associated statements must be '#pragma omp section' except for
5150     // the first one.
5151     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5152       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5153         if (SectionStmt)
5154           Diag(SectionStmt->getLocStart(),
5155                diag::err_omp_sections_substmt_not_section);
5156         return StmtError();
5157       }
5158       cast<OMPSectionDirective>(SectionStmt)
5159           ->setHasCancel(DSAStack->isCancelRegion());
5160     }
5161   } else {
5162     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5163     return StmtError();
5164   }
5165 
5166   getCurFunction()->setHasBranchProtectedScope();
5167 
5168   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5169                                       DSAStack->isCancelRegion());
5170 }
5171 
5172 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5173                                              SourceLocation StartLoc,
5174                                              SourceLocation EndLoc) {
5175   if (!AStmt)
5176     return StmtError();
5177 
5178   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5179 
5180   getCurFunction()->setHasBranchProtectedScope();
5181   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5182 
5183   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5184                                      DSAStack->isCancelRegion());
5185 }
5186 
5187 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5188                                             Stmt *AStmt,
5189                                             SourceLocation StartLoc,
5190                                             SourceLocation EndLoc) {
5191   if (!AStmt)
5192     return StmtError();
5193 
5194   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5195 
5196   getCurFunction()->setHasBranchProtectedScope();
5197 
5198   // OpenMP [2.7.3, single Construct, Restrictions]
5199   // The copyprivate clause must not be used with the nowait clause.
5200   OMPClause *Nowait = nullptr;
5201   OMPClause *Copyprivate = nullptr;
5202   for (auto *Clause : Clauses) {
5203     if (Clause->getClauseKind() == OMPC_nowait)
5204       Nowait = Clause;
5205     else if (Clause->getClauseKind() == OMPC_copyprivate)
5206       Copyprivate = Clause;
5207     if (Copyprivate && Nowait) {
5208       Diag(Copyprivate->getLocStart(),
5209            diag::err_omp_single_copyprivate_with_nowait);
5210       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5211       return StmtError();
5212     }
5213   }
5214 
5215   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5216 }
5217 
5218 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5219                                             SourceLocation StartLoc,
5220                                             SourceLocation EndLoc) {
5221   if (!AStmt)
5222     return StmtError();
5223 
5224   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5225 
5226   getCurFunction()->setHasBranchProtectedScope();
5227 
5228   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5229 }
5230 
5231 StmtResult Sema::ActOnOpenMPCriticalDirective(
5232     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5233     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5234   if (!AStmt)
5235     return StmtError();
5236 
5237   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5238 
5239   bool ErrorFound = false;
5240   llvm::APSInt Hint;
5241   SourceLocation HintLoc;
5242   bool DependentHint = false;
5243   for (auto *C : Clauses) {
5244     if (C->getClauseKind() == OMPC_hint) {
5245       if (!DirName.getName()) {
5246         Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5247         ErrorFound = true;
5248       }
5249       Expr *E = cast<OMPHintClause>(C)->getHint();
5250       if (E->isTypeDependent() || E->isValueDependent() ||
5251           E->isInstantiationDependent())
5252         DependentHint = true;
5253       else {
5254         Hint = E->EvaluateKnownConstInt(Context);
5255         HintLoc = C->getLocStart();
5256       }
5257     }
5258   }
5259   if (ErrorFound)
5260     return StmtError();
5261   auto Pair = DSAStack->getCriticalWithHint(DirName);
5262   if (Pair.first && DirName.getName() && !DependentHint) {
5263     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5264       Diag(StartLoc, diag::err_omp_critical_with_hint);
5265       if (HintLoc.isValid()) {
5266         Diag(HintLoc, diag::note_omp_critical_hint_here)
5267             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5268       } else
5269         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5270       if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5271         Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5272             << 1
5273             << C->getHint()->EvaluateKnownConstInt(Context).toString(
5274                    /*Radix=*/10, /*Signed=*/false);
5275       } else
5276         Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5277     }
5278   }
5279 
5280   getCurFunction()->setHasBranchProtectedScope();
5281 
5282   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5283                                            Clauses, AStmt);
5284   if (!Pair.first && DirName.getName() && !DependentHint)
5285     DSAStack->addCriticalWithHint(Dir, Hint);
5286   return Dir;
5287 }
5288 
5289 StmtResult Sema::ActOnOpenMPParallelForDirective(
5290     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5291     SourceLocation EndLoc,
5292     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5293   if (!AStmt)
5294     return StmtError();
5295 
5296   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5297   // 1.2.2 OpenMP Language Terminology
5298   // Structured block - An executable statement with a single entry at the
5299   // top and a single exit at the bottom.
5300   // The point of exit cannot be a branch out of the structured block.
5301   // longjmp() and throw() must not violate the entry/exit criteria.
5302   CS->getCapturedDecl()->setNothrow();
5303 
5304   OMPLoopDirective::HelperExprs B;
5305   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5306   // define the nested loops number.
5307   unsigned NestedLoopCount =
5308       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5309                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5310                       VarsWithImplicitDSA, B);
5311   if (NestedLoopCount == 0)
5312     return StmtError();
5313 
5314   assert((CurContext->isDependentContext() || B.builtAll()) &&
5315          "omp parallel for loop exprs were not built");
5316 
5317   if (!CurContext->isDependentContext()) {
5318     // Finalize the clauses that need pre-built expressions for CodeGen.
5319     for (auto C : Clauses) {
5320       if (auto LC = dyn_cast<OMPLinearClause>(C))
5321         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5322                                      B.NumIterations, *this, CurScope,
5323                                      DSAStack))
5324           return StmtError();
5325     }
5326   }
5327 
5328   getCurFunction()->setHasBranchProtectedScope();
5329   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5330                                          NestedLoopCount, Clauses, AStmt, B,
5331                                          DSAStack->isCancelRegion());
5332 }
5333 
5334 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5335     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5336     SourceLocation EndLoc,
5337     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5338   if (!AStmt)
5339     return StmtError();
5340 
5341   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5342   // 1.2.2 OpenMP Language Terminology
5343   // Structured block - An executable statement with a single entry at the
5344   // top and a single exit at the bottom.
5345   // The point of exit cannot be a branch out of the structured block.
5346   // longjmp() and throw() must not violate the entry/exit criteria.
5347   CS->getCapturedDecl()->setNothrow();
5348 
5349   OMPLoopDirective::HelperExprs B;
5350   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5351   // define the nested loops number.
5352   unsigned NestedLoopCount =
5353       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5354                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5355                       VarsWithImplicitDSA, B);
5356   if (NestedLoopCount == 0)
5357     return StmtError();
5358 
5359   if (!CurContext->isDependentContext()) {
5360     // Finalize the clauses that need pre-built expressions for CodeGen.
5361     for (auto C : Clauses) {
5362       if (auto LC = dyn_cast<OMPLinearClause>(C))
5363         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5364                                      B.NumIterations, *this, CurScope,
5365                                      DSAStack))
5366           return StmtError();
5367     }
5368   }
5369 
5370   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5371   // If both simdlen and safelen clauses are specified, the value of the simdlen
5372   // parameter must be less than or equal to the value of the safelen parameter.
5373   OMPSafelenClause *Safelen = nullptr;
5374   OMPSimdlenClause *Simdlen = nullptr;
5375   for (auto *Clause : Clauses) {
5376     if (Clause->getClauseKind() == OMPC_safelen)
5377       Safelen = cast<OMPSafelenClause>(Clause);
5378     else if (Clause->getClauseKind() == OMPC_simdlen)
5379       Simdlen = cast<OMPSimdlenClause>(Clause);
5380     if (Safelen && Simdlen)
5381       break;
5382   }
5383   if (Simdlen && Safelen &&
5384       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5385                                 Safelen->getSafelen()))
5386     return StmtError();
5387 
5388   getCurFunction()->setHasBranchProtectedScope();
5389   return OMPParallelForSimdDirective::Create(
5390       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5391 }
5392 
5393 StmtResult
5394 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5395                                            Stmt *AStmt, SourceLocation StartLoc,
5396                                            SourceLocation EndLoc) {
5397   if (!AStmt)
5398     return StmtError();
5399 
5400   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5401   auto BaseStmt = AStmt;
5402   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5403     BaseStmt = CS->getCapturedStmt();
5404   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5405     auto S = C->children();
5406     if (S.begin() == S.end())
5407       return StmtError();
5408     // All associated statements must be '#pragma omp section' except for
5409     // the first one.
5410     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5411       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5412         if (SectionStmt)
5413           Diag(SectionStmt->getLocStart(),
5414                diag::err_omp_parallel_sections_substmt_not_section);
5415         return StmtError();
5416       }
5417       cast<OMPSectionDirective>(SectionStmt)
5418           ->setHasCancel(DSAStack->isCancelRegion());
5419     }
5420   } else {
5421     Diag(AStmt->getLocStart(),
5422          diag::err_omp_parallel_sections_not_compound_stmt);
5423     return StmtError();
5424   }
5425 
5426   getCurFunction()->setHasBranchProtectedScope();
5427 
5428   return OMPParallelSectionsDirective::Create(
5429       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
5430 }
5431 
5432 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5433                                           Stmt *AStmt, SourceLocation StartLoc,
5434                                           SourceLocation EndLoc) {
5435   if (!AStmt)
5436     return StmtError();
5437 
5438   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5439   // 1.2.2 OpenMP Language Terminology
5440   // Structured block - An executable statement with a single entry at the
5441   // top and a single exit at the bottom.
5442   // The point of exit cannot be a branch out of the structured block.
5443   // longjmp() and throw() must not violate the entry/exit criteria.
5444   CS->getCapturedDecl()->setNothrow();
5445 
5446   getCurFunction()->setHasBranchProtectedScope();
5447 
5448   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5449                                   DSAStack->isCancelRegion());
5450 }
5451 
5452 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5453                                                SourceLocation EndLoc) {
5454   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5455 }
5456 
5457 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5458                                              SourceLocation EndLoc) {
5459   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5460 }
5461 
5462 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5463                                               SourceLocation EndLoc) {
5464   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5465 }
5466 
5467 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5468                                                SourceLocation StartLoc,
5469                                                SourceLocation EndLoc) {
5470   if (!AStmt)
5471     return StmtError();
5472 
5473   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5474 
5475   getCurFunction()->setHasBranchProtectedScope();
5476 
5477   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5478 }
5479 
5480 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5481                                            SourceLocation StartLoc,
5482                                            SourceLocation EndLoc) {
5483   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5484   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5485 }
5486 
5487 StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5488                                              Stmt *AStmt,
5489                                              SourceLocation StartLoc,
5490                                              SourceLocation EndLoc) {
5491   OMPClause *DependFound = nullptr;
5492   OMPClause *DependSourceClause = nullptr;
5493   OMPClause *DependSinkClause = nullptr;
5494   bool ErrorFound = false;
5495   OMPThreadsClause *TC = nullptr;
5496   OMPSIMDClause *SC = nullptr;
5497   for (auto *C : Clauses) {
5498     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5499       DependFound = C;
5500       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5501         if (DependSourceClause) {
5502           Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5503               << getOpenMPDirectiveName(OMPD_ordered)
5504               << getOpenMPClauseName(OMPC_depend) << 2;
5505           ErrorFound = true;
5506         } else
5507           DependSourceClause = C;
5508         if (DependSinkClause) {
5509           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5510               << 0;
5511           ErrorFound = true;
5512         }
5513       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5514         if (DependSourceClause) {
5515           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5516               << 1;
5517           ErrorFound = true;
5518         }
5519         DependSinkClause = C;
5520       }
5521     } else if (C->getClauseKind() == OMPC_threads)
5522       TC = cast<OMPThreadsClause>(C);
5523     else if (C->getClauseKind() == OMPC_simd)
5524       SC = cast<OMPSIMDClause>(C);
5525   }
5526   if (!ErrorFound && !SC &&
5527       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
5528     // OpenMP [2.8.1,simd Construct, Restrictions]
5529     // An ordered construct with the simd clause is the only OpenMP construct
5530     // that can appear in the simd region.
5531     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5532     ErrorFound = true;
5533   } else if (DependFound && (TC || SC)) {
5534     Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5535         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
5536     ErrorFound = true;
5537   } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
5538     Diag(DependFound->getLocStart(),
5539          diag::err_omp_ordered_directive_without_param);
5540     ErrorFound = true;
5541   } else if (TC || Clauses.empty()) {
5542     if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
5543       SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
5544       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
5545           << (TC != nullptr);
5546       Diag(Param->getLocStart(), diag::note_omp_ordered_param);
5547       ErrorFound = true;
5548     }
5549   }
5550   if ((!AStmt && !DependFound) || ErrorFound)
5551     return StmtError();
5552 
5553   if (AStmt) {
5554     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5555 
5556     getCurFunction()->setHasBranchProtectedScope();
5557   }
5558 
5559   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5560 }
5561 
5562 namespace {
5563 /// \brief Helper class for checking expression in 'omp atomic [update]'
5564 /// construct.
5565 class OpenMPAtomicUpdateChecker {
5566   /// \brief Error results for atomic update expressions.
5567   enum ExprAnalysisErrorCode {
5568     /// \brief A statement is not an expression statement.
5569     NotAnExpression,
5570     /// \brief Expression is not builtin binary or unary operation.
5571     NotABinaryOrUnaryExpression,
5572     /// \brief Unary operation is not post-/pre- increment/decrement operation.
5573     NotAnUnaryIncDecExpression,
5574     /// \brief An expression is not of scalar type.
5575     NotAScalarType,
5576     /// \brief A binary operation is not an assignment operation.
5577     NotAnAssignmentOp,
5578     /// \brief RHS part of the binary operation is not a binary expression.
5579     NotABinaryExpression,
5580     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
5581     /// expression.
5582     NotABinaryOperator,
5583     /// \brief RHS binary operation does not have reference to the updated LHS
5584     /// part.
5585     NotAnUpdateExpression,
5586     /// \brief No errors is found.
5587     NoError
5588   };
5589   /// \brief Reference to Sema.
5590   Sema &SemaRef;
5591   /// \brief A location for note diagnostics (when error is found).
5592   SourceLocation NoteLoc;
5593   /// \brief 'x' lvalue part of the source atomic expression.
5594   Expr *X;
5595   /// \brief 'expr' rvalue part of the source atomic expression.
5596   Expr *E;
5597   /// \brief Helper expression of the form
5598   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5599   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5600   Expr *UpdateExpr;
5601   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
5602   /// important for non-associative operations.
5603   bool IsXLHSInRHSPart;
5604   BinaryOperatorKind Op;
5605   SourceLocation OpLoc;
5606   /// \brief true if the source expression is a postfix unary operation, false
5607   /// if it is a prefix unary operation.
5608   bool IsPostfixUpdate;
5609 
5610 public:
5611   OpenMPAtomicUpdateChecker(Sema &SemaRef)
5612       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
5613         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
5614   /// \brief Check specified statement that it is suitable for 'atomic update'
5615   /// constructs and extract 'x', 'expr' and Operation from the original
5616   /// expression. If DiagId and NoteId == 0, then only check is performed
5617   /// without error notification.
5618   /// \param DiagId Diagnostic which should be emitted if error is found.
5619   /// \param NoteId Diagnostic note for the main error message.
5620   /// \return true if statement is not an update expression, false otherwise.
5621   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
5622   /// \brief Return the 'x' lvalue part of the source atomic expression.
5623   Expr *getX() const { return X; }
5624   /// \brief Return the 'expr' rvalue part of the source atomic expression.
5625   Expr *getExpr() const { return E; }
5626   /// \brief Return the update expression used in calculation of the updated
5627   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
5628   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
5629   Expr *getUpdateExpr() const { return UpdateExpr; }
5630   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
5631   /// false otherwise.
5632   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
5633 
5634   /// \brief true if the source expression is a postfix unary operation, false
5635   /// if it is a prefix unary operation.
5636   bool isPostfixUpdate() const { return IsPostfixUpdate; }
5637 
5638 private:
5639   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
5640                             unsigned NoteId = 0);
5641 };
5642 } // namespace
5643 
5644 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
5645     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
5646   ExprAnalysisErrorCode ErrorFound = NoError;
5647   SourceLocation ErrorLoc, NoteLoc;
5648   SourceRange ErrorRange, NoteRange;
5649   // Allowed constructs are:
5650   //  x = x binop expr;
5651   //  x = expr binop x;
5652   if (AtomicBinOp->getOpcode() == BO_Assign) {
5653     X = AtomicBinOp->getLHS();
5654     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
5655             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
5656       if (AtomicInnerBinOp->isMultiplicativeOp() ||
5657           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
5658           AtomicInnerBinOp->isBitwiseOp()) {
5659         Op = AtomicInnerBinOp->getOpcode();
5660         OpLoc = AtomicInnerBinOp->getOperatorLoc();
5661         auto *LHS = AtomicInnerBinOp->getLHS();
5662         auto *RHS = AtomicInnerBinOp->getRHS();
5663         llvm::FoldingSetNodeID XId, LHSId, RHSId;
5664         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
5665                                           /*Canonical=*/true);
5666         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
5667                                             /*Canonical=*/true);
5668         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
5669                                             /*Canonical=*/true);
5670         if (XId == LHSId) {
5671           E = RHS;
5672           IsXLHSInRHSPart = true;
5673         } else if (XId == RHSId) {
5674           E = LHS;
5675           IsXLHSInRHSPart = false;
5676         } else {
5677           ErrorLoc = AtomicInnerBinOp->getExprLoc();
5678           ErrorRange = AtomicInnerBinOp->getSourceRange();
5679           NoteLoc = X->getExprLoc();
5680           NoteRange = X->getSourceRange();
5681           ErrorFound = NotAnUpdateExpression;
5682         }
5683       } else {
5684         ErrorLoc = AtomicInnerBinOp->getExprLoc();
5685         ErrorRange = AtomicInnerBinOp->getSourceRange();
5686         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
5687         NoteRange = SourceRange(NoteLoc, NoteLoc);
5688         ErrorFound = NotABinaryOperator;
5689       }
5690     } else {
5691       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
5692       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
5693       ErrorFound = NotABinaryExpression;
5694     }
5695   } else {
5696     ErrorLoc = AtomicBinOp->getExprLoc();
5697     ErrorRange = AtomicBinOp->getSourceRange();
5698     NoteLoc = AtomicBinOp->getOperatorLoc();
5699     NoteRange = SourceRange(NoteLoc, NoteLoc);
5700     ErrorFound = NotAnAssignmentOp;
5701   }
5702   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5703     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5704     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5705     return true;
5706   } else if (SemaRef.CurContext->isDependentContext())
5707     E = X = UpdateExpr = nullptr;
5708   return ErrorFound != NoError;
5709 }
5710 
5711 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
5712                                                unsigned NoteId) {
5713   ExprAnalysisErrorCode ErrorFound = NoError;
5714   SourceLocation ErrorLoc, NoteLoc;
5715   SourceRange ErrorRange, NoteRange;
5716   // Allowed constructs are:
5717   //  x++;
5718   //  x--;
5719   //  ++x;
5720   //  --x;
5721   //  x binop= expr;
5722   //  x = x binop expr;
5723   //  x = expr binop x;
5724   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
5725     AtomicBody = AtomicBody->IgnoreParenImpCasts();
5726     if (AtomicBody->getType()->isScalarType() ||
5727         AtomicBody->isInstantiationDependent()) {
5728       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
5729               AtomicBody->IgnoreParenImpCasts())) {
5730         // Check for Compound Assignment Operation
5731         Op = BinaryOperator::getOpForCompoundAssignment(
5732             AtomicCompAssignOp->getOpcode());
5733         OpLoc = AtomicCompAssignOp->getOperatorLoc();
5734         E = AtomicCompAssignOp->getRHS();
5735         X = AtomicCompAssignOp->getLHS();
5736         IsXLHSInRHSPart = true;
5737       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
5738                      AtomicBody->IgnoreParenImpCasts())) {
5739         // Check for Binary Operation
5740         if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
5741           return true;
5742       } else if (auto *AtomicUnaryOp =
5743                  dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
5744         // Check for Unary Operation
5745         if (AtomicUnaryOp->isIncrementDecrementOp()) {
5746           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
5747           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
5748           OpLoc = AtomicUnaryOp->getOperatorLoc();
5749           X = AtomicUnaryOp->getSubExpr();
5750           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
5751           IsXLHSInRHSPart = true;
5752         } else {
5753           ErrorFound = NotAnUnaryIncDecExpression;
5754           ErrorLoc = AtomicUnaryOp->getExprLoc();
5755           ErrorRange = AtomicUnaryOp->getSourceRange();
5756           NoteLoc = AtomicUnaryOp->getOperatorLoc();
5757           NoteRange = SourceRange(NoteLoc, NoteLoc);
5758         }
5759       } else if (!AtomicBody->isInstantiationDependent()) {
5760         ErrorFound = NotABinaryOrUnaryExpression;
5761         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
5762         NoteRange = ErrorRange = AtomicBody->getSourceRange();
5763       }
5764     } else {
5765       ErrorFound = NotAScalarType;
5766       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
5767       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5768     }
5769   } else {
5770     ErrorFound = NotAnExpression;
5771     NoteLoc = ErrorLoc = S->getLocStart();
5772     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5773   }
5774   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
5775     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
5776     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
5777     return true;
5778   } else if (SemaRef.CurContext->isDependentContext())
5779     E = X = UpdateExpr = nullptr;
5780   if (ErrorFound == NoError && E && X) {
5781     // Build an update expression of form 'OpaqueValueExpr(x) binop
5782     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
5783     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
5784     auto *OVEX = new (SemaRef.getASTContext())
5785         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
5786     auto *OVEExpr = new (SemaRef.getASTContext())
5787         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
5788     auto Update =
5789         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
5790                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
5791     if (Update.isInvalid())
5792       return true;
5793     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
5794                                                Sema::AA_Casting);
5795     if (Update.isInvalid())
5796       return true;
5797     UpdateExpr = Update.get();
5798   }
5799   return ErrorFound != NoError;
5800 }
5801 
5802 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
5803                                             Stmt *AStmt,
5804                                             SourceLocation StartLoc,
5805                                             SourceLocation EndLoc) {
5806   if (!AStmt)
5807     return StmtError();
5808 
5809   auto CS = cast<CapturedStmt>(AStmt);
5810   // 1.2.2 OpenMP Language Terminology
5811   // Structured block - An executable statement with a single entry at the
5812   // top and a single exit at the bottom.
5813   // The point of exit cannot be a branch out of the structured block.
5814   // longjmp() and throw() must not violate the entry/exit criteria.
5815   OpenMPClauseKind AtomicKind = OMPC_unknown;
5816   SourceLocation AtomicKindLoc;
5817   for (auto *C : Clauses) {
5818     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
5819         C->getClauseKind() == OMPC_update ||
5820         C->getClauseKind() == OMPC_capture) {
5821       if (AtomicKind != OMPC_unknown) {
5822         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
5823             << SourceRange(C->getLocStart(), C->getLocEnd());
5824         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
5825             << getOpenMPClauseName(AtomicKind);
5826       } else {
5827         AtomicKind = C->getClauseKind();
5828         AtomicKindLoc = C->getLocStart();
5829       }
5830     }
5831   }
5832 
5833   auto Body = CS->getCapturedStmt();
5834   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
5835     Body = EWC->getSubExpr();
5836 
5837   Expr *X = nullptr;
5838   Expr *V = nullptr;
5839   Expr *E = nullptr;
5840   Expr *UE = nullptr;
5841   bool IsXLHSInRHSPart = false;
5842   bool IsPostfixUpdate = false;
5843   // OpenMP [2.12.6, atomic Construct]
5844   // In the next expressions:
5845   // * x and v (as applicable) are both l-value expressions with scalar type.
5846   // * During the execution of an atomic region, multiple syntactic
5847   // occurrences of x must designate the same storage location.
5848   // * Neither of v and expr (as applicable) may access the storage location
5849   // designated by x.
5850   // * Neither of x and expr (as applicable) may access the storage location
5851   // designated by v.
5852   // * expr is an expression with scalar type.
5853   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
5854   // * binop, binop=, ++, and -- are not overloaded operators.
5855   // * The expression x binop expr must be numerically equivalent to x binop
5856   // (expr). This requirement is satisfied if the operators in expr have
5857   // precedence greater than binop, or by using parentheses around expr or
5858   // subexpressions of expr.
5859   // * The expression expr binop x must be numerically equivalent to (expr)
5860   // binop x. This requirement is satisfied if the operators in expr have
5861   // precedence equal to or greater than binop, or by using parentheses around
5862   // expr or subexpressions of expr.
5863   // * For forms that allow multiple occurrences of x, the number of times
5864   // that x is evaluated is unspecified.
5865   if (AtomicKind == OMPC_read) {
5866     enum {
5867       NotAnExpression,
5868       NotAnAssignmentOp,
5869       NotAScalarType,
5870       NotAnLValue,
5871       NoError
5872     } ErrorFound = NoError;
5873     SourceLocation ErrorLoc, NoteLoc;
5874     SourceRange ErrorRange, NoteRange;
5875     // If clause is read:
5876     //  v = x;
5877     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5878       auto AtomicBinOp =
5879           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5880       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5881         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5882         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
5883         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5884             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
5885           if (!X->isLValue() || !V->isLValue()) {
5886             auto NotLValueExpr = X->isLValue() ? V : X;
5887             ErrorFound = NotAnLValue;
5888             ErrorLoc = AtomicBinOp->getExprLoc();
5889             ErrorRange = AtomicBinOp->getSourceRange();
5890             NoteLoc = NotLValueExpr->getExprLoc();
5891             NoteRange = NotLValueExpr->getSourceRange();
5892           }
5893         } else if (!X->isInstantiationDependent() ||
5894                    !V->isInstantiationDependent()) {
5895           auto NotScalarExpr =
5896               (X->isInstantiationDependent() || X->getType()->isScalarType())
5897                   ? V
5898                   : X;
5899           ErrorFound = NotAScalarType;
5900           ErrorLoc = AtomicBinOp->getExprLoc();
5901           ErrorRange = AtomicBinOp->getSourceRange();
5902           NoteLoc = NotScalarExpr->getExprLoc();
5903           NoteRange = NotScalarExpr->getSourceRange();
5904         }
5905       } else if (!AtomicBody->isInstantiationDependent()) {
5906         ErrorFound = NotAnAssignmentOp;
5907         ErrorLoc = AtomicBody->getExprLoc();
5908         ErrorRange = AtomicBody->getSourceRange();
5909         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5910                               : AtomicBody->getExprLoc();
5911         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5912                                 : AtomicBody->getSourceRange();
5913       }
5914     } else {
5915       ErrorFound = NotAnExpression;
5916       NoteLoc = ErrorLoc = Body->getLocStart();
5917       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5918     }
5919     if (ErrorFound != NoError) {
5920       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
5921           << ErrorRange;
5922       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5923                                                       << NoteRange;
5924       return StmtError();
5925     } else if (CurContext->isDependentContext())
5926       V = X = nullptr;
5927   } else if (AtomicKind == OMPC_write) {
5928     enum {
5929       NotAnExpression,
5930       NotAnAssignmentOp,
5931       NotAScalarType,
5932       NotAnLValue,
5933       NoError
5934     } ErrorFound = NoError;
5935     SourceLocation ErrorLoc, NoteLoc;
5936     SourceRange ErrorRange, NoteRange;
5937     // If clause is write:
5938     //  x = expr;
5939     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
5940       auto AtomicBinOp =
5941           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5942       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5943         X = AtomicBinOp->getLHS();
5944         E = AtomicBinOp->getRHS();
5945         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
5946             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
5947           if (!X->isLValue()) {
5948             ErrorFound = NotAnLValue;
5949             ErrorLoc = AtomicBinOp->getExprLoc();
5950             ErrorRange = AtomicBinOp->getSourceRange();
5951             NoteLoc = X->getExprLoc();
5952             NoteRange = X->getSourceRange();
5953           }
5954         } else if (!X->isInstantiationDependent() ||
5955                    !E->isInstantiationDependent()) {
5956           auto NotScalarExpr =
5957               (X->isInstantiationDependent() || X->getType()->isScalarType())
5958                   ? E
5959                   : X;
5960           ErrorFound = NotAScalarType;
5961           ErrorLoc = AtomicBinOp->getExprLoc();
5962           ErrorRange = AtomicBinOp->getSourceRange();
5963           NoteLoc = NotScalarExpr->getExprLoc();
5964           NoteRange = NotScalarExpr->getSourceRange();
5965         }
5966       } else if (!AtomicBody->isInstantiationDependent()) {
5967         ErrorFound = NotAnAssignmentOp;
5968         ErrorLoc = AtomicBody->getExprLoc();
5969         ErrorRange = AtomicBody->getSourceRange();
5970         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5971                               : AtomicBody->getExprLoc();
5972         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5973                                 : AtomicBody->getSourceRange();
5974       }
5975     } else {
5976       ErrorFound = NotAnExpression;
5977       NoteLoc = ErrorLoc = Body->getLocStart();
5978       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
5979     }
5980     if (ErrorFound != NoError) {
5981       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5982           << ErrorRange;
5983       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5984                                                       << NoteRange;
5985       return StmtError();
5986     } else if (CurContext->isDependentContext())
5987       E = X = nullptr;
5988   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
5989     // If clause is update:
5990     //  x++;
5991     //  x--;
5992     //  ++x;
5993     //  --x;
5994     //  x binop= expr;
5995     //  x = x binop expr;
5996     //  x = expr binop x;
5997     OpenMPAtomicUpdateChecker Checker(*this);
5998     if (Checker.checkStatement(
5999             Body, (AtomicKind == OMPC_update)
6000                       ? diag::err_omp_atomic_update_not_expression_statement
6001                       : diag::err_omp_atomic_not_expression_statement,
6002             diag::note_omp_atomic_update))
6003       return StmtError();
6004     if (!CurContext->isDependentContext()) {
6005       E = Checker.getExpr();
6006       X = Checker.getX();
6007       UE = Checker.getUpdateExpr();
6008       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6009     }
6010   } else if (AtomicKind == OMPC_capture) {
6011     enum {
6012       NotAnAssignmentOp,
6013       NotACompoundStatement,
6014       NotTwoSubstatements,
6015       NotASpecificExpression,
6016       NoError
6017     } ErrorFound = NoError;
6018     SourceLocation ErrorLoc, NoteLoc;
6019     SourceRange ErrorRange, NoteRange;
6020     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6021       // If clause is a capture:
6022       //  v = x++;
6023       //  v = x--;
6024       //  v = ++x;
6025       //  v = --x;
6026       //  v = x binop= expr;
6027       //  v = x = x binop expr;
6028       //  v = x = expr binop x;
6029       auto *AtomicBinOp =
6030           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6031       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6032         V = AtomicBinOp->getLHS();
6033         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6034         OpenMPAtomicUpdateChecker Checker(*this);
6035         if (Checker.checkStatement(
6036                 Body, diag::err_omp_atomic_capture_not_expression_statement,
6037                 diag::note_omp_atomic_update))
6038           return StmtError();
6039         E = Checker.getExpr();
6040         X = Checker.getX();
6041         UE = Checker.getUpdateExpr();
6042         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6043         IsPostfixUpdate = Checker.isPostfixUpdate();
6044       } else if (!AtomicBody->isInstantiationDependent()) {
6045         ErrorLoc = AtomicBody->getExprLoc();
6046         ErrorRange = AtomicBody->getSourceRange();
6047         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6048                               : AtomicBody->getExprLoc();
6049         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6050                                 : AtomicBody->getSourceRange();
6051         ErrorFound = NotAnAssignmentOp;
6052       }
6053       if (ErrorFound != NoError) {
6054         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6055             << ErrorRange;
6056         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6057         return StmtError();
6058       } else if (CurContext->isDependentContext()) {
6059         UE = V = E = X = nullptr;
6060       }
6061     } else {
6062       // If clause is a capture:
6063       //  { v = x; x = expr; }
6064       //  { v = x; x++; }
6065       //  { v = x; x--; }
6066       //  { v = x; ++x; }
6067       //  { v = x; --x; }
6068       //  { v = x; x binop= expr; }
6069       //  { v = x; x = x binop expr; }
6070       //  { v = x; x = expr binop x; }
6071       //  { x++; v = x; }
6072       //  { x--; v = x; }
6073       //  { ++x; v = x; }
6074       //  { --x; v = x; }
6075       //  { x binop= expr; v = x; }
6076       //  { x = x binop expr; v = x; }
6077       //  { x = expr binop x; v = x; }
6078       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6079         // Check that this is { expr1; expr2; }
6080         if (CS->size() == 2) {
6081           auto *First = CS->body_front();
6082           auto *Second = CS->body_back();
6083           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6084             First = EWC->getSubExpr()->IgnoreParenImpCasts();
6085           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6086             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6087           // Need to find what subexpression is 'v' and what is 'x'.
6088           OpenMPAtomicUpdateChecker Checker(*this);
6089           bool IsUpdateExprFound = !Checker.checkStatement(Second);
6090           BinaryOperator *BinOp = nullptr;
6091           if (IsUpdateExprFound) {
6092             BinOp = dyn_cast<BinaryOperator>(First);
6093             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6094           }
6095           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6096             //  { v = x; x++; }
6097             //  { v = x; x--; }
6098             //  { v = x; ++x; }
6099             //  { v = x; --x; }
6100             //  { v = x; x binop= expr; }
6101             //  { v = x; x = x binop expr; }
6102             //  { v = x; x = expr binop x; }
6103             // Check that the first expression has form v = x.
6104             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6105             llvm::FoldingSetNodeID XId, PossibleXId;
6106             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6107             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6108             IsUpdateExprFound = XId == PossibleXId;
6109             if (IsUpdateExprFound) {
6110               V = BinOp->getLHS();
6111               X = Checker.getX();
6112               E = Checker.getExpr();
6113               UE = Checker.getUpdateExpr();
6114               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6115               IsPostfixUpdate = true;
6116             }
6117           }
6118           if (!IsUpdateExprFound) {
6119             IsUpdateExprFound = !Checker.checkStatement(First);
6120             BinOp = nullptr;
6121             if (IsUpdateExprFound) {
6122               BinOp = dyn_cast<BinaryOperator>(Second);
6123               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6124             }
6125             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6126               //  { x++; v = x; }
6127               //  { x--; v = x; }
6128               //  { ++x; v = x; }
6129               //  { --x; v = x; }
6130               //  { x binop= expr; v = x; }
6131               //  { x = x binop expr; v = x; }
6132               //  { x = expr binop x; v = x; }
6133               // Check that the second expression has form v = x.
6134               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6135               llvm::FoldingSetNodeID XId, PossibleXId;
6136               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6137               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6138               IsUpdateExprFound = XId == PossibleXId;
6139               if (IsUpdateExprFound) {
6140                 V = BinOp->getLHS();
6141                 X = Checker.getX();
6142                 E = Checker.getExpr();
6143                 UE = Checker.getUpdateExpr();
6144                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6145                 IsPostfixUpdate = false;
6146               }
6147             }
6148           }
6149           if (!IsUpdateExprFound) {
6150             //  { v = x; x = expr; }
6151             auto *FirstExpr = dyn_cast<Expr>(First);
6152             auto *SecondExpr = dyn_cast<Expr>(Second);
6153             if (!FirstExpr || !SecondExpr ||
6154                 !(FirstExpr->isInstantiationDependent() ||
6155                   SecondExpr->isInstantiationDependent())) {
6156               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6157               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
6158                 ErrorFound = NotAnAssignmentOp;
6159                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6160                                                 : First->getLocStart();
6161                 NoteRange = ErrorRange = FirstBinOp
6162                                              ? FirstBinOp->getSourceRange()
6163                                              : SourceRange(ErrorLoc, ErrorLoc);
6164               } else {
6165                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6166                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6167                   ErrorFound = NotAnAssignmentOp;
6168                   NoteLoc = ErrorLoc = SecondBinOp
6169                                            ? SecondBinOp->getOperatorLoc()
6170                                            : Second->getLocStart();
6171                   NoteRange = ErrorRange =
6172                       SecondBinOp ? SecondBinOp->getSourceRange()
6173                                   : SourceRange(ErrorLoc, ErrorLoc);
6174                 } else {
6175                   auto *PossibleXRHSInFirst =
6176                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
6177                   auto *PossibleXLHSInSecond =
6178                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
6179                   llvm::FoldingSetNodeID X1Id, X2Id;
6180                   PossibleXRHSInFirst->Profile(X1Id, Context,
6181                                                /*Canonical=*/true);
6182                   PossibleXLHSInSecond->Profile(X2Id, Context,
6183                                                 /*Canonical=*/true);
6184                   IsUpdateExprFound = X1Id == X2Id;
6185                   if (IsUpdateExprFound) {
6186                     V = FirstBinOp->getLHS();
6187                     X = SecondBinOp->getLHS();
6188                     E = SecondBinOp->getRHS();
6189                     UE = nullptr;
6190                     IsXLHSInRHSPart = false;
6191                     IsPostfixUpdate = true;
6192                   } else {
6193                     ErrorFound = NotASpecificExpression;
6194                     ErrorLoc = FirstBinOp->getExprLoc();
6195                     ErrorRange = FirstBinOp->getSourceRange();
6196                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6197                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
6198                   }
6199                 }
6200               }
6201             }
6202           }
6203         } else {
6204           NoteLoc = ErrorLoc = Body->getLocStart();
6205           NoteRange = ErrorRange =
6206               SourceRange(Body->getLocStart(), Body->getLocStart());
6207           ErrorFound = NotTwoSubstatements;
6208         }
6209       } else {
6210         NoteLoc = ErrorLoc = Body->getLocStart();
6211         NoteRange = ErrorRange =
6212             SourceRange(Body->getLocStart(), Body->getLocStart());
6213         ErrorFound = NotACompoundStatement;
6214       }
6215       if (ErrorFound != NoError) {
6216         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6217             << ErrorRange;
6218         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6219         return StmtError();
6220       } else if (CurContext->isDependentContext()) {
6221         UE = V = E = X = nullptr;
6222       }
6223     }
6224   }
6225 
6226   getCurFunction()->setHasBranchProtectedScope();
6227 
6228   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6229                                     X, V, E, UE, IsXLHSInRHSPart,
6230                                     IsPostfixUpdate);
6231 }
6232 
6233 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6234                                             Stmt *AStmt,
6235                                             SourceLocation StartLoc,
6236                                             SourceLocation EndLoc) {
6237   if (!AStmt)
6238     return StmtError();
6239 
6240   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6241   // 1.2.2 OpenMP Language Terminology
6242   // Structured block - An executable statement with a single entry at the
6243   // top and a single exit at the bottom.
6244   // The point of exit cannot be a branch out of the structured block.
6245   // longjmp() and throw() must not violate the entry/exit criteria.
6246   CS->getCapturedDecl()->setNothrow();
6247 
6248   // OpenMP [2.16, Nesting of Regions]
6249   // If specified, a teams construct must be contained within a target
6250   // construct. That target construct must contain no statements or directives
6251   // outside of the teams construct.
6252   if (DSAStack->hasInnerTeamsRegion()) {
6253     auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6254     bool OMPTeamsFound = true;
6255     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6256       auto I = CS->body_begin();
6257       while (I != CS->body_end()) {
6258         auto OED = dyn_cast<OMPExecutableDirective>(*I);
6259         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6260           OMPTeamsFound = false;
6261           break;
6262         }
6263         ++I;
6264       }
6265       assert(I != CS->body_end() && "Not found statement");
6266       S = *I;
6267     }
6268     if (!OMPTeamsFound) {
6269       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6270       Diag(DSAStack->getInnerTeamsRegionLoc(),
6271            diag::note_omp_nested_teams_construct_here);
6272       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6273           << isa<OMPExecutableDirective>(S);
6274       return StmtError();
6275     }
6276   }
6277 
6278   getCurFunction()->setHasBranchProtectedScope();
6279 
6280   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6281 }
6282 
6283 StmtResult
6284 Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6285                                          Stmt *AStmt, SourceLocation StartLoc,
6286                                          SourceLocation EndLoc) {
6287   if (!AStmt)
6288     return StmtError();
6289 
6290   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6291   // 1.2.2 OpenMP Language Terminology
6292   // Structured block - An executable statement with a single entry at the
6293   // top and a single exit at the bottom.
6294   // The point of exit cannot be a branch out of the structured block.
6295   // longjmp() and throw() must not violate the entry/exit criteria.
6296   CS->getCapturedDecl()->setNothrow();
6297 
6298   getCurFunction()->setHasBranchProtectedScope();
6299 
6300   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6301                                             AStmt);
6302 }
6303 
6304 StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6305     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6306     SourceLocation EndLoc,
6307     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6308   if (!AStmt)
6309     return StmtError();
6310 
6311   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6312   // 1.2.2 OpenMP Language Terminology
6313   // Structured block - An executable statement with a single entry at the
6314   // top and a single exit at the bottom.
6315   // The point of exit cannot be a branch out of the structured block.
6316   // longjmp() and throw() must not violate the entry/exit criteria.
6317   CS->getCapturedDecl()->setNothrow();
6318 
6319   OMPLoopDirective::HelperExprs B;
6320   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6321   // define the nested loops number.
6322   unsigned NestedLoopCount =
6323       CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6324                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6325                       VarsWithImplicitDSA, B);
6326   if (NestedLoopCount == 0)
6327     return StmtError();
6328 
6329   assert((CurContext->isDependentContext() || B.builtAll()) &&
6330          "omp target parallel for loop exprs were not built");
6331 
6332   if (!CurContext->isDependentContext()) {
6333     // Finalize the clauses that need pre-built expressions for CodeGen.
6334     for (auto C : Clauses) {
6335       if (auto LC = dyn_cast<OMPLinearClause>(C))
6336         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6337                                      B.NumIterations, *this, CurScope,
6338                                      DSAStack))
6339           return StmtError();
6340     }
6341   }
6342 
6343   getCurFunction()->setHasBranchProtectedScope();
6344   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6345                                                NestedLoopCount, Clauses, AStmt,
6346                                                B, DSAStack->isCancelRegion());
6347 }
6348 
6349 /// \brief Check for existence of a map clause in the list of clauses.
6350 static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6351   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6352        I != E; ++I) {
6353     if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6354       return true;
6355     }
6356   }
6357 
6358   return false;
6359 }
6360 
6361 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6362                                                 Stmt *AStmt,
6363                                                 SourceLocation StartLoc,
6364                                                 SourceLocation EndLoc) {
6365   if (!AStmt)
6366     return StmtError();
6367 
6368   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6369 
6370   // OpenMP [2.10.1, Restrictions, p. 97]
6371   // At least one map clause must appear on the directive.
6372   if (!HasMapClause(Clauses)) {
6373     Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6374         getOpenMPDirectiveName(OMPD_target_data);
6375     return StmtError();
6376   }
6377 
6378   getCurFunction()->setHasBranchProtectedScope();
6379 
6380   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6381                                         AStmt);
6382 }
6383 
6384 StmtResult
6385 Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6386                                           SourceLocation StartLoc,
6387                                           SourceLocation EndLoc) {
6388   // OpenMP [2.10.2, Restrictions, p. 99]
6389   // At least one map clause must appear on the directive.
6390   if (!HasMapClause(Clauses)) {
6391     Diag(StartLoc, diag::err_omp_no_map_for_directive)
6392         << getOpenMPDirectiveName(OMPD_target_enter_data);
6393     return StmtError();
6394   }
6395 
6396   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6397                                              Clauses);
6398 }
6399 
6400 StmtResult
6401 Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6402                                          SourceLocation StartLoc,
6403                                          SourceLocation EndLoc) {
6404   // OpenMP [2.10.3, Restrictions, p. 102]
6405   // At least one map clause must appear on the directive.
6406   if (!HasMapClause(Clauses)) {
6407     Diag(StartLoc, diag::err_omp_no_map_for_directive)
6408         << getOpenMPDirectiveName(OMPD_target_exit_data);
6409     return StmtError();
6410   }
6411 
6412   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6413 }
6414 
6415 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6416                                            Stmt *AStmt, SourceLocation StartLoc,
6417                                            SourceLocation EndLoc) {
6418   if (!AStmt)
6419     return StmtError();
6420 
6421   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6422   // 1.2.2 OpenMP Language Terminology
6423   // Structured block - An executable statement with a single entry at the
6424   // top and a single exit at the bottom.
6425   // The point of exit cannot be a branch out of the structured block.
6426   // longjmp() and throw() must not violate the entry/exit criteria.
6427   CS->getCapturedDecl()->setNothrow();
6428 
6429   getCurFunction()->setHasBranchProtectedScope();
6430 
6431   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6432 }
6433 
6434 StmtResult
6435 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6436                                             SourceLocation EndLoc,
6437                                             OpenMPDirectiveKind CancelRegion) {
6438   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6439       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6440     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6441         << getOpenMPDirectiveName(CancelRegion);
6442     return StmtError();
6443   }
6444   if (DSAStack->isParentNowaitRegion()) {
6445     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6446     return StmtError();
6447   }
6448   if (DSAStack->isParentOrderedRegion()) {
6449     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6450     return StmtError();
6451   }
6452   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6453                                                CancelRegion);
6454 }
6455 
6456 StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6457                                             SourceLocation StartLoc,
6458                                             SourceLocation EndLoc,
6459                                             OpenMPDirectiveKind CancelRegion) {
6460   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6461       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6462     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6463         << getOpenMPDirectiveName(CancelRegion);
6464     return StmtError();
6465   }
6466   if (DSAStack->isParentNowaitRegion()) {
6467     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6468     return StmtError();
6469   }
6470   if (DSAStack->isParentOrderedRegion()) {
6471     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6472     return StmtError();
6473   }
6474   DSAStack->setParentCancelRegion(/*Cancel=*/true);
6475   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6476                                     CancelRegion);
6477 }
6478 
6479 static bool checkGrainsizeNumTasksClauses(Sema &S,
6480                                           ArrayRef<OMPClause *> Clauses) {
6481   OMPClause *PrevClause = nullptr;
6482   bool ErrorFound = false;
6483   for (auto *C : Clauses) {
6484     if (C->getClauseKind() == OMPC_grainsize ||
6485         C->getClauseKind() == OMPC_num_tasks) {
6486       if (!PrevClause)
6487         PrevClause = C;
6488       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6489         S.Diag(C->getLocStart(),
6490                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6491             << getOpenMPClauseName(C->getClauseKind())
6492             << getOpenMPClauseName(PrevClause->getClauseKind());
6493         S.Diag(PrevClause->getLocStart(),
6494                diag::note_omp_previous_grainsize_num_tasks)
6495             << getOpenMPClauseName(PrevClause->getClauseKind());
6496         ErrorFound = true;
6497       }
6498     }
6499   }
6500   return ErrorFound;
6501 }
6502 
6503 StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6504     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6505     SourceLocation EndLoc,
6506     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6507   if (!AStmt)
6508     return StmtError();
6509 
6510   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6511   OMPLoopDirective::HelperExprs B;
6512   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6513   // define the nested loops number.
6514   unsigned NestedLoopCount =
6515       CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
6516                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6517                       VarsWithImplicitDSA, B);
6518   if (NestedLoopCount == 0)
6519     return StmtError();
6520 
6521   assert((CurContext->isDependentContext() || B.builtAll()) &&
6522          "omp for loop exprs were not built");
6523 
6524   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6525   // The grainsize clause and num_tasks clause are mutually exclusive and may
6526   // not appear on the same taskloop directive.
6527   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6528     return StmtError();
6529 
6530   getCurFunction()->setHasBranchProtectedScope();
6531   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
6532                                       NestedLoopCount, Clauses, AStmt, B);
6533 }
6534 
6535 StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
6536     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6537     SourceLocation EndLoc,
6538     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6539   if (!AStmt)
6540     return StmtError();
6541 
6542   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6543   OMPLoopDirective::HelperExprs B;
6544   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6545   // define the nested loops number.
6546   unsigned NestedLoopCount =
6547       CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
6548                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6549                       VarsWithImplicitDSA, B);
6550   if (NestedLoopCount == 0)
6551     return StmtError();
6552 
6553   assert((CurContext->isDependentContext() || B.builtAll()) &&
6554          "omp for loop exprs were not built");
6555 
6556   if (!CurContext->isDependentContext()) {
6557     // Finalize the clauses that need pre-built expressions for CodeGen.
6558     for (auto C : Clauses) {
6559       if (auto LC = dyn_cast<OMPLinearClause>(C))
6560         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6561                                      B.NumIterations, *this, CurScope,
6562                                      DSAStack))
6563           return StmtError();
6564     }
6565   }
6566 
6567   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
6568   // The grainsize clause and num_tasks clause are mutually exclusive and may
6569   // not appear on the same taskloop directive.
6570   if (checkGrainsizeNumTasksClauses(*this, Clauses))
6571     return StmtError();
6572 
6573   getCurFunction()->setHasBranchProtectedScope();
6574   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
6575                                           NestedLoopCount, Clauses, AStmt, B);
6576 }
6577 
6578 StmtResult Sema::ActOnOpenMPDistributeDirective(
6579     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6580     SourceLocation EndLoc,
6581     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6582   if (!AStmt)
6583     return StmtError();
6584 
6585   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6586   OMPLoopDirective::HelperExprs B;
6587   // In presence of clause 'collapse' with number of loops, it will
6588   // define the nested loops number.
6589   unsigned NestedLoopCount =
6590       CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
6591                       nullptr /*ordered not a clause on distribute*/, AStmt,
6592                       *this, *DSAStack, VarsWithImplicitDSA, B);
6593   if (NestedLoopCount == 0)
6594     return StmtError();
6595 
6596   assert((CurContext->isDependentContext() || B.builtAll()) &&
6597          "omp for loop exprs were not built");
6598 
6599   getCurFunction()->setHasBranchProtectedScope();
6600   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
6601                                         NestedLoopCount, Clauses, AStmt, B);
6602 }
6603 
6604 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
6605                                              SourceLocation StartLoc,
6606                                              SourceLocation LParenLoc,
6607                                              SourceLocation EndLoc) {
6608   OMPClause *Res = nullptr;
6609   switch (Kind) {
6610   case OMPC_final:
6611     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
6612     break;
6613   case OMPC_num_threads:
6614     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
6615     break;
6616   case OMPC_safelen:
6617     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
6618     break;
6619   case OMPC_simdlen:
6620     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
6621     break;
6622   case OMPC_collapse:
6623     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
6624     break;
6625   case OMPC_ordered:
6626     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
6627     break;
6628   case OMPC_device:
6629     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
6630     break;
6631   case OMPC_num_teams:
6632     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
6633     break;
6634   case OMPC_thread_limit:
6635     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
6636     break;
6637   case OMPC_priority:
6638     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
6639     break;
6640   case OMPC_grainsize:
6641     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
6642     break;
6643   case OMPC_num_tasks:
6644     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
6645     break;
6646   case OMPC_hint:
6647     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
6648     break;
6649   case OMPC_if:
6650   case OMPC_default:
6651   case OMPC_proc_bind:
6652   case OMPC_schedule:
6653   case OMPC_private:
6654   case OMPC_firstprivate:
6655   case OMPC_lastprivate:
6656   case OMPC_shared:
6657   case OMPC_reduction:
6658   case OMPC_linear:
6659   case OMPC_aligned:
6660   case OMPC_copyin:
6661   case OMPC_copyprivate:
6662   case OMPC_nowait:
6663   case OMPC_untied:
6664   case OMPC_mergeable:
6665   case OMPC_threadprivate:
6666   case OMPC_flush:
6667   case OMPC_read:
6668   case OMPC_write:
6669   case OMPC_update:
6670   case OMPC_capture:
6671   case OMPC_seq_cst:
6672   case OMPC_depend:
6673   case OMPC_threads:
6674   case OMPC_simd:
6675   case OMPC_map:
6676   case OMPC_nogroup:
6677   case OMPC_dist_schedule:
6678   case OMPC_defaultmap:
6679   case OMPC_unknown:
6680   case OMPC_uniform:
6681     llvm_unreachable("Clause is not allowed.");
6682   }
6683   return Res;
6684 }
6685 
6686 OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
6687                                      Expr *Condition, SourceLocation StartLoc,
6688                                      SourceLocation LParenLoc,
6689                                      SourceLocation NameModifierLoc,
6690                                      SourceLocation ColonLoc,
6691                                      SourceLocation EndLoc) {
6692   Expr *ValExpr = Condition;
6693   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6694       !Condition->isInstantiationDependent() &&
6695       !Condition->containsUnexpandedParameterPack()) {
6696     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6697                                            Condition->getExprLoc(), Condition);
6698     if (Val.isInvalid())
6699       return nullptr;
6700 
6701     ValExpr = Val.get();
6702   }
6703 
6704   return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
6705                                    NameModifierLoc, ColonLoc, EndLoc);
6706 }
6707 
6708 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
6709                                         SourceLocation StartLoc,
6710                                         SourceLocation LParenLoc,
6711                                         SourceLocation EndLoc) {
6712   Expr *ValExpr = Condition;
6713   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
6714       !Condition->isInstantiationDependent() &&
6715       !Condition->containsUnexpandedParameterPack()) {
6716     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
6717                                            Condition->getExprLoc(), Condition);
6718     if (Val.isInvalid())
6719       return nullptr;
6720 
6721     ValExpr = Val.get();
6722   }
6723 
6724   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6725 }
6726 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
6727                                                         Expr *Op) {
6728   if (!Op)
6729     return ExprError();
6730 
6731   class IntConvertDiagnoser : public ICEConvertDiagnoser {
6732   public:
6733     IntConvertDiagnoser()
6734         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
6735     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
6736                                          QualType T) override {
6737       return S.Diag(Loc, diag::err_omp_not_integral) << T;
6738     }
6739     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
6740                                              QualType T) override {
6741       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
6742     }
6743     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
6744                                                QualType T,
6745                                                QualType ConvTy) override {
6746       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
6747     }
6748     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
6749                                            QualType ConvTy) override {
6750       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
6751              << ConvTy->isEnumeralType() << ConvTy;
6752     }
6753     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
6754                                             QualType T) override {
6755       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
6756     }
6757     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
6758                                         QualType ConvTy) override {
6759       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
6760              << ConvTy->isEnumeralType() << ConvTy;
6761     }
6762     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
6763                                              QualType) override {
6764       llvm_unreachable("conversion functions are permitted");
6765     }
6766   } ConvertDiagnoser;
6767   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
6768 }
6769 
6770 static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
6771                                       OpenMPClauseKind CKind,
6772                                       bool StrictlyPositive) {
6773   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
6774       !ValExpr->isInstantiationDependent()) {
6775     SourceLocation Loc = ValExpr->getExprLoc();
6776     ExprResult Value =
6777         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
6778     if (Value.isInvalid())
6779       return false;
6780 
6781     ValExpr = Value.get();
6782     // The expression must evaluate to a non-negative integer value.
6783     llvm::APSInt Result;
6784     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
6785         Result.isSigned() &&
6786         !((!StrictlyPositive && Result.isNonNegative()) ||
6787           (StrictlyPositive && Result.isStrictlyPositive()))) {
6788       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
6789           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6790           << ValExpr->getSourceRange();
6791       return false;
6792     }
6793   }
6794   return true;
6795 }
6796 
6797 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
6798                                              SourceLocation StartLoc,
6799                                              SourceLocation LParenLoc,
6800                                              SourceLocation EndLoc) {
6801   Expr *ValExpr = NumThreads;
6802 
6803   // OpenMP [2.5, Restrictions]
6804   //  The num_threads expression must evaluate to a positive integer value.
6805   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
6806                                  /*StrictlyPositive=*/true))
6807     return nullptr;
6808 
6809   return new (Context)
6810       OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
6811 }
6812 
6813 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
6814                                                        OpenMPClauseKind CKind,
6815                                                        bool StrictlyPositive) {
6816   if (!E)
6817     return ExprError();
6818   if (E->isValueDependent() || E->isTypeDependent() ||
6819       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
6820     return E;
6821   llvm::APSInt Result;
6822   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
6823   if (ICE.isInvalid())
6824     return ExprError();
6825   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
6826       (!StrictlyPositive && !Result.isNonNegative())) {
6827     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
6828         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
6829         << E->getSourceRange();
6830     return ExprError();
6831   }
6832   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
6833     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
6834         << E->getSourceRange();
6835     return ExprError();
6836   }
6837   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
6838     DSAStack->setAssociatedLoops(Result.getExtValue());
6839   else if (CKind == OMPC_ordered)
6840     DSAStack->setAssociatedLoops(Result.getExtValue());
6841   return ICE;
6842 }
6843 
6844 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
6845                                           SourceLocation LParenLoc,
6846                                           SourceLocation EndLoc) {
6847   // OpenMP [2.8.1, simd construct, Description]
6848   // The parameter of the safelen clause must be a constant
6849   // positive integer expression.
6850   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
6851   if (Safelen.isInvalid())
6852     return nullptr;
6853   return new (Context)
6854       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
6855 }
6856 
6857 OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
6858                                           SourceLocation LParenLoc,
6859                                           SourceLocation EndLoc) {
6860   // OpenMP [2.8.1, simd construct, Description]
6861   // The parameter of the simdlen clause must be a constant
6862   // positive integer expression.
6863   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
6864   if (Simdlen.isInvalid())
6865     return nullptr;
6866   return new (Context)
6867       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
6868 }
6869 
6870 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
6871                                            SourceLocation StartLoc,
6872                                            SourceLocation LParenLoc,
6873                                            SourceLocation EndLoc) {
6874   // OpenMP [2.7.1, loop construct, Description]
6875   // OpenMP [2.8.1, simd construct, Description]
6876   // OpenMP [2.9.6, distribute construct, Description]
6877   // The parameter of the collapse clause must be a constant
6878   // positive integer expression.
6879   ExprResult NumForLoopsResult =
6880       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
6881   if (NumForLoopsResult.isInvalid())
6882     return nullptr;
6883   return new (Context)
6884       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
6885 }
6886 
6887 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
6888                                           SourceLocation EndLoc,
6889                                           SourceLocation LParenLoc,
6890                                           Expr *NumForLoops) {
6891   // OpenMP [2.7.1, loop construct, Description]
6892   // OpenMP [2.8.1, simd construct, Description]
6893   // OpenMP [2.9.6, distribute construct, Description]
6894   // The parameter of the ordered clause must be a constant
6895   // positive integer expression if any.
6896   if (NumForLoops && LParenLoc.isValid()) {
6897     ExprResult NumForLoopsResult =
6898         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
6899     if (NumForLoopsResult.isInvalid())
6900       return nullptr;
6901     NumForLoops = NumForLoopsResult.get();
6902   } else
6903     NumForLoops = nullptr;
6904   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
6905   return new (Context)
6906       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
6907 }
6908 
6909 OMPClause *Sema::ActOnOpenMPSimpleClause(
6910     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
6911     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
6912   OMPClause *Res = nullptr;
6913   switch (Kind) {
6914   case OMPC_default:
6915     Res =
6916         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
6917                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
6918     break;
6919   case OMPC_proc_bind:
6920     Res = ActOnOpenMPProcBindClause(
6921         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
6922         LParenLoc, EndLoc);
6923     break;
6924   case OMPC_if:
6925   case OMPC_final:
6926   case OMPC_num_threads:
6927   case OMPC_safelen:
6928   case OMPC_simdlen:
6929   case OMPC_collapse:
6930   case OMPC_schedule:
6931   case OMPC_private:
6932   case OMPC_firstprivate:
6933   case OMPC_lastprivate:
6934   case OMPC_shared:
6935   case OMPC_reduction:
6936   case OMPC_linear:
6937   case OMPC_aligned:
6938   case OMPC_copyin:
6939   case OMPC_copyprivate:
6940   case OMPC_ordered:
6941   case OMPC_nowait:
6942   case OMPC_untied:
6943   case OMPC_mergeable:
6944   case OMPC_threadprivate:
6945   case OMPC_flush:
6946   case OMPC_read:
6947   case OMPC_write:
6948   case OMPC_update:
6949   case OMPC_capture:
6950   case OMPC_seq_cst:
6951   case OMPC_depend:
6952   case OMPC_device:
6953   case OMPC_threads:
6954   case OMPC_simd:
6955   case OMPC_map:
6956   case OMPC_num_teams:
6957   case OMPC_thread_limit:
6958   case OMPC_priority:
6959   case OMPC_grainsize:
6960   case OMPC_nogroup:
6961   case OMPC_num_tasks:
6962   case OMPC_hint:
6963   case OMPC_dist_schedule:
6964   case OMPC_defaultmap:
6965   case OMPC_unknown:
6966   case OMPC_uniform:
6967     llvm_unreachable("Clause is not allowed.");
6968   }
6969   return Res;
6970 }
6971 
6972 static std::string
6973 getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
6974                         ArrayRef<unsigned> Exclude = llvm::None) {
6975   std::string Values;
6976   unsigned Bound = Last >= 2 ? Last - 2 : 0;
6977   unsigned Skipped = Exclude.size();
6978   auto S = Exclude.begin(), E = Exclude.end();
6979   for (unsigned i = First; i < Last; ++i) {
6980     if (std::find(S, E, i) != E) {
6981       --Skipped;
6982       continue;
6983     }
6984     Values += "'";
6985     Values += getOpenMPSimpleClauseTypeName(K, i);
6986     Values += "'";
6987     if (i == Bound - Skipped)
6988       Values += " or ";
6989     else if (i != Bound + 1 - Skipped)
6990       Values += ", ";
6991   }
6992   return Values;
6993 }
6994 
6995 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
6996                                           SourceLocation KindKwLoc,
6997                                           SourceLocation StartLoc,
6998                                           SourceLocation LParenLoc,
6999                                           SourceLocation EndLoc) {
7000   if (Kind == OMPC_DEFAULT_unknown) {
7001     static_assert(OMPC_DEFAULT_unknown > 0,
7002                   "OMPC_DEFAULT_unknown not greater than 0");
7003     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7004         << getListOfPossibleValues(OMPC_default, /*First=*/0,
7005                                    /*Last=*/OMPC_DEFAULT_unknown)
7006         << getOpenMPClauseName(OMPC_default);
7007     return nullptr;
7008   }
7009   switch (Kind) {
7010   case OMPC_DEFAULT_none:
7011     DSAStack->setDefaultDSANone(KindKwLoc);
7012     break;
7013   case OMPC_DEFAULT_shared:
7014     DSAStack->setDefaultDSAShared(KindKwLoc);
7015     break;
7016   case OMPC_DEFAULT_unknown:
7017     llvm_unreachable("Clause kind is not allowed.");
7018     break;
7019   }
7020   return new (Context)
7021       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7022 }
7023 
7024 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7025                                            SourceLocation KindKwLoc,
7026                                            SourceLocation StartLoc,
7027                                            SourceLocation LParenLoc,
7028                                            SourceLocation EndLoc) {
7029   if (Kind == OMPC_PROC_BIND_unknown) {
7030     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7031         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7032                                    /*Last=*/OMPC_PROC_BIND_unknown)
7033         << getOpenMPClauseName(OMPC_proc_bind);
7034     return nullptr;
7035   }
7036   return new (Context)
7037       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7038 }
7039 
7040 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
7041     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
7042     SourceLocation StartLoc, SourceLocation LParenLoc,
7043     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
7044     SourceLocation EndLoc) {
7045   OMPClause *Res = nullptr;
7046   switch (Kind) {
7047   case OMPC_schedule:
7048     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7049     assert(Argument.size() == NumberOfElements &&
7050            ArgumentLoc.size() == NumberOfElements);
7051     Res = ActOnOpenMPScheduleClause(
7052         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7053         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7054         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7055         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7056         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
7057     break;
7058   case OMPC_if:
7059     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7060     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7061                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7062                               DelimLoc, EndLoc);
7063     break;
7064   case OMPC_dist_schedule:
7065     Res = ActOnOpenMPDistScheduleClause(
7066         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7067         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7068     break;
7069   case OMPC_defaultmap:
7070     enum { Modifier, DefaultmapKind };
7071     Res = ActOnOpenMPDefaultmapClause(
7072         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7073         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7074         StartLoc, LParenLoc, ArgumentLoc[Modifier],
7075         ArgumentLoc[DefaultmapKind], EndLoc);
7076     break;
7077   case OMPC_final:
7078   case OMPC_num_threads:
7079   case OMPC_safelen:
7080   case OMPC_simdlen:
7081   case OMPC_collapse:
7082   case OMPC_default:
7083   case OMPC_proc_bind:
7084   case OMPC_private:
7085   case OMPC_firstprivate:
7086   case OMPC_lastprivate:
7087   case OMPC_shared:
7088   case OMPC_reduction:
7089   case OMPC_linear:
7090   case OMPC_aligned:
7091   case OMPC_copyin:
7092   case OMPC_copyprivate:
7093   case OMPC_ordered:
7094   case OMPC_nowait:
7095   case OMPC_untied:
7096   case OMPC_mergeable:
7097   case OMPC_threadprivate:
7098   case OMPC_flush:
7099   case OMPC_read:
7100   case OMPC_write:
7101   case OMPC_update:
7102   case OMPC_capture:
7103   case OMPC_seq_cst:
7104   case OMPC_depend:
7105   case OMPC_device:
7106   case OMPC_threads:
7107   case OMPC_simd:
7108   case OMPC_map:
7109   case OMPC_num_teams:
7110   case OMPC_thread_limit:
7111   case OMPC_priority:
7112   case OMPC_grainsize:
7113   case OMPC_nogroup:
7114   case OMPC_num_tasks:
7115   case OMPC_hint:
7116   case OMPC_unknown:
7117   case OMPC_uniform:
7118     llvm_unreachable("Clause is not allowed.");
7119   }
7120   return Res;
7121 }
7122 
7123 static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7124                                    OpenMPScheduleClauseModifier M2,
7125                                    SourceLocation M1Loc, SourceLocation M2Loc) {
7126   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7127     SmallVector<unsigned, 2> Excluded;
7128     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7129       Excluded.push_back(M2);
7130     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7131       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7132     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7133       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7134     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7135         << getListOfPossibleValues(OMPC_schedule,
7136                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7137                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7138                                    Excluded)
7139         << getOpenMPClauseName(OMPC_schedule);
7140     return true;
7141   }
7142   return false;
7143 }
7144 
7145 OMPClause *Sema::ActOnOpenMPScheduleClause(
7146     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
7147     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
7148     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7149     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7150   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7151       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7152     return nullptr;
7153   // OpenMP, 2.7.1, Loop Construct, Restrictions
7154   // Either the monotonic modifier or the nonmonotonic modifier can be specified
7155   // but not both.
7156   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7157       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7158        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7159       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7160        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7161     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7162         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7163         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7164     return nullptr;
7165   }
7166   if (Kind == OMPC_SCHEDULE_unknown) {
7167     std::string Values;
7168     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7169       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7170       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7171                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7172                                        Exclude);
7173     } else {
7174       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7175                                        /*Last=*/OMPC_SCHEDULE_unknown);
7176     }
7177     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7178         << Values << getOpenMPClauseName(OMPC_schedule);
7179     return nullptr;
7180   }
7181   // OpenMP, 2.7.1, Loop Construct, Restrictions
7182   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7183   // schedule(guided).
7184   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7185        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7186       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7187     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7188          diag::err_omp_schedule_nonmonotonic_static);
7189     return nullptr;
7190   }
7191   Expr *ValExpr = ChunkSize;
7192   Stmt *HelperValStmt = nullptr;
7193   if (ChunkSize) {
7194     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7195         !ChunkSize->isInstantiationDependent() &&
7196         !ChunkSize->containsUnexpandedParameterPack()) {
7197       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7198       ExprResult Val =
7199           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7200       if (Val.isInvalid())
7201         return nullptr;
7202 
7203       ValExpr = Val.get();
7204 
7205       // OpenMP [2.7.1, Restrictions]
7206       //  chunk_size must be a loop invariant integer expression with a positive
7207       //  value.
7208       llvm::APSInt Result;
7209       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7210         if (Result.isSigned() && !Result.isStrictlyPositive()) {
7211           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
7212               << "schedule" << 1 << ChunkSize->getSourceRange();
7213           return nullptr;
7214         }
7215       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
7216         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7217         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7218         HelperValStmt = buildPreInits(Context, Captures);
7219       }
7220     }
7221   }
7222 
7223   return new (Context)
7224       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
7225                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
7226 }
7227 
7228 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7229                                    SourceLocation StartLoc,
7230                                    SourceLocation EndLoc) {
7231   OMPClause *Res = nullptr;
7232   switch (Kind) {
7233   case OMPC_ordered:
7234     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7235     break;
7236   case OMPC_nowait:
7237     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7238     break;
7239   case OMPC_untied:
7240     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7241     break;
7242   case OMPC_mergeable:
7243     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7244     break;
7245   case OMPC_read:
7246     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7247     break;
7248   case OMPC_write:
7249     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7250     break;
7251   case OMPC_update:
7252     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7253     break;
7254   case OMPC_capture:
7255     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7256     break;
7257   case OMPC_seq_cst:
7258     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7259     break;
7260   case OMPC_threads:
7261     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7262     break;
7263   case OMPC_simd:
7264     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7265     break;
7266   case OMPC_nogroup:
7267     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7268     break;
7269   case OMPC_if:
7270   case OMPC_final:
7271   case OMPC_num_threads:
7272   case OMPC_safelen:
7273   case OMPC_simdlen:
7274   case OMPC_collapse:
7275   case OMPC_schedule:
7276   case OMPC_private:
7277   case OMPC_firstprivate:
7278   case OMPC_lastprivate:
7279   case OMPC_shared:
7280   case OMPC_reduction:
7281   case OMPC_linear:
7282   case OMPC_aligned:
7283   case OMPC_copyin:
7284   case OMPC_copyprivate:
7285   case OMPC_default:
7286   case OMPC_proc_bind:
7287   case OMPC_threadprivate:
7288   case OMPC_flush:
7289   case OMPC_depend:
7290   case OMPC_device:
7291   case OMPC_map:
7292   case OMPC_num_teams:
7293   case OMPC_thread_limit:
7294   case OMPC_priority:
7295   case OMPC_grainsize:
7296   case OMPC_num_tasks:
7297   case OMPC_hint:
7298   case OMPC_dist_schedule:
7299   case OMPC_defaultmap:
7300   case OMPC_unknown:
7301   case OMPC_uniform:
7302     llvm_unreachable("Clause is not allowed.");
7303   }
7304   return Res;
7305 }
7306 
7307 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7308                                          SourceLocation EndLoc) {
7309   DSAStack->setNowaitRegion();
7310   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7311 }
7312 
7313 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7314                                          SourceLocation EndLoc) {
7315   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7316 }
7317 
7318 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7319                                             SourceLocation EndLoc) {
7320   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7321 }
7322 
7323 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7324                                        SourceLocation EndLoc) {
7325   return new (Context) OMPReadClause(StartLoc, EndLoc);
7326 }
7327 
7328 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7329                                         SourceLocation EndLoc) {
7330   return new (Context) OMPWriteClause(StartLoc, EndLoc);
7331 }
7332 
7333 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7334                                          SourceLocation EndLoc) {
7335   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7336 }
7337 
7338 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7339                                           SourceLocation EndLoc) {
7340   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7341 }
7342 
7343 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
7344                                          SourceLocation EndLoc) {
7345   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
7346 }
7347 
7348 OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
7349                                           SourceLocation EndLoc) {
7350   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
7351 }
7352 
7353 OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
7354                                        SourceLocation EndLoc) {
7355   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
7356 }
7357 
7358 OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
7359                                           SourceLocation EndLoc) {
7360   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
7361 }
7362 
7363 OMPClause *Sema::ActOnOpenMPVarListClause(
7364     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
7365     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
7366     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
7367     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
7368     OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
7369     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
7370     SourceLocation DepLinMapLoc) {
7371   OMPClause *Res = nullptr;
7372   switch (Kind) {
7373   case OMPC_private:
7374     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7375     break;
7376   case OMPC_firstprivate:
7377     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7378     break;
7379   case OMPC_lastprivate:
7380     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7381     break;
7382   case OMPC_shared:
7383     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
7384     break;
7385   case OMPC_reduction:
7386     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
7387                                      EndLoc, ReductionIdScopeSpec, ReductionId);
7388     break;
7389   case OMPC_linear:
7390     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
7391                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
7392     break;
7393   case OMPC_aligned:
7394     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
7395                                    ColonLoc, EndLoc);
7396     break;
7397   case OMPC_copyin:
7398     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
7399     break;
7400   case OMPC_copyprivate:
7401     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
7402     break;
7403   case OMPC_flush:
7404     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
7405     break;
7406   case OMPC_depend:
7407     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
7408                                   StartLoc, LParenLoc, EndLoc);
7409     break;
7410   case OMPC_map:
7411     Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
7412                                DepLinMapLoc, ColonLoc, VarList, StartLoc,
7413                                LParenLoc, EndLoc);
7414     break;
7415   case OMPC_if:
7416   case OMPC_final:
7417   case OMPC_num_threads:
7418   case OMPC_safelen:
7419   case OMPC_simdlen:
7420   case OMPC_collapse:
7421   case OMPC_default:
7422   case OMPC_proc_bind:
7423   case OMPC_schedule:
7424   case OMPC_ordered:
7425   case OMPC_nowait:
7426   case OMPC_untied:
7427   case OMPC_mergeable:
7428   case OMPC_threadprivate:
7429   case OMPC_read:
7430   case OMPC_write:
7431   case OMPC_update:
7432   case OMPC_capture:
7433   case OMPC_seq_cst:
7434   case OMPC_device:
7435   case OMPC_threads:
7436   case OMPC_simd:
7437   case OMPC_num_teams:
7438   case OMPC_thread_limit:
7439   case OMPC_priority:
7440   case OMPC_grainsize:
7441   case OMPC_nogroup:
7442   case OMPC_num_tasks:
7443   case OMPC_hint:
7444   case OMPC_dist_schedule:
7445   case OMPC_defaultmap:
7446   case OMPC_unknown:
7447   case OMPC_uniform:
7448     llvm_unreachable("Clause is not allowed.");
7449   }
7450   return Res;
7451 }
7452 
7453 ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
7454                                        ExprObjectKind OK, SourceLocation Loc) {
7455   ExprResult Res = BuildDeclRefExpr(
7456       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
7457   if (!Res.isUsable())
7458     return ExprError();
7459   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
7460     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
7461     if (!Res.isUsable())
7462       return ExprError();
7463   }
7464   if (VK != VK_LValue && Res.get()->isGLValue()) {
7465     Res = DefaultLvalueConversion(Res.get());
7466     if (!Res.isUsable())
7467       return ExprError();
7468   }
7469   return Res;
7470 }
7471 
7472 static std::pair<ValueDecl *, bool>
7473 getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
7474                SourceRange &ERange, bool AllowArraySection = false) {
7475   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7476       RefExpr->containsUnexpandedParameterPack())
7477     return std::make_pair(nullptr, true);
7478 
7479   // OpenMP [3.1, C/C++]
7480   //  A list item is a variable name.
7481   // OpenMP  [2.9.3.3, Restrictions, p.1]
7482   //  A variable that is part of another variable (as an array or
7483   //  structure element) cannot appear in a private clause.
7484   RefExpr = RefExpr->IgnoreParens();
7485   enum {
7486     NoArrayExpr = -1,
7487     ArraySubscript = 0,
7488     OMPArraySection = 1
7489   } IsArrayExpr = NoArrayExpr;
7490   if (AllowArraySection) {
7491     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
7492       auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7493       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7494         Base = TempASE->getBase()->IgnoreParenImpCasts();
7495       RefExpr = Base;
7496       IsArrayExpr = ArraySubscript;
7497     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
7498       auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7499       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7500         Base = TempOASE->getBase()->IgnoreParenImpCasts();
7501       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7502         Base = TempASE->getBase()->IgnoreParenImpCasts();
7503       RefExpr = Base;
7504       IsArrayExpr = OMPArraySection;
7505     }
7506   }
7507   ELoc = RefExpr->getExprLoc();
7508   ERange = RefExpr->getSourceRange();
7509   RefExpr = RefExpr->IgnoreParenImpCasts();
7510   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
7511   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
7512   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
7513       (S.getCurrentThisType().isNull() || !ME ||
7514        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
7515        !isa<FieldDecl>(ME->getMemberDecl()))) {
7516     if (IsArrayExpr != NoArrayExpr)
7517       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
7518                                                          << ERange;
7519     else {
7520       S.Diag(ELoc,
7521              AllowArraySection
7522                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
7523                  : diag::err_omp_expected_var_name_member_expr)
7524           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
7525     }
7526     return std::make_pair(nullptr, false);
7527   }
7528   return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
7529 }
7530 
7531 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
7532                                           SourceLocation StartLoc,
7533                                           SourceLocation LParenLoc,
7534                                           SourceLocation EndLoc) {
7535   SmallVector<Expr *, 8> Vars;
7536   SmallVector<Expr *, 8> PrivateCopies;
7537   for (auto &RefExpr : VarList) {
7538     assert(RefExpr && "NULL expr in OpenMP private clause.");
7539     SourceLocation ELoc;
7540     SourceRange ERange;
7541     Expr *SimpleRefExpr = RefExpr;
7542     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
7543     if (Res.second) {
7544       // It will be analyzed later.
7545       Vars.push_back(RefExpr);
7546       PrivateCopies.push_back(nullptr);
7547     }
7548     ValueDecl *D = Res.first;
7549     if (!D)
7550       continue;
7551 
7552     QualType Type = D->getType();
7553     auto *VD = dyn_cast<VarDecl>(D);
7554 
7555     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7556     //  A variable that appears in a private clause must not have an incomplete
7557     //  type or a reference type.
7558     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
7559       continue;
7560     Type = Type.getNonReferenceType();
7561 
7562     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7563     // in a Construct]
7564     //  Variables with the predetermined data-sharing attributes may not be
7565     //  listed in data-sharing attributes clauses, except for the cases
7566     //  listed below. For these exceptions only, listing a predetermined
7567     //  variable in a data-sharing attribute clause is allowed and overrides
7568     //  the variable's predetermined data-sharing attributes.
7569     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7570     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
7571       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7572                                           << getOpenMPClauseName(OMPC_private);
7573       ReportOriginalDSA(*this, DSAStack, D, DVar);
7574       continue;
7575     }
7576 
7577     // Variably modified types are not supported for tasks.
7578     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
7579         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
7580       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7581           << getOpenMPClauseName(OMPC_private) << Type
7582           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7583       bool IsDecl =
7584           !VD ||
7585           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7586       Diag(D->getLocation(),
7587            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7588           << D;
7589       continue;
7590     }
7591 
7592     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7593     // A list item cannot appear in both a map clause and a data-sharing
7594     // attribute clause on the same construct
7595     if (DSAStack->getCurrentDirective() == OMPD_target) {
7596       if (DSAStack->checkMappableExprComponentListsForDecl(
7597               VD, /* CurrentRegionOnly = */ true,
7598               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7599                   -> bool { return true; })) {
7600         Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7601             << getOpenMPClauseName(OMPC_private)
7602             << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7603         ReportOriginalDSA(*this, DSAStack, D, DVar);
7604         continue;
7605       }
7606     }
7607 
7608     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
7609     //  A variable of class type (or array thereof) that appears in a private
7610     //  clause requires an accessible, unambiguous default constructor for the
7611     //  class type.
7612     // Generate helper private variable and initialize it with the default
7613     // value. The address of the original variable is replaced by the address of
7614     // the new private variable in CodeGen. This new variable is not added to
7615     // IdResolver, so the code in the OpenMP region uses original variable for
7616     // proper diagnostics.
7617     Type = Type.getUnqualifiedType();
7618     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7619                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
7620     ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
7621     if (VDPrivate->isInvalidDecl())
7622       continue;
7623     auto VDPrivateRefExpr = buildDeclRefExpr(
7624         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
7625 
7626     DeclRefExpr *Ref = nullptr;
7627     if (!VD)
7628       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
7629     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
7630     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
7631     PrivateCopies.push_back(VDPrivateRefExpr);
7632   }
7633 
7634   if (Vars.empty())
7635     return nullptr;
7636 
7637   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7638                                   PrivateCopies);
7639 }
7640 
7641 namespace {
7642 class DiagsUninitializedSeveretyRAII {
7643 private:
7644   DiagnosticsEngine &Diags;
7645   SourceLocation SavedLoc;
7646   bool IsIgnored;
7647 
7648 public:
7649   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
7650                                  bool IsIgnored)
7651       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
7652     if (!IsIgnored) {
7653       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
7654                         /*Map*/ diag::Severity::Ignored, Loc);
7655     }
7656   }
7657   ~DiagsUninitializedSeveretyRAII() {
7658     if (!IsIgnored)
7659       Diags.popMappings(SavedLoc);
7660   }
7661 };
7662 }
7663 
7664 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
7665                                                SourceLocation StartLoc,
7666                                                SourceLocation LParenLoc,
7667                                                SourceLocation EndLoc) {
7668   SmallVector<Expr *, 8> Vars;
7669   SmallVector<Expr *, 8> PrivateCopies;
7670   SmallVector<Expr *, 8> Inits;
7671   SmallVector<Decl *, 4> ExprCaptures;
7672   bool IsImplicitClause =
7673       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
7674   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
7675 
7676   for (auto &RefExpr : VarList) {
7677     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
7678     SourceLocation ELoc;
7679     SourceRange ERange;
7680     Expr *SimpleRefExpr = RefExpr;
7681     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
7682     if (Res.second) {
7683       // It will be analyzed later.
7684       Vars.push_back(RefExpr);
7685       PrivateCopies.push_back(nullptr);
7686       Inits.push_back(nullptr);
7687     }
7688     ValueDecl *D = Res.first;
7689     if (!D)
7690       continue;
7691 
7692     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
7693     QualType Type = D->getType();
7694     auto *VD = dyn_cast<VarDecl>(D);
7695 
7696     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7697     //  A variable that appears in a private clause must not have an incomplete
7698     //  type or a reference type.
7699     if (RequireCompleteType(ELoc, Type,
7700                             diag::err_omp_firstprivate_incomplete_type))
7701       continue;
7702     Type = Type.getNonReferenceType();
7703 
7704     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
7705     //  A variable of class type (or array thereof) that appears in a private
7706     //  clause requires an accessible, unambiguous copy constructor for the
7707     //  class type.
7708     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
7709 
7710     // If an implicit firstprivate variable found it was checked already.
7711     DSAStackTy::DSAVarData TopDVar;
7712     if (!IsImplicitClause) {
7713       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7714       TopDVar = DVar;
7715       bool IsConstant = ElemType.isConstant(Context);
7716       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
7717       //  A list item that specifies a given variable may not appear in more
7718       // than one clause on the same directive, except that a variable may be
7719       //  specified in both firstprivate and lastprivate clauses.
7720       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
7721           DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
7722         Diag(ELoc, diag::err_omp_wrong_dsa)
7723             << getOpenMPClauseName(DVar.CKind)
7724             << getOpenMPClauseName(OMPC_firstprivate);
7725         ReportOriginalDSA(*this, DSAStack, D, DVar);
7726         continue;
7727       }
7728 
7729       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7730       // in a Construct]
7731       //  Variables with the predetermined data-sharing attributes may not be
7732       //  listed in data-sharing attributes clauses, except for the cases
7733       //  listed below. For these exceptions only, listing a predetermined
7734       //  variable in a data-sharing attribute clause is allowed and overrides
7735       //  the variable's predetermined data-sharing attributes.
7736       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
7737       // in a Construct, C/C++, p.2]
7738       //  Variables with const-qualified type having no mutable member may be
7739       //  listed in a firstprivate clause, even if they are static data members.
7740       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
7741           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
7742         Diag(ELoc, diag::err_omp_wrong_dsa)
7743             << getOpenMPClauseName(DVar.CKind)
7744             << getOpenMPClauseName(OMPC_firstprivate);
7745         ReportOriginalDSA(*this, DSAStack, D, DVar);
7746         continue;
7747       }
7748 
7749       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7750       // OpenMP [2.9.3.4, Restrictions, p.2]
7751       //  A list item that is private within a parallel region must not appear
7752       //  in a firstprivate clause on a worksharing construct if any of the
7753       //  worksharing regions arising from the worksharing construct ever bind
7754       //  to any of the parallel regions arising from the parallel construct.
7755       if (isOpenMPWorksharingDirective(CurrDir) &&
7756           !isOpenMPParallelDirective(CurrDir)) {
7757         DVar = DSAStack->getImplicitDSA(D, true);
7758         if (DVar.CKind != OMPC_shared &&
7759             (isOpenMPParallelDirective(DVar.DKind) ||
7760              DVar.DKind == OMPD_unknown)) {
7761           Diag(ELoc, diag::err_omp_required_access)
7762               << getOpenMPClauseName(OMPC_firstprivate)
7763               << getOpenMPClauseName(OMPC_shared);
7764           ReportOriginalDSA(*this, DSAStack, D, DVar);
7765           continue;
7766         }
7767       }
7768       // OpenMP [2.9.3.4, Restrictions, p.3]
7769       //  A list item that appears in a reduction clause of a parallel construct
7770       //  must not appear in a firstprivate clause on a worksharing or task
7771       //  construct if any of the worksharing or task regions arising from the
7772       //  worksharing or task construct ever bind to any of the parallel regions
7773       //  arising from the parallel construct.
7774       // OpenMP [2.9.3.4, Restrictions, p.4]
7775       //  A list item that appears in a reduction clause in worksharing
7776       //  construct must not appear in a firstprivate clause in a task construct
7777       //  encountered during execution of any of the worksharing regions arising
7778       //  from the worksharing construct.
7779       if (isOpenMPTaskingDirective(CurrDir)) {
7780         DVar =
7781             DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
7782                                       [](OpenMPDirectiveKind K) -> bool {
7783                                         return isOpenMPParallelDirective(K) ||
7784                                                isOpenMPWorksharingDirective(K);
7785                                       },
7786                                       false);
7787         if (DVar.CKind == OMPC_reduction &&
7788             (isOpenMPParallelDirective(DVar.DKind) ||
7789              isOpenMPWorksharingDirective(DVar.DKind))) {
7790           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
7791               << getOpenMPDirectiveName(DVar.DKind);
7792           ReportOriginalDSA(*this, DSAStack, D, DVar);
7793           continue;
7794         }
7795       }
7796 
7797       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7798       // A list item that is private within a teams region must not appear in a
7799       // firstprivate clause on a distribute construct if any of the distribute
7800       // regions arising from the distribute construct ever bind to any of the
7801       // teams regions arising from the teams construct.
7802       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
7803       // A list item that appears in a reduction clause of a teams construct
7804       // must not appear in a firstprivate clause on a distribute construct if
7805       // any of the distribute regions arising from the distribute construct
7806       // ever bind to any of the teams regions arising from the teams construct.
7807       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
7808       // A list item may appear in a firstprivate or lastprivate clause but not
7809       // both.
7810       if (CurrDir == OMPD_distribute) {
7811         DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_private),
7812                                          [](OpenMPDirectiveKind K) -> bool {
7813                                            return isOpenMPTeamsDirective(K);
7814                                          },
7815                                          false);
7816         if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
7817           Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
7818           ReportOriginalDSA(*this, DSAStack, D, DVar);
7819           continue;
7820         }
7821         DVar = DSAStack->hasInnermostDSA(D, MatchesAnyClause(OMPC_reduction),
7822                                          [](OpenMPDirectiveKind K) -> bool {
7823                                            return isOpenMPTeamsDirective(K);
7824                                          },
7825                                          false);
7826         if (DVar.CKind == OMPC_reduction &&
7827             isOpenMPTeamsDirective(DVar.DKind)) {
7828           Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
7829           ReportOriginalDSA(*this, DSAStack, D, DVar);
7830           continue;
7831         }
7832         DVar = DSAStack->getTopDSA(D, false);
7833         if (DVar.CKind == OMPC_lastprivate) {
7834           Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
7835           ReportOriginalDSA(*this, DSAStack, D, DVar);
7836           continue;
7837         }
7838       }
7839       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
7840       // A list item cannot appear in both a map clause and a data-sharing
7841       // attribute clause on the same construct
7842       if (CurrDir == OMPD_target) {
7843         if (DSAStack->checkMappableExprComponentListsForDecl(
7844                 VD, /* CurrentRegionOnly = */ true,
7845                 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
7846                     -> bool { return true; })) {
7847           Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
7848               << getOpenMPClauseName(OMPC_firstprivate)
7849               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7850           ReportOriginalDSA(*this, DSAStack, D, DVar);
7851           continue;
7852         }
7853       }
7854     }
7855 
7856     // Variably modified types are not supported for tasks.
7857     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
7858         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
7859       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
7860           << getOpenMPClauseName(OMPC_firstprivate) << Type
7861           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
7862       bool IsDecl =
7863           !VD ||
7864           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7865       Diag(D->getLocation(),
7866            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7867           << D;
7868       continue;
7869     }
7870 
7871     Type = Type.getUnqualifiedType();
7872     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
7873                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
7874     // Generate helper private variable and initialize it with the value of the
7875     // original variable. The address of the original variable is replaced by
7876     // the address of the new private variable in the CodeGen. This new variable
7877     // is not added to IdResolver, so the code in the OpenMP region uses
7878     // original variable for proper diagnostics and variable capturing.
7879     Expr *VDInitRefExpr = nullptr;
7880     // For arrays generate initializer for single element and replace it by the
7881     // original array element in CodeGen.
7882     if (Type->isArrayType()) {
7883       auto VDInit =
7884           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
7885       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
7886       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
7887       ElemType = ElemType.getUnqualifiedType();
7888       auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
7889                                       ".firstprivate.temp");
7890       InitializedEntity Entity =
7891           InitializedEntity::InitializeVariable(VDInitTemp);
7892       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
7893 
7894       InitializationSequence InitSeq(*this, Entity, Kind, Init);
7895       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
7896       if (Result.isInvalid())
7897         VDPrivate->setInvalidDecl();
7898       else
7899         VDPrivate->setInit(Result.getAs<Expr>());
7900       // Remove temp variable declaration.
7901       Context.Deallocate(VDInitTemp);
7902     } else {
7903       auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
7904                                   ".firstprivate.temp");
7905       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
7906                                        RefExpr->getExprLoc());
7907       AddInitializerToDecl(VDPrivate,
7908                            DefaultLvalueConversion(VDInitRefExpr).get(),
7909                            /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
7910     }
7911     if (VDPrivate->isInvalidDecl()) {
7912       if (IsImplicitClause) {
7913         Diag(RefExpr->getExprLoc(),
7914              diag::note_omp_task_predetermined_firstprivate_here);
7915       }
7916       continue;
7917     }
7918     CurContext->addDecl(VDPrivate);
7919     auto VDPrivateRefExpr = buildDeclRefExpr(
7920         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
7921         RefExpr->getExprLoc());
7922     DeclRefExpr *Ref = nullptr;
7923     if (!VD) {
7924       if (TopDVar.CKind == OMPC_lastprivate)
7925         Ref = TopDVar.PrivateCopy;
7926       else {
7927         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
7928         if (!IsOpenMPCapturedDecl(D))
7929           ExprCaptures.push_back(Ref->getDecl());
7930       }
7931     }
7932     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
7933     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
7934     PrivateCopies.push_back(VDPrivateRefExpr);
7935     Inits.push_back(VDInitRefExpr);
7936   }
7937 
7938   if (Vars.empty())
7939     return nullptr;
7940 
7941   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7942                                        Vars, PrivateCopies, Inits,
7943                                        buildPreInits(Context, ExprCaptures));
7944 }
7945 
7946 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
7947                                               SourceLocation StartLoc,
7948                                               SourceLocation LParenLoc,
7949                                               SourceLocation EndLoc) {
7950   SmallVector<Expr *, 8> Vars;
7951   SmallVector<Expr *, 8> SrcExprs;
7952   SmallVector<Expr *, 8> DstExprs;
7953   SmallVector<Expr *, 8> AssignmentOps;
7954   SmallVector<Decl *, 4> ExprCaptures;
7955   SmallVector<Expr *, 4> ExprPostUpdates;
7956   for (auto &RefExpr : VarList) {
7957     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
7958     SourceLocation ELoc;
7959     SourceRange ERange;
7960     Expr *SimpleRefExpr = RefExpr;
7961     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
7962     if (Res.second) {
7963       // It will be analyzed later.
7964       Vars.push_back(RefExpr);
7965       SrcExprs.push_back(nullptr);
7966       DstExprs.push_back(nullptr);
7967       AssignmentOps.push_back(nullptr);
7968     }
7969     ValueDecl *D = Res.first;
7970     if (!D)
7971       continue;
7972 
7973     QualType Type = D->getType();
7974     auto *VD = dyn_cast<VarDecl>(D);
7975 
7976     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
7977     //  A variable that appears in a lastprivate clause must not have an
7978     //  incomplete type or a reference type.
7979     if (RequireCompleteType(ELoc, Type,
7980                             diag::err_omp_lastprivate_incomplete_type))
7981       continue;
7982     Type = Type.getNonReferenceType();
7983 
7984     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7985     // in a Construct]
7986     //  Variables with the predetermined data-sharing attributes may not be
7987     //  listed in data-sharing attributes clauses, except for the cases
7988     //  listed below.
7989     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
7990     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
7991         DVar.CKind != OMPC_firstprivate &&
7992         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
7993       Diag(ELoc, diag::err_omp_wrong_dsa)
7994           << getOpenMPClauseName(DVar.CKind)
7995           << getOpenMPClauseName(OMPC_lastprivate);
7996       ReportOriginalDSA(*this, DSAStack, D, DVar);
7997       continue;
7998     }
7999 
8000     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8001     // OpenMP [2.14.3.5, Restrictions, p.2]
8002     // A list item that is private within a parallel region, or that appears in
8003     // the reduction clause of a parallel construct, must not appear in a
8004     // lastprivate clause on a worksharing construct if any of the corresponding
8005     // worksharing regions ever binds to any of the corresponding parallel
8006     // regions.
8007     DSAStackTy::DSAVarData TopDVar = DVar;
8008     if (isOpenMPWorksharingDirective(CurrDir) &&
8009         !isOpenMPParallelDirective(CurrDir)) {
8010       DVar = DSAStack->getImplicitDSA(D, true);
8011       if (DVar.CKind != OMPC_shared) {
8012         Diag(ELoc, diag::err_omp_required_access)
8013             << getOpenMPClauseName(OMPC_lastprivate)
8014             << getOpenMPClauseName(OMPC_shared);
8015         ReportOriginalDSA(*this, DSAStack, D, DVar);
8016         continue;
8017       }
8018     }
8019 
8020     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8021     // A list item may appear in a firstprivate or lastprivate clause but not
8022     // both.
8023     if (CurrDir == OMPD_distribute) {
8024       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8025       if (DVar.CKind == OMPC_firstprivate) {
8026         Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8027         ReportOriginalDSA(*this, DSAStack, D, DVar);
8028         continue;
8029       }
8030     }
8031 
8032     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
8033     //  A variable of class type (or array thereof) that appears in a
8034     //  lastprivate clause requires an accessible, unambiguous default
8035     //  constructor for the class type, unless the list item is also specified
8036     //  in a firstprivate clause.
8037     //  A variable of class type (or array thereof) that appears in a
8038     //  lastprivate clause requires an accessible, unambiguous copy assignment
8039     //  operator for the class type.
8040     Type = Context.getBaseElementType(Type).getNonReferenceType();
8041     auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
8042                                Type.getUnqualifiedType(), ".lastprivate.src",
8043                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8044     auto *PseudoSrcExpr =
8045         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
8046     auto *DstVD =
8047         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
8048                      D->hasAttrs() ? &D->getAttrs() : nullptr);
8049     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
8050     // For arrays generate assignment operation for single element and replace
8051     // it by the original array element in CodeGen.
8052     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
8053                                    PseudoDstExpr, PseudoSrcExpr);
8054     if (AssignmentOp.isInvalid())
8055       continue;
8056     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
8057                                        /*DiscardedValue=*/true);
8058     if (AssignmentOp.isInvalid())
8059       continue;
8060 
8061     DeclRefExpr *Ref = nullptr;
8062     if (!VD) {
8063       if (TopDVar.CKind == OMPC_firstprivate)
8064         Ref = TopDVar.PrivateCopy;
8065       else {
8066         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8067         if (!IsOpenMPCapturedDecl(D))
8068           ExprCaptures.push_back(Ref->getDecl());
8069       }
8070       if (TopDVar.CKind == OMPC_firstprivate ||
8071           (!IsOpenMPCapturedDecl(D) &&
8072            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
8073         ExprResult RefRes = DefaultLvalueConversion(Ref);
8074         if (!RefRes.isUsable())
8075           continue;
8076         ExprResult PostUpdateRes =
8077             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8078                        RefRes.get());
8079         if (!PostUpdateRes.isUsable())
8080           continue;
8081         ExprPostUpdates.push_back(
8082             IgnoredValueConversions(PostUpdateRes.get()).get());
8083       }
8084     }
8085     if (TopDVar.CKind != OMPC_firstprivate)
8086       DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8087     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
8088     SrcExprs.push_back(PseudoSrcExpr);
8089     DstExprs.push_back(PseudoDstExpr);
8090     AssignmentOps.push_back(AssignmentOp.get());
8091   }
8092 
8093   if (Vars.empty())
8094     return nullptr;
8095 
8096   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8097                                       Vars, SrcExprs, DstExprs, AssignmentOps,
8098                                       buildPreInits(Context, ExprCaptures),
8099                                       buildPostUpdate(*this, ExprPostUpdates));
8100 }
8101 
8102 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8103                                          SourceLocation StartLoc,
8104                                          SourceLocation LParenLoc,
8105                                          SourceLocation EndLoc) {
8106   SmallVector<Expr *, 8> Vars;
8107   for (auto &RefExpr : VarList) {
8108     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
8109     SourceLocation ELoc;
8110     SourceRange ERange;
8111     Expr *SimpleRefExpr = RefExpr;
8112     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8113     if (Res.second) {
8114       // It will be analyzed later.
8115       Vars.push_back(RefExpr);
8116     }
8117     ValueDecl *D = Res.first;
8118     if (!D)
8119       continue;
8120 
8121     auto *VD = dyn_cast<VarDecl>(D);
8122     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8123     // in a Construct]
8124     //  Variables with the predetermined data-sharing attributes may not be
8125     //  listed in data-sharing attributes clauses, except for the cases
8126     //  listed below. For these exceptions only, listing a predetermined
8127     //  variable in a data-sharing attribute clause is allowed and overrides
8128     //  the variable's predetermined data-sharing attributes.
8129     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8130     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8131         DVar.RefExpr) {
8132       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8133                                           << getOpenMPClauseName(OMPC_shared);
8134       ReportOriginalDSA(*this, DSAStack, D, DVar);
8135       continue;
8136     }
8137 
8138     DeclRefExpr *Ref = nullptr;
8139     if (!VD && IsOpenMPCapturedDecl(D))
8140       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8141     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
8142     Vars.push_back((VD || !Ref) ? RefExpr->IgnoreParens() : Ref);
8143   }
8144 
8145   if (Vars.empty())
8146     return nullptr;
8147 
8148   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8149 }
8150 
8151 namespace {
8152 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8153   DSAStackTy *Stack;
8154 
8155 public:
8156   bool VisitDeclRefExpr(DeclRefExpr *E) {
8157     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
8158       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
8159       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8160         return false;
8161       if (DVar.CKind != OMPC_unknown)
8162         return true;
8163       DSAStackTy::DSAVarData DVarPrivate =
8164           Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
8165       if (DVarPrivate.CKind != OMPC_unknown)
8166         return true;
8167       return false;
8168     }
8169     return false;
8170   }
8171   bool VisitStmt(Stmt *S) {
8172     for (auto Child : S->children()) {
8173       if (Child && Visit(Child))
8174         return true;
8175     }
8176     return false;
8177   }
8178   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
8179 };
8180 } // namespace
8181 
8182 namespace {
8183 // Transform MemberExpression for specified FieldDecl of current class to
8184 // DeclRefExpr to specified OMPCapturedExprDecl.
8185 class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8186   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8187   ValueDecl *Field;
8188   DeclRefExpr *CapturedExpr;
8189 
8190 public:
8191   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8192       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8193 
8194   ExprResult TransformMemberExpr(MemberExpr *E) {
8195     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8196         E->getMemberDecl() == Field) {
8197       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
8198       return CapturedExpr;
8199     }
8200     return BaseTransform::TransformMemberExpr(E);
8201   }
8202   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8203 };
8204 } // namespace
8205 
8206 template <typename T>
8207 static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8208                             const llvm::function_ref<T(ValueDecl *)> &Gen) {
8209   for (auto &Set : Lookups) {
8210     for (auto *D : Set) {
8211       if (auto Res = Gen(cast<ValueDecl>(D)))
8212         return Res;
8213     }
8214   }
8215   return T();
8216 }
8217 
8218 static ExprResult
8219 buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8220                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8221                          const DeclarationNameInfo &ReductionId, QualType Ty,
8222                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8223   if (ReductionIdScopeSpec.isInvalid())
8224     return ExprError();
8225   SmallVector<UnresolvedSet<8>, 4> Lookups;
8226   if (S) {
8227     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8228     Lookup.suppressDiagnostics();
8229     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8230       auto *D = Lookup.getRepresentativeDecl();
8231       do {
8232         S = S->getParent();
8233       } while (S && !S->isDeclScope(D));
8234       if (S)
8235         S = S->getParent();
8236       Lookups.push_back(UnresolvedSet<8>());
8237       Lookups.back().append(Lookup.begin(), Lookup.end());
8238       Lookup.clear();
8239     }
8240   } else if (auto *ULE =
8241                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8242     Lookups.push_back(UnresolvedSet<8>());
8243     Decl *PrevD = nullptr;
8244     for(auto *D : ULE->decls()) {
8245       if (D == PrevD)
8246         Lookups.push_back(UnresolvedSet<8>());
8247       else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8248         Lookups.back().addDecl(DRD);
8249       PrevD = D;
8250     }
8251   }
8252   if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8253       Ty->containsUnexpandedParameterPack() ||
8254       filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8255         return !D->isInvalidDecl() &&
8256                (D->getType()->isDependentType() ||
8257                 D->getType()->isInstantiationDependentType() ||
8258                 D->getType()->containsUnexpandedParameterPack());
8259       })) {
8260     UnresolvedSet<8> ResSet;
8261     for (auto &Set : Lookups) {
8262       ResSet.append(Set.begin(), Set.end());
8263       // The last item marks the end of all declarations at the specified scope.
8264       ResSet.addDecl(Set[Set.size() - 1]);
8265     }
8266     return UnresolvedLookupExpr::Create(
8267         SemaRef.Context, /*NamingClass=*/nullptr,
8268         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8269         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8270   }
8271   if (auto *VD = filterLookupForUDR<ValueDecl *>(
8272           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8273             if (!D->isInvalidDecl() &&
8274                 SemaRef.Context.hasSameType(D->getType(), Ty))
8275               return D;
8276             return nullptr;
8277           }))
8278     return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8279   if (auto *VD = filterLookupForUDR<ValueDecl *>(
8280           Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8281             if (!D->isInvalidDecl() &&
8282                 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8283                 !Ty.isMoreQualifiedThan(D->getType()))
8284               return D;
8285             return nullptr;
8286           })) {
8287     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8288                        /*DetectVirtual=*/false);
8289     if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8290       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8291               VD->getType().getUnqualifiedType()))) {
8292         if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8293                                          /*DiagID=*/0) !=
8294             Sema::AR_inaccessible) {
8295           SemaRef.BuildBasePathArray(Paths, BasePath);
8296           return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8297         }
8298       }
8299     }
8300   }
8301   if (ReductionIdScopeSpec.isSet()) {
8302     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8303     return ExprError();
8304   }
8305   return ExprEmpty();
8306 }
8307 
8308 OMPClause *Sema::ActOnOpenMPReductionClause(
8309     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8310     SourceLocation ColonLoc, SourceLocation EndLoc,
8311     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8312     ArrayRef<Expr *> UnresolvedReductions) {
8313   auto DN = ReductionId.getName();
8314   auto OOK = DN.getCXXOverloadedOperator();
8315   BinaryOperatorKind BOK = BO_Comma;
8316 
8317   // OpenMP [2.14.3.6, reduction clause]
8318   // C
8319   // reduction-identifier is either an identifier or one of the following
8320   // operators: +, -, *,  &, |, ^, && and ||
8321   // C++
8322   // reduction-identifier is either an id-expression or one of the following
8323   // operators: +, -, *, &, |, ^, && and ||
8324   // FIXME: Only 'min' and 'max' identifiers are supported for now.
8325   switch (OOK) {
8326   case OO_Plus:
8327   case OO_Minus:
8328     BOK = BO_Add;
8329     break;
8330   case OO_Star:
8331     BOK = BO_Mul;
8332     break;
8333   case OO_Amp:
8334     BOK = BO_And;
8335     break;
8336   case OO_Pipe:
8337     BOK = BO_Or;
8338     break;
8339   case OO_Caret:
8340     BOK = BO_Xor;
8341     break;
8342   case OO_AmpAmp:
8343     BOK = BO_LAnd;
8344     break;
8345   case OO_PipePipe:
8346     BOK = BO_LOr;
8347     break;
8348   case OO_New:
8349   case OO_Delete:
8350   case OO_Array_New:
8351   case OO_Array_Delete:
8352   case OO_Slash:
8353   case OO_Percent:
8354   case OO_Tilde:
8355   case OO_Exclaim:
8356   case OO_Equal:
8357   case OO_Less:
8358   case OO_Greater:
8359   case OO_LessEqual:
8360   case OO_GreaterEqual:
8361   case OO_PlusEqual:
8362   case OO_MinusEqual:
8363   case OO_StarEqual:
8364   case OO_SlashEqual:
8365   case OO_PercentEqual:
8366   case OO_CaretEqual:
8367   case OO_AmpEqual:
8368   case OO_PipeEqual:
8369   case OO_LessLess:
8370   case OO_GreaterGreater:
8371   case OO_LessLessEqual:
8372   case OO_GreaterGreaterEqual:
8373   case OO_EqualEqual:
8374   case OO_ExclaimEqual:
8375   case OO_PlusPlus:
8376   case OO_MinusMinus:
8377   case OO_Comma:
8378   case OO_ArrowStar:
8379   case OO_Arrow:
8380   case OO_Call:
8381   case OO_Subscript:
8382   case OO_Conditional:
8383   case OO_Coawait:
8384   case NUM_OVERLOADED_OPERATORS:
8385     llvm_unreachable("Unexpected reduction identifier");
8386   case OO_None:
8387     if (auto II = DN.getAsIdentifierInfo()) {
8388       if (II->isStr("max"))
8389         BOK = BO_GT;
8390       else if (II->isStr("min"))
8391         BOK = BO_LT;
8392     }
8393     break;
8394   }
8395   SourceRange ReductionIdRange;
8396   if (ReductionIdScopeSpec.isValid())
8397     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
8398   ReductionIdRange.setEnd(ReductionId.getEndLoc());
8399 
8400   SmallVector<Expr *, 8> Vars;
8401   SmallVector<Expr *, 8> Privates;
8402   SmallVector<Expr *, 8> LHSs;
8403   SmallVector<Expr *, 8> RHSs;
8404   SmallVector<Expr *, 8> ReductionOps;
8405   SmallVector<Decl *, 4> ExprCaptures;
8406   SmallVector<Expr *, 4> ExprPostUpdates;
8407   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
8408   bool FirstIter = true;
8409   for (auto RefExpr : VarList) {
8410     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
8411     // OpenMP [2.1, C/C++]
8412     //  A list item is a variable or array section, subject to the restrictions
8413     //  specified in Section 2.4 on page 42 and in each of the sections
8414     // describing clauses and directives for which a list appears.
8415     // OpenMP  [2.14.3.3, Restrictions, p.1]
8416     //  A variable that is part of another variable (as an array or
8417     //  structure element) cannot appear in a private clause.
8418     if (!FirstIter && IR != ER)
8419       ++IR;
8420     FirstIter = false;
8421     SourceLocation ELoc;
8422     SourceRange ERange;
8423     Expr *SimpleRefExpr = RefExpr;
8424     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8425                               /*AllowArraySection=*/true);
8426     if (Res.second) {
8427       // It will be analyzed later.
8428       Vars.push_back(RefExpr);
8429       Privates.push_back(nullptr);
8430       LHSs.push_back(nullptr);
8431       RHSs.push_back(nullptr);
8432       // Try to find 'declare reduction' corresponding construct before using
8433       // builtin/overloaded operators.
8434       QualType Type = Context.DependentTy;
8435       CXXCastPath BasePath;
8436       ExprResult DeclareReductionRef = buildDeclareReductionRef(
8437           *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8438           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8439       if (CurContext->isDependentContext() &&
8440           (DeclareReductionRef.isUnset() ||
8441            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
8442         ReductionOps.push_back(DeclareReductionRef.get());
8443       else
8444         ReductionOps.push_back(nullptr);
8445     }
8446     ValueDecl *D = Res.first;
8447     if (!D)
8448       continue;
8449 
8450     QualType Type;
8451     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
8452     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
8453     if (ASE)
8454       Type = ASE->getType().getNonReferenceType();
8455     else if (OASE) {
8456       auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
8457       if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
8458         Type = ATy->getElementType();
8459       else
8460         Type = BaseType->getPointeeType();
8461       Type = Type.getNonReferenceType();
8462     } else
8463       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
8464     auto *VD = dyn_cast<VarDecl>(D);
8465 
8466     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8467     //  A variable that appears in a private clause must not have an incomplete
8468     //  type or a reference type.
8469     if (RequireCompleteType(ELoc, Type,
8470                             diag::err_omp_reduction_incomplete_type))
8471       continue;
8472     // OpenMP [2.14.3.6, reduction clause, Restrictions]
8473     // A list item that appears in a reduction clause must not be
8474     // const-qualified.
8475     if (Type.getNonReferenceType().isConstant(Context)) {
8476       Diag(ELoc, diag::err_omp_const_reduction_list_item)
8477           << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
8478       if (!ASE && !OASE) {
8479         bool IsDecl = !VD ||
8480                       VD->isThisDeclarationADefinition(Context) ==
8481                           VarDecl::DeclarationOnly;
8482         Diag(D->getLocation(),
8483              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8484             << D;
8485       }
8486       continue;
8487     }
8488     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
8489     //  If a list-item is a reference type then it must bind to the same object
8490     //  for all threads of the team.
8491     if (!ASE && !OASE && VD) {
8492       VarDecl *VDDef = VD->getDefinition();
8493       if (VD->getType()->isReferenceType() && VDDef) {
8494         DSARefChecker Check(DSAStack);
8495         if (Check.Visit(VDDef->getInit())) {
8496           Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
8497           Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
8498           continue;
8499         }
8500       }
8501     }
8502 
8503     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8504     // in a Construct]
8505     //  Variables with the predetermined data-sharing attributes may not be
8506     //  listed in data-sharing attributes clauses, except for the cases
8507     //  listed below. For these exceptions only, listing a predetermined
8508     //  variable in a data-sharing attribute clause is allowed and overrides
8509     //  the variable's predetermined data-sharing attributes.
8510     // OpenMP [2.14.3.6, Restrictions, p.3]
8511     //  Any number of reduction clauses can be specified on the directive,
8512     //  but a list item can appear only once in the reduction clauses for that
8513     //  directive.
8514     DSAStackTy::DSAVarData DVar;
8515     DVar = DSAStack->getTopDSA(D, false);
8516     if (DVar.CKind == OMPC_reduction) {
8517       Diag(ELoc, diag::err_omp_once_referenced)
8518           << getOpenMPClauseName(OMPC_reduction);
8519       if (DVar.RefExpr)
8520         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
8521     } else if (DVar.CKind != OMPC_unknown) {
8522       Diag(ELoc, diag::err_omp_wrong_dsa)
8523           << getOpenMPClauseName(DVar.CKind)
8524           << getOpenMPClauseName(OMPC_reduction);
8525       ReportOriginalDSA(*this, DSAStack, D, DVar);
8526       continue;
8527     }
8528 
8529     // OpenMP [2.14.3.6, Restrictions, p.1]
8530     //  A list item that appears in a reduction clause of a worksharing
8531     //  construct must be shared in the parallel regions to which any of the
8532     //  worksharing regions arising from the worksharing construct bind.
8533     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8534     if (isOpenMPWorksharingDirective(CurrDir) &&
8535         !isOpenMPParallelDirective(CurrDir)) {
8536       DVar = DSAStack->getImplicitDSA(D, true);
8537       if (DVar.CKind != OMPC_shared) {
8538         Diag(ELoc, diag::err_omp_required_access)
8539             << getOpenMPClauseName(OMPC_reduction)
8540             << getOpenMPClauseName(OMPC_shared);
8541         ReportOriginalDSA(*this, DSAStack, D, DVar);
8542         continue;
8543       }
8544     }
8545 
8546     // Try to find 'declare reduction' corresponding construct before using
8547     // builtin/overloaded operators.
8548     CXXCastPath BasePath;
8549     ExprResult DeclareReductionRef = buildDeclareReductionRef(
8550         *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
8551         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
8552     if (DeclareReductionRef.isInvalid())
8553       continue;
8554     if (CurContext->isDependentContext() &&
8555         (DeclareReductionRef.isUnset() ||
8556          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
8557       Vars.push_back(RefExpr);
8558       Privates.push_back(nullptr);
8559       LHSs.push_back(nullptr);
8560       RHSs.push_back(nullptr);
8561       ReductionOps.push_back(DeclareReductionRef.get());
8562       continue;
8563     }
8564     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
8565       // Not allowed reduction identifier is found.
8566       Diag(ReductionId.getLocStart(),
8567            diag::err_omp_unknown_reduction_identifier)
8568           << Type << ReductionIdRange;
8569       continue;
8570     }
8571 
8572     // OpenMP [2.14.3.6, reduction clause, Restrictions]
8573     // The type of a list item that appears in a reduction clause must be valid
8574     // for the reduction-identifier. For a max or min reduction in C, the type
8575     // of the list item must be an allowed arithmetic data type: char, int,
8576     // float, double, or _Bool, possibly modified with long, short, signed, or
8577     // unsigned. For a max or min reduction in C++, the type of the list item
8578     // must be an allowed arithmetic data type: char, wchar_t, int, float,
8579     // double, or bool, possibly modified with long, short, signed, or unsigned.
8580     if (DeclareReductionRef.isUnset()) {
8581       if ((BOK == BO_GT || BOK == BO_LT) &&
8582           !(Type->isScalarType() ||
8583             (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
8584         Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
8585             << getLangOpts().CPlusPlus;
8586         if (!ASE && !OASE) {
8587           bool IsDecl = !VD ||
8588                         VD->isThisDeclarationADefinition(Context) ==
8589                             VarDecl::DeclarationOnly;
8590           Diag(D->getLocation(),
8591                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8592               << D;
8593         }
8594         continue;
8595       }
8596       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
8597           !getLangOpts().CPlusPlus && Type->isFloatingType()) {
8598         Diag(ELoc, diag::err_omp_clause_floating_type_arg);
8599         if (!ASE && !OASE) {
8600           bool IsDecl = !VD ||
8601                         VD->isThisDeclarationADefinition(Context) ==
8602                             VarDecl::DeclarationOnly;
8603           Diag(D->getLocation(),
8604                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8605               << D;
8606         }
8607         continue;
8608       }
8609     }
8610 
8611     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
8612     auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
8613                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8614     auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
8615                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8616     auto PrivateTy = Type;
8617     if (OASE ||
8618         (!ASE &&
8619          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
8620       // For arays/array sections only:
8621       // Create pseudo array type for private copy. The size for this array will
8622       // be generated during codegen.
8623       // For array subscripts or single variables Private Ty is the same as Type
8624       // (type of the variable or single array element).
8625       PrivateTy = Context.getVariableArrayType(
8626           Type, new (Context) OpaqueValueExpr(SourceLocation(),
8627                                               Context.getSizeType(), VK_RValue),
8628           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
8629     } else if (!ASE && !OASE &&
8630                Context.getAsArrayType(D->getType().getNonReferenceType()))
8631       PrivateTy = D->getType().getNonReferenceType();
8632     // Private copy.
8633     auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
8634                                    D->hasAttrs() ? &D->getAttrs() : nullptr);
8635     // Add initializer for private variable.
8636     Expr *Init = nullptr;
8637     auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
8638     auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
8639     if (DeclareReductionRef.isUsable()) {
8640       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
8641       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
8642       if (DRD->getInitializer()) {
8643         Init = DRDRef;
8644         RHSVD->setInit(DRDRef);
8645         RHSVD->setInitStyle(VarDecl::CallInit);
8646       }
8647     } else {
8648       switch (BOK) {
8649       case BO_Add:
8650       case BO_Xor:
8651       case BO_Or:
8652       case BO_LOr:
8653         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
8654         if (Type->isScalarType() || Type->isAnyComplexType())
8655           Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
8656         break;
8657       case BO_Mul:
8658       case BO_LAnd:
8659         if (Type->isScalarType() || Type->isAnyComplexType()) {
8660           // '*' and '&&' reduction ops - initializer is '1'.
8661           Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
8662         }
8663         break;
8664       case BO_And: {
8665         // '&' reduction op - initializer is '~0'.
8666         QualType OrigType = Type;
8667         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
8668           Type = ComplexTy->getElementType();
8669         if (Type->isRealFloatingType()) {
8670           llvm::APFloat InitValue =
8671               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
8672                                              /*isIEEE=*/true);
8673           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8674                                          Type, ELoc);
8675         } else if (Type->isScalarType()) {
8676           auto Size = Context.getTypeSize(Type);
8677           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
8678           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
8679           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8680         }
8681         if (Init && OrigType->isAnyComplexType()) {
8682           // Init = 0xFFFF + 0xFFFFi;
8683           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
8684           Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
8685         }
8686         Type = OrigType;
8687         break;
8688       }
8689       case BO_LT:
8690       case BO_GT: {
8691         // 'min' reduction op - initializer is 'Largest representable number in
8692         // the reduction list item type'.
8693         // 'max' reduction op - initializer is 'Least representable number in
8694         // the reduction list item type'.
8695         if (Type->isIntegerType() || Type->isPointerType()) {
8696           bool IsSigned = Type->hasSignedIntegerRepresentation();
8697           auto Size = Context.getTypeSize(Type);
8698           QualType IntTy =
8699               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
8700           llvm::APInt InitValue =
8701               (BOK != BO_LT)
8702                   ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
8703                              : llvm::APInt::getMinValue(Size)
8704                   : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
8705                              : llvm::APInt::getMaxValue(Size);
8706           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
8707           if (Type->isPointerType()) {
8708             // Cast to pointer type.
8709             auto CastExpr = BuildCStyleCastExpr(
8710                 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
8711                 SourceLocation(), Init);
8712             if (CastExpr.isInvalid())
8713               continue;
8714             Init = CastExpr.get();
8715           }
8716         } else if (Type->isRealFloatingType()) {
8717           llvm::APFloat InitValue = llvm::APFloat::getLargest(
8718               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
8719           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
8720                                          Type, ELoc);
8721         }
8722         break;
8723       }
8724       case BO_PtrMemD:
8725       case BO_PtrMemI:
8726       case BO_MulAssign:
8727       case BO_Div:
8728       case BO_Rem:
8729       case BO_Sub:
8730       case BO_Shl:
8731       case BO_Shr:
8732       case BO_LE:
8733       case BO_GE:
8734       case BO_EQ:
8735       case BO_NE:
8736       case BO_AndAssign:
8737       case BO_XorAssign:
8738       case BO_OrAssign:
8739       case BO_Assign:
8740       case BO_AddAssign:
8741       case BO_SubAssign:
8742       case BO_DivAssign:
8743       case BO_RemAssign:
8744       case BO_ShlAssign:
8745       case BO_ShrAssign:
8746       case BO_Comma:
8747         llvm_unreachable("Unexpected reduction operation");
8748       }
8749     }
8750     if (Init && DeclareReductionRef.isUnset()) {
8751       AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
8752                            /*TypeMayContainAuto=*/false);
8753     } else if (!Init)
8754       ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
8755     if (RHSVD->isInvalidDecl())
8756       continue;
8757     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
8758       Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
8759                                                             << ReductionIdRange;
8760       bool IsDecl =
8761           !VD ||
8762           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8763       Diag(D->getLocation(),
8764            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8765           << D;
8766       continue;
8767     }
8768     // Store initializer for single element in private copy. Will be used during
8769     // codegen.
8770     PrivateVD->setInit(RHSVD->getInit());
8771     PrivateVD->setInitStyle(RHSVD->getInitStyle());
8772     auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
8773     ExprResult ReductionOp;
8774     if (DeclareReductionRef.isUsable()) {
8775       QualType RedTy = DeclareReductionRef.get()->getType();
8776       QualType PtrRedTy = Context.getPointerType(RedTy);
8777       ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
8778       ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
8779       if (!BasePath.empty()) {
8780         LHS = DefaultLvalueConversion(LHS.get());
8781         RHS = DefaultLvalueConversion(RHS.get());
8782         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8783                                        CK_UncheckedDerivedToBase, LHS.get(),
8784                                        &BasePath, LHS.get()->getValueKind());
8785         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
8786                                        CK_UncheckedDerivedToBase, RHS.get(),
8787                                        &BasePath, RHS.get()->getValueKind());
8788       }
8789       FunctionProtoType::ExtProtoInfo EPI;
8790       QualType Params[] = {PtrRedTy, PtrRedTy};
8791       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
8792       auto *OVE = new (Context) OpaqueValueExpr(
8793           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
8794           DefaultLvalueConversion(DeclareReductionRef.get()).get());
8795       Expr *Args[] = {LHS.get(), RHS.get()};
8796       ReductionOp = new (Context)
8797           CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
8798     } else {
8799       ReductionOp = BuildBinOp(DSAStack->getCurScope(),
8800                                ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
8801       if (ReductionOp.isUsable()) {
8802         if (BOK != BO_LT && BOK != BO_GT) {
8803           ReductionOp =
8804               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8805                          BO_Assign, LHSDRE, ReductionOp.get());
8806         } else {
8807           auto *ConditionalOp = new (Context) ConditionalOperator(
8808               ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
8809               RHSDRE, Type, VK_LValue, OK_Ordinary);
8810           ReductionOp =
8811               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
8812                          BO_Assign, LHSDRE, ConditionalOp);
8813         }
8814         ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
8815       }
8816       if (ReductionOp.isInvalid())
8817         continue;
8818     }
8819 
8820     DeclRefExpr *Ref = nullptr;
8821     Expr *VarsExpr = RefExpr->IgnoreParens();
8822     if (!VD) {
8823       if (ASE || OASE) {
8824         TransformExprToCaptures RebuildToCapture(*this, D);
8825         VarsExpr =
8826             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
8827         Ref = RebuildToCapture.getCapturedExpr();
8828       } else {
8829         VarsExpr = Ref =
8830             buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8831       }
8832       if (!IsOpenMPCapturedDecl(D)) {
8833         ExprCaptures.push_back(Ref->getDecl());
8834         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8835           ExprResult RefRes = DefaultLvalueConversion(Ref);
8836           if (!RefRes.isUsable())
8837             continue;
8838           ExprResult PostUpdateRes =
8839               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8840                          SimpleRefExpr, RefRes.get());
8841           if (!PostUpdateRes.isUsable())
8842             continue;
8843           ExprPostUpdates.push_back(
8844               IgnoredValueConversions(PostUpdateRes.get()).get());
8845         }
8846       }
8847     }
8848     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
8849     Vars.push_back(VarsExpr);
8850     Privates.push_back(PrivateDRE);
8851     LHSs.push_back(LHSDRE);
8852     RHSs.push_back(RHSDRE);
8853     ReductionOps.push_back(ReductionOp.get());
8854   }
8855 
8856   if (Vars.empty())
8857     return nullptr;
8858 
8859   return OMPReductionClause::Create(
8860       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
8861       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
8862       LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
8863       buildPostUpdate(*this, ExprPostUpdates));
8864 }
8865 
8866 bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
8867                                      SourceLocation LinLoc) {
8868   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
8869       LinKind == OMPC_LINEAR_unknown) {
8870     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
8871     return true;
8872   }
8873   return false;
8874 }
8875 
8876 bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
8877                                  OpenMPLinearClauseKind LinKind,
8878                                  QualType Type) {
8879   auto *VD = dyn_cast_or_null<VarDecl>(D);
8880   // A variable must not have an incomplete type or a reference type.
8881   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
8882     return true;
8883   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
8884       !Type->isReferenceType()) {
8885     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
8886         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
8887     return true;
8888   }
8889   Type = Type.getNonReferenceType();
8890 
8891   // A list item must not be const-qualified.
8892   if (Type.isConstant(Context)) {
8893     Diag(ELoc, diag::err_omp_const_variable)
8894         << getOpenMPClauseName(OMPC_linear);
8895     if (D) {
8896       bool IsDecl =
8897           !VD ||
8898           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8899       Diag(D->getLocation(),
8900            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8901           << D;
8902     }
8903     return true;
8904   }
8905 
8906   // A list item must be of integral or pointer type.
8907   Type = Type.getUnqualifiedType().getCanonicalType();
8908   const auto *Ty = Type.getTypePtrOrNull();
8909   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
8910               !Ty->isPointerType())) {
8911     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
8912     if (D) {
8913       bool IsDecl =
8914           !VD ||
8915           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8916       Diag(D->getLocation(),
8917            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8918           << D;
8919     }
8920     return true;
8921   }
8922   return false;
8923 }
8924 
8925 OMPClause *Sema::ActOnOpenMPLinearClause(
8926     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
8927     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
8928     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
8929   SmallVector<Expr *, 8> Vars;
8930   SmallVector<Expr *, 8> Privates;
8931   SmallVector<Expr *, 8> Inits;
8932   SmallVector<Decl *, 4> ExprCaptures;
8933   SmallVector<Expr *, 4> ExprPostUpdates;
8934   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
8935     LinKind = OMPC_LINEAR_val;
8936   for (auto &RefExpr : VarList) {
8937     assert(RefExpr && "NULL expr in OpenMP linear clause.");
8938     SourceLocation ELoc;
8939     SourceRange ERange;
8940     Expr *SimpleRefExpr = RefExpr;
8941     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
8942                               /*AllowArraySection=*/false);
8943     if (Res.second) {
8944       // It will be analyzed later.
8945       Vars.push_back(RefExpr);
8946       Privates.push_back(nullptr);
8947       Inits.push_back(nullptr);
8948     }
8949     ValueDecl *D = Res.first;
8950     if (!D)
8951       continue;
8952 
8953     QualType Type = D->getType();
8954     auto *VD = dyn_cast<VarDecl>(D);
8955 
8956     // OpenMP [2.14.3.7, linear clause]
8957     //  A list-item cannot appear in more than one linear clause.
8958     //  A list-item that appears in a linear clause cannot appear in any
8959     //  other data-sharing attribute clause.
8960     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8961     if (DVar.RefExpr) {
8962       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8963                                           << getOpenMPClauseName(OMPC_linear);
8964       ReportOriginalDSA(*this, DSAStack, D, DVar);
8965       continue;
8966     }
8967 
8968     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
8969       continue;
8970     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
8971 
8972     // Build private copy of original var.
8973     auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
8974                                  D->hasAttrs() ? &D->getAttrs() : nullptr);
8975     auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
8976     // Build var to save initial value.
8977     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
8978     Expr *InitExpr;
8979     DeclRefExpr *Ref = nullptr;
8980     if (!VD) {
8981       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8982       if (!IsOpenMPCapturedDecl(D)) {
8983         ExprCaptures.push_back(Ref->getDecl());
8984         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
8985           ExprResult RefRes = DefaultLvalueConversion(Ref);
8986           if (!RefRes.isUsable())
8987             continue;
8988           ExprResult PostUpdateRes =
8989               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
8990                          SimpleRefExpr, RefRes.get());
8991           if (!PostUpdateRes.isUsable())
8992             continue;
8993           ExprPostUpdates.push_back(
8994               IgnoredValueConversions(PostUpdateRes.get()).get());
8995         }
8996       }
8997     }
8998     if (LinKind == OMPC_LINEAR_uval)
8999       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
9000     else
9001       InitExpr = VD ? SimpleRefExpr : Ref;
9002     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
9003                          /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9004     auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9005 
9006     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9007     Vars.push_back(VD ? RefExpr->IgnoreParens() : Ref);
9008     Privates.push_back(PrivateRef);
9009     Inits.push_back(InitRef);
9010   }
9011 
9012   if (Vars.empty())
9013     return nullptr;
9014 
9015   Expr *StepExpr = Step;
9016   Expr *CalcStepExpr = nullptr;
9017   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9018       !Step->isInstantiationDependent() &&
9019       !Step->containsUnexpandedParameterPack()) {
9020     SourceLocation StepLoc = Step->getLocStart();
9021     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
9022     if (Val.isInvalid())
9023       return nullptr;
9024     StepExpr = Val.get();
9025 
9026     // Build var to save the step value.
9027     VarDecl *SaveVar =
9028         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
9029     ExprResult SaveRef =
9030         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
9031     ExprResult CalcStep =
9032         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
9033     CalcStep = ActOnFinishFullExpr(CalcStep.get());
9034 
9035     // Warn about zero linear step (it would be probably better specified as
9036     // making corresponding variables 'const').
9037     llvm::APSInt Result;
9038     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9039     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
9040       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9041                                                      << (Vars.size() > 1);
9042     if (!IsConstant && CalcStep.isUsable()) {
9043       // Calculate the step beforehand instead of doing this on each iteration.
9044       // (This is not used if the number of iterations may be kfold-ed).
9045       CalcStepExpr = CalcStep.get();
9046     }
9047   }
9048 
9049   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9050                                  ColonLoc, EndLoc, Vars, Privates, Inits,
9051                                  StepExpr, CalcStepExpr,
9052                                  buildPreInits(Context, ExprCaptures),
9053                                  buildPostUpdate(*this, ExprPostUpdates));
9054 }
9055 
9056 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9057                                      Expr *NumIterations, Sema &SemaRef,
9058                                      Scope *S, DSAStackTy *Stack) {
9059   // Walk the vars and build update/final expressions for the CodeGen.
9060   SmallVector<Expr *, 8> Updates;
9061   SmallVector<Expr *, 8> Finals;
9062   Expr *Step = Clause.getStep();
9063   Expr *CalcStep = Clause.getCalcStep();
9064   // OpenMP [2.14.3.7, linear clause]
9065   // If linear-step is not specified it is assumed to be 1.
9066   if (Step == nullptr)
9067     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
9068   else if (CalcStep) {
9069     Step = cast<BinaryOperator>(CalcStep)->getLHS();
9070   }
9071   bool HasErrors = false;
9072   auto CurInit = Clause.inits().begin();
9073   auto CurPrivate = Clause.privates().begin();
9074   auto LinKind = Clause.getModifier();
9075   for (auto &RefExpr : Clause.varlists()) {
9076     SourceLocation ELoc;
9077     SourceRange ERange;
9078     Expr *SimpleRefExpr = RefExpr;
9079     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9080                               /*AllowArraySection=*/false);
9081     ValueDecl *D = Res.first;
9082     if (Res.second || !D) {
9083       Updates.push_back(nullptr);
9084       Finals.push_back(nullptr);
9085       HasErrors = true;
9086       continue;
9087     }
9088     if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9089       D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9090               ->getMemberDecl();
9091     }
9092     auto &&Info = Stack->isLoopControlVariable(D);
9093     Expr *InitExpr = *CurInit;
9094 
9095     // Build privatized reference to the current linear var.
9096     auto DE = cast<DeclRefExpr>(SimpleRefExpr);
9097     Expr *CapturedRef;
9098     if (LinKind == OMPC_LINEAR_uval)
9099       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9100     else
9101       CapturedRef =
9102           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9103                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9104                            /*RefersToCapture=*/true);
9105 
9106     // Build update: Var = InitExpr + IV * Step
9107     ExprResult Update;
9108     if (!Info.first) {
9109       Update =
9110           BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9111                              InitExpr, IV, Step, /* Subtract */ false);
9112     } else
9113       Update = *CurPrivate;
9114     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9115                                          /*DiscardedValue=*/true);
9116 
9117     // Build final: Var = InitExpr + NumIterations * Step
9118     ExprResult Final;
9119     if (!Info.first) {
9120       Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9121                                  InitExpr, NumIterations, Step,
9122                                  /* Subtract */ false);
9123     } else
9124       Final = *CurPrivate;
9125     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9126                                         /*DiscardedValue=*/true);
9127 
9128     if (!Update.isUsable() || !Final.isUsable()) {
9129       Updates.push_back(nullptr);
9130       Finals.push_back(nullptr);
9131       HasErrors = true;
9132     } else {
9133       Updates.push_back(Update.get());
9134       Finals.push_back(Final.get());
9135     }
9136     ++CurInit;
9137     ++CurPrivate;
9138   }
9139   Clause.setUpdates(Updates);
9140   Clause.setFinals(Finals);
9141   return HasErrors;
9142 }
9143 
9144 OMPClause *Sema::ActOnOpenMPAlignedClause(
9145     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9146     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9147 
9148   SmallVector<Expr *, 8> Vars;
9149   for (auto &RefExpr : VarList) {
9150     assert(RefExpr && "NULL expr in OpenMP linear clause.");
9151     SourceLocation ELoc;
9152     SourceRange ERange;
9153     Expr *SimpleRefExpr = RefExpr;
9154     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9155                               /*AllowArraySection=*/false);
9156     if (Res.second) {
9157       // It will be analyzed later.
9158       Vars.push_back(RefExpr);
9159     }
9160     ValueDecl *D = Res.first;
9161     if (!D)
9162       continue;
9163 
9164     QualType QType = D->getType();
9165     auto *VD = dyn_cast<VarDecl>(D);
9166 
9167     // OpenMP  [2.8.1, simd construct, Restrictions]
9168     // The type of list items appearing in the aligned clause must be
9169     // array, pointer, reference to array, or reference to pointer.
9170     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
9171     const Type *Ty = QType.getTypePtrOrNull();
9172     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
9173       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
9174           << QType << getLangOpts().CPlusPlus << ERange;
9175       bool IsDecl =
9176           !VD ||
9177           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9178       Diag(D->getLocation(),
9179            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9180           << D;
9181       continue;
9182     }
9183 
9184     // OpenMP  [2.8.1, simd construct, Restrictions]
9185     // A list-item cannot appear in more than one aligned clause.
9186     if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
9187       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
9188       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9189           << getOpenMPClauseName(OMPC_aligned);
9190       continue;
9191     }
9192 
9193     DeclRefExpr *Ref = nullptr;
9194     if (!VD && IsOpenMPCapturedDecl(D))
9195       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9196     Vars.push_back(DefaultFunctionArrayConversion(
9197                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9198                        .get());
9199   }
9200 
9201   // OpenMP [2.8.1, simd construct, Description]
9202   // The parameter of the aligned clause, alignment, must be a constant
9203   // positive integer expression.
9204   // If no optional parameter is specified, implementation-defined default
9205   // alignments for SIMD instructions on the target platforms are assumed.
9206   if (Alignment != nullptr) {
9207     ExprResult AlignResult =
9208         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9209     if (AlignResult.isInvalid())
9210       return nullptr;
9211     Alignment = AlignResult.get();
9212   }
9213   if (Vars.empty())
9214     return nullptr;
9215 
9216   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9217                                   EndLoc, Vars, Alignment);
9218 }
9219 
9220 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9221                                          SourceLocation StartLoc,
9222                                          SourceLocation LParenLoc,
9223                                          SourceLocation EndLoc) {
9224   SmallVector<Expr *, 8> Vars;
9225   SmallVector<Expr *, 8> SrcExprs;
9226   SmallVector<Expr *, 8> DstExprs;
9227   SmallVector<Expr *, 8> AssignmentOps;
9228   for (auto &RefExpr : VarList) {
9229     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9230     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
9231       // It will be analyzed later.
9232       Vars.push_back(RefExpr);
9233       SrcExprs.push_back(nullptr);
9234       DstExprs.push_back(nullptr);
9235       AssignmentOps.push_back(nullptr);
9236       continue;
9237     }
9238 
9239     SourceLocation ELoc = RefExpr->getExprLoc();
9240     // OpenMP [2.1, C/C++]
9241     //  A list item is a variable name.
9242     // OpenMP  [2.14.4.1, Restrictions, p.1]
9243     //  A list item that appears in a copyin clause must be threadprivate.
9244     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
9245     if (!DE || !isa<VarDecl>(DE->getDecl())) {
9246       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9247           << 0 << RefExpr->getSourceRange();
9248       continue;
9249     }
9250 
9251     Decl *D = DE->getDecl();
9252     VarDecl *VD = cast<VarDecl>(D);
9253 
9254     QualType Type = VD->getType();
9255     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9256       // It will be analyzed later.
9257       Vars.push_back(DE);
9258       SrcExprs.push_back(nullptr);
9259       DstExprs.push_back(nullptr);
9260       AssignmentOps.push_back(nullptr);
9261       continue;
9262     }
9263 
9264     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9265     //  A list item that appears in a copyin clause must be threadprivate.
9266     if (!DSAStack->isThreadPrivate(VD)) {
9267       Diag(ELoc, diag::err_omp_required_access)
9268           << getOpenMPClauseName(OMPC_copyin)
9269           << getOpenMPDirectiveName(OMPD_threadprivate);
9270       continue;
9271     }
9272 
9273     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9274     //  A variable of class type (or array thereof) that appears in a
9275     //  copyin clause requires an accessible, unambiguous copy assignment
9276     //  operator for the class type.
9277     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
9278     auto *SrcVD =
9279         buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9280                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9281     auto *PseudoSrcExpr = buildDeclRefExpr(
9282         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9283     auto *DstVD =
9284         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9285                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9286     auto *PseudoDstExpr =
9287         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
9288     // For arrays generate assignment operation for single element and replace
9289     // it by the original array element in CodeGen.
9290     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9291                                    PseudoDstExpr, PseudoSrcExpr);
9292     if (AssignmentOp.isInvalid())
9293       continue;
9294     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9295                                        /*DiscardedValue=*/true);
9296     if (AssignmentOp.isInvalid())
9297       continue;
9298 
9299     DSAStack->addDSA(VD, DE, OMPC_copyin);
9300     Vars.push_back(DE);
9301     SrcExprs.push_back(PseudoSrcExpr);
9302     DstExprs.push_back(PseudoDstExpr);
9303     AssignmentOps.push_back(AssignmentOp.get());
9304   }
9305 
9306   if (Vars.empty())
9307     return nullptr;
9308 
9309   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9310                                  SrcExprs, DstExprs, AssignmentOps);
9311 }
9312 
9313 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9314                                               SourceLocation StartLoc,
9315                                               SourceLocation LParenLoc,
9316                                               SourceLocation EndLoc) {
9317   SmallVector<Expr *, 8> Vars;
9318   SmallVector<Expr *, 8> SrcExprs;
9319   SmallVector<Expr *, 8> DstExprs;
9320   SmallVector<Expr *, 8> AssignmentOps;
9321   for (auto &RefExpr : VarList) {
9322     assert(RefExpr && "NULL expr in OpenMP linear clause.");
9323     SourceLocation ELoc;
9324     SourceRange ERange;
9325     Expr *SimpleRefExpr = RefExpr;
9326     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9327                               /*AllowArraySection=*/false);
9328     if (Res.second) {
9329       // It will be analyzed later.
9330       Vars.push_back(RefExpr);
9331       SrcExprs.push_back(nullptr);
9332       DstExprs.push_back(nullptr);
9333       AssignmentOps.push_back(nullptr);
9334     }
9335     ValueDecl *D = Res.first;
9336     if (!D)
9337       continue;
9338 
9339     QualType Type = D->getType();
9340     auto *VD = dyn_cast<VarDecl>(D);
9341 
9342     // OpenMP [2.14.4.2, Restrictions, p.2]
9343     //  A list item that appears in a copyprivate clause may not appear in a
9344     //  private or firstprivate clause on the single construct.
9345     if (!VD || !DSAStack->isThreadPrivate(VD)) {
9346       auto DVar = DSAStack->getTopDSA(D, false);
9347       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
9348           DVar.RefExpr) {
9349         Diag(ELoc, diag::err_omp_wrong_dsa)
9350             << getOpenMPClauseName(DVar.CKind)
9351             << getOpenMPClauseName(OMPC_copyprivate);
9352         ReportOriginalDSA(*this, DSAStack, D, DVar);
9353         continue;
9354       }
9355 
9356       // OpenMP [2.11.4.2, Restrictions, p.1]
9357       //  All list items that appear in a copyprivate clause must be either
9358       //  threadprivate or private in the enclosing context.
9359       if (DVar.CKind == OMPC_unknown) {
9360         DVar = DSAStack->getImplicitDSA(D, false);
9361         if (DVar.CKind == OMPC_shared) {
9362           Diag(ELoc, diag::err_omp_required_access)
9363               << getOpenMPClauseName(OMPC_copyprivate)
9364               << "threadprivate or private in the enclosing context";
9365           ReportOriginalDSA(*this, DSAStack, D, DVar);
9366           continue;
9367         }
9368       }
9369     }
9370 
9371     // Variably modified types are not supported.
9372     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
9373       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9374           << getOpenMPClauseName(OMPC_copyprivate) << Type
9375           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9376       bool IsDecl =
9377           !VD ||
9378           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9379       Diag(D->getLocation(),
9380            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9381           << D;
9382       continue;
9383     }
9384 
9385     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9386     //  A variable of class type (or array thereof) that appears in a
9387     //  copyin clause requires an accessible, unambiguous copy assignment
9388     //  operator for the class type.
9389     Type = Context.getBaseElementType(Type.getNonReferenceType())
9390                .getUnqualifiedType();
9391     auto *SrcVD =
9392         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
9393                      D->hasAttrs() ? &D->getAttrs() : nullptr);
9394     auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
9395     auto *DstVD =
9396         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
9397                      D->hasAttrs() ? &D->getAttrs() : nullptr);
9398     auto *PseudoDstExpr =
9399         buildDeclRefExpr(*this, DstVD, Type, ELoc);
9400     auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9401                                    PseudoDstExpr, PseudoSrcExpr);
9402     if (AssignmentOp.isInvalid())
9403       continue;
9404     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
9405                                        /*DiscardedValue=*/true);
9406     if (AssignmentOp.isInvalid())
9407       continue;
9408 
9409     // No need to mark vars as copyprivate, they are already threadprivate or
9410     // implicitly private.
9411     assert(VD || IsOpenMPCapturedDecl(D));
9412     Vars.push_back(
9413         VD ? RefExpr->IgnoreParens()
9414            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
9415     SrcExprs.push_back(PseudoSrcExpr);
9416     DstExprs.push_back(PseudoDstExpr);
9417     AssignmentOps.push_back(AssignmentOp.get());
9418   }
9419 
9420   if (Vars.empty())
9421     return nullptr;
9422 
9423   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
9424                                       Vars, SrcExprs, DstExprs, AssignmentOps);
9425 }
9426 
9427 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
9428                                         SourceLocation StartLoc,
9429                                         SourceLocation LParenLoc,
9430                                         SourceLocation EndLoc) {
9431   if (VarList.empty())
9432     return nullptr;
9433 
9434   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
9435 }
9436 
9437 OMPClause *
9438 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
9439                               SourceLocation DepLoc, SourceLocation ColonLoc,
9440                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
9441                               SourceLocation LParenLoc, SourceLocation EndLoc) {
9442   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
9443       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
9444     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
9445         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
9446     return nullptr;
9447   }
9448   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
9449       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
9450        DepKind == OMPC_DEPEND_sink)) {
9451     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
9452     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
9453         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
9454                                    /*Last=*/OMPC_DEPEND_unknown, Except)
9455         << getOpenMPClauseName(OMPC_depend);
9456     return nullptr;
9457   }
9458   SmallVector<Expr *, 8> Vars;
9459   llvm::APSInt DepCounter(/*BitWidth=*/32);
9460   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
9461   if (DepKind == OMPC_DEPEND_sink) {
9462     if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
9463       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
9464       TotalDepCount.setIsUnsigned(/*Val=*/true);
9465     }
9466   }
9467   if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
9468       DSAStack->getParentOrderedRegionParam()) {
9469     for (auto &RefExpr : VarList) {
9470       assert(RefExpr && "NULL expr in OpenMP shared clause.");
9471       if (isa<DependentScopeDeclRefExpr>(RefExpr) ||
9472           (DepKind == OMPC_DEPEND_sink && CurContext->isDependentContext())) {
9473         // It will be analyzed later.
9474         Vars.push_back(RefExpr);
9475         continue;
9476       }
9477 
9478       SourceLocation ELoc = RefExpr->getExprLoc();
9479       auto *SimpleExpr = RefExpr->IgnoreParenCasts();
9480       if (DepKind == OMPC_DEPEND_sink) {
9481         if (DepCounter >= TotalDepCount) {
9482           Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
9483           continue;
9484         }
9485         ++DepCounter;
9486         // OpenMP  [2.13.9, Summary]
9487         // depend(dependence-type : vec), where dependence-type is:
9488         // 'sink' and where vec is the iteration vector, which has the form:
9489         //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
9490         // where n is the value specified by the ordered clause in the loop
9491         // directive, xi denotes the loop iteration variable of the i-th nested
9492         // loop associated with the loop directive, and di is a constant
9493         // non-negative integer.
9494         SimpleExpr = SimpleExpr->IgnoreImplicit();
9495         auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9496         if (!DE) {
9497           OverloadedOperatorKind OOK = OO_None;
9498           SourceLocation OOLoc;
9499           Expr *LHS, *RHS;
9500           if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
9501             OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
9502             OOLoc = BO->getOperatorLoc();
9503             LHS = BO->getLHS()->IgnoreParenImpCasts();
9504             RHS = BO->getRHS()->IgnoreParenImpCasts();
9505           } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
9506             OOK = OCE->getOperator();
9507             OOLoc = OCE->getOperatorLoc();
9508             LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9509             RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
9510           } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
9511             OOK = MCE->getMethodDecl()
9512                       ->getNameInfo()
9513                       .getName()
9514                       .getCXXOverloadedOperator();
9515             OOLoc = MCE->getCallee()->getExprLoc();
9516             LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
9517             RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
9518           } else {
9519             Diag(ELoc, diag::err_omp_depend_sink_wrong_expr);
9520             continue;
9521           }
9522           DE = dyn_cast<DeclRefExpr>(LHS);
9523           if (!DE) {
9524             Diag(LHS->getExprLoc(),
9525                  diag::err_omp_depend_sink_expected_loop_iteration)
9526                 << DSAStack->getParentLoopControlVariable(
9527                     DepCounter.getZExtValue());
9528             continue;
9529           }
9530           if (OOK != OO_Plus && OOK != OO_Minus) {
9531             Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
9532             continue;
9533           }
9534           ExprResult Res = VerifyPositiveIntegerConstantInClause(
9535               RHS, OMPC_depend, /*StrictlyPositive=*/false);
9536           if (Res.isInvalid())
9537             continue;
9538         }
9539         auto *VD = dyn_cast<VarDecl>(DE->getDecl());
9540         if (!CurContext->isDependentContext() &&
9541             DSAStack->getParentOrderedRegionParam() &&
9542             (!VD ||
9543              DepCounter != DSAStack->isParentLoopControlVariable(VD).first)) {
9544           Diag(DE->getExprLoc(),
9545                diag::err_omp_depend_sink_expected_loop_iteration)
9546               << DSAStack->getParentLoopControlVariable(
9547                   DepCounter.getZExtValue());
9548           continue;
9549         }
9550       } else {
9551         // OpenMP  [2.11.1.1, Restrictions, p.3]
9552         //  A variable that is part of another variable (such as a field of a
9553         //  structure) but is not an array element or an array section cannot
9554         //  appear  in a depend clause.
9555         auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
9556         auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
9557         auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
9558         if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
9559             (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
9560             (ASE &&
9561              !ASE->getBase()
9562                   ->getType()
9563                   .getNonReferenceType()
9564                   ->isPointerType() &&
9565              !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
9566           Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
9567               << 0 << RefExpr->getSourceRange();
9568           continue;
9569         }
9570       }
9571 
9572       Vars.push_back(RefExpr->IgnoreParenImpCasts());
9573     }
9574 
9575     if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
9576         TotalDepCount > VarList.size() &&
9577         DSAStack->getParentOrderedRegionParam()) {
9578       Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
9579           << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
9580     }
9581     if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
9582         Vars.empty())
9583       return nullptr;
9584   }
9585 
9586   return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
9587                                  DepLoc, ColonLoc, Vars);
9588 }
9589 
9590 OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
9591                                          SourceLocation LParenLoc,
9592                                          SourceLocation EndLoc) {
9593   Expr *ValExpr = Device;
9594 
9595   // OpenMP [2.9.1, Restrictions]
9596   // The device expression must evaluate to a non-negative integer value.
9597   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
9598                                  /*StrictlyPositive=*/false))
9599     return nullptr;
9600 
9601   return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9602 }
9603 
9604 static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
9605                                    DSAStackTy *Stack, CXXRecordDecl *RD) {
9606   if (!RD || RD->isInvalidDecl())
9607     return true;
9608 
9609   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
9610     if (auto *CTD = CTSD->getSpecializedTemplate())
9611       RD = CTD->getTemplatedDecl();
9612   auto QTy = SemaRef.Context.getRecordType(RD);
9613   if (RD->isDynamicClass()) {
9614     SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9615     SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
9616     return false;
9617   }
9618   auto *DC = RD;
9619   bool IsCorrect = true;
9620   for (auto *I : DC->decls()) {
9621     if (I) {
9622       if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
9623         if (MD->isStatic()) {
9624           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9625           SemaRef.Diag(MD->getLocation(),
9626                        diag::note_omp_static_member_in_target);
9627           IsCorrect = false;
9628         }
9629       } else if (auto *VD = dyn_cast<VarDecl>(I)) {
9630         if (VD->isStaticDataMember()) {
9631           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
9632           SemaRef.Diag(VD->getLocation(),
9633                        diag::note_omp_static_member_in_target);
9634           IsCorrect = false;
9635         }
9636       }
9637     }
9638   }
9639 
9640   for (auto &I : RD->bases()) {
9641     if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
9642                                 I.getType()->getAsCXXRecordDecl()))
9643       IsCorrect = false;
9644   }
9645   return IsCorrect;
9646 }
9647 
9648 static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
9649                               DSAStackTy *Stack, QualType QTy) {
9650   NamedDecl *ND;
9651   if (QTy->isIncompleteType(&ND)) {
9652     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
9653     return false;
9654   } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
9655     if (!RD->isInvalidDecl() &&
9656         !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
9657       return false;
9658   }
9659   return true;
9660 }
9661 
9662 /// \brief Return true if it can be proven that the provided array expression
9663 /// (array section or array subscript) does NOT specify the whole size of the
9664 /// array whose base type is \a BaseQTy.
9665 static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
9666                                                         const Expr *E,
9667                                                         QualType BaseQTy) {
9668   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9669 
9670   // If this is an array subscript, it refers to the whole size if the size of
9671   // the dimension is constant and equals 1. Also, an array section assumes the
9672   // format of an array subscript if no colon is used.
9673   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
9674     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9675       return ATy->getSize().getSExtValue() != 1;
9676     // Size can't be evaluated statically.
9677     return false;
9678   }
9679 
9680   assert(OASE && "Expecting array section if not an array subscript.");
9681   auto *LowerBound = OASE->getLowerBound();
9682   auto *Length = OASE->getLength();
9683 
9684   // If there is a lower bound that does not evaluates to zero, we are not
9685   // convering the whole dimension.
9686   if (LowerBound) {
9687     llvm::APSInt ConstLowerBound;
9688     if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
9689       return false; // Can't get the integer value as a constant.
9690     if (ConstLowerBound.getSExtValue())
9691       return true;
9692   }
9693 
9694   // If we don't have a length we covering the whole dimension.
9695   if (!Length)
9696     return false;
9697 
9698   // If the base is a pointer, we don't have a way to get the size of the
9699   // pointee.
9700   if (BaseQTy->isPointerType())
9701     return false;
9702 
9703   // We can only check if the length is the same as the size of the dimension
9704   // if we have a constant array.
9705   auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
9706   if (!CATy)
9707     return false;
9708 
9709   llvm::APSInt ConstLength;
9710   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9711     return false; // Can't get the integer value as a constant.
9712 
9713   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
9714 }
9715 
9716 // Return true if it can be proven that the provided array expression (array
9717 // section or array subscript) does NOT specify a single element of the array
9718 // whose base type is \a BaseQTy.
9719 static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
9720                                                        const Expr *E,
9721                                                        QualType BaseQTy) {
9722   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
9723 
9724   // An array subscript always refer to a single element. Also, an array section
9725   // assumes the format of an array subscript if no colon is used.
9726   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
9727     return false;
9728 
9729   assert(OASE && "Expecting array section if not an array subscript.");
9730   auto *Length = OASE->getLength();
9731 
9732   // If we don't have a length we have to check if the array has unitary size
9733   // for this dimension. Also, we should always expect a length if the base type
9734   // is pointer.
9735   if (!Length) {
9736     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
9737       return ATy->getSize().getSExtValue() != 1;
9738     // We cannot assume anything.
9739     return false;
9740   }
9741 
9742   // Check if the length evaluates to 1.
9743   llvm::APSInt ConstLength;
9744   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
9745     return false; // Can't get the integer value as a constant.
9746 
9747   return ConstLength.getSExtValue() != 1;
9748 }
9749 
9750 // Return the expression of the base of the map clause or null if it cannot
9751 // be determined and do all the necessary checks to see if the expression is
9752 // valid as a standalone map clause expression. In the process, record all the
9753 // components of the expression.
9754 static Expr *CheckMapClauseExpressionBase(
9755     Sema &SemaRef, Expr *E,
9756     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents) {
9757   SourceLocation ELoc = E->getExprLoc();
9758   SourceRange ERange = E->getSourceRange();
9759 
9760   // The base of elements of list in a map clause have to be either:
9761   //  - a reference to variable or field.
9762   //  - a member expression.
9763   //  - an array expression.
9764   //
9765   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
9766   // reference to 'r'.
9767   //
9768   // If we have:
9769   //
9770   // struct SS {
9771   //   Bla S;
9772   //   foo() {
9773   //     #pragma omp target map (S.Arr[:12]);
9774   //   }
9775   // }
9776   //
9777   // We want to retrieve the member expression 'this->S';
9778 
9779   Expr *RelevantExpr = nullptr;
9780 
9781   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
9782   //  If a list item is an array section, it must specify contiguous storage.
9783   //
9784   // For this restriction it is sufficient that we make sure only references
9785   // to variables or fields and array expressions, and that no array sections
9786   // exist except in the rightmost expression (unless they cover the whole
9787   // dimension of the array). E.g. these would be invalid:
9788   //
9789   //   r.ArrS[3:5].Arr[6:7]
9790   //
9791   //   r.ArrS[3:5].x
9792   //
9793   // but these would be valid:
9794   //   r.ArrS[3].Arr[6:7]
9795   //
9796   //   r.ArrS[3].x
9797 
9798   bool AllowUnitySizeArraySection = true;
9799   bool AllowWholeSizeArraySection = true;
9800 
9801   while (!RelevantExpr) {
9802     E = E->IgnoreParenImpCasts();
9803 
9804     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
9805       if (!isa<VarDecl>(CurE->getDecl()))
9806         break;
9807 
9808       RelevantExpr = CurE;
9809 
9810       // If we got a reference to a declaration, we should not expect any array
9811       // section before that.
9812       AllowUnitySizeArraySection = false;
9813       AllowWholeSizeArraySection = false;
9814 
9815       // Record the component.
9816       CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
9817           CurE, CurE->getDecl()));
9818       continue;
9819     }
9820 
9821     if (auto *CurE = dyn_cast<MemberExpr>(E)) {
9822       auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
9823 
9824       if (isa<CXXThisExpr>(BaseE))
9825         // We found a base expression: this->Val.
9826         RelevantExpr = CurE;
9827       else
9828         E = BaseE;
9829 
9830       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
9831         SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
9832             << CurE->getSourceRange();
9833         break;
9834       }
9835 
9836       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
9837 
9838       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
9839       //  A bit-field cannot appear in a map clause.
9840       //
9841       if (FD->isBitField()) {
9842         SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_map_clause)
9843             << CurE->getSourceRange();
9844         break;
9845       }
9846 
9847       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9848       //  If the type of a list item is a reference to a type T then the type
9849       //  will be considered to be T for all purposes of this clause.
9850       QualType CurType = BaseE->getType().getNonReferenceType();
9851 
9852       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
9853       //  A list item cannot be a variable that is a member of a structure with
9854       //  a union type.
9855       //
9856       if (auto *RT = CurType->getAs<RecordType>())
9857         if (RT->isUnionType()) {
9858           SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
9859               << CurE->getSourceRange();
9860           break;
9861         }
9862 
9863       // If we got a member expression, we should not expect any array section
9864       // before that:
9865       //
9866       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
9867       //  If a list item is an element of a structure, only the rightmost symbol
9868       //  of the variable reference can be an array section.
9869       //
9870       AllowUnitySizeArraySection = false;
9871       AllowWholeSizeArraySection = false;
9872 
9873       // Record the component.
9874       CurComponents.push_back(
9875           OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
9876       continue;
9877     }
9878 
9879     if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
9880       E = CurE->getBase()->IgnoreParenImpCasts();
9881 
9882       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
9883         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9884             << 0 << CurE->getSourceRange();
9885         break;
9886       }
9887 
9888       // If we got an array subscript that express the whole dimension we
9889       // can have any array expressions before. If it only expressing part of
9890       // the dimension, we can only have unitary-size array expressions.
9891       if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
9892                                                       E->getType()))
9893         AllowWholeSizeArraySection = false;
9894 
9895       // Record the component - we don't have any declaration associated.
9896       CurComponents.push_back(
9897           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
9898       continue;
9899     }
9900 
9901     if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
9902       E = CurE->getBase()->IgnoreParenImpCasts();
9903 
9904       auto CurType =
9905           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
9906 
9907       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
9908       //  If the type of a list item is a reference to a type T then the type
9909       //  will be considered to be T for all purposes of this clause.
9910       if (CurType->isReferenceType())
9911         CurType = CurType->getPointeeType();
9912 
9913       bool IsPointer = CurType->isAnyPointerType();
9914 
9915       if (!IsPointer && !CurType->isArrayType()) {
9916         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
9917             << 0 << CurE->getSourceRange();
9918         break;
9919       }
9920 
9921       bool NotWhole =
9922           CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
9923       bool NotUnity =
9924           CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
9925 
9926       if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
9927         // Any array section is currently allowed.
9928         //
9929         // If this array section refers to the whole dimension we can still
9930         // accept other array sections before this one, except if the base is a
9931         // pointer. Otherwise, only unitary sections are accepted.
9932         if (NotWhole || IsPointer)
9933           AllowWholeSizeArraySection = false;
9934       } else if ((AllowUnitySizeArraySection && NotUnity) ||
9935                  (AllowWholeSizeArraySection && NotWhole)) {
9936         // A unity or whole array section is not allowed and that is not
9937         // compatible with the properties of the current array section.
9938         SemaRef.Diag(
9939             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
9940             << CurE->getSourceRange();
9941         break;
9942       }
9943 
9944       // Record the component - we don't have any declaration associated.
9945       CurComponents.push_back(
9946           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
9947       continue;
9948     }
9949 
9950     // If nothing else worked, this is not a valid map clause expression.
9951     SemaRef.Diag(ELoc,
9952                  diag::err_omp_expected_named_var_member_or_array_expression)
9953         << ERange;
9954     break;
9955   }
9956 
9957   return RelevantExpr;
9958 }
9959 
9960 // Return true if expression E associated with value VD has conflicts with other
9961 // map information.
9962 static bool CheckMapConflicts(
9963     Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
9964     bool CurrentRegionOnly,
9965     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents) {
9966   assert(VD && E);
9967   SourceLocation ELoc = E->getExprLoc();
9968   SourceRange ERange = E->getSourceRange();
9969 
9970   // In order to easily check the conflicts we need to match each component of
9971   // the expression under test with the components of the expressions that are
9972   // already in the stack.
9973 
9974   assert(!CurComponents.empty() && "Map clause expression with no components!");
9975   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
9976          "Map clause expression with unexpected base!");
9977 
9978   // Variables to help detecting enclosing problems in data environment nests.
9979   bool IsEnclosedByDataEnvironmentExpr = false;
9980   const Expr *EnclosingExpr = nullptr;
9981 
9982   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
9983       VD, CurrentRegionOnly,
9984       [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
9985               StackComponents) -> bool {
9986 
9987         assert(!StackComponents.empty() &&
9988                "Map clause expression with no components!");
9989         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
9990                "Map clause expression with unexpected base!");
9991 
9992         // The whole expression in the stack.
9993         auto *RE = StackComponents.front().getAssociatedExpression();
9994 
9995         // Expressions must start from the same base. Here we detect at which
9996         // point both expressions diverge from each other and see if we can
9997         // detect if the memory referred to both expressions is contiguous and
9998         // do not overlap.
9999         auto CI = CurComponents.rbegin();
10000         auto CE = CurComponents.rend();
10001         auto SI = StackComponents.rbegin();
10002         auto SE = StackComponents.rend();
10003         for (; CI != CE && SI != SE; ++CI, ++SI) {
10004 
10005           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10006           //  At most one list item can be an array item derived from a given
10007           //  variable in map clauses of the same construct.
10008           if (CurrentRegionOnly &&
10009               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10010                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10011               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10012                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10013             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
10014                          diag::err_omp_multiple_array_items_in_map_clause)
10015                 << CI->getAssociatedExpression()->getSourceRange();
10016             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10017                          diag::note_used_here)
10018                 << SI->getAssociatedExpression()->getSourceRange();
10019             return true;
10020           }
10021 
10022           // Do both expressions have the same kind?
10023           if (CI->getAssociatedExpression()->getStmtClass() !=
10024               SI->getAssociatedExpression()->getStmtClass())
10025             break;
10026 
10027           // Are we dealing with different variables/fields?
10028           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
10029             break;
10030         }
10031 
10032         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10033         //  List items of map clauses in the same construct must not share
10034         //  original storage.
10035         //
10036         // If the expressions are exactly the same or one is a subset of the
10037         // other, it means they are sharing storage.
10038         if (CI == CE && SI == SE) {
10039           if (CurrentRegionOnly) {
10040             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10041             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10042                 << RE->getSourceRange();
10043             return true;
10044           } else {
10045             // If we find the same expression in the enclosing data environment,
10046             // that is legal.
10047             IsEnclosedByDataEnvironmentExpr = true;
10048             return false;
10049           }
10050         }
10051 
10052         QualType DerivedType =
10053             std::prev(CI)->getAssociatedDeclaration()->getType();
10054         SourceLocation DerivedLoc =
10055             std::prev(CI)->getAssociatedExpression()->getExprLoc();
10056 
10057         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10058         //  If the type of a list item is a reference to a type T then the type
10059         //  will be considered to be T for all purposes of this clause.
10060         DerivedType = DerivedType.getNonReferenceType();
10061 
10062         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10063         //  A variable for which the type is pointer and an array section
10064         //  derived from that variable must not appear as list items of map
10065         //  clauses of the same construct.
10066         //
10067         // Also, cover one of the cases in:
10068         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10069         //  If any part of the original storage of a list item has corresponding
10070         //  storage in the device data environment, all of the original storage
10071         //  must have corresponding storage in the device data environment.
10072         //
10073         if (DerivedType->isAnyPointerType()) {
10074           if (CI == CE || SI == SE) {
10075             SemaRef.Diag(
10076                 DerivedLoc,
10077                 diag::err_omp_pointer_mapped_along_with_derived_section)
10078                 << DerivedLoc;
10079           } else {
10080             assert(CI != CE && SI != SE);
10081             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10082                 << DerivedLoc;
10083           }
10084           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10085               << RE->getSourceRange();
10086           return true;
10087         }
10088 
10089         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10090         //  List items of map clauses in the same construct must not share
10091         //  original storage.
10092         //
10093         // An expression is a subset of the other.
10094         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10095           SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10096           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10097               << RE->getSourceRange();
10098           return true;
10099         }
10100 
10101         // The current expression uses the same base as other expression in the
10102         // data environment but does not contain it completely.
10103         if (!CurrentRegionOnly && SI != SE)
10104           EnclosingExpr = RE;
10105 
10106         // The current expression is a subset of the expression in the data
10107         // environment.
10108         IsEnclosedByDataEnvironmentExpr |=
10109             (!CurrentRegionOnly && CI != CE && SI == SE);
10110 
10111         return false;
10112       });
10113 
10114   if (CurrentRegionOnly)
10115     return FoundError;
10116 
10117   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10118   //  If any part of the original storage of a list item has corresponding
10119   //  storage in the device data environment, all of the original storage must
10120   //  have corresponding storage in the device data environment.
10121   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10122   //  If a list item is an element of a structure, and a different element of
10123   //  the structure has a corresponding list item in the device data environment
10124   //  prior to a task encountering the construct associated with the map clause,
10125   //  then the list item must also have a corresponding list item in the device
10126   //  data environment prior to the task encountering the construct.
10127   //
10128   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10129     SemaRef.Diag(ELoc,
10130                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
10131         << ERange;
10132     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10133         << EnclosingExpr->getSourceRange();
10134     return true;
10135   }
10136 
10137   return FoundError;
10138 }
10139 
10140 OMPClause *
10141 Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
10142                            OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
10143                            SourceLocation MapLoc, SourceLocation ColonLoc,
10144                            ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10145                            SourceLocation LParenLoc, SourceLocation EndLoc) {
10146   SmallVector<Expr *, 4> Vars;
10147 
10148   // Keep track of the mappable components and base declarations in this clause.
10149   // Each entry in the list is going to have a list of components associated. We
10150   // record each set of the components so that we can build the clause later on.
10151   // In the end we should have the same amount of declarations and component
10152   // lists.
10153   OMPClauseMappableExprCommon::MappableExprComponentLists ClauseComponents;
10154   SmallVector<ValueDecl *, 16> ClauseBaseDeclarations;
10155 
10156   ClauseComponents.reserve(VarList.size());
10157   ClauseBaseDeclarations.reserve(VarList.size());
10158 
10159   for (auto &RE : VarList) {
10160     assert(RE && "Null expr in omp map");
10161     if (isa<DependentScopeDeclRefExpr>(RE)) {
10162       // It will be analyzed later.
10163       Vars.push_back(RE);
10164       continue;
10165     }
10166     SourceLocation ELoc = RE->getExprLoc();
10167 
10168     auto *VE = RE->IgnoreParenLValueCasts();
10169 
10170     if (VE->isValueDependent() || VE->isTypeDependent() ||
10171         VE->isInstantiationDependent() ||
10172         VE->containsUnexpandedParameterPack()) {
10173       // We can only analyze this information once the missing information is
10174       // resolved.
10175       Vars.push_back(RE);
10176       continue;
10177     }
10178 
10179     auto *SimpleExpr = RE->IgnoreParenCasts();
10180 
10181     if (!RE->IgnoreParenImpCasts()->isLValue()) {
10182       Diag(ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
10183           << RE->getSourceRange();
10184       continue;
10185     }
10186 
10187     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10188     ValueDecl *CurDeclaration = nullptr;
10189 
10190     // Obtain the array or member expression bases if required. Also, fill the
10191     // components array with all the components identified in the process.
10192     auto *BE = CheckMapClauseExpressionBase(*this, SimpleExpr, CurComponents);
10193     if (!BE)
10194       continue;
10195 
10196     assert(!CurComponents.empty() &&
10197            "Invalid mappable expression information.");
10198 
10199     // For the following checks, we rely on the base declaration which is
10200     // expected to be associated with the last component. The declaration is
10201     // expected to be a variable or a field (if 'this' is being mapped).
10202     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10203     assert(CurDeclaration && "Null decl on map clause.");
10204     assert(
10205         CurDeclaration->isCanonicalDecl() &&
10206         "Expecting components to have associated only canonical declarations.");
10207 
10208     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10209     auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
10210 
10211     assert((VD || FD) && "Only variables or fields are expected here!");
10212     (void)FD;
10213 
10214     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10215     //  threadprivate variables cannot appear in a map clause.
10216     if (VD && DSAStack->isThreadPrivate(VD)) {
10217       auto DVar = DSAStack->getTopDSA(VD, false);
10218       Diag(ELoc, diag::err_omp_threadprivate_in_map);
10219       ReportOriginalDSA(*this, DSAStack, VD, DVar);
10220       continue;
10221     }
10222 
10223     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10224     //  A list item cannot appear in both a map clause and a data-sharing
10225     //  attribute clause on the same construct.
10226     //
10227     // TODO: Implement this check - it cannot currently be tested because of
10228     // missing implementation of the other data sharing clauses in target
10229     // directives.
10230 
10231     // Check conflicts with other map clause expressions. We check the conflicts
10232     // with the current construct separately from the enclosing data
10233     // environment, because the restrictions are different.
10234     if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10235                           /*CurrentRegionOnly=*/true, CurComponents))
10236       break;
10237     if (CheckMapConflicts(*this, DSAStack, CurDeclaration, SimpleExpr,
10238                           /*CurrentRegionOnly=*/false, CurComponents))
10239       break;
10240 
10241     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10242     //  If the type of a list item is a reference to a type T then the type will
10243     //  be considered to be T for all purposes of this clause.
10244     QualType Type = CurDeclaration->getType().getNonReferenceType();
10245 
10246     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10247     //  A list item must have a mappable type.
10248     if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
10249                            DSAStack, Type))
10250       continue;
10251 
10252     // target enter data
10253     // OpenMP [2.10.2, Restrictions, p. 99]
10254     // A map-type must be specified in all map clauses and must be either
10255     // to or alloc.
10256     OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
10257     if (DKind == OMPD_target_enter_data &&
10258         !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10259       Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10260           << (IsMapTypeImplicit ? 1 : 0)
10261           << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10262           << getOpenMPDirectiveName(DKind);
10263       continue;
10264     }
10265 
10266     // target exit_data
10267     // OpenMP [2.10.3, Restrictions, p. 102]
10268     // A map-type must be specified in all map clauses and must be either
10269     // from, release, or delete.
10270     DKind = DSAStack->getCurrentDirective();
10271     if (DKind == OMPD_target_exit_data &&
10272         !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10273           MapType == OMPC_MAP_delete)) {
10274       Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10275           << (IsMapTypeImplicit ? 1 : 0)
10276           << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10277           << getOpenMPDirectiveName(DKind);
10278       continue;
10279     }
10280 
10281     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10282     // A list item cannot appear in both a map clause and a data-sharing
10283     // attribute clause on the same construct
10284     if (DKind == OMPD_target && VD) {
10285       auto DVar = DSAStack->getTopDSA(VD, false);
10286       if (isOpenMPPrivate(DVar.CKind)) {
10287         Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
10288             << getOpenMPClauseName(DVar.CKind)
10289             << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10290         ReportOriginalDSA(*this, DSAStack, CurDeclaration, DVar);
10291         continue;
10292       }
10293     }
10294 
10295     // Save the current expression.
10296     Vars.push_back(RE);
10297 
10298     // Store the components in the stack so that they can be used to check
10299     // against other clauses later on.
10300     DSAStack->addMappableExpressionComponents(CurDeclaration, CurComponents);
10301 
10302     // Save the components and declaration to create the clause. For purposes of
10303     // the clause creation, any component list that has has base 'this' uses
10304     // null has
10305     ClauseComponents.resize(ClauseComponents.size() + 1);
10306     ClauseComponents.back().append(CurComponents.begin(), CurComponents.end());
10307     ClauseBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
10308                                                          : CurDeclaration);
10309   }
10310 
10311   // We need to produce a map clause even if we don't have variables so that
10312   // other diagnostics related with non-existing map clauses are accurate.
10313   return OMPMapClause::Create(
10314       Context, StartLoc, LParenLoc, EndLoc, Vars, ClauseBaseDeclarations,
10315       ClauseComponents, MapTypeModifier, MapType, IsMapTypeImplicit, MapLoc);
10316 }
10317 
10318 QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
10319                                                TypeResult ParsedType) {
10320   assert(ParsedType.isUsable());
10321 
10322   QualType ReductionType = GetTypeFromParser(ParsedType.get());
10323   if (ReductionType.isNull())
10324     return QualType();
10325 
10326   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
10327   // A type name in a declare reduction directive cannot be a function type, an
10328   // array type, a reference type, or a type qualified with const, volatile or
10329   // restrict.
10330   if (ReductionType.hasQualifiers()) {
10331     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
10332     return QualType();
10333   }
10334 
10335   if (ReductionType->isFunctionType()) {
10336     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
10337     return QualType();
10338   }
10339   if (ReductionType->isReferenceType()) {
10340     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
10341     return QualType();
10342   }
10343   if (ReductionType->isArrayType()) {
10344     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
10345     return QualType();
10346   }
10347   return ReductionType;
10348 }
10349 
10350 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
10351     Scope *S, DeclContext *DC, DeclarationName Name,
10352     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
10353     AccessSpecifier AS, Decl *PrevDeclInScope) {
10354   SmallVector<Decl *, 8> Decls;
10355   Decls.reserve(ReductionTypes.size());
10356 
10357   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
10358                       ForRedeclaration);
10359   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
10360   // A reduction-identifier may not be re-declared in the current scope for the
10361   // same type or for a type that is compatible according to the base language
10362   // rules.
10363   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
10364   OMPDeclareReductionDecl *PrevDRD = nullptr;
10365   bool InCompoundScope = true;
10366   if (S != nullptr) {
10367     // Find previous declaration with the same name not referenced in other
10368     // declarations.
10369     FunctionScopeInfo *ParentFn = getEnclosingFunction();
10370     InCompoundScope =
10371         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
10372     LookupName(Lookup, S);
10373     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
10374                          /*AllowInlineNamespace=*/false);
10375     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
10376     auto Filter = Lookup.makeFilter();
10377     while (Filter.hasNext()) {
10378       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
10379       if (InCompoundScope) {
10380         auto I = UsedAsPrevious.find(PrevDecl);
10381         if (I == UsedAsPrevious.end())
10382           UsedAsPrevious[PrevDecl] = false;
10383         if (auto *D = PrevDecl->getPrevDeclInScope())
10384           UsedAsPrevious[D] = true;
10385       }
10386       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
10387           PrevDecl->getLocation();
10388     }
10389     Filter.done();
10390     if (InCompoundScope) {
10391       for (auto &PrevData : UsedAsPrevious) {
10392         if (!PrevData.second) {
10393           PrevDRD = PrevData.first;
10394           break;
10395         }
10396       }
10397     }
10398   } else if (PrevDeclInScope != nullptr) {
10399     auto *PrevDRDInScope = PrevDRD =
10400         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
10401     do {
10402       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
10403           PrevDRDInScope->getLocation();
10404       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
10405     } while (PrevDRDInScope != nullptr);
10406   }
10407   for (auto &TyData : ReductionTypes) {
10408     auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
10409     bool Invalid = false;
10410     if (I != PreviousRedeclTypes.end()) {
10411       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
10412           << TyData.first;
10413       Diag(I->second, diag::note_previous_definition);
10414       Invalid = true;
10415     }
10416     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
10417     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
10418                                                 Name, TyData.first, PrevDRD);
10419     DC->addDecl(DRD);
10420     DRD->setAccess(AS);
10421     Decls.push_back(DRD);
10422     if (Invalid)
10423       DRD->setInvalidDecl();
10424     else
10425       PrevDRD = DRD;
10426   }
10427 
10428   return DeclGroupPtrTy::make(
10429       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
10430 }
10431 
10432 void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
10433   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10434 
10435   // Enter new function scope.
10436   PushFunctionScope();
10437   getCurFunction()->setHasBranchProtectedScope();
10438   getCurFunction()->setHasOMPDeclareReductionCombiner();
10439 
10440   if (S != nullptr)
10441     PushDeclContext(S, DRD);
10442   else
10443     CurContext = DRD;
10444 
10445   PushExpressionEvaluationContext(PotentiallyEvaluated);
10446 
10447   QualType ReductionType = DRD->getType();
10448   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
10449   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
10450   // uses semantics of argument handles by value, but it should be passed by
10451   // reference. C lang does not support references, so pass all parameters as
10452   // pointers.
10453   // Create 'T omp_in;' variable.
10454   auto *OmpInParm =
10455       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
10456   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
10457   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
10458   // uses semantics of argument handles by value, but it should be passed by
10459   // reference. C lang does not support references, so pass all parameters as
10460   // pointers.
10461   // Create 'T omp_out;' variable.
10462   auto *OmpOutParm =
10463       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
10464   if (S != nullptr) {
10465     PushOnScopeChains(OmpInParm, S);
10466     PushOnScopeChains(OmpOutParm, S);
10467   } else {
10468     DRD->addDecl(OmpInParm);
10469     DRD->addDecl(OmpOutParm);
10470   }
10471 }
10472 
10473 void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
10474   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10475   DiscardCleanupsInEvaluationContext();
10476   PopExpressionEvaluationContext();
10477 
10478   PopDeclContext();
10479   PopFunctionScopeInfo();
10480 
10481   if (Combiner != nullptr)
10482     DRD->setCombiner(Combiner);
10483   else
10484     DRD->setInvalidDecl();
10485 }
10486 
10487 void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
10488   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10489 
10490   // Enter new function scope.
10491   PushFunctionScope();
10492   getCurFunction()->setHasBranchProtectedScope();
10493 
10494   if (S != nullptr)
10495     PushDeclContext(S, DRD);
10496   else
10497     CurContext = DRD;
10498 
10499   PushExpressionEvaluationContext(PotentiallyEvaluated);
10500 
10501   QualType ReductionType = DRD->getType();
10502   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
10503   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
10504   // uses semantics of argument handles by value, but it should be passed by
10505   // reference. C lang does not support references, so pass all parameters as
10506   // pointers.
10507   // Create 'T omp_priv;' variable.
10508   auto *OmpPrivParm =
10509       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
10510   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
10511   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
10512   // uses semantics of argument handles by value, but it should be passed by
10513   // reference. C lang does not support references, so pass all parameters as
10514   // pointers.
10515   // Create 'T omp_orig;' variable.
10516   auto *OmpOrigParm =
10517       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
10518   if (S != nullptr) {
10519     PushOnScopeChains(OmpPrivParm, S);
10520     PushOnScopeChains(OmpOrigParm, S);
10521   } else {
10522     DRD->addDecl(OmpPrivParm);
10523     DRD->addDecl(OmpOrigParm);
10524   }
10525 }
10526 
10527 void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
10528                                                      Expr *Initializer) {
10529   auto *DRD = cast<OMPDeclareReductionDecl>(D);
10530   DiscardCleanupsInEvaluationContext();
10531   PopExpressionEvaluationContext();
10532 
10533   PopDeclContext();
10534   PopFunctionScopeInfo();
10535 
10536   if (Initializer != nullptr)
10537     DRD->setInitializer(Initializer);
10538   else
10539     DRD->setInvalidDecl();
10540 }
10541 
10542 Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
10543     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
10544   for (auto *D : DeclReductions.get()) {
10545     if (IsValid) {
10546       auto *DRD = cast<OMPDeclareReductionDecl>(D);
10547       if (S != nullptr)
10548         PushOnScopeChains(DRD, S, /*AddToContext=*/false);
10549     } else
10550       D->setInvalidDecl();
10551   }
10552   return DeclReductions;
10553 }
10554 
10555 OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
10556                                            SourceLocation StartLoc,
10557                                            SourceLocation LParenLoc,
10558                                            SourceLocation EndLoc) {
10559   Expr *ValExpr = NumTeams;
10560 
10561   // OpenMP [teams Constrcut, Restrictions]
10562   // The num_teams expression must evaluate to a positive integer value.
10563   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
10564                                  /*StrictlyPositive=*/true))
10565     return nullptr;
10566 
10567   return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10568 }
10569 
10570 OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
10571                                               SourceLocation StartLoc,
10572                                               SourceLocation LParenLoc,
10573                                               SourceLocation EndLoc) {
10574   Expr *ValExpr = ThreadLimit;
10575 
10576   // OpenMP [teams Constrcut, Restrictions]
10577   // The thread_limit expression must evaluate to a positive integer value.
10578   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
10579                                  /*StrictlyPositive=*/true))
10580     return nullptr;
10581 
10582   return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
10583                                             EndLoc);
10584 }
10585 
10586 OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
10587                                            SourceLocation StartLoc,
10588                                            SourceLocation LParenLoc,
10589                                            SourceLocation EndLoc) {
10590   Expr *ValExpr = Priority;
10591 
10592   // OpenMP [2.9.1, task Constrcut]
10593   // The priority-value is a non-negative numerical scalar expression.
10594   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
10595                                  /*StrictlyPositive=*/false))
10596     return nullptr;
10597 
10598   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10599 }
10600 
10601 OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
10602                                             SourceLocation StartLoc,
10603                                             SourceLocation LParenLoc,
10604                                             SourceLocation EndLoc) {
10605   Expr *ValExpr = Grainsize;
10606 
10607   // OpenMP [2.9.2, taskloop Constrcut]
10608   // The parameter of the grainsize clause must be a positive integer
10609   // expression.
10610   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
10611                                  /*StrictlyPositive=*/true))
10612     return nullptr;
10613 
10614   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10615 }
10616 
10617 OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
10618                                            SourceLocation StartLoc,
10619                                            SourceLocation LParenLoc,
10620                                            SourceLocation EndLoc) {
10621   Expr *ValExpr = NumTasks;
10622 
10623   // OpenMP [2.9.2, taskloop Constrcut]
10624   // The parameter of the num_tasks clause must be a positive integer
10625   // expression.
10626   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
10627                                  /*StrictlyPositive=*/true))
10628     return nullptr;
10629 
10630   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10631 }
10632 
10633 OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
10634                                        SourceLocation LParenLoc,
10635                                        SourceLocation EndLoc) {
10636   // OpenMP [2.13.2, critical construct, Description]
10637   // ... where hint-expression is an integer constant expression that evaluates
10638   // to a valid lock hint.
10639   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
10640   if (HintExpr.isInvalid())
10641     return nullptr;
10642   return new (Context)
10643       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
10644 }
10645 
10646 OMPClause *Sema::ActOnOpenMPDistScheduleClause(
10647     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
10648     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
10649     SourceLocation EndLoc) {
10650   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
10651     std::string Values;
10652     Values += "'";
10653     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
10654     Values += "'";
10655     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
10656         << Values << getOpenMPClauseName(OMPC_dist_schedule);
10657     return nullptr;
10658   }
10659   Expr *ValExpr = ChunkSize;
10660   Stmt *HelperValStmt = nullptr;
10661   if (ChunkSize) {
10662     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
10663         !ChunkSize->isInstantiationDependent() &&
10664         !ChunkSize->containsUnexpandedParameterPack()) {
10665       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
10666       ExprResult Val =
10667           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
10668       if (Val.isInvalid())
10669         return nullptr;
10670 
10671       ValExpr = Val.get();
10672 
10673       // OpenMP [2.7.1, Restrictions]
10674       //  chunk_size must be a loop invariant integer expression with a positive
10675       //  value.
10676       llvm::APSInt Result;
10677       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
10678         if (Result.isSigned() && !Result.isStrictlyPositive()) {
10679           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
10680               << "dist_schedule" << ChunkSize->getSourceRange();
10681           return nullptr;
10682         }
10683       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
10684         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
10685         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
10686         HelperValStmt = buildPreInits(Context, Captures);
10687       }
10688     }
10689   }
10690 
10691   return new (Context)
10692       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
10693                             Kind, ValExpr, HelperValStmt);
10694 }
10695 
10696 OMPClause *Sema::ActOnOpenMPDefaultmapClause(
10697     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
10698     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
10699     SourceLocation KindLoc, SourceLocation EndLoc) {
10700   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
10701   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
10702       Kind != OMPC_DEFAULTMAP_scalar) {
10703     std::string Value;
10704     SourceLocation Loc;
10705     Value += "'";
10706     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
10707       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10708                  OMPC_DEFAULTMAP_MODIFIER_tofrom);
10709       Loc = MLoc;
10710     } else {
10711       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
10712                  OMPC_DEFAULTMAP_scalar);
10713       Loc = KindLoc;
10714     }
10715     Value += "'";
10716     Diag(Loc, diag::err_omp_unexpected_clause_value)
10717         << Value << getOpenMPClauseName(OMPC_defaultmap);
10718     return nullptr;
10719   }
10720 
10721   return new (Context)
10722       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
10723 }
10724 
10725 bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
10726   DeclContext *CurLexicalContext = getCurLexicalContext();
10727   if (!CurLexicalContext->isFileContext() &&
10728       !CurLexicalContext->isExternCContext() &&
10729       !CurLexicalContext->isExternCXXContext()) {
10730     Diag(Loc, diag::err_omp_region_not_file_context);
10731     return false;
10732   }
10733   if (IsInOpenMPDeclareTargetContext) {
10734     Diag(Loc, diag::err_omp_enclosed_declare_target);
10735     return false;
10736   }
10737 
10738   IsInOpenMPDeclareTargetContext = true;
10739   return true;
10740 }
10741 
10742 void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
10743   assert(IsInOpenMPDeclareTargetContext &&
10744          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
10745 
10746   IsInOpenMPDeclareTargetContext = false;
10747 }
10748 
10749 static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
10750                                      Sema &SemaRef, Decl *D) {
10751   if (!D)
10752     return;
10753   Decl *LD = nullptr;
10754   if (isa<TagDecl>(D)) {
10755     LD = cast<TagDecl>(D)->getDefinition();
10756   } else if (isa<VarDecl>(D)) {
10757     LD = cast<VarDecl>(D)->getDefinition();
10758 
10759     // If this is an implicit variable that is legal and we do not need to do
10760     // anything.
10761     if (cast<VarDecl>(D)->isImplicit()) {
10762       D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10763       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10764         ML->DeclarationMarkedOpenMPDeclareTarget(D);
10765       return;
10766     }
10767 
10768   } else if (isa<FunctionDecl>(D)) {
10769     const FunctionDecl *FD = nullptr;
10770     if (cast<FunctionDecl>(D)->hasBody(FD))
10771       LD = const_cast<FunctionDecl *>(FD);
10772 
10773     // If the definition is associated with the current declaration in the
10774     // target region (it can be e.g. a lambda) that is legal and we do not need
10775     // to do anything else.
10776     if (LD == D) {
10777       D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10778       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10779         ML->DeclarationMarkedOpenMPDeclareTarget(D);
10780       return;
10781     }
10782   }
10783   if (!LD)
10784     LD = D;
10785   if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
10786       (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
10787     // Outlined declaration is not declared target.
10788     if (LD->isOutOfLine()) {
10789       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10790       SemaRef.Diag(SL, diag::note_used_here) << SR;
10791     } else {
10792       DeclContext *DC = LD->getDeclContext();
10793       while (DC) {
10794         if (isa<FunctionDecl>(DC) &&
10795             cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
10796           break;
10797         DC = DC->getParent();
10798       }
10799       if (DC)
10800         return;
10801 
10802       // Is not declared in target context.
10803       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
10804       SemaRef.Diag(SL, diag::note_used_here) << SR;
10805     }
10806     // Mark decl as declared target to prevent further diagnostic.
10807     D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(SemaRef.Context));
10808     if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
10809       ML->DeclarationMarkedOpenMPDeclareTarget(D);
10810   }
10811 }
10812 
10813 static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
10814                                    Sema &SemaRef, DSAStackTy *Stack,
10815                                    ValueDecl *VD) {
10816   if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
10817     return true;
10818   if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
10819     return false;
10820   return true;
10821 }
10822 
10823 void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
10824   if (!D || D->isInvalidDecl())
10825     return;
10826   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
10827   SourceLocation SL = E ? E->getLocStart() : D->getLocation();
10828   // 2.10.6: threadprivate variable cannot appear in a declare target directive.
10829   if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
10830     if (DSAStack->isThreadPrivate(VD)) {
10831       Diag(SL, diag::err_omp_threadprivate_in_target);
10832       ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
10833       return;
10834     }
10835   }
10836   if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
10837     // Problem if any with var declared with incomplete type will be reported
10838     // as normal, so no need to check it here.
10839     if ((E || !VD->getType()->isIncompleteType()) &&
10840         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
10841       // Mark decl as declared target to prevent further diagnostic.
10842       if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
10843         VD->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10844         if (ASTMutationListener *ML = Context.getASTMutationListener())
10845           ML->DeclarationMarkedOpenMPDeclareTarget(VD);
10846       }
10847       return;
10848     }
10849   }
10850   if (!E) {
10851     // Checking declaration inside declare target region.
10852     if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
10853         (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
10854       D->addAttr(OMPDeclareTargetDeclAttr::CreateImplicit(Context));
10855       if (ASTMutationListener *ML = Context.getASTMutationListener())
10856         ML->DeclarationMarkedOpenMPDeclareTarget(D);
10857     }
10858     return;
10859   }
10860   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
10861 }
10862