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