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 "clang/AST/ASTContext.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclCXX.h"
19 #include "clang/AST/DeclOpenMP.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/AST/StmtOpenMP.h"
22 #include "clang/AST/StmtVisitor.h"
23 #include "clang/Basic/OpenMPKinds.h"
24 #include "clang/Basic/TargetInfo.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "clang/Sema/Initialization.h"
27 #include "clang/Sema/Lookup.h"
28 #include "clang/Sema/Scope.h"
29 #include "clang/Sema/ScopeInfo.h"
30 #include "clang/Sema/SemaInternal.h"
31 using namespace clang;
32 
33 //===----------------------------------------------------------------------===//
34 // Stack of data-sharing attributes for variables
35 //===----------------------------------------------------------------------===//
36 
37 namespace {
38 /// \brief Default data sharing attributes, which can be applied to directive.
39 enum DefaultDataSharingAttributes {
40   DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
41   DSA_none = 1 << 0,   /// \brief Default data sharing attribute 'none'.
42   DSA_shared = 1 << 1  /// \brief Default data sharing attribute 'shared'.
43 };
44 
45 template <class T> struct MatchesAny {
46   explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
47   bool operator()(T Kind) {
48     for (auto KindEl : Arr)
49       if (KindEl == Kind)
50         return true;
51     return false;
52   }
53 
54 private:
55   ArrayRef<T> Arr;
56 };
57 struct MatchesAlways {
58   MatchesAlways() {}
59   template <class T> bool operator()(T) { return true; }
60 };
61 
62 typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
63 typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
64 
65 /// \brief Stack for tracking declarations used in OpenMP directives and
66 /// clauses and their data-sharing attributes.
67 class DSAStackTy {
68 public:
69   struct DSAVarData {
70     OpenMPDirectiveKind DKind;
71     OpenMPClauseKind CKind;
72     DeclRefExpr *RefExpr;
73     SourceLocation ImplicitDSALoc;
74     DSAVarData()
75         : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
76           ImplicitDSALoc() {}
77   };
78 
79 private:
80   struct DSAInfo {
81     OpenMPClauseKind Attributes;
82     DeclRefExpr *RefExpr;
83   };
84   typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
85   typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
86   typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
87 
88   struct SharingMapTy {
89     DeclSAMapTy SharingMap;
90     AlignedMapTy AlignedMap;
91     LoopControlVariablesSetTy LCVSet;
92     DefaultDataSharingAttributes DefaultAttr;
93     SourceLocation DefaultAttrLoc;
94     OpenMPDirectiveKind Directive;
95     DeclarationNameInfo DirectiveName;
96     Scope *CurScope;
97     SourceLocation ConstructLoc;
98     bool OrderedRegion;
99     bool NowaitRegion;
100     unsigned CollapseNumber;
101     SourceLocation InnerTeamsRegionLoc;
102     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
103                  Scope *CurScope, SourceLocation Loc)
104         : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
105           Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
106           ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
107           CollapseNumber(1), InnerTeamsRegionLoc() {}
108     SharingMapTy()
109         : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
110           Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
111           ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
112           CollapseNumber(1), InnerTeamsRegionLoc() {}
113   };
114 
115   typedef SmallVector<SharingMapTy, 64> StackTy;
116 
117   /// \brief Stack of used declaration and their data-sharing attributes.
118   StackTy Stack;
119   /// \brief true, if check for DSA must be from parent directive, false, if
120   /// from current directive.
121   OpenMPClauseKind ClauseKindMode;
122   Sema &SemaRef;
123   bool ForceCapturing;
124 
125   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
126 
127   DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
128 
129   /// \brief Checks if the variable is a local for OpenMP region.
130   bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
131 
132 public:
133   explicit DSAStackTy(Sema &S)
134       : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
135         ForceCapturing(false) {}
136 
137   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
138   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
139 
140   bool isForceVarCapturing() const { return ForceCapturing; }
141   void setForceVarCapturing(bool V) { ForceCapturing = V; }
142 
143   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
144             Scope *CurScope, SourceLocation Loc) {
145     Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
146     Stack.back().DefaultAttrLoc = Loc;
147   }
148 
149   void pop() {
150     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
151     Stack.pop_back();
152   }
153 
154   /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
155   /// add it and return NULL; otherwise return previous occurrence's expression
156   /// for diagnostics.
157   DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
158 
159   /// \brief Register specified variable as loop control variable.
160   void addLoopControlVariable(VarDecl *D);
161   /// \brief Check if the specified variable is a loop control variable for
162   /// current region.
163   bool isLoopControlVariable(VarDecl *D);
164 
165   /// \brief Adds explicit data sharing attribute to the specified declaration.
166   void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
167 
168   /// \brief Returns data sharing attributes from top of the stack for the
169   /// specified declaration.
170   DSAVarData getTopDSA(VarDecl *D, bool FromParent);
171   /// \brief Returns data-sharing attributes for the specified declaration.
172   DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
173   /// \brief Checks if the specified variables has data-sharing attributes which
174   /// match specified \a CPred predicate in any directive which matches \a DPred
175   /// predicate.
176   template <class ClausesPredicate, class DirectivesPredicate>
177   DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
178                     DirectivesPredicate DPred, bool FromParent);
179   /// \brief Checks if the specified variables has data-sharing attributes which
180   /// match specified \a CPred predicate in any innermost directive which
181   /// matches \a DPred predicate.
182   template <class ClausesPredicate, class DirectivesPredicate>
183   DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
184                              DirectivesPredicate DPred,
185                              bool FromParent);
186   /// \brief Checks if the specified variables has explicit data-sharing
187   /// attributes which match specified \a CPred predicate at the specified
188   /// OpenMP region.
189   bool hasExplicitDSA(VarDecl *D,
190                       const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
191                       unsigned Level);
192   /// \brief Finds a directive which matches specified \a DPred predicate.
193   template <class NamedDirectivesPredicate>
194   bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
195 
196   /// \brief Returns currently analyzed directive.
197   OpenMPDirectiveKind getCurrentDirective() const {
198     return Stack.back().Directive;
199   }
200   /// \brief Returns parent directive.
201   OpenMPDirectiveKind getParentDirective() const {
202     if (Stack.size() > 2)
203       return Stack[Stack.size() - 2].Directive;
204     return OMPD_unknown;
205   }
206 
207   /// \brief Set default data sharing attribute to none.
208   void setDefaultDSANone(SourceLocation Loc) {
209     Stack.back().DefaultAttr = DSA_none;
210     Stack.back().DefaultAttrLoc = Loc;
211   }
212   /// \brief Set default data sharing attribute to shared.
213   void setDefaultDSAShared(SourceLocation Loc) {
214     Stack.back().DefaultAttr = DSA_shared;
215     Stack.back().DefaultAttrLoc = Loc;
216   }
217 
218   DefaultDataSharingAttributes getDefaultDSA() const {
219     return Stack.back().DefaultAttr;
220   }
221   SourceLocation getDefaultDSALocation() const {
222     return Stack.back().DefaultAttrLoc;
223   }
224 
225   /// \brief Checks if the specified variable is a threadprivate.
226   bool isThreadPrivate(VarDecl *D) {
227     DSAVarData DVar = getTopDSA(D, false);
228     return isOpenMPThreadPrivate(DVar.CKind);
229   }
230 
231   /// \brief Marks current region as ordered (it has an 'ordered' clause).
232   void setOrderedRegion(bool IsOrdered = true) {
233     Stack.back().OrderedRegion = IsOrdered;
234   }
235   /// \brief Returns true, if parent region is ordered (has associated
236   /// 'ordered' clause), false - otherwise.
237   bool isParentOrderedRegion() const {
238     if (Stack.size() > 2)
239       return Stack[Stack.size() - 2].OrderedRegion;
240     return false;
241   }
242   /// \brief Marks current region as nowait (it has a 'nowait' clause).
243   void setNowaitRegion(bool IsNowait = true) {
244     Stack.back().NowaitRegion = IsNowait;
245   }
246   /// \brief Returns true, if parent region is nowait (has associated
247   /// 'nowait' clause), false - otherwise.
248   bool isParentNowaitRegion() const {
249     if (Stack.size() > 2)
250       return Stack[Stack.size() - 2].NowaitRegion;
251     return false;
252   }
253 
254   /// \brief Set collapse value for the region.
255   void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
256   /// \brief Return collapse value for region.
257   unsigned getCollapseNumber() const {
258     return Stack.back().CollapseNumber;
259   }
260 
261   /// \brief Marks current target region as one with closely nested teams
262   /// region.
263   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
264     if (Stack.size() > 2)
265       Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
266   }
267   /// \brief Returns true, if current region has closely nested teams region.
268   bool hasInnerTeamsRegion() const {
269     return getInnerTeamsRegionLoc().isValid();
270   }
271   /// \brief Returns location of the nested teams region (if any).
272   SourceLocation getInnerTeamsRegionLoc() const {
273     if (Stack.size() > 1)
274       return Stack.back().InnerTeamsRegionLoc;
275     return SourceLocation();
276   }
277 
278   Scope *getCurScope() const { return Stack.back().CurScope; }
279   Scope *getCurScope() { return Stack.back().CurScope; }
280   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
281 };
282 bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
283   return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
284          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
285 }
286 } // namespace
287 
288 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
289                                           VarDecl *D) {
290   D = D->getCanonicalDecl();
291   DSAVarData DVar;
292   if (Iter == std::prev(Stack.rend())) {
293     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
294     // in a region but not in construct]
295     //  File-scope or namespace-scope variables referenced in called routines
296     //  in the region are shared unless they appear in a threadprivate
297     //  directive.
298     if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
299       DVar.CKind = OMPC_shared;
300 
301     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
302     // in a region but not in construct]
303     //  Variables with static storage duration that are declared in called
304     //  routines in the region are shared.
305     if (D->hasGlobalStorage())
306       DVar.CKind = OMPC_shared;
307 
308     return DVar;
309   }
310 
311   DVar.DKind = Iter->Directive;
312   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
313   // in a Construct, C/C++, predetermined, p.1]
314   // Variables with automatic storage duration that are declared in a scope
315   // inside the construct are private.
316   if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
317       (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
318     DVar.CKind = OMPC_private;
319     return DVar;
320   }
321 
322   // Explicitly specified attributes and local variables with predetermined
323   // attributes.
324   if (Iter->SharingMap.count(D)) {
325     DVar.RefExpr = Iter->SharingMap[D].RefExpr;
326     DVar.CKind = Iter->SharingMap[D].Attributes;
327     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
328     return DVar;
329   }
330 
331   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
332   // in a Construct, C/C++, implicitly determined, p.1]
333   //  In a parallel or task construct, the data-sharing attributes of these
334   //  variables are determined by the default clause, if present.
335   switch (Iter->DefaultAttr) {
336   case DSA_shared:
337     DVar.CKind = OMPC_shared;
338     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
339     return DVar;
340   case DSA_none:
341     return DVar;
342   case DSA_unspecified:
343     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
344     // in a Construct, implicitly determined, p.2]
345     //  In a parallel construct, if no default clause is present, these
346     //  variables are shared.
347     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
348     if (isOpenMPParallelDirective(DVar.DKind) ||
349         isOpenMPTeamsDirective(DVar.DKind)) {
350       DVar.CKind = OMPC_shared;
351       return DVar;
352     }
353 
354     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
355     // in a Construct, implicitly determined, p.4]
356     //  In a task construct, if no default clause is present, a variable that in
357     //  the enclosing context is determined to be shared by all implicit tasks
358     //  bound to the current team is shared.
359     if (DVar.DKind == OMPD_task) {
360       DSAVarData DVarTemp;
361       for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
362            I != EE; ++I) {
363         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
364         // Referenced
365         // in a Construct, implicitly determined, p.6]
366         //  In a task construct, if no default clause is present, a variable
367         //  whose data-sharing attribute is not determined by the rules above is
368         //  firstprivate.
369         DVarTemp = getDSA(I, D);
370         if (DVarTemp.CKind != OMPC_shared) {
371           DVar.RefExpr = nullptr;
372           DVar.DKind = OMPD_task;
373           DVar.CKind = OMPC_firstprivate;
374           return DVar;
375         }
376         if (isParallelOrTaskRegion(I->Directive))
377           break;
378       }
379       DVar.DKind = OMPD_task;
380       DVar.CKind =
381           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
382       return DVar;
383     }
384   }
385   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
386   // in a Construct, implicitly determined, p.3]
387   //  For constructs other than task, if no default clause is present, these
388   //  variables inherit their data-sharing attributes from the enclosing
389   //  context.
390   return getDSA(std::next(Iter), D);
391 }
392 
393 DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
394   assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
395   D = D->getCanonicalDecl();
396   auto It = Stack.back().AlignedMap.find(D);
397   if (It == Stack.back().AlignedMap.end()) {
398     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
399     Stack.back().AlignedMap[D] = NewDE;
400     return nullptr;
401   } else {
402     assert(It->second && "Unexpected nullptr expr in the aligned map");
403     return It->second;
404   }
405   return nullptr;
406 }
407 
408 void DSAStackTy::addLoopControlVariable(VarDecl *D) {
409   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
410   D = D->getCanonicalDecl();
411   Stack.back().LCVSet.insert(D);
412 }
413 
414 bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
415   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
416   D = D->getCanonicalDecl();
417   return Stack.back().LCVSet.count(D) > 0;
418 }
419 
420 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
421   D = D->getCanonicalDecl();
422   if (A == OMPC_threadprivate) {
423     Stack[0].SharingMap[D].Attributes = A;
424     Stack[0].SharingMap[D].RefExpr = E;
425   } else {
426     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
427     Stack.back().SharingMap[D].Attributes = A;
428     Stack.back().SharingMap[D].RefExpr = E;
429   }
430 }
431 
432 bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
433   D = D->getCanonicalDecl();
434   if (Stack.size() > 2) {
435     reverse_iterator I = Iter, E = std::prev(Stack.rend());
436     Scope *TopScope = nullptr;
437     while (I != E && !isParallelOrTaskRegion(I->Directive)) {
438       ++I;
439     }
440     if (I == E)
441       return false;
442     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
443     Scope *CurScope = getCurScope();
444     while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
445       CurScope = CurScope->getParent();
446     }
447     return CurScope != TopScope;
448   }
449   return false;
450 }
451 
452 /// \brief Build a variable declaration for OpenMP loop iteration variable.
453 static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
454                              StringRef Name) {
455   DeclContext *DC = SemaRef.CurContext;
456   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
457   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
458   VarDecl *Decl =
459       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
460   Decl->setImplicit();
461   return Decl;
462 }
463 
464 static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
465                                      SourceLocation Loc,
466                                      bool RefersToCapture = false) {
467   D->setReferenced();
468   D->markUsed(S.Context);
469   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
470                              SourceLocation(), D, RefersToCapture, Loc, Ty,
471                              VK_LValue);
472 }
473 
474 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
475   D = D->getCanonicalDecl();
476   DSAVarData DVar;
477 
478   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
479   // in a Construct, C/C++, predetermined, p.1]
480   //  Variables appearing in threadprivate directives are threadprivate.
481   if ((D->getTLSKind() != VarDecl::TLS_None &&
482        !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
483          SemaRef.getLangOpts().OpenMPUseTLS &&
484          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
485       (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
486        !D->isLocalVarDecl())) {
487     addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
488                                D->getLocation()),
489            OMPC_threadprivate);
490   }
491   if (Stack[0].SharingMap.count(D)) {
492     DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
493     DVar.CKind = OMPC_threadprivate;
494     return DVar;
495   }
496 
497   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
498   // in a Construct, C/C++, predetermined, p.1]
499   // Variables with automatic storage duration that are declared in a scope
500   // inside the construct are private.
501   OpenMPDirectiveKind Kind =
502       FromParent ? getParentDirective() : getCurrentDirective();
503   auto StartI = std::next(Stack.rbegin());
504   auto EndI = std::prev(Stack.rend());
505   if (FromParent && StartI != EndI) {
506     StartI = std::next(StartI);
507   }
508   if (!isParallelOrTaskRegion(Kind)) {
509     if (isOpenMPLocal(D, StartI) &&
510         ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
511                                   D->getStorageClass() == SC_None)) ||
512          isa<ParmVarDecl>(D))) {
513       DVar.CKind = OMPC_private;
514       return DVar;
515     }
516 
517     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
518     // in a Construct, C/C++, predetermined, p.4]
519     //  Static data members are shared.
520     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
521     // in a Construct, C/C++, predetermined, p.7]
522     //  Variables with static storage duration that are declared in a scope
523     //  inside the construct are shared.
524     if (D->isStaticDataMember() || D->isStaticLocal()) {
525       DSAVarData DVarTemp =
526           hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
527       if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
528         return DVar;
529 
530       DVar.CKind = OMPC_shared;
531       return DVar;
532     }
533   }
534 
535   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
536   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
537   Type = SemaRef.getASTContext().getBaseElementType(Type);
538   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
539   // in a Construct, C/C++, predetermined, p.6]
540   //  Variables with const qualified type having no mutable member are
541   //  shared.
542   CXXRecordDecl *RD =
543       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
544   if (IsConstant &&
545       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
546     // Variables with const-qualified type having no mutable member may be
547     // listed in a firstprivate clause, even if they are static data members.
548     DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
549                                  MatchesAlways(), FromParent);
550     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
551       return DVar;
552 
553     DVar.CKind = OMPC_shared;
554     return DVar;
555   }
556 
557   // Explicitly specified attributes and local variables with predetermined
558   // attributes.
559   auto I = std::prev(StartI);
560   if (I->SharingMap.count(D)) {
561     DVar.RefExpr = I->SharingMap[D].RefExpr;
562     DVar.CKind = I->SharingMap[D].Attributes;
563     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
564   }
565 
566   return DVar;
567 }
568 
569 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
570   D = D->getCanonicalDecl();
571   auto StartI = Stack.rbegin();
572   auto EndI = std::prev(Stack.rend());
573   if (FromParent && StartI != EndI) {
574     StartI = std::next(StartI);
575   }
576   return getDSA(StartI, D);
577 }
578 
579 template <class ClausesPredicate, class DirectivesPredicate>
580 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
581                                           DirectivesPredicate DPred,
582                                           bool FromParent) {
583   D = D->getCanonicalDecl();
584   auto StartI = std::next(Stack.rbegin());
585   auto EndI = std::prev(Stack.rend());
586   if (FromParent && StartI != EndI) {
587     StartI = std::next(StartI);
588   }
589   for (auto I = StartI, EE = EndI; I != EE; ++I) {
590     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
591       continue;
592     DSAVarData DVar = getDSA(I, D);
593     if (CPred(DVar.CKind))
594       return DVar;
595   }
596   return DSAVarData();
597 }
598 
599 template <class ClausesPredicate, class DirectivesPredicate>
600 DSAStackTy::DSAVarData
601 DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
602                             DirectivesPredicate DPred, bool FromParent) {
603   D = D->getCanonicalDecl();
604   auto StartI = std::next(Stack.rbegin());
605   auto EndI = std::prev(Stack.rend());
606   if (FromParent && StartI != EndI) {
607     StartI = std::next(StartI);
608   }
609   for (auto I = StartI, EE = EndI; I != EE; ++I) {
610     if (!DPred(I->Directive))
611       break;
612     DSAVarData DVar = getDSA(I, D);
613     if (CPred(DVar.CKind))
614       return DVar;
615     return DSAVarData();
616   }
617   return DSAVarData();
618 }
619 
620 bool DSAStackTy::hasExplicitDSA(
621     VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
622     unsigned Level) {
623   if (CPred(ClauseKindMode))
624     return true;
625   if (isClauseParsingMode())
626     ++Level;
627   D = D->getCanonicalDecl();
628   auto StartI = Stack.rbegin();
629   auto EndI = std::prev(Stack.rend());
630   if (std::distance(StartI, EndI) <= (int)Level)
631     return false;
632   std::advance(StartI, Level);
633   return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
634          CPred(StartI->SharingMap[D].Attributes);
635 }
636 
637 template <class NamedDirectivesPredicate>
638 bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
639   auto StartI = std::next(Stack.rbegin());
640   auto EndI = std::prev(Stack.rend());
641   if (FromParent && StartI != EndI) {
642     StartI = std::next(StartI);
643   }
644   for (auto I = StartI, EE = EndI; I != EE; ++I) {
645     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
646       return true;
647   }
648   return false;
649 }
650 
651 void Sema::InitDataSharingAttributesStack() {
652   VarDataSharingAttributesStack = new DSAStackTy(*this);
653 }
654 
655 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
656 
657 bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
658   assert(LangOpts.OpenMP && "OpenMP is not allowed");
659   VD = VD->getCanonicalDecl();
660   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
661       (!DSAStack->isClauseParsingMode() ||
662        DSAStack->getParentDirective() != OMPD_unknown)) {
663     if (DSAStack->isLoopControlVariable(VD) ||
664         (VD->hasLocalStorage() &&
665          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
666         DSAStack->isForceVarCapturing())
667       return true;
668     auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
669     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
670       return true;
671     DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
672                                    DSAStack->isClauseParsingMode());
673     return DVarPrivate.CKind != OMPC_unknown;
674   }
675   return false;
676 }
677 
678 bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
679   assert(LangOpts.OpenMP && "OpenMP is not allowed");
680   return DSAStack->hasExplicitDSA(
681       VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
682 }
683 
684 void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
685 
686 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
687                                const DeclarationNameInfo &DirName,
688                                Scope *CurScope, SourceLocation Loc) {
689   DSAStack->push(DKind, DirName, CurScope, Loc);
690   PushExpressionEvaluationContext(PotentiallyEvaluated);
691 }
692 
693 void Sema::StartOpenMPClause(OpenMPClauseKind K) {
694   DSAStack->setClauseParsingMode(K);
695 }
696 
697 void Sema::EndOpenMPClause() {
698   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
699 }
700 
701 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
702   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
703   //  A variable of class type (or array thereof) that appears in a lastprivate
704   //  clause requires an accessible, unambiguous default constructor for the
705   //  class type, unless the list item is also specified in a firstprivate
706   //  clause.
707   if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
708     for (auto *C : D->clauses()) {
709       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
710         SmallVector<Expr *, 8> PrivateCopies;
711         for (auto *DE : Clause->varlists()) {
712           if (DE->isValueDependent() || DE->isTypeDependent()) {
713             PrivateCopies.push_back(nullptr);
714             continue;
715           }
716           auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
717           QualType Type = VD->getType();
718           auto DVar = DSAStack->getTopDSA(VD, false);
719           if (DVar.CKind == OMPC_lastprivate) {
720             // Generate helper private variable and initialize it with the
721             // default value. The address of the original variable is replaced
722             // by the address of the new private variable in CodeGen. This new
723             // variable is not added to IdResolver, so the code in the OpenMP
724             // region uses original variable for proper diagnostics.
725             auto *VDPrivate =
726                 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
727                              VD->getName());
728             ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
729             if (VDPrivate->isInvalidDecl())
730               continue;
731             PrivateCopies.push_back(buildDeclRefExpr(
732                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
733           } else {
734             // The variable is also a firstprivate, so initialization sequence
735             // for private copy is generated already.
736             PrivateCopies.push_back(nullptr);
737           }
738         }
739         // Set initializers to private copies if no errors were found.
740         if (PrivateCopies.size() == Clause->varlist_size()) {
741           Clause->setPrivateCopies(PrivateCopies);
742         }
743       }
744     }
745   }
746 
747   DSAStack->pop();
748   DiscardCleanupsInEvaluationContext();
749   PopExpressionEvaluationContext();
750 }
751 
752 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
753                                      Expr *NumIterations, Sema &SemaRef,
754                                      Scope *S);
755 
756 namespace {
757 
758 class VarDeclFilterCCC : public CorrectionCandidateCallback {
759 private:
760   Sema &SemaRef;
761 
762 public:
763   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
764   bool ValidateCandidate(const TypoCorrection &Candidate) override {
765     NamedDecl *ND = Candidate.getCorrectionDecl();
766     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
767       return VD->hasGlobalStorage() &&
768              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
769                                    SemaRef.getCurScope());
770     }
771     return false;
772   }
773 };
774 } // namespace
775 
776 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
777                                          CXXScopeSpec &ScopeSpec,
778                                          const DeclarationNameInfo &Id) {
779   LookupResult Lookup(*this, Id, LookupOrdinaryName);
780   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
781 
782   if (Lookup.isAmbiguous())
783     return ExprError();
784 
785   VarDecl *VD;
786   if (!Lookup.isSingleResult()) {
787     if (TypoCorrection Corrected = CorrectTypo(
788             Id, LookupOrdinaryName, CurScope, nullptr,
789             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
790       diagnoseTypo(Corrected,
791                    PDiag(Lookup.empty()
792                              ? diag::err_undeclared_var_use_suggest
793                              : diag::err_omp_expected_var_arg_suggest)
794                        << Id.getName());
795       VD = Corrected.getCorrectionDeclAs<VarDecl>();
796     } else {
797       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
798                                        : diag::err_omp_expected_var_arg)
799           << Id.getName();
800       return ExprError();
801     }
802   } else {
803     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
804       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
805       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
806       return ExprError();
807     }
808   }
809   Lookup.suppressDiagnostics();
810 
811   // OpenMP [2.9.2, Syntax, C/C++]
812   //   Variables must be file-scope, namespace-scope, or static block-scope.
813   if (!VD->hasGlobalStorage()) {
814     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
815         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
816     bool IsDecl =
817         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
818     Diag(VD->getLocation(),
819          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
820         << VD;
821     return ExprError();
822   }
823 
824   VarDecl *CanonicalVD = VD->getCanonicalDecl();
825   NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
826   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
827   //   A threadprivate directive for file-scope variables must appear outside
828   //   any definition or declaration.
829   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
830       !getCurLexicalContext()->isTranslationUnit()) {
831     Diag(Id.getLoc(), diag::err_omp_var_scope)
832         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
833     bool IsDecl =
834         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
835     Diag(VD->getLocation(),
836          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
837         << VD;
838     return ExprError();
839   }
840   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
841   //   A threadprivate directive for static class member variables must appear
842   //   in the class definition, in the same scope in which the member
843   //   variables are declared.
844   if (CanonicalVD->isStaticDataMember() &&
845       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
846     Diag(Id.getLoc(), diag::err_omp_var_scope)
847         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
848     bool IsDecl =
849         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
850     Diag(VD->getLocation(),
851          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
852         << VD;
853     return ExprError();
854   }
855   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
856   //   A threadprivate directive for namespace-scope variables must appear
857   //   outside any definition or declaration other than the namespace
858   //   definition itself.
859   if (CanonicalVD->getDeclContext()->isNamespace() &&
860       (!getCurLexicalContext()->isFileContext() ||
861        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
862     Diag(Id.getLoc(), diag::err_omp_var_scope)
863         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
864     bool IsDecl =
865         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
866     Diag(VD->getLocation(),
867          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
868         << VD;
869     return ExprError();
870   }
871   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
872   //   A threadprivate directive for static block-scope variables must appear
873   //   in the scope of the variable and not in a nested scope.
874   if (CanonicalVD->isStaticLocal() && CurScope &&
875       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
876     Diag(Id.getLoc(), diag::err_omp_var_scope)
877         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
878     bool IsDecl =
879         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
880     Diag(VD->getLocation(),
881          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
882         << VD;
883     return ExprError();
884   }
885 
886   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
887   //   A threadprivate directive must lexically precede all references to any
888   //   of the variables in its list.
889   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
890     Diag(Id.getLoc(), diag::err_omp_var_used)
891         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
892     return ExprError();
893   }
894 
895   QualType ExprType = VD->getType().getNonReferenceType();
896   ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
897   return DE;
898 }
899 
900 Sema::DeclGroupPtrTy
901 Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
902                                         ArrayRef<Expr *> VarList) {
903   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
904     CurContext->addDecl(D);
905     return DeclGroupPtrTy::make(DeclGroupRef(D));
906   }
907   return DeclGroupPtrTy();
908 }
909 
910 namespace {
911 class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
912   Sema &SemaRef;
913 
914 public:
915   bool VisitDeclRefExpr(const DeclRefExpr *E) {
916     if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
917       if (VD->hasLocalStorage()) {
918         SemaRef.Diag(E->getLocStart(),
919                      diag::err_omp_local_var_in_threadprivate_init)
920             << E->getSourceRange();
921         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
922             << VD << VD->getSourceRange();
923         return true;
924       }
925     }
926     return false;
927   }
928   bool VisitStmt(const Stmt *S) {
929     for (auto Child : S->children()) {
930       if (Child && Visit(Child))
931         return true;
932     }
933     return false;
934   }
935   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
936 };
937 } // namespace
938 
939 OMPThreadPrivateDecl *
940 Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
941   SmallVector<Expr *, 8> Vars;
942   for (auto &RefExpr : VarList) {
943     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
944     VarDecl *VD = cast<VarDecl>(DE->getDecl());
945     SourceLocation ILoc = DE->getExprLoc();
946 
947     QualType QType = VD->getType();
948     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
949       // It will be analyzed later.
950       Vars.push_back(DE);
951       continue;
952     }
953 
954     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
955     //   A threadprivate variable must not have an incomplete type.
956     if (RequireCompleteType(ILoc, VD->getType(),
957                             diag::err_omp_threadprivate_incomplete_type)) {
958       continue;
959     }
960 
961     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
962     //   A threadprivate variable must not have a reference type.
963     if (VD->getType()->isReferenceType()) {
964       Diag(ILoc, diag::err_omp_ref_type_arg)
965           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
966       bool IsDecl =
967           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
968       Diag(VD->getLocation(),
969            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
970           << VD;
971       continue;
972     }
973 
974     // Check if this is a TLS variable. If TLS is not being supported, produce
975     // the corresponding diagnostic.
976     if ((VD->getTLSKind() != VarDecl::TLS_None &&
977          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
978            getLangOpts().OpenMPUseTLS &&
979            getASTContext().getTargetInfo().isTLSSupported())) ||
980         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
981          !VD->isLocalVarDecl())) {
982       Diag(ILoc, diag::err_omp_var_thread_local)
983           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
984       bool IsDecl =
985           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
986       Diag(VD->getLocation(),
987            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
988           << VD;
989       continue;
990     }
991 
992     // Check if initial value of threadprivate variable reference variable with
993     // local storage (it is not supported by runtime).
994     if (auto Init = VD->getAnyInitializer()) {
995       LocalVarRefChecker Checker(*this);
996       if (Checker.Visit(Init))
997         continue;
998     }
999 
1000     Vars.push_back(RefExpr);
1001     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1002     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1003         Context, SourceRange(Loc, Loc)));
1004     if (auto *ML = Context.getASTMutationListener())
1005       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1006   }
1007   OMPThreadPrivateDecl *D = nullptr;
1008   if (!Vars.empty()) {
1009     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1010                                      Vars);
1011     D->setAccess(AS_public);
1012   }
1013   return D;
1014 }
1015 
1016 static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1017                               const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1018                               bool IsLoopIterVar = false) {
1019   if (DVar.RefExpr) {
1020     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1021         << getOpenMPClauseName(DVar.CKind);
1022     return;
1023   }
1024   enum {
1025     PDSA_StaticMemberShared,
1026     PDSA_StaticLocalVarShared,
1027     PDSA_LoopIterVarPrivate,
1028     PDSA_LoopIterVarLinear,
1029     PDSA_LoopIterVarLastprivate,
1030     PDSA_ConstVarShared,
1031     PDSA_GlobalVarShared,
1032     PDSA_TaskVarFirstprivate,
1033     PDSA_LocalVarPrivate,
1034     PDSA_Implicit
1035   } Reason = PDSA_Implicit;
1036   bool ReportHint = false;
1037   auto ReportLoc = VD->getLocation();
1038   if (IsLoopIterVar) {
1039     if (DVar.CKind == OMPC_private)
1040       Reason = PDSA_LoopIterVarPrivate;
1041     else if (DVar.CKind == OMPC_lastprivate)
1042       Reason = PDSA_LoopIterVarLastprivate;
1043     else
1044       Reason = PDSA_LoopIterVarLinear;
1045   } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1046     Reason = PDSA_TaskVarFirstprivate;
1047     ReportLoc = DVar.ImplicitDSALoc;
1048   } else if (VD->isStaticLocal())
1049     Reason = PDSA_StaticLocalVarShared;
1050   else if (VD->isStaticDataMember())
1051     Reason = PDSA_StaticMemberShared;
1052   else if (VD->isFileVarDecl())
1053     Reason = PDSA_GlobalVarShared;
1054   else if (VD->getType().isConstant(SemaRef.getASTContext()))
1055     Reason = PDSA_ConstVarShared;
1056   else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1057     ReportHint = true;
1058     Reason = PDSA_LocalVarPrivate;
1059   }
1060   if (Reason != PDSA_Implicit) {
1061     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1062         << Reason << ReportHint
1063         << getOpenMPDirectiveName(Stack->getCurrentDirective());
1064   } else if (DVar.ImplicitDSALoc.isValid()) {
1065     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1066         << getOpenMPClauseName(DVar.CKind);
1067   }
1068 }
1069 
1070 namespace {
1071 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1072   DSAStackTy *Stack;
1073   Sema &SemaRef;
1074   bool ErrorFound;
1075   CapturedStmt *CS;
1076   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1077   llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
1078 
1079 public:
1080   void VisitDeclRefExpr(DeclRefExpr *E) {
1081     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1082       // Skip internally declared variables.
1083       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1084         return;
1085 
1086       auto DVar = Stack->getTopDSA(VD, false);
1087       // Check if the variable has explicit DSA set and stop analysis if it so.
1088       if (DVar.RefExpr) return;
1089 
1090       auto ELoc = E->getExprLoc();
1091       auto DKind = Stack->getCurrentDirective();
1092       // The default(none) clause requires that each variable that is referenced
1093       // in the construct, and does not have a predetermined data-sharing
1094       // attribute, must have its data-sharing attribute explicitly determined
1095       // by being listed in a data-sharing attribute clause.
1096       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1097           isParallelOrTaskRegion(DKind) &&
1098           VarsWithInheritedDSA.count(VD) == 0) {
1099         VarsWithInheritedDSA[VD] = E;
1100         return;
1101       }
1102 
1103       // OpenMP [2.9.3.6, Restrictions, p.2]
1104       //  A list item that appears in a reduction clause of the innermost
1105       //  enclosing worksharing or parallel construct may not be accessed in an
1106       //  explicit task.
1107       DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
1108                                     [](OpenMPDirectiveKind K) -> bool {
1109                                       return isOpenMPParallelDirective(K) ||
1110                                              isOpenMPWorksharingDirective(K) ||
1111                                              isOpenMPTeamsDirective(K);
1112                                     },
1113                                     false);
1114       if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1115         ErrorFound = true;
1116         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1117         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1118         return;
1119       }
1120 
1121       // Define implicit data-sharing attributes for task.
1122       DVar = Stack->getImplicitDSA(VD, false);
1123       if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
1124         ImplicitFirstprivate.push_back(E);
1125     }
1126   }
1127   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1128     for (auto *C : S->clauses()) {
1129       // Skip analysis of arguments of implicitly defined firstprivate clause
1130       // for task directives.
1131       if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1132         for (auto *CC : C->children()) {
1133           if (CC)
1134             Visit(CC);
1135         }
1136     }
1137   }
1138   void VisitStmt(Stmt *S) {
1139     for (auto *C : S->children()) {
1140       if (C && !isa<OMPExecutableDirective>(C))
1141         Visit(C);
1142     }
1143   }
1144 
1145   bool isErrorFound() { return ErrorFound; }
1146   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
1147   llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1148     return VarsWithInheritedDSA;
1149   }
1150 
1151   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1152       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1153 };
1154 } // namespace
1155 
1156 void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1157   switch (DKind) {
1158   case OMPD_parallel: {
1159     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1160     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1161     Sema::CapturedParamNameType Params[] = {
1162         std::make_pair(".global_tid.", KmpInt32PtrTy),
1163         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1164         std::make_pair(StringRef(), QualType()) // __context with shared vars
1165     };
1166     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1167                              Params);
1168     break;
1169   }
1170   case OMPD_simd: {
1171     Sema::CapturedParamNameType Params[] = {
1172         std::make_pair(StringRef(), QualType()) // __context with shared vars
1173     };
1174     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1175                              Params);
1176     break;
1177   }
1178   case OMPD_for: {
1179     Sema::CapturedParamNameType Params[] = {
1180         std::make_pair(StringRef(), QualType()) // __context with shared vars
1181     };
1182     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1183                              Params);
1184     break;
1185   }
1186   case OMPD_for_simd: {
1187     Sema::CapturedParamNameType Params[] = {
1188         std::make_pair(StringRef(), QualType()) // __context with shared vars
1189     };
1190     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1191                              Params);
1192     break;
1193   }
1194   case OMPD_sections: {
1195     Sema::CapturedParamNameType Params[] = {
1196         std::make_pair(StringRef(), QualType()) // __context with shared vars
1197     };
1198     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1199                              Params);
1200     break;
1201   }
1202   case OMPD_section: {
1203     Sema::CapturedParamNameType Params[] = {
1204         std::make_pair(StringRef(), QualType()) // __context with shared vars
1205     };
1206     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1207                              Params);
1208     break;
1209   }
1210   case OMPD_single: {
1211     Sema::CapturedParamNameType Params[] = {
1212         std::make_pair(StringRef(), QualType()) // __context with shared vars
1213     };
1214     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1215                              Params);
1216     break;
1217   }
1218   case OMPD_master: {
1219     Sema::CapturedParamNameType Params[] = {
1220         std::make_pair(StringRef(), QualType()) // __context with shared vars
1221     };
1222     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1223                              Params);
1224     break;
1225   }
1226   case OMPD_critical: {
1227     Sema::CapturedParamNameType Params[] = {
1228         std::make_pair(StringRef(), QualType()) // __context with shared vars
1229     };
1230     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1231                              Params);
1232     break;
1233   }
1234   case OMPD_parallel_for: {
1235     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1236     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1237     Sema::CapturedParamNameType Params[] = {
1238         std::make_pair(".global_tid.", KmpInt32PtrTy),
1239         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1240         std::make_pair(StringRef(), QualType()) // __context with shared vars
1241     };
1242     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1243                              Params);
1244     break;
1245   }
1246   case OMPD_parallel_for_simd: {
1247     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1248     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1249     Sema::CapturedParamNameType Params[] = {
1250         std::make_pair(".global_tid.", KmpInt32PtrTy),
1251         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1252         std::make_pair(StringRef(), QualType()) // __context with shared vars
1253     };
1254     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1255                              Params);
1256     break;
1257   }
1258   case OMPD_parallel_sections: {
1259     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1260     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1261     Sema::CapturedParamNameType Params[] = {
1262         std::make_pair(".global_tid.", KmpInt32PtrTy),
1263         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1264         std::make_pair(StringRef(), QualType()) // __context with shared vars
1265     };
1266     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1267                              Params);
1268     break;
1269   }
1270   case OMPD_task: {
1271     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1272     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1273     FunctionProtoType::ExtProtoInfo EPI;
1274     EPI.Variadic = true;
1275     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1276     Sema::CapturedParamNameType Params[] = {
1277         std::make_pair(".global_tid.", KmpInt32Ty),
1278         std::make_pair(".part_id.", KmpInt32Ty),
1279         std::make_pair(".privates.",
1280                        Context.VoidPtrTy.withConst().withRestrict()),
1281         std::make_pair(
1282             ".copy_fn.",
1283             Context.getPointerType(CopyFnType).withConst().withRestrict()),
1284         std::make_pair(StringRef(), QualType()) // __context with shared vars
1285     };
1286     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1287                              Params);
1288     // Mark this captured region as inlined, because we don't use outlined
1289     // function directly.
1290     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1291         AlwaysInlineAttr::CreateImplicit(
1292             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1293     break;
1294   }
1295   case OMPD_ordered: {
1296     Sema::CapturedParamNameType Params[] = {
1297         std::make_pair(StringRef(), QualType()) // __context with shared vars
1298     };
1299     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1300                              Params);
1301     break;
1302   }
1303   case OMPD_atomic: {
1304     Sema::CapturedParamNameType Params[] = {
1305         std::make_pair(StringRef(), QualType()) // __context with shared vars
1306     };
1307     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1308                              Params);
1309     break;
1310   }
1311   case OMPD_target_data:
1312   case OMPD_target: {
1313     Sema::CapturedParamNameType Params[] = {
1314         std::make_pair(StringRef(), QualType()) // __context with shared vars
1315     };
1316     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1317                              Params);
1318     break;
1319   }
1320   case OMPD_teams: {
1321     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1322     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1323     Sema::CapturedParamNameType Params[] = {
1324         std::make_pair(".global_tid.", KmpInt32PtrTy),
1325         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1326         std::make_pair(StringRef(), QualType()) // __context with shared vars
1327     };
1328     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1329                              Params);
1330     break;
1331   }
1332   case OMPD_taskgroup: {
1333     Sema::CapturedParamNameType Params[] = {
1334         std::make_pair(StringRef(), QualType()) // __context with shared vars
1335     };
1336     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1337                              Params);
1338     break;
1339   }
1340   case OMPD_threadprivate:
1341   case OMPD_taskyield:
1342   case OMPD_barrier:
1343   case OMPD_taskwait:
1344   case OMPD_cancellation_point:
1345   case OMPD_cancel:
1346   case OMPD_flush:
1347     llvm_unreachable("OpenMP Directive is not allowed");
1348   case OMPD_unknown:
1349     llvm_unreachable("Unknown OpenMP directive");
1350   }
1351 }
1352 
1353 StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1354                                       ArrayRef<OMPClause *> Clauses) {
1355   if (!S.isUsable()) {
1356     ActOnCapturedRegionError();
1357     return StmtError();
1358   }
1359   // This is required for proper codegen.
1360   for (auto *Clause : Clauses) {
1361     if (isOpenMPPrivate(Clause->getClauseKind()) ||
1362         Clause->getClauseKind() == OMPC_copyprivate ||
1363         (getLangOpts().OpenMPUseTLS &&
1364          getASTContext().getTargetInfo().isTLSSupported() &&
1365          Clause->getClauseKind() == OMPC_copyin)) {
1366       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
1367       // Mark all variables in private list clauses as used in inner region.
1368       for (auto *VarRef : Clause->children()) {
1369         if (auto *E = cast_or_null<Expr>(VarRef)) {
1370           MarkDeclarationsReferencedInExpr(E);
1371         }
1372       }
1373       DSAStack->setForceVarCapturing(/*V=*/false);
1374     } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1375                Clause->getClauseKind() == OMPC_schedule) {
1376       // Mark all variables in private list clauses as used in inner region.
1377       // Required for proper codegen of combined directives.
1378       // TODO: add processing for other clauses.
1379       if (auto *E = cast_or_null<Expr>(
1380               cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1381           MarkDeclarationsReferencedInExpr(E);
1382         }
1383     }
1384   }
1385   return ActOnCapturedRegionEnd(S.get());
1386 }
1387 
1388 static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1389                                   OpenMPDirectiveKind CurrentRegion,
1390                                   const DeclarationNameInfo &CurrentName,
1391                                   OpenMPDirectiveKind CancelRegion,
1392                                   SourceLocation StartLoc) {
1393   // Allowed nesting of constructs
1394   // +------------------+-----------------+------------------------------------+
1395   // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1396   // +------------------+-----------------+------------------------------------+
1397   // | parallel         | parallel        | *                                  |
1398   // | parallel         | for             | *                                  |
1399   // | parallel         | for simd        | *                                  |
1400   // | parallel         | master          | *                                  |
1401   // | parallel         | critical        | *                                  |
1402   // | parallel         | simd            | *                                  |
1403   // | parallel         | sections        | *                                  |
1404   // | parallel         | section         | +                                  |
1405   // | parallel         | single          | *                                  |
1406   // | parallel         | parallel for    | *                                  |
1407   // | parallel         |parallel for simd| *                                  |
1408   // | parallel         |parallel sections| *                                  |
1409   // | parallel         | task            | *                                  |
1410   // | parallel         | taskyield       | *                                  |
1411   // | parallel         | barrier         | *                                  |
1412   // | parallel         | taskwait        | *                                  |
1413   // | parallel         | taskgroup       | *                                  |
1414   // | parallel         | flush           | *                                  |
1415   // | parallel         | ordered         | +                                  |
1416   // | parallel         | atomic          | *                                  |
1417   // | parallel         | target          | *                                  |
1418   // | parallel         | teams           | +                                  |
1419   // | parallel         | cancellation    |                                    |
1420   // |                  | point           | !                                  |
1421   // | parallel         | cancel          | !                                  |
1422   // +------------------+-----------------+------------------------------------+
1423   // | for              | parallel        | *                                  |
1424   // | for              | for             | +                                  |
1425   // | for              | for simd        | +                                  |
1426   // | for              | master          | +                                  |
1427   // | for              | critical        | *                                  |
1428   // | for              | simd            | *                                  |
1429   // | for              | sections        | +                                  |
1430   // | for              | section         | +                                  |
1431   // | for              | single          | +                                  |
1432   // | for              | parallel for    | *                                  |
1433   // | for              |parallel for simd| *                                  |
1434   // | for              |parallel sections| *                                  |
1435   // | for              | task            | *                                  |
1436   // | for              | taskyield       | *                                  |
1437   // | for              | barrier         | +                                  |
1438   // | for              | taskwait        | *                                  |
1439   // | for              | taskgroup       | *                                  |
1440   // | for              | flush           | *                                  |
1441   // | for              | ordered         | * (if construct is ordered)        |
1442   // | for              | atomic          | *                                  |
1443   // | for              | target          | *                                  |
1444   // | for              | teams           | +                                  |
1445   // | for              | cancellation    |                                    |
1446   // |                  | point           | !                                  |
1447   // | for              | cancel          | !                                  |
1448   // +------------------+-----------------+------------------------------------+
1449   // | master           | parallel        | *                                  |
1450   // | master           | for             | +                                  |
1451   // | master           | for simd        | +                                  |
1452   // | master           | master          | *                                  |
1453   // | master           | critical        | *                                  |
1454   // | master           | simd            | *                                  |
1455   // | master           | sections        | +                                  |
1456   // | master           | section         | +                                  |
1457   // | master           | single          | +                                  |
1458   // | master           | parallel for    | *                                  |
1459   // | master           |parallel for simd| *                                  |
1460   // | master           |parallel sections| *                                  |
1461   // | master           | task            | *                                  |
1462   // | master           | taskyield       | *                                  |
1463   // | master           | barrier         | +                                  |
1464   // | master           | taskwait        | *                                  |
1465   // | master           | taskgroup       | *                                  |
1466   // | master           | flush           | *                                  |
1467   // | master           | ordered         | +                                  |
1468   // | master           | atomic          | *                                  |
1469   // | master           | target          | *                                  |
1470   // | master           | teams           | +                                  |
1471   // | master           | cancellation    |                                    |
1472   // |                  | point           |                                    |
1473   // | master           | cancel          |                                    |
1474   // +------------------+-----------------+------------------------------------+
1475   // | critical         | parallel        | *                                  |
1476   // | critical         | for             | +                                  |
1477   // | critical         | for simd        | +                                  |
1478   // | critical         | master          | *                                  |
1479   // | critical         | critical        | * (should have different names)    |
1480   // | critical         | simd            | *                                  |
1481   // | critical         | sections        | +                                  |
1482   // | critical         | section         | +                                  |
1483   // | critical         | single          | +                                  |
1484   // | critical         | parallel for    | *                                  |
1485   // | critical         |parallel for simd| *                                  |
1486   // | critical         |parallel sections| *                                  |
1487   // | critical         | task            | *                                  |
1488   // | critical         | taskyield       | *                                  |
1489   // | critical         | barrier         | +                                  |
1490   // | critical         | taskwait        | *                                  |
1491   // | critical         | taskgroup       | *                                  |
1492   // | critical         | ordered         | +                                  |
1493   // | critical         | atomic          | *                                  |
1494   // | critical         | target          | *                                  |
1495   // | critical         | teams           | +                                  |
1496   // | critical         | cancellation    |                                    |
1497   // |                  | point           |                                    |
1498   // | critical         | cancel          |                                    |
1499   // +------------------+-----------------+------------------------------------+
1500   // | simd             | parallel        |                                    |
1501   // | simd             | for             |                                    |
1502   // | simd             | for simd        |                                    |
1503   // | simd             | master          |                                    |
1504   // | simd             | critical        |                                    |
1505   // | simd             | simd            |                                    |
1506   // | simd             | sections        |                                    |
1507   // | simd             | section         |                                    |
1508   // | simd             | single          |                                    |
1509   // | simd             | parallel for    |                                    |
1510   // | simd             |parallel for simd|                                    |
1511   // | simd             |parallel sections|                                    |
1512   // | simd             | task            |                                    |
1513   // | simd             | taskyield       |                                    |
1514   // | simd             | barrier         |                                    |
1515   // | simd             | taskwait        |                                    |
1516   // | simd             | taskgroup       |                                    |
1517   // | simd             | flush           |                                    |
1518   // | simd             | ordered         |                                    |
1519   // | simd             | atomic          |                                    |
1520   // | simd             | target          |                                    |
1521   // | simd             | teams           |                                    |
1522   // | simd             | cancellation    |                                    |
1523   // |                  | point           |                                    |
1524   // | simd             | cancel          |                                    |
1525   // +------------------+-----------------+------------------------------------+
1526   // | for simd         | parallel        |                                    |
1527   // | for simd         | for             |                                    |
1528   // | for simd         | for simd        |                                    |
1529   // | for simd         | master          |                                    |
1530   // | for simd         | critical        |                                    |
1531   // | for simd         | simd            |                                    |
1532   // | for simd         | sections        |                                    |
1533   // | for simd         | section         |                                    |
1534   // | for simd         | single          |                                    |
1535   // | for simd         | parallel for    |                                    |
1536   // | for simd         |parallel for simd|                                    |
1537   // | for simd         |parallel sections|                                    |
1538   // | for simd         | task            |                                    |
1539   // | for simd         | taskyield       |                                    |
1540   // | for simd         | barrier         |                                    |
1541   // | for simd         | taskwait        |                                    |
1542   // | for simd         | taskgroup       |                                    |
1543   // | for simd         | flush           |                                    |
1544   // | for simd         | ordered         |                                    |
1545   // | for simd         | atomic          |                                    |
1546   // | for simd         | target          |                                    |
1547   // | for simd         | teams           |                                    |
1548   // | for simd         | cancellation    |                                    |
1549   // |                  | point           |                                    |
1550   // | for simd         | cancel          |                                    |
1551   // +------------------+-----------------+------------------------------------+
1552   // | parallel for simd| parallel        |                                    |
1553   // | parallel for simd| for             |                                    |
1554   // | parallel for simd| for simd        |                                    |
1555   // | parallel for simd| master          |                                    |
1556   // | parallel for simd| critical        |                                    |
1557   // | parallel for simd| simd            |                                    |
1558   // | parallel for simd| sections        |                                    |
1559   // | parallel for simd| section         |                                    |
1560   // | parallel for simd| single          |                                    |
1561   // | parallel for simd| parallel for    |                                    |
1562   // | parallel for simd|parallel for simd|                                    |
1563   // | parallel for simd|parallel sections|                                    |
1564   // | parallel for simd| task            |                                    |
1565   // | parallel for simd| taskyield       |                                    |
1566   // | parallel for simd| barrier         |                                    |
1567   // | parallel for simd| taskwait        |                                    |
1568   // | parallel for simd| taskgroup       |                                    |
1569   // | parallel for simd| flush           |                                    |
1570   // | parallel for simd| ordered         |                                    |
1571   // | parallel for simd| atomic          |                                    |
1572   // | parallel for simd| target          |                                    |
1573   // | parallel for simd| teams           |                                    |
1574   // | parallel for simd| cancellation    |                                    |
1575   // |                  | point           |                                    |
1576   // | parallel for simd| cancel          |                                    |
1577   // +------------------+-----------------+------------------------------------+
1578   // | sections         | parallel        | *                                  |
1579   // | sections         | for             | +                                  |
1580   // | sections         | for simd        | +                                  |
1581   // | sections         | master          | +                                  |
1582   // | sections         | critical        | *                                  |
1583   // | sections         | simd            | *                                  |
1584   // | sections         | sections        | +                                  |
1585   // | sections         | section         | *                                  |
1586   // | sections         | single          | +                                  |
1587   // | sections         | parallel for    | *                                  |
1588   // | sections         |parallel for simd| *                                  |
1589   // | sections         |parallel sections| *                                  |
1590   // | sections         | task            | *                                  |
1591   // | sections         | taskyield       | *                                  |
1592   // | sections         | barrier         | +                                  |
1593   // | sections         | taskwait        | *                                  |
1594   // | sections         | taskgroup       | *                                  |
1595   // | sections         | flush           | *                                  |
1596   // | sections         | ordered         | +                                  |
1597   // | sections         | atomic          | *                                  |
1598   // | sections         | target          | *                                  |
1599   // | sections         | teams           | +                                  |
1600   // | sections         | cancellation    |                                    |
1601   // |                  | point           | !                                  |
1602   // | sections         | cancel          | !                                  |
1603   // +------------------+-----------------+------------------------------------+
1604   // | section          | parallel        | *                                  |
1605   // | section          | for             | +                                  |
1606   // | section          | for simd        | +                                  |
1607   // | section          | master          | +                                  |
1608   // | section          | critical        | *                                  |
1609   // | section          | simd            | *                                  |
1610   // | section          | sections        | +                                  |
1611   // | section          | section         | +                                  |
1612   // | section          | single          | +                                  |
1613   // | section          | parallel for    | *                                  |
1614   // | section          |parallel for simd| *                                  |
1615   // | section          |parallel sections| *                                  |
1616   // | section          | task            | *                                  |
1617   // | section          | taskyield       | *                                  |
1618   // | section          | barrier         | +                                  |
1619   // | section          | taskwait        | *                                  |
1620   // | section          | taskgroup       | *                                  |
1621   // | section          | flush           | *                                  |
1622   // | section          | ordered         | +                                  |
1623   // | section          | atomic          | *                                  |
1624   // | section          | target          | *                                  |
1625   // | section          | teams           | +                                  |
1626   // | section          | cancellation    |                                    |
1627   // |                  | point           | !                                  |
1628   // | section          | cancel          | !                                  |
1629   // +------------------+-----------------+------------------------------------+
1630   // | single           | parallel        | *                                  |
1631   // | single           | for             | +                                  |
1632   // | single           | for simd        | +                                  |
1633   // | single           | master          | +                                  |
1634   // | single           | critical        | *                                  |
1635   // | single           | simd            | *                                  |
1636   // | single           | sections        | +                                  |
1637   // | single           | section         | +                                  |
1638   // | single           | single          | +                                  |
1639   // | single           | parallel for    | *                                  |
1640   // | single           |parallel for simd| *                                  |
1641   // | single           |parallel sections| *                                  |
1642   // | single           | task            | *                                  |
1643   // | single           | taskyield       | *                                  |
1644   // | single           | barrier         | +                                  |
1645   // | single           | taskwait        | *                                  |
1646   // | single           | taskgroup       | *                                  |
1647   // | single           | flush           | *                                  |
1648   // | single           | ordered         | +                                  |
1649   // | single           | atomic          | *                                  |
1650   // | single           | target          | *                                  |
1651   // | single           | teams           | +                                  |
1652   // | single           | cancellation    |                                    |
1653   // |                  | point           |                                    |
1654   // | single           | cancel          |                                    |
1655   // +------------------+-----------------+------------------------------------+
1656   // | parallel for     | parallel        | *                                  |
1657   // | parallel for     | for             | +                                  |
1658   // | parallel for     | for simd        | +                                  |
1659   // | parallel for     | master          | +                                  |
1660   // | parallel for     | critical        | *                                  |
1661   // | parallel for     | simd            | *                                  |
1662   // | parallel for     | sections        | +                                  |
1663   // | parallel for     | section         | +                                  |
1664   // | parallel for     | single          | +                                  |
1665   // | parallel for     | parallel for    | *                                  |
1666   // | parallel for     |parallel for simd| *                                  |
1667   // | parallel for     |parallel sections| *                                  |
1668   // | parallel for     | task            | *                                  |
1669   // | parallel for     | taskyield       | *                                  |
1670   // | parallel for     | barrier         | +                                  |
1671   // | parallel for     | taskwait        | *                                  |
1672   // | parallel for     | taskgroup       | *                                  |
1673   // | parallel for     | flush           | *                                  |
1674   // | parallel for     | ordered         | * (if construct is ordered)        |
1675   // | parallel for     | atomic          | *                                  |
1676   // | parallel for     | target          | *                                  |
1677   // | parallel for     | teams           | +                                  |
1678   // | parallel for     | cancellation    |                                    |
1679   // |                  | point           | !                                  |
1680   // | parallel for     | cancel          | !                                  |
1681   // +------------------+-----------------+------------------------------------+
1682   // | parallel sections| parallel        | *                                  |
1683   // | parallel sections| for             | +                                  |
1684   // | parallel sections| for simd        | +                                  |
1685   // | parallel sections| master          | +                                  |
1686   // | parallel sections| critical        | +                                  |
1687   // | parallel sections| simd            | *                                  |
1688   // | parallel sections| sections        | +                                  |
1689   // | parallel sections| section         | *                                  |
1690   // | parallel sections| single          | +                                  |
1691   // | parallel sections| parallel for    | *                                  |
1692   // | parallel sections|parallel for simd| *                                  |
1693   // | parallel sections|parallel sections| *                                  |
1694   // | parallel sections| task            | *                                  |
1695   // | parallel sections| taskyield       | *                                  |
1696   // | parallel sections| barrier         | +                                  |
1697   // | parallel sections| taskwait        | *                                  |
1698   // | parallel sections| taskgroup       | *                                  |
1699   // | parallel sections| flush           | *                                  |
1700   // | parallel sections| ordered         | +                                  |
1701   // | parallel sections| atomic          | *                                  |
1702   // | parallel sections| target          | *                                  |
1703   // | parallel sections| teams           | +                                  |
1704   // | parallel sections| cancellation    |                                    |
1705   // |                  | point           | !                                  |
1706   // | parallel sections| cancel          | !                                  |
1707   // +------------------+-----------------+------------------------------------+
1708   // | task             | parallel        | *                                  |
1709   // | task             | for             | +                                  |
1710   // | task             | for simd        | +                                  |
1711   // | task             | master          | +                                  |
1712   // | task             | critical        | *                                  |
1713   // | task             | simd            | *                                  |
1714   // | task             | sections        | +                                  |
1715   // | task             | section         | +                                  |
1716   // | task             | single          | +                                  |
1717   // | task             | parallel for    | *                                  |
1718   // | task             |parallel for simd| *                                  |
1719   // | task             |parallel sections| *                                  |
1720   // | task             | task            | *                                  |
1721   // | task             | taskyield       | *                                  |
1722   // | task             | barrier         | +                                  |
1723   // | task             | taskwait        | *                                  |
1724   // | task             | taskgroup       | *                                  |
1725   // | task             | flush           | *                                  |
1726   // | task             | ordered         | +                                  |
1727   // | task             | atomic          | *                                  |
1728   // | task             | target          | *                                  |
1729   // | task             | teams           | +                                  |
1730   // | task             | cancellation    |                                    |
1731   // |                  | point           | !                                  |
1732   // | task             | cancel          | !                                  |
1733   // +------------------+-----------------+------------------------------------+
1734   // | ordered          | parallel        | *                                  |
1735   // | ordered          | for             | +                                  |
1736   // | ordered          | for simd        | +                                  |
1737   // | ordered          | master          | *                                  |
1738   // | ordered          | critical        | *                                  |
1739   // | ordered          | simd            | *                                  |
1740   // | ordered          | sections        | +                                  |
1741   // | ordered          | section         | +                                  |
1742   // | ordered          | single          | +                                  |
1743   // | ordered          | parallel for    | *                                  |
1744   // | ordered          |parallel for simd| *                                  |
1745   // | ordered          |parallel sections| *                                  |
1746   // | ordered          | task            | *                                  |
1747   // | ordered          | taskyield       | *                                  |
1748   // | ordered          | barrier         | +                                  |
1749   // | ordered          | taskwait        | *                                  |
1750   // | ordered          | taskgroup       | *                                  |
1751   // | ordered          | flush           | *                                  |
1752   // | ordered          | ordered         | +                                  |
1753   // | ordered          | atomic          | *                                  |
1754   // | ordered          | target          | *                                  |
1755   // | ordered          | teams           | +                                  |
1756   // | ordered          | cancellation    |                                    |
1757   // |                  | point           |                                    |
1758   // | ordered          | cancel          |                                    |
1759   // +------------------+-----------------+------------------------------------+
1760   // | atomic           | parallel        |                                    |
1761   // | atomic           | for             |                                    |
1762   // | atomic           | for simd        |                                    |
1763   // | atomic           | master          |                                    |
1764   // | atomic           | critical        |                                    |
1765   // | atomic           | simd            |                                    |
1766   // | atomic           | sections        |                                    |
1767   // | atomic           | section         |                                    |
1768   // | atomic           | single          |                                    |
1769   // | atomic           | parallel for    |                                    |
1770   // | atomic           |parallel for simd|                                    |
1771   // | atomic           |parallel sections|                                    |
1772   // | atomic           | task            |                                    |
1773   // | atomic           | taskyield       |                                    |
1774   // | atomic           | barrier         |                                    |
1775   // | atomic           | taskwait        |                                    |
1776   // | atomic           | taskgroup       |                                    |
1777   // | atomic           | flush           |                                    |
1778   // | atomic           | ordered         |                                    |
1779   // | atomic           | atomic          |                                    |
1780   // | atomic           | target          |                                    |
1781   // | atomic           | teams           |                                    |
1782   // | atomic           | cancellation    |                                    |
1783   // |                  | point           |                                    |
1784   // | atomic           | cancel          |                                    |
1785   // +------------------+-----------------+------------------------------------+
1786   // | target           | parallel        | *                                  |
1787   // | target           | for             | *                                  |
1788   // | target           | for simd        | *                                  |
1789   // | target           | master          | *                                  |
1790   // | target           | critical        | *                                  |
1791   // | target           | simd            | *                                  |
1792   // | target           | sections        | *                                  |
1793   // | target           | section         | *                                  |
1794   // | target           | single          | *                                  |
1795   // | target           | parallel for    | *                                  |
1796   // | target           |parallel for simd| *                                  |
1797   // | target           |parallel sections| *                                  |
1798   // | target           | task            | *                                  |
1799   // | target           | taskyield       | *                                  |
1800   // | target           | barrier         | *                                  |
1801   // | target           | taskwait        | *                                  |
1802   // | target           | taskgroup       | *                                  |
1803   // | target           | flush           | *                                  |
1804   // | target           | ordered         | *                                  |
1805   // | target           | atomic          | *                                  |
1806   // | target           | target          | *                                  |
1807   // | target           | teams           | *                                  |
1808   // | target           | cancellation    |                                    |
1809   // |                  | point           |                                    |
1810   // | target           | cancel          |                                    |
1811   // +------------------+-----------------+------------------------------------+
1812   // | teams            | parallel        | *                                  |
1813   // | teams            | for             | +                                  |
1814   // | teams            | for simd        | +                                  |
1815   // | teams            | master          | +                                  |
1816   // | teams            | critical        | +                                  |
1817   // | teams            | simd            | +                                  |
1818   // | teams            | sections        | +                                  |
1819   // | teams            | section         | +                                  |
1820   // | teams            | single          | +                                  |
1821   // | teams            | parallel for    | *                                  |
1822   // | teams            |parallel for simd| *                                  |
1823   // | teams            |parallel sections| *                                  |
1824   // | teams            | task            | +                                  |
1825   // | teams            | taskyield       | +                                  |
1826   // | teams            | barrier         | +                                  |
1827   // | teams            | taskwait        | +                                  |
1828   // | teams            | taskgroup       | +                                  |
1829   // | teams            | flush           | +                                  |
1830   // | teams            | ordered         | +                                  |
1831   // | teams            | atomic          | +                                  |
1832   // | teams            | target          | +                                  |
1833   // | teams            | teams           | +                                  |
1834   // | teams            | cancellation    |                                    |
1835   // |                  | point           |                                    |
1836   // | teams            | cancel          |                                    |
1837   // +------------------+-----------------+------------------------------------+
1838   if (Stack->getCurScope()) {
1839     auto ParentRegion = Stack->getParentDirective();
1840     bool NestingProhibited = false;
1841     bool CloseNesting = true;
1842     enum {
1843       NoRecommend,
1844       ShouldBeInParallelRegion,
1845       ShouldBeInOrderedRegion,
1846       ShouldBeInTargetRegion
1847     } Recommend = NoRecommend;
1848     if (isOpenMPSimdDirective(ParentRegion)) {
1849       // OpenMP [2.16, Nesting of Regions]
1850       // OpenMP constructs may not be nested inside a simd region.
1851       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1852       return true;
1853     }
1854     if (ParentRegion == OMPD_atomic) {
1855       // OpenMP [2.16, Nesting of Regions]
1856       // OpenMP constructs may not be nested inside an atomic region.
1857       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1858       return true;
1859     }
1860     if (CurrentRegion == OMPD_section) {
1861       // OpenMP [2.7.2, sections Construct, Restrictions]
1862       // Orphaned section directives are prohibited. That is, the section
1863       // directives must appear within the sections construct and must not be
1864       // encountered elsewhere in the sections region.
1865       if (ParentRegion != OMPD_sections &&
1866           ParentRegion != OMPD_parallel_sections) {
1867         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1868             << (ParentRegion != OMPD_unknown)
1869             << getOpenMPDirectiveName(ParentRegion);
1870         return true;
1871       }
1872       return false;
1873     }
1874     // Allow some constructs to be orphaned (they could be used in functions,
1875     // called from OpenMP regions with the required preconditions).
1876     if (ParentRegion == OMPD_unknown)
1877       return false;
1878     if (CurrentRegion == OMPD_cancellation_point ||
1879         CurrentRegion == OMPD_cancel) {
1880       // OpenMP [2.16, Nesting of Regions]
1881       // A cancellation point construct for which construct-type-clause is
1882       // taskgroup must be nested inside a task construct. A cancellation
1883       // point construct for which construct-type-clause is not taskgroup must
1884       // be closely nested inside an OpenMP construct that matches the type
1885       // specified in construct-type-clause.
1886       // A cancel construct for which construct-type-clause is taskgroup must be
1887       // nested inside a task construct. A cancel construct for which
1888       // construct-type-clause is not taskgroup must be closely nested inside an
1889       // OpenMP construct that matches the type specified in
1890       // construct-type-clause.
1891       NestingProhibited =
1892           !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1893             (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1894             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1895             (CancelRegion == OMPD_sections &&
1896              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1897     } else if (CurrentRegion == OMPD_master) {
1898       // OpenMP [2.16, Nesting of Regions]
1899       // A master region may not be closely nested inside a worksharing,
1900       // atomic, or explicit task region.
1901       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1902                           ParentRegion == OMPD_task;
1903     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1904       // OpenMP [2.16, Nesting of Regions]
1905       // A critical region may not be nested (closely or otherwise) inside a
1906       // critical region with the same name. Note that this restriction is not
1907       // sufficient to prevent deadlock.
1908       SourceLocation PreviousCriticalLoc;
1909       bool DeadLock =
1910           Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1911                                   OpenMPDirectiveKind K,
1912                                   const DeclarationNameInfo &DNI,
1913                                   SourceLocation Loc)
1914                                   ->bool {
1915                                 if (K == OMPD_critical &&
1916                                     DNI.getName() == CurrentName.getName()) {
1917                                   PreviousCriticalLoc = Loc;
1918                                   return true;
1919                                 } else
1920                                   return false;
1921                               },
1922                               false /* skip top directive */);
1923       if (DeadLock) {
1924         SemaRef.Diag(StartLoc,
1925                      diag::err_omp_prohibited_region_critical_same_name)
1926             << CurrentName.getName();
1927         if (PreviousCriticalLoc.isValid())
1928           SemaRef.Diag(PreviousCriticalLoc,
1929                        diag::note_omp_previous_critical_region);
1930         return true;
1931       }
1932     } else if (CurrentRegion == OMPD_barrier) {
1933       // OpenMP [2.16, Nesting of Regions]
1934       // A barrier region may not be closely nested inside a worksharing,
1935       // explicit task, critical, ordered, atomic, or master region.
1936       NestingProhibited =
1937           isOpenMPWorksharingDirective(ParentRegion) ||
1938           ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1939           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1940     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1941                !isOpenMPParallelDirective(CurrentRegion)) {
1942       // OpenMP [2.16, Nesting of Regions]
1943       // A worksharing region may not be closely nested inside a worksharing,
1944       // explicit task, critical, ordered, atomic, or master region.
1945       NestingProhibited =
1946           isOpenMPWorksharingDirective(ParentRegion) ||
1947           ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1948           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1949       Recommend = ShouldBeInParallelRegion;
1950     } else if (CurrentRegion == OMPD_ordered) {
1951       // OpenMP [2.16, Nesting of Regions]
1952       // An ordered region may not be closely nested inside a critical,
1953       // atomic, or explicit task region.
1954       // An ordered region must be closely nested inside a loop region (or
1955       // parallel loop region) with an ordered clause.
1956       NestingProhibited = ParentRegion == OMPD_critical ||
1957                           ParentRegion == OMPD_task ||
1958                           !Stack->isParentOrderedRegion();
1959       Recommend = ShouldBeInOrderedRegion;
1960     } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1961       // OpenMP [2.16, Nesting of Regions]
1962       // If specified, a teams construct must be contained within a target
1963       // construct.
1964       NestingProhibited = ParentRegion != OMPD_target;
1965       Recommend = ShouldBeInTargetRegion;
1966       Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1967     }
1968     if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1969       // OpenMP [2.16, Nesting of Regions]
1970       // distribute, parallel, parallel sections, parallel workshare, and the
1971       // parallel loop and parallel loop SIMD constructs are the only OpenMP
1972       // constructs that can be closely nested in the teams region.
1973       // TODO: add distribute directive.
1974       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1975       Recommend = ShouldBeInParallelRegion;
1976     }
1977     if (NestingProhibited) {
1978       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
1979           << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1980           << getOpenMPDirectiveName(CurrentRegion);
1981       return true;
1982     }
1983   }
1984   return false;
1985 }
1986 
1987 StmtResult Sema::ActOnOpenMPExecutableDirective(
1988     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1989     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1990     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
1991   StmtResult Res = StmtError();
1992   if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1993                             StartLoc))
1994     return StmtError();
1995 
1996   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1997   llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
1998   bool ErrorFound = false;
1999   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
2000   if (AStmt) {
2001     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2002 
2003     // Check default data sharing attributes for referenced variables.
2004     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2005     DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2006     if (DSAChecker.isErrorFound())
2007       return StmtError();
2008     // Generate list of implicitly defined firstprivate variables.
2009     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
2010 
2011     if (!DSAChecker.getImplicitFirstprivate().empty()) {
2012       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2013               DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2014               SourceLocation(), SourceLocation())) {
2015         ClausesWithImplicit.push_back(Implicit);
2016         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2017                      DSAChecker.getImplicitFirstprivate().size();
2018       } else
2019         ErrorFound = true;
2020     }
2021   }
2022 
2023   switch (Kind) {
2024   case OMPD_parallel:
2025     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2026                                        EndLoc);
2027     break;
2028   case OMPD_simd:
2029     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2030                                    VarsWithInheritedDSA);
2031     break;
2032   case OMPD_for:
2033     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2034                                   VarsWithInheritedDSA);
2035     break;
2036   case OMPD_for_simd:
2037     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2038                                       EndLoc, VarsWithInheritedDSA);
2039     break;
2040   case OMPD_sections:
2041     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2042                                        EndLoc);
2043     break;
2044   case OMPD_section:
2045     assert(ClausesWithImplicit.empty() &&
2046            "No clauses are allowed for 'omp section' directive");
2047     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2048     break;
2049   case OMPD_single:
2050     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2051                                      EndLoc);
2052     break;
2053   case OMPD_master:
2054     assert(ClausesWithImplicit.empty() &&
2055            "No clauses are allowed for 'omp master' directive");
2056     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2057     break;
2058   case OMPD_critical:
2059     assert(ClausesWithImplicit.empty() &&
2060            "No clauses are allowed for 'omp critical' directive");
2061     Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2062     break;
2063   case OMPD_parallel_for:
2064     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2065                                           EndLoc, VarsWithInheritedDSA);
2066     break;
2067   case OMPD_parallel_for_simd:
2068     Res = ActOnOpenMPParallelForSimdDirective(
2069         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2070     break;
2071   case OMPD_parallel_sections:
2072     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2073                                                StartLoc, EndLoc);
2074     break;
2075   case OMPD_task:
2076     Res =
2077         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2078     break;
2079   case OMPD_taskyield:
2080     assert(ClausesWithImplicit.empty() &&
2081            "No clauses are allowed for 'omp taskyield' directive");
2082     assert(AStmt == nullptr &&
2083            "No associated statement allowed for 'omp taskyield' directive");
2084     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2085     break;
2086   case OMPD_barrier:
2087     assert(ClausesWithImplicit.empty() &&
2088            "No clauses are allowed for 'omp barrier' directive");
2089     assert(AStmt == nullptr &&
2090            "No associated statement allowed for 'omp barrier' directive");
2091     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2092     break;
2093   case OMPD_taskwait:
2094     assert(ClausesWithImplicit.empty() &&
2095            "No clauses are allowed for 'omp taskwait' directive");
2096     assert(AStmt == nullptr &&
2097            "No associated statement allowed for 'omp taskwait' directive");
2098     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2099     break;
2100   case OMPD_taskgroup:
2101     assert(ClausesWithImplicit.empty() &&
2102            "No clauses are allowed for 'omp taskgroup' directive");
2103     Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2104     break;
2105   case OMPD_flush:
2106     assert(AStmt == nullptr &&
2107            "No associated statement allowed for 'omp flush' directive");
2108     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2109     break;
2110   case OMPD_ordered:
2111     assert(ClausesWithImplicit.empty() &&
2112            "No clauses are allowed for 'omp ordered' directive");
2113     Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2114     break;
2115   case OMPD_atomic:
2116     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2117                                      EndLoc);
2118     break;
2119   case OMPD_teams:
2120     Res =
2121         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2122     break;
2123   case OMPD_target:
2124     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2125                                      EndLoc);
2126     break;
2127   case OMPD_cancellation_point:
2128     assert(ClausesWithImplicit.empty() &&
2129            "No clauses are allowed for 'omp cancellation point' directive");
2130     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2131                                "cancellation point' directive");
2132     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2133     break;
2134   case OMPD_cancel:
2135     assert(ClausesWithImplicit.empty() &&
2136            "No clauses are allowed for 'omp cancel' directive");
2137     assert(AStmt == nullptr &&
2138            "No associated statement allowed for 'omp cancel' directive");
2139     Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2140     break;
2141   case OMPD_target_data:
2142     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2143                                          EndLoc);
2144     break;
2145   case OMPD_threadprivate:
2146     llvm_unreachable("OpenMP Directive is not allowed");
2147   case OMPD_unknown:
2148     llvm_unreachable("Unknown OpenMP directive");
2149   }
2150 
2151   for (auto P : VarsWithInheritedDSA) {
2152     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2153         << P.first << P.second->getSourceRange();
2154   }
2155   if (!VarsWithInheritedDSA.empty())
2156     return StmtError();
2157 
2158   if (ErrorFound)
2159     return StmtError();
2160   return Res;
2161 }
2162 
2163 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2164                                               Stmt *AStmt,
2165                                               SourceLocation StartLoc,
2166                                               SourceLocation EndLoc) {
2167   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2168   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2169   // 1.2.2 OpenMP Language Terminology
2170   // Structured block - An executable statement with a single entry at the
2171   // top and a single exit at the bottom.
2172   // The point of exit cannot be a branch out of the structured block.
2173   // longjmp() and throw() must not violate the entry/exit criteria.
2174   CS->getCapturedDecl()->setNothrow();
2175 
2176   getCurFunction()->setHasBranchProtectedScope();
2177 
2178   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2179                                       AStmt);
2180 }
2181 
2182 namespace {
2183 /// \brief Helper class for checking canonical form of the OpenMP loops and
2184 /// extracting iteration space of each loop in the loop nest, that will be used
2185 /// for IR generation.
2186 class OpenMPIterationSpaceChecker {
2187   /// \brief Reference to Sema.
2188   Sema &SemaRef;
2189   /// \brief A location for diagnostics (when there is no some better location).
2190   SourceLocation DefaultLoc;
2191   /// \brief A location for diagnostics (when increment is not compatible).
2192   SourceLocation ConditionLoc;
2193   /// \brief A source location for referring to loop init later.
2194   SourceRange InitSrcRange;
2195   /// \brief A source location for referring to condition later.
2196   SourceRange ConditionSrcRange;
2197   /// \brief A source location for referring to increment later.
2198   SourceRange IncrementSrcRange;
2199   /// \brief Loop variable.
2200   VarDecl *Var;
2201   /// \brief Reference to loop variable.
2202   DeclRefExpr *VarRef;
2203   /// \brief Lower bound (initializer for the var).
2204   Expr *LB;
2205   /// \brief Upper bound.
2206   Expr *UB;
2207   /// \brief Loop step (increment).
2208   Expr *Step;
2209   /// \brief This flag is true when condition is one of:
2210   ///   Var <  UB
2211   ///   Var <= UB
2212   ///   UB  >  Var
2213   ///   UB  >= Var
2214   bool TestIsLessOp;
2215   /// \brief This flag is true when condition is strict ( < or > ).
2216   bool TestIsStrictOp;
2217   /// \brief This flag is true when step is subtracted on each iteration.
2218   bool SubtractStep;
2219 
2220 public:
2221   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2222       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
2223         InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2224         IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
2225         LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2226         TestIsStrictOp(false), SubtractStep(false) {}
2227   /// \brief Check init-expr for canonical loop form and save loop counter
2228   /// variable - #Var and its initialization value - #LB.
2229   bool CheckInit(Stmt *S, bool EmitDiags = true);
2230   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2231   /// for less/greater and for strict/non-strict comparison.
2232   bool CheckCond(Expr *S);
2233   /// \brief Check incr-expr for canonical loop form and return true if it
2234   /// does not conform, otherwise save loop step (#Step).
2235   bool CheckInc(Expr *S);
2236   /// \brief Return the loop counter variable.
2237   VarDecl *GetLoopVar() const { return Var; }
2238   /// \brief Return the reference expression to loop counter variable.
2239   DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
2240   /// \brief Source range of the loop init.
2241   SourceRange GetInitSrcRange() const { return InitSrcRange; }
2242   /// \brief Source range of the loop condition.
2243   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2244   /// \brief Source range of the loop increment.
2245   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2246   /// \brief True if the step should be subtracted.
2247   bool ShouldSubtractStep() const { return SubtractStep; }
2248   /// \brief Build the expression to calculate the number of iterations.
2249   Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
2250   /// \brief Build the precondition expression for the loops.
2251   Expr *BuildPreCond(Scope *S, Expr *Cond) const;
2252   /// \brief Build reference expression to the counter be used for codegen.
2253   Expr *BuildCounterVar() const;
2254   /// \brief Build initization of the counter be used for codegen.
2255   Expr *BuildCounterInit() const;
2256   /// \brief Build step of the counter be used for codegen.
2257   Expr *BuildCounterStep() const;
2258   /// \brief Return true if any expression is dependent.
2259   bool Dependent() const;
2260 
2261 private:
2262   /// \brief Check the right-hand side of an assignment in the increment
2263   /// expression.
2264   bool CheckIncRHS(Expr *RHS);
2265   /// \brief Helper to set loop counter variable and its initializer.
2266   bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
2267   /// \brief Helper to set upper bound.
2268   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2269              const SourceLocation &SL);
2270   /// \brief Helper to set loop increment.
2271   bool SetStep(Expr *NewStep, bool Subtract);
2272 };
2273 
2274 bool OpenMPIterationSpaceChecker::Dependent() const {
2275   if (!Var) {
2276     assert(!LB && !UB && !Step);
2277     return false;
2278   }
2279   return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2280          (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2281 }
2282 
2283 template <typename T>
2284 static T *getExprAsWritten(T *E) {
2285   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2286     E = ExprTemp->getSubExpr();
2287 
2288   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2289     E = MTE->GetTemporaryExpr();
2290 
2291   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2292     E = Binder->getSubExpr();
2293 
2294   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2295     E = ICE->getSubExprAsWritten();
2296   return E->IgnoreParens();
2297 }
2298 
2299 bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2300                                               DeclRefExpr *NewVarRefExpr,
2301                                               Expr *NewLB) {
2302   // State consistency checking to ensure correct usage.
2303   assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2304          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
2305   if (!NewVar || !NewLB)
2306     return true;
2307   Var = NewVar;
2308   VarRef = NewVarRefExpr;
2309   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2310     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2311       if ((Ctor->isCopyOrMoveConstructor() ||
2312            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2313           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
2314         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
2315   LB = NewLB;
2316   return false;
2317 }
2318 
2319 bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2320                                         const SourceRange &SR,
2321                                         const SourceLocation &SL) {
2322   // State consistency checking to ensure correct usage.
2323   assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2324          !TestIsLessOp && !TestIsStrictOp);
2325   if (!NewUB)
2326     return true;
2327   UB = NewUB;
2328   TestIsLessOp = LessOp;
2329   TestIsStrictOp = StrictOp;
2330   ConditionSrcRange = SR;
2331   ConditionLoc = SL;
2332   return false;
2333 }
2334 
2335 bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2336   // State consistency checking to ensure correct usage.
2337   assert(Var != nullptr && LB != nullptr && Step == nullptr);
2338   if (!NewStep)
2339     return true;
2340   if (!NewStep->isValueDependent()) {
2341     // Check that the step is integer expression.
2342     SourceLocation StepLoc = NewStep->getLocStart();
2343     ExprResult Val =
2344         SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2345     if (Val.isInvalid())
2346       return true;
2347     NewStep = Val.get();
2348 
2349     // OpenMP [2.6, Canonical Loop Form, Restrictions]
2350     //  If test-expr is of form var relational-op b and relational-op is < or
2351     //  <= then incr-expr must cause var to increase on each iteration of the
2352     //  loop. If test-expr is of form var relational-op b and relational-op is
2353     //  > or >= then incr-expr must cause var to decrease on each iteration of
2354     //  the loop.
2355     //  If test-expr is of form b relational-op var and relational-op is < or
2356     //  <= then incr-expr must cause var to decrease on each iteration of the
2357     //  loop. If test-expr is of form b relational-op var and relational-op is
2358     //  > or >= then incr-expr must cause var to increase on each iteration of
2359     //  the loop.
2360     llvm::APSInt Result;
2361     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2362     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2363     bool IsConstNeg =
2364         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
2365     bool IsConstPos =
2366         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
2367     bool IsConstZero = IsConstant && !Result.getBoolValue();
2368     if (UB && (IsConstZero ||
2369                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
2370                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
2371       SemaRef.Diag(NewStep->getExprLoc(),
2372                    diag::err_omp_loop_incr_not_compatible)
2373           << Var << TestIsLessOp << NewStep->getSourceRange();
2374       SemaRef.Diag(ConditionLoc,
2375                    diag::note_omp_loop_cond_requres_compatible_incr)
2376           << TestIsLessOp << ConditionSrcRange;
2377       return true;
2378     }
2379     if (TestIsLessOp == Subtract) {
2380       NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2381                                              NewStep).get();
2382       Subtract = !Subtract;
2383     }
2384   }
2385 
2386   Step = NewStep;
2387   SubtractStep = Subtract;
2388   return false;
2389 }
2390 
2391 bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
2392   // Check init-expr for canonical loop form and save loop counter
2393   // variable - #Var and its initialization value - #LB.
2394   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2395   //   var = lb
2396   //   integer-type var = lb
2397   //   random-access-iterator-type var = lb
2398   //   pointer-type var = lb
2399   //
2400   if (!S) {
2401     if (EmitDiags) {
2402       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2403     }
2404     return true;
2405   }
2406   InitSrcRange = S->getSourceRange();
2407   if (Expr *E = dyn_cast<Expr>(S))
2408     S = E->IgnoreParens();
2409   if (auto BO = dyn_cast<BinaryOperator>(S)) {
2410     if (BO->getOpcode() == BO_Assign)
2411       if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
2412         return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2413                            BO->getRHS());
2414   } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2415     if (DS->isSingleDecl()) {
2416       if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2417         if (Var->hasInit()) {
2418           // Accept non-canonical init form here but emit ext. warning.
2419           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
2420             SemaRef.Diag(S->getLocStart(),
2421                          diag::ext_omp_loop_not_canonical_init)
2422                 << S->getSourceRange();
2423           return SetVarAndLB(Var, nullptr, Var->getInit());
2424         }
2425       }
2426     }
2427   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2428     if (CE->getOperator() == OO_Equal)
2429       if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
2430         return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2431                            CE->getArg(1));
2432 
2433   if (EmitDiags) {
2434     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2435         << S->getSourceRange();
2436   }
2437   return true;
2438 }
2439 
2440 /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
2441 /// variable (which may be the loop variable) if possible.
2442 static const VarDecl *GetInitVarDecl(const Expr *E) {
2443   if (!E)
2444     return nullptr;
2445   E = getExprAsWritten(E);
2446   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2447     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2448       if ((Ctor->isCopyOrMoveConstructor() ||
2449            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2450           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
2451         E = CE->getArg(0)->IgnoreParenImpCasts();
2452   auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2453   if (!DRE)
2454     return nullptr;
2455   return dyn_cast<VarDecl>(DRE->getDecl());
2456 }
2457 
2458 bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2459   // Check test-expr for canonical form, save upper-bound UB, flags for
2460   // less/greater and for strict/non-strict comparison.
2461   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2462   //   var relational-op b
2463   //   b relational-op var
2464   //
2465   if (!S) {
2466     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2467     return true;
2468   }
2469   S = getExprAsWritten(S);
2470   SourceLocation CondLoc = S->getLocStart();
2471   if (auto BO = dyn_cast<BinaryOperator>(S)) {
2472     if (BO->isRelationalOp()) {
2473       if (GetInitVarDecl(BO->getLHS()) == Var)
2474         return SetUB(BO->getRHS(),
2475                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2476                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2477                      BO->getSourceRange(), BO->getOperatorLoc());
2478       if (GetInitVarDecl(BO->getRHS()) == Var)
2479         return SetUB(BO->getLHS(),
2480                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2481                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2482                      BO->getSourceRange(), BO->getOperatorLoc());
2483     }
2484   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2485     if (CE->getNumArgs() == 2) {
2486       auto Op = CE->getOperator();
2487       switch (Op) {
2488       case OO_Greater:
2489       case OO_GreaterEqual:
2490       case OO_Less:
2491       case OO_LessEqual:
2492         if (GetInitVarDecl(CE->getArg(0)) == Var)
2493           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2494                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2495                        CE->getOperatorLoc());
2496         if (GetInitVarDecl(CE->getArg(1)) == Var)
2497           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2498                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2499                        CE->getOperatorLoc());
2500         break;
2501       default:
2502         break;
2503       }
2504     }
2505   }
2506   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2507       << S->getSourceRange() << Var;
2508   return true;
2509 }
2510 
2511 bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2512   // RHS of canonical loop form increment can be:
2513   //   var + incr
2514   //   incr + var
2515   //   var - incr
2516   //
2517   RHS = RHS->IgnoreParenImpCasts();
2518   if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2519     if (BO->isAdditiveOp()) {
2520       bool IsAdd = BO->getOpcode() == BO_Add;
2521       if (GetInitVarDecl(BO->getLHS()) == Var)
2522         return SetStep(BO->getRHS(), !IsAdd);
2523       if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2524         return SetStep(BO->getLHS(), false);
2525     }
2526   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2527     bool IsAdd = CE->getOperator() == OO_Plus;
2528     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2529       if (GetInitVarDecl(CE->getArg(0)) == Var)
2530         return SetStep(CE->getArg(1), !IsAdd);
2531       if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2532         return SetStep(CE->getArg(0), false);
2533     }
2534   }
2535   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2536       << RHS->getSourceRange() << Var;
2537   return true;
2538 }
2539 
2540 bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2541   // Check incr-expr for canonical loop form and return true if it
2542   // does not conform.
2543   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2544   //   ++var
2545   //   var++
2546   //   --var
2547   //   var--
2548   //   var += incr
2549   //   var -= incr
2550   //   var = var + incr
2551   //   var = incr + var
2552   //   var = var - incr
2553   //
2554   if (!S) {
2555     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2556     return true;
2557   }
2558   IncrementSrcRange = S->getSourceRange();
2559   S = S->IgnoreParens();
2560   if (auto UO = dyn_cast<UnaryOperator>(S)) {
2561     if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2562       return SetStep(
2563           SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2564                                        (UO->isDecrementOp() ? -1 : 1)).get(),
2565           false);
2566   } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2567     switch (BO->getOpcode()) {
2568     case BO_AddAssign:
2569     case BO_SubAssign:
2570       if (GetInitVarDecl(BO->getLHS()) == Var)
2571         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2572       break;
2573     case BO_Assign:
2574       if (GetInitVarDecl(BO->getLHS()) == Var)
2575         return CheckIncRHS(BO->getRHS());
2576       break;
2577     default:
2578       break;
2579     }
2580   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2581     switch (CE->getOperator()) {
2582     case OO_PlusPlus:
2583     case OO_MinusMinus:
2584       if (GetInitVarDecl(CE->getArg(0)) == Var)
2585         return SetStep(
2586             SemaRef.ActOnIntegerConstant(
2587                         CE->getLocStart(),
2588                         ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2589             false);
2590       break;
2591     case OO_PlusEqual:
2592     case OO_MinusEqual:
2593       if (GetInitVarDecl(CE->getArg(0)) == Var)
2594         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2595       break;
2596     case OO_Equal:
2597       if (GetInitVarDecl(CE->getArg(0)) == Var)
2598         return CheckIncRHS(CE->getArg(1));
2599       break;
2600     default:
2601       break;
2602     }
2603   }
2604   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2605       << S->getSourceRange() << Var;
2606   return true;
2607 }
2608 
2609 /// \brief Build the expression to calculate the number of iterations.
2610 Expr *
2611 OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2612                                                 const bool LimitedType) const {
2613   ExprResult Diff;
2614   if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2615       SemaRef.getLangOpts().CPlusPlus) {
2616     // Upper - Lower
2617     Expr *Upper = TestIsLessOp ? UB : LB;
2618     Expr *Lower = TestIsLessOp ? LB : UB;
2619 
2620     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2621 
2622     if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2623       // BuildBinOp already emitted error, this one is to point user to upper
2624       // and lower bound, and to tell what is passed to 'operator-'.
2625       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2626           << Upper->getSourceRange() << Lower->getSourceRange();
2627       return nullptr;
2628     }
2629   }
2630 
2631   if (!Diff.isUsable())
2632     return nullptr;
2633 
2634   // Upper - Lower [- 1]
2635   if (TestIsStrictOp)
2636     Diff = SemaRef.BuildBinOp(
2637         S, DefaultLoc, BO_Sub, Diff.get(),
2638         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2639   if (!Diff.isUsable())
2640     return nullptr;
2641 
2642   // Upper - Lower [- 1] + Step
2643   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2644                             Step->IgnoreImplicit());
2645   if (!Diff.isUsable())
2646     return nullptr;
2647 
2648   // Parentheses (for dumping/debugging purposes only).
2649   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2650   if (!Diff.isUsable())
2651     return nullptr;
2652 
2653   // (Upper - Lower [- 1] + Step) / Step
2654   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2655                             Step->IgnoreImplicit());
2656   if (!Diff.isUsable())
2657     return nullptr;
2658 
2659   // OpenMP runtime requires 32-bit or 64-bit loop variables.
2660   if (LimitedType) {
2661     auto &C = SemaRef.Context;
2662     QualType Type = Diff.get()->getType();
2663     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2664     if (NewSize != C.getTypeSize(Type)) {
2665       if (NewSize < C.getTypeSize(Type)) {
2666         assert(NewSize == 64 && "incorrect loop var size");
2667         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2668             << InitSrcRange << ConditionSrcRange;
2669       }
2670       QualType NewType = C.getIntTypeForBitwidth(
2671           NewSize, Type->hasSignedIntegerRepresentation());
2672       Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2673                                                Sema::AA_Converting, true);
2674       if (!Diff.isUsable())
2675         return nullptr;
2676     }
2677   }
2678 
2679   return Diff.get();
2680 }
2681 
2682 Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2683   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2684   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2685   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2686   auto CondExpr = SemaRef.BuildBinOp(
2687       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2688                                   : (TestIsStrictOp ? BO_GT : BO_GE),
2689       LB, UB);
2690   if (CondExpr.isUsable()) {
2691     CondExpr = SemaRef.PerformImplicitConversion(
2692         CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2693         /*AllowExplicit=*/true);
2694   }
2695   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2696   // Otherwise use original loop conditon and evaluate it in runtime.
2697   return CondExpr.isUsable() ? CondExpr.get() : Cond;
2698 }
2699 
2700 /// \brief Build reference expression to the counter be used for codegen.
2701 Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2702   return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc);
2703 }
2704 
2705 /// \brief Build initization of the counter be used for codegen.
2706 Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2707 
2708 /// \brief Build step of the counter be used for codegen.
2709 Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2710 
2711 /// \brief Iteration space of a single for loop.
2712 struct LoopIterationSpace {
2713   /// \brief Condition of the loop.
2714   Expr *PreCond;
2715   /// \brief This expression calculates the number of iterations in the loop.
2716   /// It is always possible to calculate it before starting the loop.
2717   Expr *NumIterations;
2718   /// \brief The loop counter variable.
2719   Expr *CounterVar;
2720   /// \brief This is initializer for the initial value of #CounterVar.
2721   Expr *CounterInit;
2722   /// \brief This is step for the #CounterVar used to generate its update:
2723   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2724   Expr *CounterStep;
2725   /// \brief Should step be subtracted?
2726   bool Subtract;
2727   /// \brief Source range of the loop init.
2728   SourceRange InitSrcRange;
2729   /// \brief Source range of the loop condition.
2730   SourceRange CondSrcRange;
2731   /// \brief Source range of the loop increment.
2732   SourceRange IncSrcRange;
2733 };
2734 
2735 } // namespace
2736 
2737 void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2738   assert(getLangOpts().OpenMP && "OpenMP is not active.");
2739   assert(Init && "Expected loop in canonical form.");
2740   unsigned CollapseIteration = DSAStack->getCollapseNumber();
2741   if (CollapseIteration > 0 &&
2742       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2743     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2744     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2745       DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2746     }
2747     DSAStack->setCollapseNumber(CollapseIteration - 1);
2748   }
2749 }
2750 
2751 /// \brief Called on a for stmt to check and extract its iteration space
2752 /// for further processing (such as collapsing).
2753 static bool CheckOpenMPIterationSpace(
2754     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2755     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2756     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
2757     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2758     LoopIterationSpace &ResultIterSpace) {
2759   // OpenMP [2.6, Canonical Loop Form]
2760   //   for (init-expr; test-expr; incr-expr) structured-block
2761   auto For = dyn_cast_or_null<ForStmt>(S);
2762   if (!For) {
2763     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
2764         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
2765         << getOpenMPDirectiveName(DKind) << NestedLoopCount
2766         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
2767     if (NestedLoopCount > 1) {
2768       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
2769         SemaRef.Diag(DSA.getConstructLoc(),
2770                      diag::note_omp_collapse_ordered_expr)
2771             << 2 << CollapseLoopCountExpr->getSourceRange()
2772             << OrderedLoopCountExpr->getSourceRange();
2773       else if (CollapseLoopCountExpr)
2774         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
2775                      diag::note_omp_collapse_ordered_expr)
2776             << 0 << CollapseLoopCountExpr->getSourceRange();
2777       else
2778         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
2779                      diag::note_omp_collapse_ordered_expr)
2780             << 1 << OrderedLoopCountExpr->getSourceRange();
2781     }
2782     return true;
2783   }
2784   assert(For->getBody());
2785 
2786   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2787 
2788   // Check init.
2789   auto Init = For->getInit();
2790   if (ISC.CheckInit(Init)) {
2791     return true;
2792   }
2793 
2794   bool HasErrors = false;
2795 
2796   // Check loop variable's type.
2797   auto Var = ISC.GetLoopVar();
2798 
2799   // OpenMP [2.6, Canonical Loop Form]
2800   // Var is one of the following:
2801   //   A variable of signed or unsigned integer type.
2802   //   For C++, a variable of a random access iterator type.
2803   //   For C, a variable of a pointer type.
2804   auto VarType = Var->getType();
2805   if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2806       !VarType->isPointerType() &&
2807       !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2808     SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2809         << SemaRef.getLangOpts().CPlusPlus;
2810     HasErrors = true;
2811   }
2812 
2813   // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2814   // Construct
2815   // The loop iteration variable(s) in the associated for-loop(s) of a for or
2816   // parallel for construct is (are) private.
2817   // The loop iteration variable in the associated for-loop of a simd construct
2818   // with just one associated for-loop is linear with a constant-linear-step
2819   // that is the increment of the associated for-loop.
2820   // Exclude loop var from the list of variables with implicitly defined data
2821   // sharing attributes.
2822   VarsWithImplicitDSA.erase(Var);
2823 
2824   // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2825   // a Construct, C/C++].
2826   // The loop iteration variable in the associated for-loop of a simd construct
2827   // with just one associated for-loop may be listed in a linear clause with a
2828   // constant-linear-step that is the increment of the associated for-loop.
2829   // The loop iteration variable(s) in the associated for-loop(s) of a for or
2830   // parallel for construct may be listed in a private or lastprivate clause.
2831   DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
2832   auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2833   // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2834   // declared in the loop and it is predetermined as a private.
2835   auto PredeterminedCKind =
2836       isOpenMPSimdDirective(DKind)
2837           ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2838           : OMPC_private;
2839   if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
2840         DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
2841        (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2842         DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2843         DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2844       ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2845        DVar.RefExpr != nullptr)) {
2846     SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
2847         << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2848         << getOpenMPClauseName(PredeterminedCKind);
2849     if (DVar.RefExpr == nullptr)
2850       DVar.CKind = PredeterminedCKind;
2851     ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
2852     HasErrors = true;
2853   } else if (LoopVarRefExpr != nullptr) {
2854     // Make the loop iteration variable private (for worksharing constructs),
2855     // linear (for simd directives with the only one associated loop) or
2856     // lastprivate (for simd directives with several collapsed or ordered
2857     // loops).
2858     if (DVar.CKind == OMPC_unknown)
2859       DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2860                         /*FromParent=*/false);
2861     DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
2862   }
2863 
2864   assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
2865 
2866   // Check test-expr.
2867   HasErrors |= ISC.CheckCond(For->getCond());
2868 
2869   // Check incr-expr.
2870   HasErrors |= ISC.CheckInc(For->getInc());
2871 
2872   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
2873     return HasErrors;
2874 
2875   // Build the loop's iteration space representation.
2876   ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
2877   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2878       DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
2879   ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2880   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2881   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2882   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2883   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2884   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2885   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2886 
2887   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2888                 ResultIterSpace.NumIterations == nullptr ||
2889                 ResultIterSpace.CounterVar == nullptr ||
2890                 ResultIterSpace.CounterInit == nullptr ||
2891                 ResultIterSpace.CounterStep == nullptr);
2892 
2893   return HasErrors;
2894 }
2895 
2896 /// \brief Build 'VarRef = Start + Iter * Step'.
2897 static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2898                                      SourceLocation Loc, ExprResult VarRef,
2899                                      ExprResult Start, ExprResult Iter,
2900                                      ExprResult Step, bool Subtract) {
2901   // Add parentheses (for debugging purposes only).
2902   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2903   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2904       !Step.isUsable())
2905     return ExprError();
2906 
2907   ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2908                                          Step.get()->IgnoreImplicit());
2909   if (!Update.isUsable())
2910     return ExprError();
2911 
2912   // Build 'VarRef = Start + Iter * Step'.
2913   Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2914                               Start.get()->IgnoreImplicit(), Update.get());
2915   if (!Update.isUsable())
2916     return ExprError();
2917 
2918   Update = SemaRef.PerformImplicitConversion(
2919       Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2920   if (!Update.isUsable())
2921     return ExprError();
2922 
2923   Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2924   return Update;
2925 }
2926 
2927 /// \brief Convert integer expression \a E to make it have at least \a Bits
2928 /// bits.
2929 static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2930                                       Sema &SemaRef) {
2931   if (E == nullptr)
2932     return ExprError();
2933   auto &C = SemaRef.Context;
2934   QualType OldType = E->getType();
2935   unsigned HasBits = C.getTypeSize(OldType);
2936   if (HasBits >= Bits)
2937     return ExprResult(E);
2938   // OK to convert to signed, because new type has more bits than old.
2939   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2940   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2941                                            true);
2942 }
2943 
2944 /// \brief Check if the given expression \a E is a constant integer that fits
2945 /// into \a Bits bits.
2946 static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2947   if (E == nullptr)
2948     return false;
2949   llvm::APSInt Result;
2950   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2951     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2952   return false;
2953 }
2954 
2955 /// \brief Called on a for stmt to check itself and nested loops (if any).
2956 /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2957 /// number of collapsed loops otherwise.
2958 static unsigned
2959 CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
2960                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
2961                 DSAStackTy &DSA,
2962                 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2963                 OMPLoopDirective::HelperExprs &Built) {
2964   unsigned NestedLoopCount = 1;
2965   if (CollapseLoopCountExpr) {
2966     // Found 'collapse' clause - calculate collapse number.
2967     llvm::APSInt Result;
2968     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2969       NestedLoopCount += Result.getLimitedValue() - 1;
2970   }
2971   if (OrderedLoopCountExpr) {
2972     // Found 'ordered' clause - calculate collapse number.
2973     llvm::APSInt Result;
2974     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2975       NestedLoopCount += Result.getLimitedValue() - 1;
2976   }
2977   // This is helper routine for loop directives (e.g., 'for', 'simd',
2978   // 'for simd', etc.).
2979   SmallVector<LoopIterationSpace, 4> IterSpaces;
2980   IterSpaces.resize(NestedLoopCount);
2981   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
2982   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
2983     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
2984                                   NestedLoopCount, CollapseLoopCountExpr,
2985                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
2986                                   IterSpaces[Cnt]))
2987       return 0;
2988     // Move on to the next nested for loop, or to the loop body.
2989     // OpenMP [2.8.1, simd construct, Restrictions]
2990     // All loops associated with the construct must be perfectly nested; that
2991     // is, there must be no intervening code nor any OpenMP directive between
2992     // any two loops.
2993     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
2994   }
2995 
2996   Built.clear(/* size */ NestedLoopCount);
2997 
2998   if (SemaRef.CurContext->isDependentContext())
2999     return NestedLoopCount;
3000 
3001   // An example of what is generated for the following code:
3002   //
3003   //   #pragma omp simd collapse(2) ordered(2)
3004   //   for (i = 0; i < NI; ++i)
3005   //     for (k = 0; k < NK; ++k)
3006   //       for (j = J0; j < NJ; j+=2) {
3007   //         <loop body>
3008   //       }
3009   //
3010   // We generate the code below.
3011   // Note: the loop body may be outlined in CodeGen.
3012   // Note: some counters may be C++ classes, operator- is used to find number of
3013   // iterations and operator+= to calculate counter value.
3014   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3015   // or i64 is currently supported).
3016   //
3017   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3018   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3019   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3020   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3021   //     // similar updates for vars in clauses (e.g. 'linear')
3022   //     <loop body (using local i and j)>
3023   //   }
3024   //   i = NI; // assign final values of counters
3025   //   j = NJ;
3026   //
3027 
3028   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3029   // the iteration counts of the collapsed for loops.
3030   // Precondition tests if there is at least one iteration (all conditions are
3031   // true).
3032   auto PreCond = ExprResult(IterSpaces[0].PreCond);
3033   auto N0 = IterSpaces[0].NumIterations;
3034   ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
3035   ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
3036 
3037   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3038     return NestedLoopCount;
3039 
3040   auto &C = SemaRef.Context;
3041   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3042 
3043   Scope *CurScope = DSA.getCurScope();
3044   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
3045     if (PreCond.isUsable()) {
3046       PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3047                                    PreCond.get(), IterSpaces[Cnt].PreCond);
3048     }
3049     auto N = IterSpaces[Cnt].NumIterations;
3050     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3051     if (LastIteration32.isUsable())
3052       LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3053                                            LastIteration32.get(), N);
3054     if (LastIteration64.isUsable())
3055       LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3056                                            LastIteration64.get(), N);
3057   }
3058 
3059   // Choose either the 32-bit or 64-bit version.
3060   ExprResult LastIteration = LastIteration64;
3061   if (LastIteration32.isUsable() &&
3062       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3063       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3064        FitsInto(
3065            32 /* Bits */,
3066            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3067            LastIteration64.get(), SemaRef)))
3068     LastIteration = LastIteration32;
3069 
3070   if (!LastIteration.isUsable())
3071     return 0;
3072 
3073   // Save the number of iterations.
3074   ExprResult NumIterations = LastIteration;
3075   {
3076     LastIteration = SemaRef.BuildBinOp(
3077         CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3078         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3079     if (!LastIteration.isUsable())
3080       return 0;
3081   }
3082 
3083   // Calculate the last iteration number beforehand instead of doing this on
3084   // each iteration. Do not do this if the number of iterations may be kfold-ed.
3085   llvm::APSInt Result;
3086   bool IsConstant =
3087       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3088   ExprResult CalcLastIteration;
3089   if (!IsConstant) {
3090     SourceLocation SaveLoc;
3091     VarDecl *SaveVar =
3092         buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
3093                      ".omp.last.iteration");
3094     ExprResult SaveRef = buildDeclRefExpr(
3095         SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
3096     CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3097                                            SaveRef.get(), LastIteration.get());
3098     LastIteration = SaveRef;
3099 
3100     // Prepare SaveRef + 1.
3101     NumIterations = SemaRef.BuildBinOp(
3102         CurScope, SaveLoc, BO_Add, SaveRef.get(),
3103         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3104     if (!NumIterations.isUsable())
3105       return 0;
3106   }
3107 
3108   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3109 
3110   QualType VType = LastIteration.get()->getType();
3111   // Build variables passed into runtime, nesessary for worksharing directives.
3112   ExprResult LB, UB, IL, ST, EUB;
3113   if (isOpenMPWorksharingDirective(DKind)) {
3114     // Lower bound variable, initialized with zero.
3115     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3116     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
3117     SemaRef.AddInitializerToDecl(
3118         LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3119         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3120 
3121     // Upper bound variable, initialized with last iteration number.
3122     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3123     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
3124     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3125                                  /*DirectInit*/ false,
3126                                  /*TypeMayContainAuto*/ false);
3127 
3128     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3129     // This will be used to implement clause 'lastprivate'.
3130     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
3131     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3132     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
3133     SemaRef.AddInitializerToDecl(
3134         ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3135         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3136 
3137     // Stride variable returned by runtime (we initialize it to 1 by default).
3138     VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3139     ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
3140     SemaRef.AddInitializerToDecl(
3141         STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3142         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3143 
3144     // Build expression: UB = min(UB, LastIteration)
3145     // It is nesessary for CodeGen of directives with static scheduling.
3146     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3147                                                 UB.get(), LastIteration.get());
3148     ExprResult CondOp = SemaRef.ActOnConditionalOp(
3149         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3150     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3151                              CondOp.get());
3152     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3153   }
3154 
3155   // Build the iteration variable and its initialization before loop.
3156   ExprResult IV;
3157   ExprResult Init;
3158   {
3159     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3160     IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
3161     Expr *RHS = isOpenMPWorksharingDirective(DKind)
3162                     ? LB.get()
3163                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3164     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3165     Init = SemaRef.ActOnFinishFullExpr(Init.get());
3166   }
3167 
3168   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
3169   SourceLocation CondLoc;
3170   ExprResult Cond =
3171       isOpenMPWorksharingDirective(DKind)
3172           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3173           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3174                                NumIterations.get());
3175 
3176   // Loop increment (IV = IV + 1)
3177   SourceLocation IncLoc;
3178   ExprResult Inc =
3179       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3180                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3181   if (!Inc.isUsable())
3182     return 0;
3183   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
3184   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3185   if (!Inc.isUsable())
3186     return 0;
3187 
3188   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3189   // Used for directives with static scheduling.
3190   ExprResult NextLB, NextUB;
3191   if (isOpenMPWorksharingDirective(DKind)) {
3192     // LB + ST
3193     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3194     if (!NextLB.isUsable())
3195       return 0;
3196     // LB = LB + ST
3197     NextLB =
3198         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3199     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3200     if (!NextLB.isUsable())
3201       return 0;
3202     // UB + ST
3203     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3204     if (!NextUB.isUsable())
3205       return 0;
3206     // UB = UB + ST
3207     NextUB =
3208         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3209     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3210     if (!NextUB.isUsable())
3211       return 0;
3212   }
3213 
3214   // Build updates and final values of the loop counters.
3215   bool HasErrors = false;
3216   Built.Counters.resize(NestedLoopCount);
3217   Built.Updates.resize(NestedLoopCount);
3218   Built.Finals.resize(NestedLoopCount);
3219   {
3220     ExprResult Div;
3221     // Go from inner nested loop to outer.
3222     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3223       LoopIterationSpace &IS = IterSpaces[Cnt];
3224       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3225       // Build: Iter = (IV / Div) % IS.NumIters
3226       // where Div is product of previous iterations' IS.NumIters.
3227       ExprResult Iter;
3228       if (Div.isUsable()) {
3229         Iter =
3230             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3231       } else {
3232         Iter = IV;
3233         assert((Cnt == (int)NestedLoopCount - 1) &&
3234                "unusable div expected on first iteration only");
3235       }
3236 
3237       if (Cnt != 0 && Iter.isUsable())
3238         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3239                                   IS.NumIterations);
3240       if (!Iter.isUsable()) {
3241         HasErrors = true;
3242         break;
3243       }
3244 
3245       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3246       auto *CounterVar = buildDeclRefExpr(
3247           SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3248           IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3249           /*RefersToCapture=*/true);
3250       ExprResult Update =
3251           BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
3252                              IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3253       if (!Update.isUsable()) {
3254         HasErrors = true;
3255         break;
3256       }
3257 
3258       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3259       ExprResult Final = BuildCounterUpdate(
3260           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
3261           IS.NumIterations, IS.CounterStep, IS.Subtract);
3262       if (!Final.isUsable()) {
3263         HasErrors = true;
3264         break;
3265       }
3266 
3267       // Build Div for the next iteration: Div <- Div * IS.NumIters
3268       if (Cnt != 0) {
3269         if (Div.isUnset())
3270           Div = IS.NumIterations;
3271         else
3272           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3273                                    IS.NumIterations);
3274 
3275         // Add parentheses (for debugging purposes only).
3276         if (Div.isUsable())
3277           Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3278         if (!Div.isUsable()) {
3279           HasErrors = true;
3280           break;
3281         }
3282       }
3283       if (!Update.isUsable() || !Final.isUsable()) {
3284         HasErrors = true;
3285         break;
3286       }
3287       // Save results
3288       Built.Counters[Cnt] = IS.CounterVar;
3289       Built.Updates[Cnt] = Update.get();
3290       Built.Finals[Cnt] = Final.get();
3291     }
3292   }
3293 
3294   if (HasErrors)
3295     return 0;
3296 
3297   // Save results
3298   Built.IterationVarRef = IV.get();
3299   Built.LastIteration = LastIteration.get();
3300   Built.NumIterations = NumIterations.get();
3301   Built.CalcLastIteration =
3302       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
3303   Built.PreCond = PreCond.get();
3304   Built.Cond = Cond.get();
3305   Built.Init = Init.get();
3306   Built.Inc = Inc.get();
3307   Built.LB = LB.get();
3308   Built.UB = UB.get();
3309   Built.IL = IL.get();
3310   Built.ST = ST.get();
3311   Built.EUB = EUB.get();
3312   Built.NLB = NextLB.get();
3313   Built.NUB = NextUB.get();
3314 
3315   return NestedLoopCount;
3316 }
3317 
3318 static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
3319   auto &&CollapseFilter = [](const OMPClause *C) -> bool {
3320     return C->getClauseKind() == OMPC_collapse;
3321   };
3322   OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
3323       Clauses, std::move(CollapseFilter));
3324   if (I)
3325     return cast<OMPCollapseClause>(*I)->getNumForLoops();
3326   return nullptr;
3327 }
3328 
3329 static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
3330   auto &&OrderedFilter = [](const OMPClause *C) -> bool {
3331     return C->getClauseKind() == OMPC_ordered;
3332   };
3333   OMPExecutableDirective::filtered_clause_iterator<decltype(OrderedFilter)> I(
3334       Clauses, std::move(OrderedFilter));
3335   if (I)
3336     return cast<OMPOrderedClause>(*I)->getNumForLoops();
3337   return nullptr;
3338 }
3339 
3340 StmtResult Sema::ActOnOpenMPSimdDirective(
3341     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3342     SourceLocation EndLoc,
3343     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3344   OMPLoopDirective::HelperExprs B;
3345   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3346   // define the nested loops number.
3347   unsigned NestedLoopCount = CheckOpenMPLoop(
3348       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3349       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
3350   if (NestedLoopCount == 0)
3351     return StmtError();
3352 
3353   assert((CurContext->isDependentContext() || B.builtAll()) &&
3354          "omp simd loop exprs were not built");
3355 
3356   if (!CurContext->isDependentContext()) {
3357     // Finalize the clauses that need pre-built expressions for CodeGen.
3358     for (auto C : Clauses) {
3359       if (auto LC = dyn_cast<OMPLinearClause>(C))
3360         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3361                                      B.NumIterations, *this, CurScope))
3362           return StmtError();
3363     }
3364   }
3365 
3366   getCurFunction()->setHasBranchProtectedScope();
3367   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3368                                   Clauses, AStmt, B);
3369 }
3370 
3371 StmtResult Sema::ActOnOpenMPForDirective(
3372     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3373     SourceLocation EndLoc,
3374     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3375   OMPLoopDirective::HelperExprs B;
3376   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3377   // define the nested loops number.
3378   unsigned NestedLoopCount = CheckOpenMPLoop(
3379       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3380       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
3381   if (NestedLoopCount == 0)
3382     return StmtError();
3383 
3384   assert((CurContext->isDependentContext() || B.builtAll()) &&
3385          "omp for loop exprs were not built");
3386 
3387   if (!CurContext->isDependentContext()) {
3388     // Finalize the clauses that need pre-built expressions for CodeGen.
3389     for (auto C : Clauses) {
3390       if (auto LC = dyn_cast<OMPLinearClause>(C))
3391         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3392                                      B.NumIterations, *this, CurScope))
3393           return StmtError();
3394     }
3395   }
3396 
3397   getCurFunction()->setHasBranchProtectedScope();
3398   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3399                                  Clauses, AStmt, B);
3400 }
3401 
3402 StmtResult Sema::ActOnOpenMPForSimdDirective(
3403     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3404     SourceLocation EndLoc,
3405     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3406   OMPLoopDirective::HelperExprs B;
3407   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3408   // define the nested loops number.
3409   unsigned NestedLoopCount =
3410       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3411                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3412                       VarsWithImplicitDSA, B);
3413   if (NestedLoopCount == 0)
3414     return StmtError();
3415 
3416   assert((CurContext->isDependentContext() || B.builtAll()) &&
3417          "omp for simd loop exprs were not built");
3418 
3419   if (!CurContext->isDependentContext()) {
3420     // Finalize the clauses that need pre-built expressions for CodeGen.
3421     for (auto C : Clauses) {
3422       if (auto LC = dyn_cast<OMPLinearClause>(C))
3423         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3424                                      B.NumIterations, *this, CurScope))
3425           return StmtError();
3426     }
3427   }
3428 
3429   getCurFunction()->setHasBranchProtectedScope();
3430   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3431                                      Clauses, AStmt, B);
3432 }
3433 
3434 StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3435                                               Stmt *AStmt,
3436                                               SourceLocation StartLoc,
3437                                               SourceLocation EndLoc) {
3438   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3439   auto BaseStmt = AStmt;
3440   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3441     BaseStmt = CS->getCapturedStmt();
3442   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3443     auto S = C->children();
3444     if (S.begin() == S.end())
3445       return StmtError();
3446     // All associated statements must be '#pragma omp section' except for
3447     // the first one.
3448     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
3449       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3450         if (SectionStmt)
3451           Diag(SectionStmt->getLocStart(),
3452                diag::err_omp_sections_substmt_not_section);
3453         return StmtError();
3454       }
3455     }
3456   } else {
3457     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3458     return StmtError();
3459   }
3460 
3461   getCurFunction()->setHasBranchProtectedScope();
3462 
3463   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3464                                       AStmt);
3465 }
3466 
3467 StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3468                                              SourceLocation StartLoc,
3469                                              SourceLocation EndLoc) {
3470   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3471 
3472   getCurFunction()->setHasBranchProtectedScope();
3473 
3474   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3475 }
3476 
3477 StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3478                                             Stmt *AStmt,
3479                                             SourceLocation StartLoc,
3480                                             SourceLocation EndLoc) {
3481   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3482 
3483   getCurFunction()->setHasBranchProtectedScope();
3484 
3485   // OpenMP [2.7.3, single Construct, Restrictions]
3486   // The copyprivate clause must not be used with the nowait clause.
3487   OMPClause *Nowait = nullptr;
3488   OMPClause *Copyprivate = nullptr;
3489   for (auto *Clause : Clauses) {
3490     if (Clause->getClauseKind() == OMPC_nowait)
3491       Nowait = Clause;
3492     else if (Clause->getClauseKind() == OMPC_copyprivate)
3493       Copyprivate = Clause;
3494     if (Copyprivate && Nowait) {
3495       Diag(Copyprivate->getLocStart(),
3496            diag::err_omp_single_copyprivate_with_nowait);
3497       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3498       return StmtError();
3499     }
3500   }
3501 
3502   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3503 }
3504 
3505 StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3506                                             SourceLocation StartLoc,
3507                                             SourceLocation EndLoc) {
3508   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3509 
3510   getCurFunction()->setHasBranchProtectedScope();
3511 
3512   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3513 }
3514 
3515 StmtResult
3516 Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3517                                    Stmt *AStmt, SourceLocation StartLoc,
3518                                    SourceLocation EndLoc) {
3519   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3520 
3521   getCurFunction()->setHasBranchProtectedScope();
3522 
3523   return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3524                                       AStmt);
3525 }
3526 
3527 StmtResult Sema::ActOnOpenMPParallelForDirective(
3528     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3529     SourceLocation EndLoc,
3530     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3531   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3532   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3533   // 1.2.2 OpenMP Language Terminology
3534   // Structured block - An executable statement with a single entry at the
3535   // top and a single exit at the bottom.
3536   // The point of exit cannot be a branch out of the structured block.
3537   // longjmp() and throw() must not violate the entry/exit criteria.
3538   CS->getCapturedDecl()->setNothrow();
3539 
3540   OMPLoopDirective::HelperExprs B;
3541   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3542   // define the nested loops number.
3543   unsigned NestedLoopCount =
3544       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3545                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3546                       VarsWithImplicitDSA, B);
3547   if (NestedLoopCount == 0)
3548     return StmtError();
3549 
3550   assert((CurContext->isDependentContext() || B.builtAll()) &&
3551          "omp parallel for loop exprs were not built");
3552 
3553   if (!CurContext->isDependentContext()) {
3554     // Finalize the clauses that need pre-built expressions for CodeGen.
3555     for (auto C : Clauses) {
3556       if (auto LC = dyn_cast<OMPLinearClause>(C))
3557         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3558                                      B.NumIterations, *this, CurScope))
3559           return StmtError();
3560     }
3561   }
3562 
3563   getCurFunction()->setHasBranchProtectedScope();
3564   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3565                                          NestedLoopCount, Clauses, AStmt, B);
3566 }
3567 
3568 StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3569     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3570     SourceLocation EndLoc,
3571     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3572   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3573   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3574   // 1.2.2 OpenMP Language Terminology
3575   // Structured block - An executable statement with a single entry at the
3576   // top and a single exit at the bottom.
3577   // The point of exit cannot be a branch out of the structured block.
3578   // longjmp() and throw() must not violate the entry/exit criteria.
3579   CS->getCapturedDecl()->setNothrow();
3580 
3581   OMPLoopDirective::HelperExprs B;
3582   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3583   // define the nested loops number.
3584   unsigned NestedLoopCount =
3585       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3586                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3587                       VarsWithImplicitDSA, B);
3588   if (NestedLoopCount == 0)
3589     return StmtError();
3590 
3591   if (!CurContext->isDependentContext()) {
3592     // Finalize the clauses that need pre-built expressions for CodeGen.
3593     for (auto C : Clauses) {
3594       if (auto LC = dyn_cast<OMPLinearClause>(C))
3595         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3596                                      B.NumIterations, *this, CurScope))
3597           return StmtError();
3598     }
3599   }
3600 
3601   getCurFunction()->setHasBranchProtectedScope();
3602   return OMPParallelForSimdDirective::Create(
3603       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
3604 }
3605 
3606 StmtResult
3607 Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3608                                            Stmt *AStmt, SourceLocation StartLoc,
3609                                            SourceLocation EndLoc) {
3610   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3611   auto BaseStmt = AStmt;
3612   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3613     BaseStmt = CS->getCapturedStmt();
3614   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3615     auto S = C->children();
3616     if (S.begin() == S.end())
3617       return StmtError();
3618     // All associated statements must be '#pragma omp section' except for
3619     // the first one.
3620     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
3621       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3622         if (SectionStmt)
3623           Diag(SectionStmt->getLocStart(),
3624                diag::err_omp_parallel_sections_substmt_not_section);
3625         return StmtError();
3626       }
3627     }
3628   } else {
3629     Diag(AStmt->getLocStart(),
3630          diag::err_omp_parallel_sections_not_compound_stmt);
3631     return StmtError();
3632   }
3633 
3634   getCurFunction()->setHasBranchProtectedScope();
3635 
3636   return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3637                                               Clauses, AStmt);
3638 }
3639 
3640 StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3641                                           Stmt *AStmt, SourceLocation StartLoc,
3642                                           SourceLocation EndLoc) {
3643   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3644   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3645   // 1.2.2 OpenMP Language Terminology
3646   // Structured block - An executable statement with a single entry at the
3647   // top and a single exit at the bottom.
3648   // The point of exit cannot be a branch out of the structured block.
3649   // longjmp() and throw() must not violate the entry/exit criteria.
3650   CS->getCapturedDecl()->setNothrow();
3651 
3652   getCurFunction()->setHasBranchProtectedScope();
3653 
3654   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3655 }
3656 
3657 StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3658                                                SourceLocation EndLoc) {
3659   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3660 }
3661 
3662 StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3663                                              SourceLocation EndLoc) {
3664   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3665 }
3666 
3667 StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3668                                               SourceLocation EndLoc) {
3669   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3670 }
3671 
3672 StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3673                                                SourceLocation StartLoc,
3674                                                SourceLocation EndLoc) {
3675   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3676 
3677   getCurFunction()->setHasBranchProtectedScope();
3678 
3679   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3680 }
3681 
3682 StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3683                                            SourceLocation StartLoc,
3684                                            SourceLocation EndLoc) {
3685   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3686   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3687 }
3688 
3689 StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3690                                              SourceLocation StartLoc,
3691                                              SourceLocation EndLoc) {
3692   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3693 
3694   getCurFunction()->setHasBranchProtectedScope();
3695 
3696   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3697 }
3698 
3699 namespace {
3700 /// \brief Helper class for checking expression in 'omp atomic [update]'
3701 /// construct.
3702 class OpenMPAtomicUpdateChecker {
3703   /// \brief Error results for atomic update expressions.
3704   enum ExprAnalysisErrorCode {
3705     /// \brief A statement is not an expression statement.
3706     NotAnExpression,
3707     /// \brief Expression is not builtin binary or unary operation.
3708     NotABinaryOrUnaryExpression,
3709     /// \brief Unary operation is not post-/pre- increment/decrement operation.
3710     NotAnUnaryIncDecExpression,
3711     /// \brief An expression is not of scalar type.
3712     NotAScalarType,
3713     /// \brief A binary operation is not an assignment operation.
3714     NotAnAssignmentOp,
3715     /// \brief RHS part of the binary operation is not a binary expression.
3716     NotABinaryExpression,
3717     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3718     /// expression.
3719     NotABinaryOperator,
3720     /// \brief RHS binary operation does not have reference to the updated LHS
3721     /// part.
3722     NotAnUpdateExpression,
3723     /// \brief No errors is found.
3724     NoError
3725   };
3726   /// \brief Reference to Sema.
3727   Sema &SemaRef;
3728   /// \brief A location for note diagnostics (when error is found).
3729   SourceLocation NoteLoc;
3730   /// \brief 'x' lvalue part of the source atomic expression.
3731   Expr *X;
3732   /// \brief 'expr' rvalue part of the source atomic expression.
3733   Expr *E;
3734   /// \brief Helper expression of the form
3735   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3736   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3737   Expr *UpdateExpr;
3738   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3739   /// important for non-associative operations.
3740   bool IsXLHSInRHSPart;
3741   BinaryOperatorKind Op;
3742   SourceLocation OpLoc;
3743   /// \brief true if the source expression is a postfix unary operation, false
3744   /// if it is a prefix unary operation.
3745   bool IsPostfixUpdate;
3746 
3747 public:
3748   OpenMPAtomicUpdateChecker(Sema &SemaRef)
3749       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
3750         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
3751   /// \brief Check specified statement that it is suitable for 'atomic update'
3752   /// constructs and extract 'x', 'expr' and Operation from the original
3753   /// expression. If DiagId and NoteId == 0, then only check is performed
3754   /// without error notification.
3755   /// \param DiagId Diagnostic which should be emitted if error is found.
3756   /// \param NoteId Diagnostic note for the main error message.
3757   /// \return true if statement is not an update expression, false otherwise.
3758   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
3759   /// \brief Return the 'x' lvalue part of the source atomic expression.
3760   Expr *getX() const { return X; }
3761   /// \brief Return the 'expr' rvalue part of the source atomic expression.
3762   Expr *getExpr() const { return E; }
3763   /// \brief Return the update expression used in calculation of the updated
3764   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3765   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3766   Expr *getUpdateExpr() const { return UpdateExpr; }
3767   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3768   /// false otherwise.
3769   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3770 
3771   /// \brief true if the source expression is a postfix unary operation, false
3772   /// if it is a prefix unary operation.
3773   bool isPostfixUpdate() const { return IsPostfixUpdate; }
3774 
3775 private:
3776   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3777                             unsigned NoteId = 0);
3778 };
3779 } // namespace
3780 
3781 bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3782     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3783   ExprAnalysisErrorCode ErrorFound = NoError;
3784   SourceLocation ErrorLoc, NoteLoc;
3785   SourceRange ErrorRange, NoteRange;
3786   // Allowed constructs are:
3787   //  x = x binop expr;
3788   //  x = expr binop x;
3789   if (AtomicBinOp->getOpcode() == BO_Assign) {
3790     X = AtomicBinOp->getLHS();
3791     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3792             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3793       if (AtomicInnerBinOp->isMultiplicativeOp() ||
3794           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3795           AtomicInnerBinOp->isBitwiseOp()) {
3796         Op = AtomicInnerBinOp->getOpcode();
3797         OpLoc = AtomicInnerBinOp->getOperatorLoc();
3798         auto *LHS = AtomicInnerBinOp->getLHS();
3799         auto *RHS = AtomicInnerBinOp->getRHS();
3800         llvm::FoldingSetNodeID XId, LHSId, RHSId;
3801         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3802                                           /*Canonical=*/true);
3803         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3804                                             /*Canonical=*/true);
3805         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3806                                             /*Canonical=*/true);
3807         if (XId == LHSId) {
3808           E = RHS;
3809           IsXLHSInRHSPart = true;
3810         } else if (XId == RHSId) {
3811           E = LHS;
3812           IsXLHSInRHSPart = false;
3813         } else {
3814           ErrorLoc = AtomicInnerBinOp->getExprLoc();
3815           ErrorRange = AtomicInnerBinOp->getSourceRange();
3816           NoteLoc = X->getExprLoc();
3817           NoteRange = X->getSourceRange();
3818           ErrorFound = NotAnUpdateExpression;
3819         }
3820       } else {
3821         ErrorLoc = AtomicInnerBinOp->getExprLoc();
3822         ErrorRange = AtomicInnerBinOp->getSourceRange();
3823         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3824         NoteRange = SourceRange(NoteLoc, NoteLoc);
3825         ErrorFound = NotABinaryOperator;
3826       }
3827     } else {
3828       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3829       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3830       ErrorFound = NotABinaryExpression;
3831     }
3832   } else {
3833     ErrorLoc = AtomicBinOp->getExprLoc();
3834     ErrorRange = AtomicBinOp->getSourceRange();
3835     NoteLoc = AtomicBinOp->getOperatorLoc();
3836     NoteRange = SourceRange(NoteLoc, NoteLoc);
3837     ErrorFound = NotAnAssignmentOp;
3838   }
3839   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
3840     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3841     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3842     return true;
3843   } else if (SemaRef.CurContext->isDependentContext())
3844     E = X = UpdateExpr = nullptr;
3845   return ErrorFound != NoError;
3846 }
3847 
3848 bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3849                                                unsigned NoteId) {
3850   ExprAnalysisErrorCode ErrorFound = NoError;
3851   SourceLocation ErrorLoc, NoteLoc;
3852   SourceRange ErrorRange, NoteRange;
3853   // Allowed constructs are:
3854   //  x++;
3855   //  x--;
3856   //  ++x;
3857   //  --x;
3858   //  x binop= expr;
3859   //  x = x binop expr;
3860   //  x = expr binop x;
3861   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3862     AtomicBody = AtomicBody->IgnoreParenImpCasts();
3863     if (AtomicBody->getType()->isScalarType() ||
3864         AtomicBody->isInstantiationDependent()) {
3865       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3866               AtomicBody->IgnoreParenImpCasts())) {
3867         // Check for Compound Assignment Operation
3868         Op = BinaryOperator::getOpForCompoundAssignment(
3869             AtomicCompAssignOp->getOpcode());
3870         OpLoc = AtomicCompAssignOp->getOperatorLoc();
3871         E = AtomicCompAssignOp->getRHS();
3872         X = AtomicCompAssignOp->getLHS();
3873         IsXLHSInRHSPart = true;
3874       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3875                      AtomicBody->IgnoreParenImpCasts())) {
3876         // Check for Binary Operation
3877         if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3878           return true;
3879       } else if (auto *AtomicUnaryOp =
3880                  dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3881         // Check for Unary Operation
3882         if (AtomicUnaryOp->isIncrementDecrementOp()) {
3883           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
3884           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3885           OpLoc = AtomicUnaryOp->getOperatorLoc();
3886           X = AtomicUnaryOp->getSubExpr();
3887           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3888           IsXLHSInRHSPart = true;
3889         } else {
3890           ErrorFound = NotAnUnaryIncDecExpression;
3891           ErrorLoc = AtomicUnaryOp->getExprLoc();
3892           ErrorRange = AtomicUnaryOp->getSourceRange();
3893           NoteLoc = AtomicUnaryOp->getOperatorLoc();
3894           NoteRange = SourceRange(NoteLoc, NoteLoc);
3895         }
3896       } else {
3897         ErrorFound = NotABinaryOrUnaryExpression;
3898         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3899         NoteRange = ErrorRange = AtomicBody->getSourceRange();
3900       }
3901     } else {
3902       ErrorFound = NotAScalarType;
3903       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3904       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3905     }
3906   } else {
3907     ErrorFound = NotAnExpression;
3908     NoteLoc = ErrorLoc = S->getLocStart();
3909     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3910   }
3911   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
3912     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3913     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3914     return true;
3915   } else if (SemaRef.CurContext->isDependentContext())
3916     E = X = UpdateExpr = nullptr;
3917   if (ErrorFound == NoError && E && X) {
3918     // Build an update expression of form 'OpaqueValueExpr(x) binop
3919     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3920     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3921     auto *OVEX = new (SemaRef.getASTContext())
3922         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3923     auto *OVEExpr = new (SemaRef.getASTContext())
3924         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3925     auto Update =
3926         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3927                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
3928     if (Update.isInvalid())
3929       return true;
3930     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3931                                                Sema::AA_Casting);
3932     if (Update.isInvalid())
3933       return true;
3934     UpdateExpr = Update.get();
3935   }
3936   return ErrorFound != NoError;
3937 }
3938 
3939 StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3940                                             Stmt *AStmt,
3941                                             SourceLocation StartLoc,
3942                                             SourceLocation EndLoc) {
3943   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3944   auto CS = cast<CapturedStmt>(AStmt);
3945   // 1.2.2 OpenMP Language Terminology
3946   // Structured block - An executable statement with a single entry at the
3947   // top and a single exit at the bottom.
3948   // The point of exit cannot be a branch out of the structured block.
3949   // longjmp() and throw() must not violate the entry/exit criteria.
3950   OpenMPClauseKind AtomicKind = OMPC_unknown;
3951   SourceLocation AtomicKindLoc;
3952   for (auto *C : Clauses) {
3953     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
3954         C->getClauseKind() == OMPC_update ||
3955         C->getClauseKind() == OMPC_capture) {
3956       if (AtomicKind != OMPC_unknown) {
3957         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3958             << SourceRange(C->getLocStart(), C->getLocEnd());
3959         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3960             << getOpenMPClauseName(AtomicKind);
3961       } else {
3962         AtomicKind = C->getClauseKind();
3963         AtomicKindLoc = C->getLocStart();
3964       }
3965     }
3966   }
3967 
3968   auto Body = CS->getCapturedStmt();
3969   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3970     Body = EWC->getSubExpr();
3971 
3972   Expr *X = nullptr;
3973   Expr *V = nullptr;
3974   Expr *E = nullptr;
3975   Expr *UE = nullptr;
3976   bool IsXLHSInRHSPart = false;
3977   bool IsPostfixUpdate = false;
3978   // OpenMP [2.12.6, atomic Construct]
3979   // In the next expressions:
3980   // * x and v (as applicable) are both l-value expressions with scalar type.
3981   // * During the execution of an atomic region, multiple syntactic
3982   // occurrences of x must designate the same storage location.
3983   // * Neither of v and expr (as applicable) may access the storage location
3984   // designated by x.
3985   // * Neither of x and expr (as applicable) may access the storage location
3986   // designated by v.
3987   // * expr is an expression with scalar type.
3988   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3989   // * binop, binop=, ++, and -- are not overloaded operators.
3990   // * The expression x binop expr must be numerically equivalent to x binop
3991   // (expr). This requirement is satisfied if the operators in expr have
3992   // precedence greater than binop, or by using parentheses around expr or
3993   // subexpressions of expr.
3994   // * The expression expr binop x must be numerically equivalent to (expr)
3995   // binop x. This requirement is satisfied if the operators in expr have
3996   // precedence equal to or greater than binop, or by using parentheses around
3997   // expr or subexpressions of expr.
3998   // * For forms that allow multiple occurrences of x, the number of times
3999   // that x is evaluated is unspecified.
4000   if (AtomicKind == OMPC_read) {
4001     enum {
4002       NotAnExpression,
4003       NotAnAssignmentOp,
4004       NotAScalarType,
4005       NotAnLValue,
4006       NoError
4007     } ErrorFound = NoError;
4008     SourceLocation ErrorLoc, NoteLoc;
4009     SourceRange ErrorRange, NoteRange;
4010     // If clause is read:
4011     //  v = x;
4012     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4013       auto AtomicBinOp =
4014           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4015       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4016         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4017         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4018         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4019             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4020           if (!X->isLValue() || !V->isLValue()) {
4021             auto NotLValueExpr = X->isLValue() ? V : X;
4022             ErrorFound = NotAnLValue;
4023             ErrorLoc = AtomicBinOp->getExprLoc();
4024             ErrorRange = AtomicBinOp->getSourceRange();
4025             NoteLoc = NotLValueExpr->getExprLoc();
4026             NoteRange = NotLValueExpr->getSourceRange();
4027           }
4028         } else if (!X->isInstantiationDependent() ||
4029                    !V->isInstantiationDependent()) {
4030           auto NotScalarExpr =
4031               (X->isInstantiationDependent() || X->getType()->isScalarType())
4032                   ? V
4033                   : X;
4034           ErrorFound = NotAScalarType;
4035           ErrorLoc = AtomicBinOp->getExprLoc();
4036           ErrorRange = AtomicBinOp->getSourceRange();
4037           NoteLoc = NotScalarExpr->getExprLoc();
4038           NoteRange = NotScalarExpr->getSourceRange();
4039         }
4040       } else {
4041         ErrorFound = NotAnAssignmentOp;
4042         ErrorLoc = AtomicBody->getExprLoc();
4043         ErrorRange = AtomicBody->getSourceRange();
4044         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4045                               : AtomicBody->getExprLoc();
4046         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4047                                 : AtomicBody->getSourceRange();
4048       }
4049     } else {
4050       ErrorFound = NotAnExpression;
4051       NoteLoc = ErrorLoc = Body->getLocStart();
4052       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4053     }
4054     if (ErrorFound != NoError) {
4055       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4056           << ErrorRange;
4057       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4058                                                       << NoteRange;
4059       return StmtError();
4060     } else if (CurContext->isDependentContext())
4061       V = X = nullptr;
4062   } else if (AtomicKind == OMPC_write) {
4063     enum {
4064       NotAnExpression,
4065       NotAnAssignmentOp,
4066       NotAScalarType,
4067       NotAnLValue,
4068       NoError
4069     } ErrorFound = NoError;
4070     SourceLocation ErrorLoc, NoteLoc;
4071     SourceRange ErrorRange, NoteRange;
4072     // If clause is write:
4073     //  x = expr;
4074     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4075       auto AtomicBinOp =
4076           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4077       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4078         X = AtomicBinOp->getLHS();
4079         E = AtomicBinOp->getRHS();
4080         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4081             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4082           if (!X->isLValue()) {
4083             ErrorFound = NotAnLValue;
4084             ErrorLoc = AtomicBinOp->getExprLoc();
4085             ErrorRange = AtomicBinOp->getSourceRange();
4086             NoteLoc = X->getExprLoc();
4087             NoteRange = X->getSourceRange();
4088           }
4089         } else if (!X->isInstantiationDependent() ||
4090                    !E->isInstantiationDependent()) {
4091           auto NotScalarExpr =
4092               (X->isInstantiationDependent() || X->getType()->isScalarType())
4093                   ? E
4094                   : X;
4095           ErrorFound = NotAScalarType;
4096           ErrorLoc = AtomicBinOp->getExprLoc();
4097           ErrorRange = AtomicBinOp->getSourceRange();
4098           NoteLoc = NotScalarExpr->getExprLoc();
4099           NoteRange = NotScalarExpr->getSourceRange();
4100         }
4101       } else {
4102         ErrorFound = NotAnAssignmentOp;
4103         ErrorLoc = AtomicBody->getExprLoc();
4104         ErrorRange = AtomicBody->getSourceRange();
4105         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4106                               : AtomicBody->getExprLoc();
4107         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4108                                 : AtomicBody->getSourceRange();
4109       }
4110     } else {
4111       ErrorFound = NotAnExpression;
4112       NoteLoc = ErrorLoc = Body->getLocStart();
4113       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4114     }
4115     if (ErrorFound != NoError) {
4116       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4117           << ErrorRange;
4118       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4119                                                       << NoteRange;
4120       return StmtError();
4121     } else if (CurContext->isDependentContext())
4122       E = X = nullptr;
4123   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
4124     // If clause is update:
4125     //  x++;
4126     //  x--;
4127     //  ++x;
4128     //  --x;
4129     //  x binop= expr;
4130     //  x = x binop expr;
4131     //  x = expr binop x;
4132     OpenMPAtomicUpdateChecker Checker(*this);
4133     if (Checker.checkStatement(
4134             Body, (AtomicKind == OMPC_update)
4135                       ? diag::err_omp_atomic_update_not_expression_statement
4136                       : diag::err_omp_atomic_not_expression_statement,
4137             diag::note_omp_atomic_update))
4138       return StmtError();
4139     if (!CurContext->isDependentContext()) {
4140       E = Checker.getExpr();
4141       X = Checker.getX();
4142       UE = Checker.getUpdateExpr();
4143       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4144     }
4145   } else if (AtomicKind == OMPC_capture) {
4146     enum {
4147       NotAnAssignmentOp,
4148       NotACompoundStatement,
4149       NotTwoSubstatements,
4150       NotASpecificExpression,
4151       NoError
4152     } ErrorFound = NoError;
4153     SourceLocation ErrorLoc, NoteLoc;
4154     SourceRange ErrorRange, NoteRange;
4155     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4156       // If clause is a capture:
4157       //  v = x++;
4158       //  v = x--;
4159       //  v = ++x;
4160       //  v = --x;
4161       //  v = x binop= expr;
4162       //  v = x = x binop expr;
4163       //  v = x = expr binop x;
4164       auto *AtomicBinOp =
4165           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4166       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4167         V = AtomicBinOp->getLHS();
4168         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4169         OpenMPAtomicUpdateChecker Checker(*this);
4170         if (Checker.checkStatement(
4171                 Body, diag::err_omp_atomic_capture_not_expression_statement,
4172                 diag::note_omp_atomic_update))
4173           return StmtError();
4174         E = Checker.getExpr();
4175         X = Checker.getX();
4176         UE = Checker.getUpdateExpr();
4177         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4178         IsPostfixUpdate = Checker.isPostfixUpdate();
4179       } else {
4180         ErrorLoc = AtomicBody->getExprLoc();
4181         ErrorRange = AtomicBody->getSourceRange();
4182         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4183                               : AtomicBody->getExprLoc();
4184         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4185                                 : AtomicBody->getSourceRange();
4186         ErrorFound = NotAnAssignmentOp;
4187       }
4188       if (ErrorFound != NoError) {
4189         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4190             << ErrorRange;
4191         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4192         return StmtError();
4193       } else if (CurContext->isDependentContext()) {
4194         UE = V = E = X = nullptr;
4195       }
4196     } else {
4197       // If clause is a capture:
4198       //  { v = x; x = expr; }
4199       //  { v = x; x++; }
4200       //  { v = x; x--; }
4201       //  { v = x; ++x; }
4202       //  { v = x; --x; }
4203       //  { v = x; x binop= expr; }
4204       //  { v = x; x = x binop expr; }
4205       //  { v = x; x = expr binop x; }
4206       //  { x++; v = x; }
4207       //  { x--; v = x; }
4208       //  { ++x; v = x; }
4209       //  { --x; v = x; }
4210       //  { x binop= expr; v = x; }
4211       //  { x = x binop expr; v = x; }
4212       //  { x = expr binop x; v = x; }
4213       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4214         // Check that this is { expr1; expr2; }
4215         if (CS->size() == 2) {
4216           auto *First = CS->body_front();
4217           auto *Second = CS->body_back();
4218           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4219             First = EWC->getSubExpr()->IgnoreParenImpCasts();
4220           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4221             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4222           // Need to find what subexpression is 'v' and what is 'x'.
4223           OpenMPAtomicUpdateChecker Checker(*this);
4224           bool IsUpdateExprFound = !Checker.checkStatement(Second);
4225           BinaryOperator *BinOp = nullptr;
4226           if (IsUpdateExprFound) {
4227             BinOp = dyn_cast<BinaryOperator>(First);
4228             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4229           }
4230           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4231             //  { v = x; x++; }
4232             //  { v = x; x--; }
4233             //  { v = x; ++x; }
4234             //  { v = x; --x; }
4235             //  { v = x; x binop= expr; }
4236             //  { v = x; x = x binop expr; }
4237             //  { v = x; x = expr binop x; }
4238             // Check that the first expression has form v = x.
4239             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4240             llvm::FoldingSetNodeID XId, PossibleXId;
4241             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4242             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4243             IsUpdateExprFound = XId == PossibleXId;
4244             if (IsUpdateExprFound) {
4245               V = BinOp->getLHS();
4246               X = Checker.getX();
4247               E = Checker.getExpr();
4248               UE = Checker.getUpdateExpr();
4249               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4250               IsPostfixUpdate = true;
4251             }
4252           }
4253           if (!IsUpdateExprFound) {
4254             IsUpdateExprFound = !Checker.checkStatement(First);
4255             BinOp = nullptr;
4256             if (IsUpdateExprFound) {
4257               BinOp = dyn_cast<BinaryOperator>(Second);
4258               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4259             }
4260             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4261               //  { x++; v = x; }
4262               //  { x--; v = x; }
4263               //  { ++x; v = x; }
4264               //  { --x; v = x; }
4265               //  { x binop= expr; v = x; }
4266               //  { x = x binop expr; v = x; }
4267               //  { x = expr binop x; v = x; }
4268               // Check that the second expression has form v = x.
4269               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4270               llvm::FoldingSetNodeID XId, PossibleXId;
4271               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4272               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4273               IsUpdateExprFound = XId == PossibleXId;
4274               if (IsUpdateExprFound) {
4275                 V = BinOp->getLHS();
4276                 X = Checker.getX();
4277                 E = Checker.getExpr();
4278                 UE = Checker.getUpdateExpr();
4279                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4280                 IsPostfixUpdate = false;
4281               }
4282             }
4283           }
4284           if (!IsUpdateExprFound) {
4285             //  { v = x; x = expr; }
4286             auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4287             if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4288               ErrorFound = NotAnAssignmentOp;
4289               NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4290                                               : First->getLocStart();
4291               NoteRange = ErrorRange = FirstBinOp
4292                                            ? FirstBinOp->getSourceRange()
4293                                            : SourceRange(ErrorLoc, ErrorLoc);
4294             } else {
4295               auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4296               if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4297                 ErrorFound = NotAnAssignmentOp;
4298                 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4299                                                  : Second->getLocStart();
4300                 NoteRange = ErrorRange = SecondBinOp
4301                                              ? SecondBinOp->getSourceRange()
4302                                              : SourceRange(ErrorLoc, ErrorLoc);
4303               } else {
4304                 auto *PossibleXRHSInFirst =
4305                     FirstBinOp->getRHS()->IgnoreParenImpCasts();
4306                 auto *PossibleXLHSInSecond =
4307                     SecondBinOp->getLHS()->IgnoreParenImpCasts();
4308                 llvm::FoldingSetNodeID X1Id, X2Id;
4309                 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4310                 PossibleXLHSInSecond->Profile(X2Id, Context,
4311                                               /*Canonical=*/true);
4312                 IsUpdateExprFound = X1Id == X2Id;
4313                 if (IsUpdateExprFound) {
4314                   V = FirstBinOp->getLHS();
4315                   X = SecondBinOp->getLHS();
4316                   E = SecondBinOp->getRHS();
4317                   UE = nullptr;
4318                   IsXLHSInRHSPart = false;
4319                   IsPostfixUpdate = true;
4320                 } else {
4321                   ErrorFound = NotASpecificExpression;
4322                   ErrorLoc = FirstBinOp->getExprLoc();
4323                   ErrorRange = FirstBinOp->getSourceRange();
4324                   NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4325                   NoteRange = SecondBinOp->getRHS()->getSourceRange();
4326                 }
4327               }
4328             }
4329           }
4330         } else {
4331           NoteLoc = ErrorLoc = Body->getLocStart();
4332           NoteRange = ErrorRange =
4333               SourceRange(Body->getLocStart(), Body->getLocStart());
4334           ErrorFound = NotTwoSubstatements;
4335         }
4336       } else {
4337         NoteLoc = ErrorLoc = Body->getLocStart();
4338         NoteRange = ErrorRange =
4339             SourceRange(Body->getLocStart(), Body->getLocStart());
4340         ErrorFound = NotACompoundStatement;
4341       }
4342       if (ErrorFound != NoError) {
4343         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4344             << ErrorRange;
4345         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4346         return StmtError();
4347       } else if (CurContext->isDependentContext()) {
4348         UE = V = E = X = nullptr;
4349       }
4350     }
4351   }
4352 
4353   getCurFunction()->setHasBranchProtectedScope();
4354 
4355   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4356                                     X, V, E, UE, IsXLHSInRHSPart,
4357                                     IsPostfixUpdate);
4358 }
4359 
4360 StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4361                                             Stmt *AStmt,
4362                                             SourceLocation StartLoc,
4363                                             SourceLocation EndLoc) {
4364   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4365 
4366   // OpenMP [2.16, Nesting of Regions]
4367   // If specified, a teams construct must be contained within a target
4368   // construct. That target construct must contain no statements or directives
4369   // outside of the teams construct.
4370   if (DSAStack->hasInnerTeamsRegion()) {
4371     auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4372     bool OMPTeamsFound = true;
4373     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4374       auto I = CS->body_begin();
4375       while (I != CS->body_end()) {
4376         auto OED = dyn_cast<OMPExecutableDirective>(*I);
4377         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4378           OMPTeamsFound = false;
4379           break;
4380         }
4381         ++I;
4382       }
4383       assert(I != CS->body_end() && "Not found statement");
4384       S = *I;
4385     }
4386     if (!OMPTeamsFound) {
4387       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4388       Diag(DSAStack->getInnerTeamsRegionLoc(),
4389            diag::note_omp_nested_teams_construct_here);
4390       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4391           << isa<OMPExecutableDirective>(S);
4392       return StmtError();
4393     }
4394   }
4395 
4396   getCurFunction()->setHasBranchProtectedScope();
4397 
4398   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4399 }
4400 
4401 StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4402                                                 Stmt *AStmt,
4403                                                 SourceLocation StartLoc,
4404                                                 SourceLocation EndLoc) {
4405   getCurFunction()->setHasBranchProtectedScope();
4406 
4407   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4408                                         AStmt);
4409 }
4410 
4411 StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4412                                            Stmt *AStmt, SourceLocation StartLoc,
4413                                            SourceLocation EndLoc) {
4414   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4415   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4416   // 1.2.2 OpenMP Language Terminology
4417   // Structured block - An executable statement with a single entry at the
4418   // top and a single exit at the bottom.
4419   // The point of exit cannot be a branch out of the structured block.
4420   // longjmp() and throw() must not violate the entry/exit criteria.
4421   CS->getCapturedDecl()->setNothrow();
4422 
4423   getCurFunction()->setHasBranchProtectedScope();
4424 
4425   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4426 }
4427 
4428 StmtResult
4429 Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4430                                             SourceLocation EndLoc,
4431                                             OpenMPDirectiveKind CancelRegion) {
4432   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4433       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4434     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4435         << getOpenMPDirectiveName(CancelRegion);
4436     return StmtError();
4437   }
4438   if (DSAStack->isParentNowaitRegion()) {
4439     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4440     return StmtError();
4441   }
4442   if (DSAStack->isParentOrderedRegion()) {
4443     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4444     return StmtError();
4445   }
4446   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4447                                                CancelRegion);
4448 }
4449 
4450 StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4451                                             SourceLocation EndLoc,
4452                                             OpenMPDirectiveKind CancelRegion) {
4453   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4454       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4455     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4456         << getOpenMPDirectiveName(CancelRegion);
4457     return StmtError();
4458   }
4459   if (DSAStack->isParentNowaitRegion()) {
4460     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4461     return StmtError();
4462   }
4463   if (DSAStack->isParentOrderedRegion()) {
4464     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4465     return StmtError();
4466   }
4467   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4468 }
4469 
4470 OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
4471                                              SourceLocation StartLoc,
4472                                              SourceLocation LParenLoc,
4473                                              SourceLocation EndLoc) {
4474   OMPClause *Res = nullptr;
4475   switch (Kind) {
4476   case OMPC_if:
4477     Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4478     break;
4479   case OMPC_final:
4480     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4481     break;
4482   case OMPC_num_threads:
4483     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4484     break;
4485   case OMPC_safelen:
4486     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4487     break;
4488   case OMPC_collapse:
4489     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4490     break;
4491   case OMPC_ordered:
4492     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4493     break;
4494   case OMPC_default:
4495   case OMPC_proc_bind:
4496   case OMPC_schedule:
4497   case OMPC_private:
4498   case OMPC_firstprivate:
4499   case OMPC_lastprivate:
4500   case OMPC_shared:
4501   case OMPC_reduction:
4502   case OMPC_linear:
4503   case OMPC_aligned:
4504   case OMPC_copyin:
4505   case OMPC_copyprivate:
4506   case OMPC_nowait:
4507   case OMPC_untied:
4508   case OMPC_mergeable:
4509   case OMPC_threadprivate:
4510   case OMPC_flush:
4511   case OMPC_read:
4512   case OMPC_write:
4513   case OMPC_update:
4514   case OMPC_capture:
4515   case OMPC_seq_cst:
4516   case OMPC_depend:
4517   case OMPC_unknown:
4518     llvm_unreachable("Clause is not allowed.");
4519   }
4520   return Res;
4521 }
4522 
4523 OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
4524                                      SourceLocation LParenLoc,
4525                                      SourceLocation EndLoc) {
4526   Expr *ValExpr = Condition;
4527   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4528       !Condition->isInstantiationDependent() &&
4529       !Condition->containsUnexpandedParameterPack()) {
4530     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4531                                            Condition->getExprLoc(), Condition);
4532     if (Val.isInvalid())
4533       return nullptr;
4534 
4535     ValExpr = Val.get();
4536   }
4537 
4538   return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4539 }
4540 
4541 OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4542                                         SourceLocation StartLoc,
4543                                         SourceLocation LParenLoc,
4544                                         SourceLocation EndLoc) {
4545   Expr *ValExpr = Condition;
4546   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4547       !Condition->isInstantiationDependent() &&
4548       !Condition->containsUnexpandedParameterPack()) {
4549     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4550                                            Condition->getExprLoc(), Condition);
4551     if (Val.isInvalid())
4552       return nullptr;
4553 
4554     ValExpr = Val.get();
4555   }
4556 
4557   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4558 }
4559 ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4560                                                         Expr *Op) {
4561   if (!Op)
4562     return ExprError();
4563 
4564   class IntConvertDiagnoser : public ICEConvertDiagnoser {
4565   public:
4566     IntConvertDiagnoser()
4567         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
4568     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4569                                          QualType T) override {
4570       return S.Diag(Loc, diag::err_omp_not_integral) << T;
4571     }
4572     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4573                                              QualType T) override {
4574       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4575     }
4576     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4577                                                QualType T,
4578                                                QualType ConvTy) override {
4579       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4580     }
4581     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4582                                            QualType ConvTy) override {
4583       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
4584              << ConvTy->isEnumeralType() << ConvTy;
4585     }
4586     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4587                                             QualType T) override {
4588       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4589     }
4590     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4591                                         QualType ConvTy) override {
4592       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
4593              << ConvTy->isEnumeralType() << ConvTy;
4594     }
4595     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4596                                              QualType) override {
4597       llvm_unreachable("conversion functions are permitted");
4598     }
4599   } ConvertDiagnoser;
4600   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4601 }
4602 
4603 OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4604                                              SourceLocation StartLoc,
4605                                              SourceLocation LParenLoc,
4606                                              SourceLocation EndLoc) {
4607   Expr *ValExpr = NumThreads;
4608   if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
4609       !NumThreads->containsUnexpandedParameterPack()) {
4610     SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4611     ExprResult Val =
4612         PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
4613     if (Val.isInvalid())
4614       return nullptr;
4615 
4616     ValExpr = Val.get();
4617 
4618     // OpenMP [2.5, Restrictions]
4619     //  The num_threads expression must evaluate to a positive integer value.
4620     llvm::APSInt Result;
4621     if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4622         !Result.isStrictlyPositive()) {
4623       Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4624           << "num_threads" << NumThreads->getSourceRange();
4625       return nullptr;
4626     }
4627   }
4628 
4629   return new (Context)
4630       OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4631 }
4632 
4633 ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4634                                                        OpenMPClauseKind CKind) {
4635   if (!E)
4636     return ExprError();
4637   if (E->isValueDependent() || E->isTypeDependent() ||
4638       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4639     return E;
4640   llvm::APSInt Result;
4641   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4642   if (ICE.isInvalid())
4643     return ExprError();
4644   if (!Result.isStrictlyPositive()) {
4645     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4646         << getOpenMPClauseName(CKind) << E->getSourceRange();
4647     return ExprError();
4648   }
4649   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4650     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4651         << E->getSourceRange();
4652     return ExprError();
4653   }
4654   if (CKind == OMPC_collapse) {
4655     DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4656                                 Result.getExtValue());
4657   } else if (CKind == OMPC_ordered) {
4658     DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4659                                 Result.getExtValue());
4660   }
4661   return ICE;
4662 }
4663 
4664 OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4665                                           SourceLocation LParenLoc,
4666                                           SourceLocation EndLoc) {
4667   // OpenMP [2.8.1, simd construct, Description]
4668   // The parameter of the safelen clause must be a constant
4669   // positive integer expression.
4670   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4671   if (Safelen.isInvalid())
4672     return nullptr;
4673   return new (Context)
4674       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
4675 }
4676 
4677 OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4678                                            SourceLocation StartLoc,
4679                                            SourceLocation LParenLoc,
4680                                            SourceLocation EndLoc) {
4681   // OpenMP [2.7.1, loop construct, Description]
4682   // OpenMP [2.8.1, simd construct, Description]
4683   // OpenMP [2.9.6, distribute construct, Description]
4684   // The parameter of the collapse clause must be a constant
4685   // positive integer expression.
4686   ExprResult NumForLoopsResult =
4687       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4688   if (NumForLoopsResult.isInvalid())
4689     return nullptr;
4690   return new (Context)
4691       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
4692 }
4693 
4694 OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4695                                           SourceLocation EndLoc,
4696                                           SourceLocation LParenLoc,
4697                                           Expr *NumForLoops) {
4698   DSAStack->setOrderedRegion();
4699   // OpenMP [2.7.1, loop construct, Description]
4700   // OpenMP [2.8.1, simd construct, Description]
4701   // OpenMP [2.9.6, distribute construct, Description]
4702   // The parameter of the ordered clause must be a constant
4703   // positive integer expression if any.
4704   if (NumForLoops && LParenLoc.isValid()) {
4705     ExprResult NumForLoopsResult =
4706         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
4707     if (NumForLoopsResult.isInvalid())
4708       return nullptr;
4709     NumForLoops = NumForLoopsResult.get();
4710   }
4711   return new (Context)
4712       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
4713 }
4714 
4715 OMPClause *Sema::ActOnOpenMPSimpleClause(
4716     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4717     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
4718   OMPClause *Res = nullptr;
4719   switch (Kind) {
4720   case OMPC_default:
4721     Res =
4722         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4723                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
4724     break;
4725   case OMPC_proc_bind:
4726     Res = ActOnOpenMPProcBindClause(
4727         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4728         LParenLoc, EndLoc);
4729     break;
4730   case OMPC_if:
4731   case OMPC_final:
4732   case OMPC_num_threads:
4733   case OMPC_safelen:
4734   case OMPC_collapse:
4735   case OMPC_schedule:
4736   case OMPC_private:
4737   case OMPC_firstprivate:
4738   case OMPC_lastprivate:
4739   case OMPC_shared:
4740   case OMPC_reduction:
4741   case OMPC_linear:
4742   case OMPC_aligned:
4743   case OMPC_copyin:
4744   case OMPC_copyprivate:
4745   case OMPC_ordered:
4746   case OMPC_nowait:
4747   case OMPC_untied:
4748   case OMPC_mergeable:
4749   case OMPC_threadprivate:
4750   case OMPC_flush:
4751   case OMPC_read:
4752   case OMPC_write:
4753   case OMPC_update:
4754   case OMPC_capture:
4755   case OMPC_seq_cst:
4756   case OMPC_depend:
4757   case OMPC_unknown:
4758     llvm_unreachable("Clause is not allowed.");
4759   }
4760   return Res;
4761 }
4762 
4763 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4764                                           SourceLocation KindKwLoc,
4765                                           SourceLocation StartLoc,
4766                                           SourceLocation LParenLoc,
4767                                           SourceLocation EndLoc) {
4768   if (Kind == OMPC_DEFAULT_unknown) {
4769     std::string Values;
4770     static_assert(OMPC_DEFAULT_unknown > 0,
4771                   "OMPC_DEFAULT_unknown not greater than 0");
4772     std::string Sep(", ");
4773     for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
4774       Values += "'";
4775       Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4776       Values += "'";
4777       switch (i) {
4778       case OMPC_DEFAULT_unknown - 2:
4779         Values += " or ";
4780         break;
4781       case OMPC_DEFAULT_unknown - 1:
4782         break;
4783       default:
4784         Values += Sep;
4785         break;
4786       }
4787     }
4788     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
4789         << Values << getOpenMPClauseName(OMPC_default);
4790     return nullptr;
4791   }
4792   switch (Kind) {
4793   case OMPC_DEFAULT_none:
4794     DSAStack->setDefaultDSANone(KindKwLoc);
4795     break;
4796   case OMPC_DEFAULT_shared:
4797     DSAStack->setDefaultDSAShared(KindKwLoc);
4798     break;
4799   case OMPC_DEFAULT_unknown:
4800     llvm_unreachable("Clause kind is not allowed.");
4801     break;
4802   }
4803   return new (Context)
4804       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
4805 }
4806 
4807 OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4808                                            SourceLocation KindKwLoc,
4809                                            SourceLocation StartLoc,
4810                                            SourceLocation LParenLoc,
4811                                            SourceLocation EndLoc) {
4812   if (Kind == OMPC_PROC_BIND_unknown) {
4813     std::string Values;
4814     std::string Sep(", ");
4815     for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4816       Values += "'";
4817       Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4818       Values += "'";
4819       switch (i) {
4820       case OMPC_PROC_BIND_unknown - 2:
4821         Values += " or ";
4822         break;
4823       case OMPC_PROC_BIND_unknown - 1:
4824         break;
4825       default:
4826         Values += Sep;
4827         break;
4828       }
4829     }
4830     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
4831         << Values << getOpenMPClauseName(OMPC_proc_bind);
4832     return nullptr;
4833   }
4834   return new (Context)
4835       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
4836 }
4837 
4838 OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4839     OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4840     SourceLocation StartLoc, SourceLocation LParenLoc,
4841     SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4842     SourceLocation EndLoc) {
4843   OMPClause *Res = nullptr;
4844   switch (Kind) {
4845   case OMPC_schedule:
4846     Res = ActOnOpenMPScheduleClause(
4847         static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4848         LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4849     break;
4850   case OMPC_if:
4851   case OMPC_final:
4852   case OMPC_num_threads:
4853   case OMPC_safelen:
4854   case OMPC_collapse:
4855   case OMPC_default:
4856   case OMPC_proc_bind:
4857   case OMPC_private:
4858   case OMPC_firstprivate:
4859   case OMPC_lastprivate:
4860   case OMPC_shared:
4861   case OMPC_reduction:
4862   case OMPC_linear:
4863   case OMPC_aligned:
4864   case OMPC_copyin:
4865   case OMPC_copyprivate:
4866   case OMPC_ordered:
4867   case OMPC_nowait:
4868   case OMPC_untied:
4869   case OMPC_mergeable:
4870   case OMPC_threadprivate:
4871   case OMPC_flush:
4872   case OMPC_read:
4873   case OMPC_write:
4874   case OMPC_update:
4875   case OMPC_capture:
4876   case OMPC_seq_cst:
4877   case OMPC_depend:
4878   case OMPC_unknown:
4879     llvm_unreachable("Clause is not allowed.");
4880   }
4881   return Res;
4882 }
4883 
4884 OMPClause *Sema::ActOnOpenMPScheduleClause(
4885     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4886     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4887     SourceLocation EndLoc) {
4888   if (Kind == OMPC_SCHEDULE_unknown) {
4889     std::string Values;
4890     std::string Sep(", ");
4891     for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4892       Values += "'";
4893       Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4894       Values += "'";
4895       switch (i) {
4896       case OMPC_SCHEDULE_unknown - 2:
4897         Values += " or ";
4898         break;
4899       case OMPC_SCHEDULE_unknown - 1:
4900         break;
4901       default:
4902         Values += Sep;
4903         break;
4904       }
4905     }
4906     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4907         << Values << getOpenMPClauseName(OMPC_schedule);
4908     return nullptr;
4909   }
4910   Expr *ValExpr = ChunkSize;
4911   Expr *HelperValExpr = nullptr;
4912   if (ChunkSize) {
4913     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4914         !ChunkSize->isInstantiationDependent() &&
4915         !ChunkSize->containsUnexpandedParameterPack()) {
4916       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4917       ExprResult Val =
4918           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4919       if (Val.isInvalid())
4920         return nullptr;
4921 
4922       ValExpr = Val.get();
4923 
4924       // OpenMP [2.7.1, Restrictions]
4925       //  chunk_size must be a loop invariant integer expression with a positive
4926       //  value.
4927       llvm::APSInt Result;
4928       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4929         if (Result.isSigned() && !Result.isStrictlyPositive()) {
4930           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4931               << "schedule" << ChunkSize->getSourceRange();
4932           return nullptr;
4933         }
4934       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4935         auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4936                                     ChunkSize->getType(), ".chunk.");
4937         auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4938                                            ChunkSize->getExprLoc(),
4939                                            /*RefersToCapture=*/true);
4940         HelperValExpr = ImpVarRef;
4941       }
4942     }
4943   }
4944 
4945   return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
4946                                          EndLoc, Kind, ValExpr, HelperValExpr);
4947 }
4948 
4949 OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4950                                    SourceLocation StartLoc,
4951                                    SourceLocation EndLoc) {
4952   OMPClause *Res = nullptr;
4953   switch (Kind) {
4954   case OMPC_ordered:
4955     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4956     break;
4957   case OMPC_nowait:
4958     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4959     break;
4960   case OMPC_untied:
4961     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4962     break;
4963   case OMPC_mergeable:
4964     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4965     break;
4966   case OMPC_read:
4967     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4968     break;
4969   case OMPC_write:
4970     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4971     break;
4972   case OMPC_update:
4973     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4974     break;
4975   case OMPC_capture:
4976     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4977     break;
4978   case OMPC_seq_cst:
4979     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4980     break;
4981   case OMPC_if:
4982   case OMPC_final:
4983   case OMPC_num_threads:
4984   case OMPC_safelen:
4985   case OMPC_collapse:
4986   case OMPC_schedule:
4987   case OMPC_private:
4988   case OMPC_firstprivate:
4989   case OMPC_lastprivate:
4990   case OMPC_shared:
4991   case OMPC_reduction:
4992   case OMPC_linear:
4993   case OMPC_aligned:
4994   case OMPC_copyin:
4995   case OMPC_copyprivate:
4996   case OMPC_default:
4997   case OMPC_proc_bind:
4998   case OMPC_threadprivate:
4999   case OMPC_flush:
5000   case OMPC_depend:
5001   case OMPC_unknown:
5002     llvm_unreachable("Clause is not allowed.");
5003   }
5004   return Res;
5005 }
5006 
5007 OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5008                                          SourceLocation EndLoc) {
5009   DSAStack->setNowaitRegion();
5010   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5011 }
5012 
5013 OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5014                                          SourceLocation EndLoc) {
5015   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5016 }
5017 
5018 OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5019                                             SourceLocation EndLoc) {
5020   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5021 }
5022 
5023 OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5024                                        SourceLocation EndLoc) {
5025   return new (Context) OMPReadClause(StartLoc, EndLoc);
5026 }
5027 
5028 OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5029                                         SourceLocation EndLoc) {
5030   return new (Context) OMPWriteClause(StartLoc, EndLoc);
5031 }
5032 
5033 OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5034                                          SourceLocation EndLoc) {
5035   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5036 }
5037 
5038 OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5039                                           SourceLocation EndLoc) {
5040   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5041 }
5042 
5043 OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5044                                          SourceLocation EndLoc) {
5045   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5046 }
5047 
5048 OMPClause *Sema::ActOnOpenMPVarListClause(
5049     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5050     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5051     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
5052     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
5053     SourceLocation DepLoc) {
5054   OMPClause *Res = nullptr;
5055   switch (Kind) {
5056   case OMPC_private:
5057     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5058     break;
5059   case OMPC_firstprivate:
5060     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5061     break;
5062   case OMPC_lastprivate:
5063     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5064     break;
5065   case OMPC_shared:
5066     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5067     break;
5068   case OMPC_reduction:
5069     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5070                                      EndLoc, ReductionIdScopeSpec, ReductionId);
5071     break;
5072   case OMPC_linear:
5073     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
5074                                   ColonLoc, EndLoc);
5075     break;
5076   case OMPC_aligned:
5077     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5078                                    ColonLoc, EndLoc);
5079     break;
5080   case OMPC_copyin:
5081     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5082     break;
5083   case OMPC_copyprivate:
5084     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5085     break;
5086   case OMPC_flush:
5087     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5088     break;
5089   case OMPC_depend:
5090     Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
5091                                   LParenLoc, EndLoc);
5092     break;
5093   case OMPC_if:
5094   case OMPC_final:
5095   case OMPC_num_threads:
5096   case OMPC_safelen:
5097   case OMPC_collapse:
5098   case OMPC_default:
5099   case OMPC_proc_bind:
5100   case OMPC_schedule:
5101   case OMPC_ordered:
5102   case OMPC_nowait:
5103   case OMPC_untied:
5104   case OMPC_mergeable:
5105   case OMPC_threadprivate:
5106   case OMPC_read:
5107   case OMPC_write:
5108   case OMPC_update:
5109   case OMPC_capture:
5110   case OMPC_seq_cst:
5111   case OMPC_unknown:
5112     llvm_unreachable("Clause is not allowed.");
5113   }
5114   return Res;
5115 }
5116 
5117 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5118                                           SourceLocation StartLoc,
5119                                           SourceLocation LParenLoc,
5120                                           SourceLocation EndLoc) {
5121   SmallVector<Expr *, 8> Vars;
5122   SmallVector<Expr *, 8> PrivateCopies;
5123   for (auto &RefExpr : VarList) {
5124     assert(RefExpr && "NULL expr in OpenMP private clause.");
5125     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5126       // It will be analyzed later.
5127       Vars.push_back(RefExpr);
5128       PrivateCopies.push_back(nullptr);
5129       continue;
5130     }
5131 
5132     SourceLocation ELoc = RefExpr->getExprLoc();
5133     // OpenMP [2.1, C/C++]
5134     //  A list item is a variable name.
5135     // OpenMP  [2.9.3.3, Restrictions, p.1]
5136     //  A variable that is part of another variable (as an array or
5137     //  structure element) cannot appear in a private clause.
5138     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5139     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5140       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5141       continue;
5142     }
5143     Decl *D = DE->getDecl();
5144     VarDecl *VD = cast<VarDecl>(D);
5145 
5146     QualType Type = VD->getType();
5147     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5148       // It will be analyzed later.
5149       Vars.push_back(DE);
5150       PrivateCopies.push_back(nullptr);
5151       continue;
5152     }
5153 
5154     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5155     //  A variable that appears in a private clause must not have an incomplete
5156     //  type or a reference type.
5157     if (RequireCompleteType(ELoc, Type,
5158                             diag::err_omp_private_incomplete_type)) {
5159       continue;
5160     }
5161     if (Type->isReferenceType()) {
5162       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5163           << getOpenMPClauseName(OMPC_private) << Type;
5164       bool IsDecl =
5165           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5166       Diag(VD->getLocation(),
5167            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5168           << VD;
5169       continue;
5170     }
5171 
5172     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5173     // in a Construct]
5174     //  Variables with the predetermined data-sharing attributes may not be
5175     //  listed in data-sharing attributes clauses, except for the cases
5176     //  listed below. For these exceptions only, listing a predetermined
5177     //  variable in a data-sharing attribute clause is allowed and overrides
5178     //  the variable's predetermined data-sharing attributes.
5179     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5180     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
5181       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5182                                           << getOpenMPClauseName(OMPC_private);
5183       ReportOriginalDSA(*this, DSAStack, VD, DVar);
5184       continue;
5185     }
5186 
5187     // Variably modified types are not supported for tasks.
5188     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
5189         DSAStack->getCurrentDirective() == OMPD_task) {
5190       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5191           << getOpenMPClauseName(OMPC_private) << Type
5192           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5193       bool IsDecl =
5194           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5195       Diag(VD->getLocation(),
5196            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5197           << VD;
5198       continue;
5199     }
5200 
5201     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5202     //  A variable of class type (or array thereof) that appears in a private
5203     //  clause requires an accessible, unambiguous default constructor for the
5204     //  class type.
5205     // Generate helper private variable and initialize it with the default
5206     // value. The address of the original variable is replaced by the address of
5207     // the new private variable in CodeGen. This new variable is not added to
5208     // IdResolver, so the code in the OpenMP region uses original variable for
5209     // proper diagnostics.
5210     Type = Type.getUnqualifiedType();
5211     auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
5212     ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
5213     if (VDPrivate->isInvalidDecl())
5214       continue;
5215     auto VDPrivateRefExpr = buildDeclRefExpr(
5216         *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
5217 
5218     DSAStack->addDSA(VD, DE, OMPC_private);
5219     Vars.push_back(DE);
5220     PrivateCopies.push_back(VDPrivateRefExpr);
5221   }
5222 
5223   if (Vars.empty())
5224     return nullptr;
5225 
5226   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5227                                   PrivateCopies);
5228 }
5229 
5230 namespace {
5231 class DiagsUninitializedSeveretyRAII {
5232 private:
5233   DiagnosticsEngine &Diags;
5234   SourceLocation SavedLoc;
5235   bool IsIgnored;
5236 
5237 public:
5238   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5239                                  bool IsIgnored)
5240       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5241     if (!IsIgnored) {
5242       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5243                         /*Map*/ diag::Severity::Ignored, Loc);
5244     }
5245   }
5246   ~DiagsUninitializedSeveretyRAII() {
5247     if (!IsIgnored)
5248       Diags.popMappings(SavedLoc);
5249   }
5250 };
5251 }
5252 
5253 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5254                                                SourceLocation StartLoc,
5255                                                SourceLocation LParenLoc,
5256                                                SourceLocation EndLoc) {
5257   SmallVector<Expr *, 8> Vars;
5258   SmallVector<Expr *, 8> PrivateCopies;
5259   SmallVector<Expr *, 8> Inits;
5260   bool IsImplicitClause =
5261       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5262   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5263 
5264   for (auto &RefExpr : VarList) {
5265     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5266     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5267       // It will be analyzed later.
5268       Vars.push_back(RefExpr);
5269       PrivateCopies.push_back(nullptr);
5270       Inits.push_back(nullptr);
5271       continue;
5272     }
5273 
5274     SourceLocation ELoc =
5275         IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
5276     // OpenMP [2.1, C/C++]
5277     //  A list item is a variable name.
5278     // OpenMP  [2.9.3.3, Restrictions, p.1]
5279     //  A variable that is part of another variable (as an array or
5280     //  structure element) cannot appear in a private clause.
5281     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5282     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5283       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5284       continue;
5285     }
5286     Decl *D = DE->getDecl();
5287     VarDecl *VD = cast<VarDecl>(D);
5288 
5289     QualType Type = VD->getType();
5290     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5291       // It will be analyzed later.
5292       Vars.push_back(DE);
5293       PrivateCopies.push_back(nullptr);
5294       Inits.push_back(nullptr);
5295       continue;
5296     }
5297 
5298     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5299     //  A variable that appears in a private clause must not have an incomplete
5300     //  type or a reference type.
5301     if (RequireCompleteType(ELoc, Type,
5302                             diag::err_omp_firstprivate_incomplete_type)) {
5303       continue;
5304     }
5305     if (Type->isReferenceType()) {
5306       if (IsImplicitClause) {
5307         Diag(ImplicitClauseLoc,
5308              diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5309             << Type;
5310         Diag(RefExpr->getExprLoc(), diag::note_used_here);
5311       } else {
5312         Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5313             << getOpenMPClauseName(OMPC_firstprivate) << Type;
5314       }
5315       bool IsDecl =
5316           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5317       Diag(VD->getLocation(),
5318            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5319           << VD;
5320       continue;
5321     }
5322 
5323     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5324     //  A variable of class type (or array thereof) that appears in a private
5325     //  clause requires an accessible, unambiguous copy constructor for the
5326     //  class type.
5327     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
5328 
5329     // If an implicit firstprivate variable found it was checked already.
5330     if (!IsImplicitClause) {
5331       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5332       bool IsConstant = ElemType.isConstant(Context);
5333       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5334       //  A list item that specifies a given variable may not appear in more
5335       // than one clause on the same directive, except that a variable may be
5336       //  specified in both firstprivate and lastprivate clauses.
5337       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
5338           DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
5339         Diag(ELoc, diag::err_omp_wrong_dsa)
5340             << getOpenMPClauseName(DVar.CKind)
5341             << getOpenMPClauseName(OMPC_firstprivate);
5342         ReportOriginalDSA(*this, DSAStack, VD, DVar);
5343         continue;
5344       }
5345 
5346       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5347       // in a Construct]
5348       //  Variables with the predetermined data-sharing attributes may not be
5349       //  listed in data-sharing attributes clauses, except for the cases
5350       //  listed below. For these exceptions only, listing a predetermined
5351       //  variable in a data-sharing attribute clause is allowed and overrides
5352       //  the variable's predetermined data-sharing attributes.
5353       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5354       // in a Construct, C/C++, p.2]
5355       //  Variables with const-qualified type having no mutable member may be
5356       //  listed in a firstprivate clause, even if they are static data members.
5357       if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5358           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5359         Diag(ELoc, diag::err_omp_wrong_dsa)
5360             << getOpenMPClauseName(DVar.CKind)
5361             << getOpenMPClauseName(OMPC_firstprivate);
5362         ReportOriginalDSA(*this, DSAStack, VD, DVar);
5363         continue;
5364       }
5365 
5366       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5367       // OpenMP [2.9.3.4, Restrictions, p.2]
5368       //  A list item that is private within a parallel region must not appear
5369       //  in a firstprivate clause on a worksharing construct if any of the
5370       //  worksharing regions arising from the worksharing construct ever bind
5371       //  to any of the parallel regions arising from the parallel construct.
5372       if (isOpenMPWorksharingDirective(CurrDir) &&
5373           !isOpenMPParallelDirective(CurrDir)) {
5374         DVar = DSAStack->getImplicitDSA(VD, true);
5375         if (DVar.CKind != OMPC_shared &&
5376             (isOpenMPParallelDirective(DVar.DKind) ||
5377              DVar.DKind == OMPD_unknown)) {
5378           Diag(ELoc, diag::err_omp_required_access)
5379               << getOpenMPClauseName(OMPC_firstprivate)
5380               << getOpenMPClauseName(OMPC_shared);
5381           ReportOriginalDSA(*this, DSAStack, VD, DVar);
5382           continue;
5383         }
5384       }
5385       // OpenMP [2.9.3.4, Restrictions, p.3]
5386       //  A list item that appears in a reduction clause of a parallel construct
5387       //  must not appear in a firstprivate clause on a worksharing or task
5388       //  construct if any of the worksharing or task regions arising from the
5389       //  worksharing or task construct ever bind to any of the parallel regions
5390       //  arising from the parallel construct.
5391       // OpenMP [2.9.3.4, Restrictions, p.4]
5392       //  A list item that appears in a reduction clause in worksharing
5393       //  construct must not appear in a firstprivate clause in a task construct
5394       //  encountered during execution of any of the worksharing regions arising
5395       //  from the worksharing construct.
5396       if (CurrDir == OMPD_task) {
5397         DVar =
5398             DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5399                                       [](OpenMPDirectiveKind K) -> bool {
5400                                         return isOpenMPParallelDirective(K) ||
5401                                                isOpenMPWorksharingDirective(K);
5402                                       },
5403                                       false);
5404         if (DVar.CKind == OMPC_reduction &&
5405             (isOpenMPParallelDirective(DVar.DKind) ||
5406              isOpenMPWorksharingDirective(DVar.DKind))) {
5407           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5408               << getOpenMPDirectiveName(DVar.DKind);
5409           ReportOriginalDSA(*this, DSAStack, VD, DVar);
5410           continue;
5411         }
5412       }
5413     }
5414 
5415     // Variably modified types are not supported for tasks.
5416     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
5417         DSAStack->getCurrentDirective() == OMPD_task) {
5418       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5419           << getOpenMPClauseName(OMPC_firstprivate) << Type
5420           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5421       bool IsDecl =
5422           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5423       Diag(VD->getLocation(),
5424            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5425           << VD;
5426       continue;
5427     }
5428 
5429     Type = Type.getUnqualifiedType();
5430     auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
5431     // Generate helper private variable and initialize it with the value of the
5432     // original variable. The address of the original variable is replaced by
5433     // the address of the new private variable in the CodeGen. This new variable
5434     // is not added to IdResolver, so the code in the OpenMP region uses
5435     // original variable for proper diagnostics and variable capturing.
5436     Expr *VDInitRefExpr = nullptr;
5437     // For arrays generate initializer for single element and replace it by the
5438     // original array element in CodeGen.
5439     if (Type->isArrayType()) {
5440       auto VDInit =
5441           buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5442       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
5443       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
5444       ElemType = ElemType.getUnqualifiedType();
5445       auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5446                                       ".firstprivate.temp");
5447       InitializedEntity Entity =
5448           InitializedEntity::InitializeVariable(VDInitTemp);
5449       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5450 
5451       InitializationSequence InitSeq(*this, Entity, Kind, Init);
5452       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5453       if (Result.isInvalid())
5454         VDPrivate->setInvalidDecl();
5455       else
5456         VDPrivate->setInit(Result.getAs<Expr>());
5457     } else {
5458       auto *VDInit =
5459           buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
5460       VDInitRefExpr =
5461           buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
5462       AddInitializerToDecl(VDPrivate,
5463                            DefaultLvalueConversion(VDInitRefExpr).get(),
5464                            /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
5465     }
5466     if (VDPrivate->isInvalidDecl()) {
5467       if (IsImplicitClause) {
5468         Diag(DE->getExprLoc(),
5469              diag::note_omp_task_predetermined_firstprivate_here);
5470       }
5471       continue;
5472     }
5473     CurContext->addDecl(VDPrivate);
5474     auto VDPrivateRefExpr = buildDeclRefExpr(
5475         *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
5476     DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5477     Vars.push_back(DE);
5478     PrivateCopies.push_back(VDPrivateRefExpr);
5479     Inits.push_back(VDInitRefExpr);
5480   }
5481 
5482   if (Vars.empty())
5483     return nullptr;
5484 
5485   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
5486                                        Vars, PrivateCopies, Inits);
5487 }
5488 
5489 OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5490                                               SourceLocation StartLoc,
5491                                               SourceLocation LParenLoc,
5492                                               SourceLocation EndLoc) {
5493   SmallVector<Expr *, 8> Vars;
5494   SmallVector<Expr *, 8> SrcExprs;
5495   SmallVector<Expr *, 8> DstExprs;
5496   SmallVector<Expr *, 8> AssignmentOps;
5497   for (auto &RefExpr : VarList) {
5498     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5499     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5500       // It will be analyzed later.
5501       Vars.push_back(RefExpr);
5502       SrcExprs.push_back(nullptr);
5503       DstExprs.push_back(nullptr);
5504       AssignmentOps.push_back(nullptr);
5505       continue;
5506     }
5507 
5508     SourceLocation ELoc = RefExpr->getExprLoc();
5509     // OpenMP [2.1, C/C++]
5510     //  A list item is a variable name.
5511     // OpenMP  [2.14.3.5, Restrictions, p.1]
5512     //  A variable that is part of another variable (as an array or structure
5513     //  element) cannot appear in a lastprivate clause.
5514     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5515     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5516       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5517       continue;
5518     }
5519     Decl *D = DE->getDecl();
5520     VarDecl *VD = cast<VarDecl>(D);
5521 
5522     QualType Type = VD->getType();
5523     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5524       // It will be analyzed later.
5525       Vars.push_back(DE);
5526       SrcExprs.push_back(nullptr);
5527       DstExprs.push_back(nullptr);
5528       AssignmentOps.push_back(nullptr);
5529       continue;
5530     }
5531 
5532     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5533     //  A variable that appears in a lastprivate clause must not have an
5534     //  incomplete type or a reference type.
5535     if (RequireCompleteType(ELoc, Type,
5536                             diag::err_omp_lastprivate_incomplete_type)) {
5537       continue;
5538     }
5539     if (Type->isReferenceType()) {
5540       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5541           << getOpenMPClauseName(OMPC_lastprivate) << Type;
5542       bool IsDecl =
5543           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5544       Diag(VD->getLocation(),
5545            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5546           << VD;
5547       continue;
5548     }
5549 
5550     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5551     // in a Construct]
5552     //  Variables with the predetermined data-sharing attributes may not be
5553     //  listed in data-sharing attributes clauses, except for the cases
5554     //  listed below.
5555     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5556     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5557         DVar.CKind != OMPC_firstprivate &&
5558         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5559       Diag(ELoc, diag::err_omp_wrong_dsa)
5560           << getOpenMPClauseName(DVar.CKind)
5561           << getOpenMPClauseName(OMPC_lastprivate);
5562       ReportOriginalDSA(*this, DSAStack, VD, DVar);
5563       continue;
5564     }
5565 
5566     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5567     // OpenMP [2.14.3.5, Restrictions, p.2]
5568     // A list item that is private within a parallel region, or that appears in
5569     // the reduction clause of a parallel construct, must not appear in a
5570     // lastprivate clause on a worksharing construct if any of the corresponding
5571     // worksharing regions ever binds to any of the corresponding parallel
5572     // regions.
5573     DSAStackTy::DSAVarData TopDVar = DVar;
5574     if (isOpenMPWorksharingDirective(CurrDir) &&
5575         !isOpenMPParallelDirective(CurrDir)) {
5576       DVar = DSAStack->getImplicitDSA(VD, true);
5577       if (DVar.CKind != OMPC_shared) {
5578         Diag(ELoc, diag::err_omp_required_access)
5579             << getOpenMPClauseName(OMPC_lastprivate)
5580             << getOpenMPClauseName(OMPC_shared);
5581         ReportOriginalDSA(*this, DSAStack, VD, DVar);
5582         continue;
5583       }
5584     }
5585     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
5586     //  A variable of class type (or array thereof) that appears in a
5587     //  lastprivate clause requires an accessible, unambiguous default
5588     //  constructor for the class type, unless the list item is also specified
5589     //  in a firstprivate clause.
5590     //  A variable of class type (or array thereof) that appears in a
5591     //  lastprivate clause requires an accessible, unambiguous copy assignment
5592     //  operator for the class type.
5593     Type = Context.getBaseElementType(Type).getNonReferenceType();
5594     auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
5595                                Type.getUnqualifiedType(), ".lastprivate.src");
5596     auto *PseudoSrcExpr = buildDeclRefExpr(
5597         *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
5598     auto *DstVD =
5599         buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
5600     auto *PseudoDstExpr =
5601         buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
5602     // For arrays generate assignment operation for single element and replace
5603     // it by the original array element in CodeGen.
5604     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5605                                    PseudoDstExpr, PseudoSrcExpr);
5606     if (AssignmentOp.isInvalid())
5607       continue;
5608     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5609                                        /*DiscardedValue=*/true);
5610     if (AssignmentOp.isInvalid())
5611       continue;
5612 
5613     if (TopDVar.CKind != OMPC_firstprivate)
5614       DSAStack->addDSA(VD, DE, OMPC_lastprivate);
5615     Vars.push_back(DE);
5616     SrcExprs.push_back(PseudoSrcExpr);
5617     DstExprs.push_back(PseudoDstExpr);
5618     AssignmentOps.push_back(AssignmentOp.get());
5619   }
5620 
5621   if (Vars.empty())
5622     return nullptr;
5623 
5624   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
5625                                       Vars, SrcExprs, DstExprs, AssignmentOps);
5626 }
5627 
5628 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5629                                          SourceLocation StartLoc,
5630                                          SourceLocation LParenLoc,
5631                                          SourceLocation EndLoc) {
5632   SmallVector<Expr *, 8> Vars;
5633   for (auto &RefExpr : VarList) {
5634     assert(RefExpr && "NULL expr in OpenMP shared clause.");
5635     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5636       // It will be analyzed later.
5637       Vars.push_back(RefExpr);
5638       continue;
5639     }
5640 
5641     SourceLocation ELoc = RefExpr->getExprLoc();
5642     // OpenMP [2.1, C/C++]
5643     //  A list item is a variable name.
5644     // OpenMP  [2.14.3.2, Restrictions, p.1]
5645     //  A variable that is part of another variable (as an array or structure
5646     //  element) cannot appear in a shared unless it is a static data member
5647     //  of a C++ class.
5648     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5649     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5650       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5651       continue;
5652     }
5653     Decl *D = DE->getDecl();
5654     VarDecl *VD = cast<VarDecl>(D);
5655 
5656     QualType Type = VD->getType();
5657     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5658       // It will be analyzed later.
5659       Vars.push_back(DE);
5660       continue;
5661     }
5662 
5663     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5664     // in a Construct]
5665     //  Variables with the predetermined data-sharing attributes may not be
5666     //  listed in data-sharing attributes clauses, except for the cases
5667     //  listed below. For these exceptions only, listing a predetermined
5668     //  variable in a data-sharing attribute clause is allowed and overrides
5669     //  the variable's predetermined data-sharing attributes.
5670     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5671     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5672         DVar.RefExpr) {
5673       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5674                                           << getOpenMPClauseName(OMPC_shared);
5675       ReportOriginalDSA(*this, DSAStack, VD, DVar);
5676       continue;
5677     }
5678 
5679     DSAStack->addDSA(VD, DE, OMPC_shared);
5680     Vars.push_back(DE);
5681   }
5682 
5683   if (Vars.empty())
5684     return nullptr;
5685 
5686   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5687 }
5688 
5689 namespace {
5690 class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5691   DSAStackTy *Stack;
5692 
5693 public:
5694   bool VisitDeclRefExpr(DeclRefExpr *E) {
5695     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
5696       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
5697       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5698         return false;
5699       if (DVar.CKind != OMPC_unknown)
5700         return true;
5701       DSAStackTy::DSAVarData DVarPrivate =
5702           Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
5703       if (DVarPrivate.CKind != OMPC_unknown)
5704         return true;
5705       return false;
5706     }
5707     return false;
5708   }
5709   bool VisitStmt(Stmt *S) {
5710     for (auto Child : S->children()) {
5711       if (Child && Visit(Child))
5712         return true;
5713     }
5714     return false;
5715   }
5716   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
5717 };
5718 } // namespace
5719 
5720 OMPClause *Sema::ActOnOpenMPReductionClause(
5721     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5722     SourceLocation ColonLoc, SourceLocation EndLoc,
5723     CXXScopeSpec &ReductionIdScopeSpec,
5724     const DeclarationNameInfo &ReductionId) {
5725   // TODO: Allow scope specification search when 'declare reduction' is
5726   // supported.
5727   assert(ReductionIdScopeSpec.isEmpty() &&
5728          "No support for scoped reduction identifiers yet.");
5729 
5730   auto DN = ReductionId.getName();
5731   auto OOK = DN.getCXXOverloadedOperator();
5732   BinaryOperatorKind BOK = BO_Comma;
5733 
5734   // OpenMP [2.14.3.6, reduction clause]
5735   // C
5736   // reduction-identifier is either an identifier or one of the following
5737   // operators: +, -, *,  &, |, ^, && and ||
5738   // C++
5739   // reduction-identifier is either an id-expression or one of the following
5740   // operators: +, -, *, &, |, ^, && and ||
5741   // FIXME: Only 'min' and 'max' identifiers are supported for now.
5742   switch (OOK) {
5743   case OO_Plus:
5744   case OO_Minus:
5745     BOK = BO_Add;
5746     break;
5747   case OO_Star:
5748     BOK = BO_Mul;
5749     break;
5750   case OO_Amp:
5751     BOK = BO_And;
5752     break;
5753   case OO_Pipe:
5754     BOK = BO_Or;
5755     break;
5756   case OO_Caret:
5757     BOK = BO_Xor;
5758     break;
5759   case OO_AmpAmp:
5760     BOK = BO_LAnd;
5761     break;
5762   case OO_PipePipe:
5763     BOK = BO_LOr;
5764     break;
5765   case OO_New:
5766   case OO_Delete:
5767   case OO_Array_New:
5768   case OO_Array_Delete:
5769   case OO_Slash:
5770   case OO_Percent:
5771   case OO_Tilde:
5772   case OO_Exclaim:
5773   case OO_Equal:
5774   case OO_Less:
5775   case OO_Greater:
5776   case OO_LessEqual:
5777   case OO_GreaterEqual:
5778   case OO_PlusEqual:
5779   case OO_MinusEqual:
5780   case OO_StarEqual:
5781   case OO_SlashEqual:
5782   case OO_PercentEqual:
5783   case OO_CaretEqual:
5784   case OO_AmpEqual:
5785   case OO_PipeEqual:
5786   case OO_LessLess:
5787   case OO_GreaterGreater:
5788   case OO_LessLessEqual:
5789   case OO_GreaterGreaterEqual:
5790   case OO_EqualEqual:
5791   case OO_ExclaimEqual:
5792   case OO_PlusPlus:
5793   case OO_MinusMinus:
5794   case OO_Comma:
5795   case OO_ArrowStar:
5796   case OO_Arrow:
5797   case OO_Call:
5798   case OO_Subscript:
5799   case OO_Conditional:
5800   case NUM_OVERLOADED_OPERATORS:
5801     llvm_unreachable("Unexpected reduction identifier");
5802   case OO_None:
5803     if (auto II = DN.getAsIdentifierInfo()) {
5804       if (II->isStr("max"))
5805         BOK = BO_GT;
5806       else if (II->isStr("min"))
5807         BOK = BO_LT;
5808     }
5809     break;
5810   }
5811   SourceRange ReductionIdRange;
5812   if (ReductionIdScopeSpec.isValid()) {
5813     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5814   }
5815   ReductionIdRange.setEnd(ReductionId.getEndLoc());
5816   if (BOK == BO_Comma) {
5817     // Not allowed reduction identifier is found.
5818     Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5819         << ReductionIdRange;
5820     return nullptr;
5821   }
5822 
5823   SmallVector<Expr *, 8> Vars;
5824   SmallVector<Expr *, 8> LHSs;
5825   SmallVector<Expr *, 8> RHSs;
5826   SmallVector<Expr *, 8> ReductionOps;
5827   for (auto RefExpr : VarList) {
5828     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5829     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5830       // It will be analyzed later.
5831       Vars.push_back(RefExpr);
5832       LHSs.push_back(nullptr);
5833       RHSs.push_back(nullptr);
5834       ReductionOps.push_back(nullptr);
5835       continue;
5836     }
5837 
5838     if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5839         RefExpr->isInstantiationDependent() ||
5840         RefExpr->containsUnexpandedParameterPack()) {
5841       // It will be analyzed later.
5842       Vars.push_back(RefExpr);
5843       LHSs.push_back(nullptr);
5844       RHSs.push_back(nullptr);
5845       ReductionOps.push_back(nullptr);
5846       continue;
5847     }
5848 
5849     auto ELoc = RefExpr->getExprLoc();
5850     auto ERange = RefExpr->getSourceRange();
5851     // OpenMP [2.1, C/C++]
5852     //  A list item is a variable or array section, subject to the restrictions
5853     //  specified in Section 2.4 on page 42 and in each of the sections
5854     // describing clauses and directives for which a list appears.
5855     // OpenMP  [2.14.3.3, Restrictions, p.1]
5856     //  A variable that is part of another variable (as an array or
5857     //  structure element) cannot appear in a private clause.
5858     auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5859     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5860       Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5861       continue;
5862     }
5863     auto D = DE->getDecl();
5864     auto VD = cast<VarDecl>(D);
5865     auto Type = VD->getType();
5866     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5867     //  A variable that appears in a private clause must not have an incomplete
5868     //  type or a reference type.
5869     if (RequireCompleteType(ELoc, Type,
5870                             diag::err_omp_reduction_incomplete_type))
5871       continue;
5872     // OpenMP [2.14.3.6, reduction clause, Restrictions]
5873     // Arrays may not appear in a reduction clause.
5874     if (Type.getNonReferenceType()->isArrayType()) {
5875       Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5876       bool IsDecl =
5877           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5878       Diag(VD->getLocation(),
5879            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5880           << VD;
5881       continue;
5882     }
5883     // OpenMP [2.14.3.6, reduction clause, Restrictions]
5884     // A list item that appears in a reduction clause must not be
5885     // const-qualified.
5886     if (Type.getNonReferenceType().isConstant(Context)) {
5887       Diag(ELoc, diag::err_omp_const_variable)
5888           << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5889       bool IsDecl =
5890           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5891       Diag(VD->getLocation(),
5892            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5893           << VD;
5894       continue;
5895     }
5896     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5897     //  If a list-item is a reference type then it must bind to the same object
5898     //  for all threads of the team.
5899     VarDecl *VDDef = VD->getDefinition();
5900     if (Type->isReferenceType() && VDDef) {
5901       DSARefChecker Check(DSAStack);
5902       if (Check.Visit(VDDef->getInit())) {
5903         Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5904         Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5905         continue;
5906       }
5907     }
5908     // OpenMP [2.14.3.6, reduction clause, Restrictions]
5909     // The type of a list item that appears in a reduction clause must be valid
5910     // for the reduction-identifier. For a max or min reduction in C, the type
5911     // of the list item must be an allowed arithmetic data type: char, int,
5912     // float, double, or _Bool, possibly modified with long, short, signed, or
5913     // unsigned. For a max or min reduction in C++, the type of the list item
5914     // must be an allowed arithmetic data type: char, wchar_t, int, float,
5915     // double, or bool, possibly modified with long, short, signed, or unsigned.
5916     if ((BOK == BO_GT || BOK == BO_LT) &&
5917         !(Type->isScalarType() ||
5918           (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5919       Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5920           << getLangOpts().CPlusPlus;
5921       bool IsDecl =
5922           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5923       Diag(VD->getLocation(),
5924            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5925           << VD;
5926       continue;
5927     }
5928     if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5929         !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5930       Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5931       bool IsDecl =
5932           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5933       Diag(VD->getLocation(),
5934            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5935           << VD;
5936       continue;
5937     }
5938     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5939     // in a Construct]
5940     //  Variables with the predetermined data-sharing attributes may not be
5941     //  listed in data-sharing attributes clauses, except for the cases
5942     //  listed below. For these exceptions only, listing a predetermined
5943     //  variable in a data-sharing attribute clause is allowed and overrides
5944     //  the variable's predetermined data-sharing attributes.
5945     // OpenMP [2.14.3.6, Restrictions, p.3]
5946     //  Any number of reduction clauses can be specified on the directive,
5947     //  but a list item can appear only once in the reduction clauses for that
5948     //  directive.
5949     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5950     if (DVar.CKind == OMPC_reduction) {
5951       Diag(ELoc, diag::err_omp_once_referenced)
5952           << getOpenMPClauseName(OMPC_reduction);
5953       if (DVar.RefExpr) {
5954         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5955       }
5956     } else if (DVar.CKind != OMPC_unknown) {
5957       Diag(ELoc, diag::err_omp_wrong_dsa)
5958           << getOpenMPClauseName(DVar.CKind)
5959           << getOpenMPClauseName(OMPC_reduction);
5960       ReportOriginalDSA(*this, DSAStack, VD, DVar);
5961       continue;
5962     }
5963 
5964     // OpenMP [2.14.3.6, Restrictions, p.1]
5965     //  A list item that appears in a reduction clause of a worksharing
5966     //  construct must be shared in the parallel regions to which any of the
5967     //  worksharing regions arising from the worksharing construct bind.
5968     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5969     if (isOpenMPWorksharingDirective(CurrDir) &&
5970         !isOpenMPParallelDirective(CurrDir)) {
5971       DVar = DSAStack->getImplicitDSA(VD, true);
5972       if (DVar.CKind != OMPC_shared) {
5973         Diag(ELoc, diag::err_omp_required_access)
5974             << getOpenMPClauseName(OMPC_reduction)
5975             << getOpenMPClauseName(OMPC_shared);
5976         ReportOriginalDSA(*this, DSAStack, VD, DVar);
5977         continue;
5978       }
5979     }
5980     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
5981     auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5982     auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
5983     // Add initializer for private variable.
5984     Expr *Init = nullptr;
5985     switch (BOK) {
5986     case BO_Add:
5987     case BO_Xor:
5988     case BO_Or:
5989     case BO_LOr:
5990       // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5991       if (Type->isScalarType() || Type->isAnyComplexType()) {
5992         Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
5993       }
5994       break;
5995     case BO_Mul:
5996     case BO_LAnd:
5997       if (Type->isScalarType() || Type->isAnyComplexType()) {
5998         // '*' and '&&' reduction ops - initializer is '1'.
5999         Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6000       }
6001       break;
6002     case BO_And: {
6003       // '&' reduction op - initializer is '~0'.
6004       QualType OrigType = Type;
6005       if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6006         Type = ComplexTy->getElementType();
6007       }
6008       if (Type->isRealFloatingType()) {
6009         llvm::APFloat InitValue =
6010             llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6011                                            /*isIEEE=*/true);
6012         Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6013                                        Type, ELoc);
6014       } else if (Type->isScalarType()) {
6015         auto Size = Context.getTypeSize(Type);
6016         QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6017         llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6018         Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6019       }
6020       if (Init && OrigType->isAnyComplexType()) {
6021         // Init = 0xFFFF + 0xFFFFi;
6022         auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6023         Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6024       }
6025       Type = OrigType;
6026       break;
6027     }
6028     case BO_LT:
6029     case BO_GT: {
6030       // 'min' reduction op - initializer is 'Largest representable number in
6031       // the reduction list item type'.
6032       // 'max' reduction op - initializer is 'Least representable number in
6033       // the reduction list item type'.
6034       if (Type->isIntegerType() || Type->isPointerType()) {
6035         bool IsSigned = Type->hasSignedIntegerRepresentation();
6036         auto Size = Context.getTypeSize(Type);
6037         QualType IntTy =
6038             Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6039         llvm::APInt InitValue =
6040             (BOK != BO_LT)
6041                 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6042                            : llvm::APInt::getMinValue(Size)
6043                 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6044                            : llvm::APInt::getMaxValue(Size);
6045         Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6046         if (Type->isPointerType()) {
6047           // Cast to pointer type.
6048           auto CastExpr = BuildCStyleCastExpr(
6049               SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6050               SourceLocation(), Init);
6051           if (CastExpr.isInvalid())
6052             continue;
6053           Init = CastExpr.get();
6054         }
6055       } else if (Type->isRealFloatingType()) {
6056         llvm::APFloat InitValue = llvm::APFloat::getLargest(
6057             Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6058         Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6059                                        Type, ELoc);
6060       }
6061       break;
6062     }
6063     case BO_PtrMemD:
6064     case BO_PtrMemI:
6065     case BO_MulAssign:
6066     case BO_Div:
6067     case BO_Rem:
6068     case BO_Sub:
6069     case BO_Shl:
6070     case BO_Shr:
6071     case BO_LE:
6072     case BO_GE:
6073     case BO_EQ:
6074     case BO_NE:
6075     case BO_AndAssign:
6076     case BO_XorAssign:
6077     case BO_OrAssign:
6078     case BO_Assign:
6079     case BO_AddAssign:
6080     case BO_SubAssign:
6081     case BO_DivAssign:
6082     case BO_RemAssign:
6083     case BO_ShlAssign:
6084     case BO_ShrAssign:
6085     case BO_Comma:
6086       llvm_unreachable("Unexpected reduction operation");
6087     }
6088     if (Init) {
6089       AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6090                            /*TypeMayContainAuto=*/false);
6091     } else {
6092       ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6093     }
6094     if (!RHSVD->hasInit()) {
6095       Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6096                                                             << ReductionIdRange;
6097       bool IsDecl =
6098           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6099       Diag(VD->getLocation(),
6100            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6101           << VD;
6102       continue;
6103     }
6104     auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6105     auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
6106     ExprResult ReductionOp =
6107         BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6108                    LHSDRE, RHSDRE);
6109     if (ReductionOp.isUsable()) {
6110       if (BOK != BO_LT && BOK != BO_GT) {
6111         ReductionOp =
6112             BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6113                        BO_Assign, LHSDRE, ReductionOp.get());
6114       } else {
6115         auto *ConditionalOp = new (Context) ConditionalOperator(
6116             ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6117             RHSDRE, Type, VK_LValue, OK_Ordinary);
6118         ReductionOp =
6119             BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6120                        BO_Assign, LHSDRE, ConditionalOp);
6121       }
6122       if (ReductionOp.isUsable()) {
6123         ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
6124       }
6125     }
6126     if (ReductionOp.isInvalid())
6127       continue;
6128 
6129     DSAStack->addDSA(VD, DE, OMPC_reduction);
6130     Vars.push_back(DE);
6131     LHSs.push_back(LHSDRE);
6132     RHSs.push_back(RHSDRE);
6133     ReductionOps.push_back(ReductionOp.get());
6134   }
6135 
6136   if (Vars.empty())
6137     return nullptr;
6138 
6139   return OMPReductionClause::Create(
6140       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
6141       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6142       RHSs, ReductionOps);
6143 }
6144 
6145 OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6146                                          SourceLocation StartLoc,
6147                                          SourceLocation LParenLoc,
6148                                          SourceLocation ColonLoc,
6149                                          SourceLocation EndLoc) {
6150   SmallVector<Expr *, 8> Vars;
6151   SmallVector<Expr *, 8> Inits;
6152   for (auto &RefExpr : VarList) {
6153     assert(RefExpr && "NULL expr in OpenMP linear clause.");
6154     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6155       // It will be analyzed later.
6156       Vars.push_back(RefExpr);
6157       Inits.push_back(nullptr);
6158       continue;
6159     }
6160 
6161     // OpenMP [2.14.3.7, linear clause]
6162     // A list item that appears in a linear clause is subject to the private
6163     // clause semantics described in Section 2.14.3.3 on page 159 except as
6164     // noted. In addition, the value of the new list item on each iteration
6165     // of the associated loop(s) corresponds to the value of the original
6166     // list item before entering the construct plus the logical number of
6167     // the iteration times linear-step.
6168 
6169     SourceLocation ELoc = RefExpr->getExprLoc();
6170     // OpenMP [2.1, C/C++]
6171     //  A list item is a variable name.
6172     // OpenMP  [2.14.3.3, Restrictions, p.1]
6173     //  A variable that is part of another variable (as an array or
6174     //  structure element) cannot appear in a private clause.
6175     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6176     if (!DE || !isa<VarDecl>(DE->getDecl())) {
6177       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6178       continue;
6179     }
6180 
6181     VarDecl *VD = cast<VarDecl>(DE->getDecl());
6182 
6183     // OpenMP [2.14.3.7, linear clause]
6184     //  A list-item cannot appear in more than one linear clause.
6185     //  A list-item that appears in a linear clause cannot appear in any
6186     //  other data-sharing attribute clause.
6187     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6188     if (DVar.RefExpr) {
6189       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6190                                           << getOpenMPClauseName(OMPC_linear);
6191       ReportOriginalDSA(*this, DSAStack, VD, DVar);
6192       continue;
6193     }
6194 
6195     QualType QType = VD->getType();
6196     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6197       // It will be analyzed later.
6198       Vars.push_back(DE);
6199       Inits.push_back(nullptr);
6200       continue;
6201     }
6202 
6203     // A variable must not have an incomplete type or a reference type.
6204     if (RequireCompleteType(ELoc, QType,
6205                             diag::err_omp_linear_incomplete_type)) {
6206       continue;
6207     }
6208     if (QType->isReferenceType()) {
6209       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6210           << getOpenMPClauseName(OMPC_linear) << QType;
6211       bool IsDecl =
6212           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6213       Diag(VD->getLocation(),
6214            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6215           << VD;
6216       continue;
6217     }
6218 
6219     // A list item must not be const-qualified.
6220     if (QType.isConstant(Context)) {
6221       Diag(ELoc, diag::err_omp_const_variable)
6222           << getOpenMPClauseName(OMPC_linear);
6223       bool IsDecl =
6224           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6225       Diag(VD->getLocation(),
6226            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6227           << VD;
6228       continue;
6229     }
6230 
6231     // A list item must be of integral or pointer type.
6232     QType = QType.getUnqualifiedType().getCanonicalType();
6233     const Type *Ty = QType.getTypePtrOrNull();
6234     if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6235                 !Ty->isPointerType())) {
6236       Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6237       bool IsDecl =
6238           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6239       Diag(VD->getLocation(),
6240            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6241           << VD;
6242       continue;
6243     }
6244 
6245     // Build var to save initial value.
6246     VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
6247     AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6248                          /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
6249     auto InitRef = buildDeclRefExpr(
6250         *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
6251     DSAStack->addDSA(VD, DE, OMPC_linear);
6252     Vars.push_back(DE);
6253     Inits.push_back(InitRef);
6254   }
6255 
6256   if (Vars.empty())
6257     return nullptr;
6258 
6259   Expr *StepExpr = Step;
6260   Expr *CalcStepExpr = nullptr;
6261   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6262       !Step->isInstantiationDependent() &&
6263       !Step->containsUnexpandedParameterPack()) {
6264     SourceLocation StepLoc = Step->getLocStart();
6265     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
6266     if (Val.isInvalid())
6267       return nullptr;
6268     StepExpr = Val.get();
6269 
6270     // Build var to save the step value.
6271     VarDecl *SaveVar =
6272         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
6273     ExprResult SaveRef =
6274         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
6275     ExprResult CalcStep =
6276         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6277 
6278     // Warn about zero linear step (it would be probably better specified as
6279     // making corresponding variables 'const').
6280     llvm::APSInt Result;
6281     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6282     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
6283       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6284                                                      << (Vars.size() > 1);
6285     if (!IsConstant && CalcStep.isUsable()) {
6286       // Calculate the step beforehand instead of doing this on each iteration.
6287       // (This is not used if the number of iterations may be kfold-ed).
6288       CalcStepExpr = CalcStep.get();
6289     }
6290   }
6291 
6292   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
6293                                  Vars, Inits, StepExpr, CalcStepExpr);
6294 }
6295 
6296 static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6297                                      Expr *NumIterations, Sema &SemaRef,
6298                                      Scope *S) {
6299   // Walk the vars and build update/final expressions for the CodeGen.
6300   SmallVector<Expr *, 8> Updates;
6301   SmallVector<Expr *, 8> Finals;
6302   Expr *Step = Clause.getStep();
6303   Expr *CalcStep = Clause.getCalcStep();
6304   // OpenMP [2.14.3.7, linear clause]
6305   // If linear-step is not specified it is assumed to be 1.
6306   if (Step == nullptr)
6307     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6308   else if (CalcStep)
6309     Step = cast<BinaryOperator>(CalcStep)->getLHS();
6310   bool HasErrors = false;
6311   auto CurInit = Clause.inits().begin();
6312   for (auto &RefExpr : Clause.varlists()) {
6313     Expr *InitExpr = *CurInit;
6314 
6315     // Build privatized reference to the current linear var.
6316     auto DE = cast<DeclRefExpr>(RefExpr);
6317     auto PrivateRef =
6318         buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6319                          DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6320                          /*RefersToCapture=*/true);
6321 
6322     // Build update: Var = InitExpr + IV * Step
6323     ExprResult Update =
6324         BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6325                            InitExpr, IV, Step, /* Subtract */ false);
6326     Update = SemaRef.ActOnFinishFullExpr(Update.get());
6327 
6328     // Build final: Var = InitExpr + NumIterations * Step
6329     ExprResult Final =
6330         BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6331                            InitExpr, NumIterations, Step, /* Subtract */ false);
6332     Final = SemaRef.ActOnFinishFullExpr(Final.get());
6333     if (!Update.isUsable() || !Final.isUsable()) {
6334       Updates.push_back(nullptr);
6335       Finals.push_back(nullptr);
6336       HasErrors = true;
6337     } else {
6338       Updates.push_back(Update.get());
6339       Finals.push_back(Final.get());
6340     }
6341     ++CurInit;
6342   }
6343   Clause.setUpdates(Updates);
6344   Clause.setFinals(Finals);
6345   return HasErrors;
6346 }
6347 
6348 OMPClause *Sema::ActOnOpenMPAlignedClause(
6349     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6350     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6351 
6352   SmallVector<Expr *, 8> Vars;
6353   for (auto &RefExpr : VarList) {
6354     assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6355     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6356       // It will be analyzed later.
6357       Vars.push_back(RefExpr);
6358       continue;
6359     }
6360 
6361     SourceLocation ELoc = RefExpr->getExprLoc();
6362     // OpenMP [2.1, C/C++]
6363     //  A list item is a variable name.
6364     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6365     if (!DE || !isa<VarDecl>(DE->getDecl())) {
6366       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6367       continue;
6368     }
6369 
6370     VarDecl *VD = cast<VarDecl>(DE->getDecl());
6371 
6372     // OpenMP  [2.8.1, simd construct, Restrictions]
6373     // The type of list items appearing in the aligned clause must be
6374     // array, pointer, reference to array, or reference to pointer.
6375     QualType QType = VD->getType();
6376     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
6377     const Type *Ty = QType.getTypePtrOrNull();
6378     if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6379                 !Ty->isPointerType())) {
6380       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6381           << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6382       bool IsDecl =
6383           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6384       Diag(VD->getLocation(),
6385            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6386           << VD;
6387       continue;
6388     }
6389 
6390     // OpenMP  [2.8.1, simd construct, Restrictions]
6391     // A list-item cannot appear in more than one aligned clause.
6392     if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6393       Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6394       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6395           << getOpenMPClauseName(OMPC_aligned);
6396       continue;
6397     }
6398 
6399     Vars.push_back(DE);
6400   }
6401 
6402   // OpenMP [2.8.1, simd construct, Description]
6403   // The parameter of the aligned clause, alignment, must be a constant
6404   // positive integer expression.
6405   // If no optional parameter is specified, implementation-defined default
6406   // alignments for SIMD instructions on the target platforms are assumed.
6407   if (Alignment != nullptr) {
6408     ExprResult AlignResult =
6409         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6410     if (AlignResult.isInvalid())
6411       return nullptr;
6412     Alignment = AlignResult.get();
6413   }
6414   if (Vars.empty())
6415     return nullptr;
6416 
6417   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6418                                   EndLoc, Vars, Alignment);
6419 }
6420 
6421 OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6422                                          SourceLocation StartLoc,
6423                                          SourceLocation LParenLoc,
6424                                          SourceLocation EndLoc) {
6425   SmallVector<Expr *, 8> Vars;
6426   SmallVector<Expr *, 8> SrcExprs;
6427   SmallVector<Expr *, 8> DstExprs;
6428   SmallVector<Expr *, 8> AssignmentOps;
6429   for (auto &RefExpr : VarList) {
6430     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6431     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6432       // It will be analyzed later.
6433       Vars.push_back(RefExpr);
6434       SrcExprs.push_back(nullptr);
6435       DstExprs.push_back(nullptr);
6436       AssignmentOps.push_back(nullptr);
6437       continue;
6438     }
6439 
6440     SourceLocation ELoc = RefExpr->getExprLoc();
6441     // OpenMP [2.1, C/C++]
6442     //  A list item is a variable name.
6443     // OpenMP  [2.14.4.1, Restrictions, p.1]
6444     //  A list item that appears in a copyin clause must be threadprivate.
6445     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6446     if (!DE || !isa<VarDecl>(DE->getDecl())) {
6447       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6448       continue;
6449     }
6450 
6451     Decl *D = DE->getDecl();
6452     VarDecl *VD = cast<VarDecl>(D);
6453 
6454     QualType Type = VD->getType();
6455     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6456       // It will be analyzed later.
6457       Vars.push_back(DE);
6458       SrcExprs.push_back(nullptr);
6459       DstExprs.push_back(nullptr);
6460       AssignmentOps.push_back(nullptr);
6461       continue;
6462     }
6463 
6464     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6465     //  A list item that appears in a copyin clause must be threadprivate.
6466     if (!DSAStack->isThreadPrivate(VD)) {
6467       Diag(ELoc, diag::err_omp_required_access)
6468           << getOpenMPClauseName(OMPC_copyin)
6469           << getOpenMPDirectiveName(OMPD_threadprivate);
6470       continue;
6471     }
6472 
6473     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6474     //  A variable of class type (or array thereof) that appears in a
6475     //  copyin clause requires an accessible, unambiguous copy assignment
6476     //  operator for the class type.
6477     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
6478     auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
6479                                ElemType.getUnqualifiedType(), ".copyin.src");
6480     auto *PseudoSrcExpr = buildDeclRefExpr(
6481         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6482     auto *DstVD =
6483         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
6484     auto *PseudoDstExpr =
6485         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
6486     // For arrays generate assignment operation for single element and replace
6487     // it by the original array element in CodeGen.
6488     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6489                                    PseudoDstExpr, PseudoSrcExpr);
6490     if (AssignmentOp.isInvalid())
6491       continue;
6492     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6493                                        /*DiscardedValue=*/true);
6494     if (AssignmentOp.isInvalid())
6495       continue;
6496 
6497     DSAStack->addDSA(VD, DE, OMPC_copyin);
6498     Vars.push_back(DE);
6499     SrcExprs.push_back(PseudoSrcExpr);
6500     DstExprs.push_back(PseudoDstExpr);
6501     AssignmentOps.push_back(AssignmentOp.get());
6502   }
6503 
6504   if (Vars.empty())
6505     return nullptr;
6506 
6507   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6508                                  SrcExprs, DstExprs, AssignmentOps);
6509 }
6510 
6511 OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6512                                               SourceLocation StartLoc,
6513                                               SourceLocation LParenLoc,
6514                                               SourceLocation EndLoc) {
6515   SmallVector<Expr *, 8> Vars;
6516   SmallVector<Expr *, 8> SrcExprs;
6517   SmallVector<Expr *, 8> DstExprs;
6518   SmallVector<Expr *, 8> AssignmentOps;
6519   for (auto &RefExpr : VarList) {
6520     assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6521     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6522       // It will be analyzed later.
6523       Vars.push_back(RefExpr);
6524       SrcExprs.push_back(nullptr);
6525       DstExprs.push_back(nullptr);
6526       AssignmentOps.push_back(nullptr);
6527       continue;
6528     }
6529 
6530     SourceLocation ELoc = RefExpr->getExprLoc();
6531     // OpenMP [2.1, C/C++]
6532     //  A list item is a variable name.
6533     // OpenMP  [2.14.4.1, Restrictions, p.1]
6534     //  A list item that appears in a copyin clause must be threadprivate.
6535     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6536     if (!DE || !isa<VarDecl>(DE->getDecl())) {
6537       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6538       continue;
6539     }
6540 
6541     Decl *D = DE->getDecl();
6542     VarDecl *VD = cast<VarDecl>(D);
6543 
6544     QualType Type = VD->getType();
6545     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6546       // It will be analyzed later.
6547       Vars.push_back(DE);
6548       SrcExprs.push_back(nullptr);
6549       DstExprs.push_back(nullptr);
6550       AssignmentOps.push_back(nullptr);
6551       continue;
6552     }
6553 
6554     // OpenMP [2.14.4.2, Restrictions, p.2]
6555     //  A list item that appears in a copyprivate clause may not appear in a
6556     //  private or firstprivate clause on the single construct.
6557     if (!DSAStack->isThreadPrivate(VD)) {
6558       auto DVar = DSAStack->getTopDSA(VD, false);
6559       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6560           DVar.RefExpr) {
6561         Diag(ELoc, diag::err_omp_wrong_dsa)
6562             << getOpenMPClauseName(DVar.CKind)
6563             << getOpenMPClauseName(OMPC_copyprivate);
6564         ReportOriginalDSA(*this, DSAStack, VD, DVar);
6565         continue;
6566       }
6567 
6568       // OpenMP [2.11.4.2, Restrictions, p.1]
6569       //  All list items that appear in a copyprivate clause must be either
6570       //  threadprivate or private in the enclosing context.
6571       if (DVar.CKind == OMPC_unknown) {
6572         DVar = DSAStack->getImplicitDSA(VD, false);
6573         if (DVar.CKind == OMPC_shared) {
6574           Diag(ELoc, diag::err_omp_required_access)
6575               << getOpenMPClauseName(OMPC_copyprivate)
6576               << "threadprivate or private in the enclosing context";
6577           ReportOriginalDSA(*this, DSAStack, VD, DVar);
6578           continue;
6579         }
6580       }
6581     }
6582 
6583     // Variably modified types are not supported.
6584     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
6585       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6586           << getOpenMPClauseName(OMPC_copyprivate) << Type
6587           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6588       bool IsDecl =
6589           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6590       Diag(VD->getLocation(),
6591            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6592           << VD;
6593       continue;
6594     }
6595 
6596     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6597     //  A variable of class type (or array thereof) that appears in a
6598     //  copyin clause requires an accessible, unambiguous copy assignment
6599     //  operator for the class type.
6600     Type = Context.getBaseElementType(Type).getUnqualifiedType();
6601     auto *SrcVD =
6602         buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
6603     auto *PseudoSrcExpr =
6604         buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
6605     auto *DstVD =
6606         buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
6607     auto *PseudoDstExpr =
6608         buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
6609     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6610                                    PseudoDstExpr, PseudoSrcExpr);
6611     if (AssignmentOp.isInvalid())
6612       continue;
6613     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6614                                        /*DiscardedValue=*/true);
6615     if (AssignmentOp.isInvalid())
6616       continue;
6617 
6618     // No need to mark vars as copyprivate, they are already threadprivate or
6619     // implicitly private.
6620     Vars.push_back(DE);
6621     SrcExprs.push_back(PseudoSrcExpr);
6622     DstExprs.push_back(PseudoDstExpr);
6623     AssignmentOps.push_back(AssignmentOp.get());
6624   }
6625 
6626   if (Vars.empty())
6627     return nullptr;
6628 
6629   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6630                                       Vars, SrcExprs, DstExprs, AssignmentOps);
6631 }
6632 
6633 OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6634                                         SourceLocation StartLoc,
6635                                         SourceLocation LParenLoc,
6636                                         SourceLocation EndLoc) {
6637   if (VarList.empty())
6638     return nullptr;
6639 
6640   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6641 }
6642 
6643 OMPClause *
6644 Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6645                               SourceLocation DepLoc, SourceLocation ColonLoc,
6646                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6647                               SourceLocation LParenLoc, SourceLocation EndLoc) {
6648   if (DepKind == OMPC_DEPEND_unknown) {
6649     std::string Values;
6650     std::string Sep(", ");
6651     for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6652       Values += "'";
6653       Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6654       Values += "'";
6655       switch (i) {
6656       case OMPC_DEPEND_unknown - 2:
6657         Values += " or ";
6658         break;
6659       case OMPC_DEPEND_unknown - 1:
6660         break;
6661       default:
6662         Values += Sep;
6663         break;
6664       }
6665     }
6666     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6667         << Values << getOpenMPClauseName(OMPC_depend);
6668     return nullptr;
6669   }
6670   SmallVector<Expr *, 8> Vars;
6671   for (auto &RefExpr : VarList) {
6672     assert(RefExpr && "NULL expr in OpenMP shared clause.");
6673     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6674       // It will be analyzed later.
6675       Vars.push_back(RefExpr);
6676       continue;
6677     }
6678 
6679     SourceLocation ELoc = RefExpr->getExprLoc();
6680     // OpenMP  [2.11.1.1, Restrictions, p.3]
6681     //  A variable that is part of another variable (such as a field of a
6682     //  structure) but is not an array element or an array section cannot appear
6683     //  in a depend clause.
6684     auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6685     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6686     ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6687     if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6688         (DE && !isa<VarDecl>(DE->getDecl())) ||
6689         (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6690          !ASE->getBase()->getType()->isArrayType())) {
6691       Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6692           << RefExpr->getSourceRange();
6693       continue;
6694     }
6695 
6696     Vars.push_back(RefExpr->IgnoreParenImpCasts());
6697   }
6698 
6699   if (Vars.empty())
6700     return nullptr;
6701 
6702   return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6703                                  DepLoc, ColonLoc, Vars);
6704 }
6705