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