xref: /minix3/external/bsd/llvm/dist/clang/lib/Sema/SemaOpenMP.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc /// \file
10f4a2713aSLionel Sambuc /// \brief This file implements semantic analysis for OpenMP directives and
11f4a2713aSLionel Sambuc /// clauses.
12f4a2713aSLionel Sambuc ///
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTContext.h"
16*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTMutationListener.h"
17f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/DeclOpenMP.h"
20f4a2713aSLionel Sambuc #include "clang/AST/StmtCXX.h"
21f4a2713aSLionel Sambuc #include "clang/AST/StmtOpenMP.h"
22f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
23*0a6a1f1dSLionel Sambuc #include "clang/Basic/OpenMPKinds.h"
24f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/Initialization.h"
26f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
27f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
28f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
29*0a6a1f1dSLionel Sambuc #include "clang/Sema/SemaInternal.h"
30f4a2713aSLionel Sambuc using namespace clang;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
33f4a2713aSLionel Sambuc // Stack of data-sharing attributes for variables
34f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc namespace {
37f4a2713aSLionel Sambuc /// \brief Default data sharing attributes, which can be applied to directive.
38f4a2713aSLionel Sambuc enum DefaultDataSharingAttributes {
39f4a2713aSLionel Sambuc   DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
40f4a2713aSLionel Sambuc   DSA_none = 1 << 0,   /// \brief Default data sharing attribute 'none'.
41f4a2713aSLionel Sambuc   DSA_shared = 1 << 1  /// \brief Default data sharing attribute 'shared'.
42f4a2713aSLionel Sambuc };
43f4a2713aSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc template <class T> struct MatchesAny {
MatchesAny__anon8add7ef70111::MatchesAny45*0a6a1f1dSLionel Sambuc   explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
operator ()__anon8add7ef70111::MatchesAny46*0a6a1f1dSLionel Sambuc   bool operator()(T Kind) {
47*0a6a1f1dSLionel Sambuc     for (auto KindEl : Arr)
48*0a6a1f1dSLionel Sambuc       if (KindEl == Kind)
49*0a6a1f1dSLionel Sambuc         return true;
50*0a6a1f1dSLionel Sambuc     return false;
51*0a6a1f1dSLionel Sambuc   }
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc private:
54*0a6a1f1dSLionel Sambuc   ArrayRef<T> Arr;
55*0a6a1f1dSLionel Sambuc };
56*0a6a1f1dSLionel Sambuc struct MatchesAlways {
MatchesAlways__anon8add7ef70111::MatchesAlways57*0a6a1f1dSLionel Sambuc   MatchesAlways() {}
operator ()__anon8add7ef70111::MatchesAlways58*0a6a1f1dSLionel Sambuc   template <class T> bool operator()(T) { return true; }
59*0a6a1f1dSLionel Sambuc };
60*0a6a1f1dSLionel Sambuc 
61*0a6a1f1dSLionel Sambuc typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
62*0a6a1f1dSLionel Sambuc typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
63*0a6a1f1dSLionel Sambuc 
64f4a2713aSLionel Sambuc /// \brief Stack for tracking declarations used in OpenMP directives and
65f4a2713aSLionel Sambuc /// clauses and their data-sharing attributes.
66f4a2713aSLionel Sambuc class DSAStackTy {
67f4a2713aSLionel Sambuc public:
68f4a2713aSLionel Sambuc   struct DSAVarData {
69f4a2713aSLionel Sambuc     OpenMPDirectiveKind DKind;
70f4a2713aSLionel Sambuc     OpenMPClauseKind CKind;
71f4a2713aSLionel Sambuc     DeclRefExpr *RefExpr;
72*0a6a1f1dSLionel Sambuc     SourceLocation ImplicitDSALoc;
DSAVarData__anon8add7ef70111::DSAStackTy::DSAVarData73*0a6a1f1dSLionel Sambuc     DSAVarData()
74*0a6a1f1dSLionel Sambuc         : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
75*0a6a1f1dSLionel Sambuc           ImplicitDSALoc() {}
76f4a2713aSLionel Sambuc   };
77*0a6a1f1dSLionel Sambuc 
78f4a2713aSLionel Sambuc private:
79f4a2713aSLionel Sambuc   struct DSAInfo {
80f4a2713aSLionel Sambuc     OpenMPClauseKind Attributes;
81f4a2713aSLionel Sambuc     DeclRefExpr *RefExpr;
82f4a2713aSLionel Sambuc   };
83f4a2713aSLionel Sambuc   typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
84*0a6a1f1dSLionel Sambuc   typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   struct SharingMapTy {
87f4a2713aSLionel Sambuc     DeclSAMapTy SharingMap;
88*0a6a1f1dSLionel Sambuc     AlignedMapTy AlignedMap;
89f4a2713aSLionel Sambuc     DefaultDataSharingAttributes DefaultAttr;
90*0a6a1f1dSLionel Sambuc     SourceLocation DefaultAttrLoc;
91f4a2713aSLionel Sambuc     OpenMPDirectiveKind Directive;
92f4a2713aSLionel Sambuc     DeclarationNameInfo DirectiveName;
93f4a2713aSLionel Sambuc     Scope *CurScope;
94*0a6a1f1dSLionel Sambuc     SourceLocation ConstructLoc;
95*0a6a1f1dSLionel Sambuc     bool OrderedRegion;
96*0a6a1f1dSLionel Sambuc     SourceLocation InnerTeamsRegionLoc;
SharingMapTy__anon8add7ef70111::DSAStackTy::SharingMapTy97*0a6a1f1dSLionel Sambuc     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
98*0a6a1f1dSLionel Sambuc                  Scope *CurScope, SourceLocation Loc)
99*0a6a1f1dSLionel Sambuc         : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
100*0a6a1f1dSLionel Sambuc           Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
101*0a6a1f1dSLionel Sambuc           ConstructLoc(Loc), OrderedRegion(false), InnerTeamsRegionLoc() {}
SharingMapTy__anon8add7ef70111::DSAStackTy::SharingMapTy102f4a2713aSLionel Sambuc     SharingMapTy()
103*0a6a1f1dSLionel Sambuc         : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
104*0a6a1f1dSLionel Sambuc           Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
105*0a6a1f1dSLionel Sambuc           ConstructLoc(), OrderedRegion(false), InnerTeamsRegionLoc() {}
106f4a2713aSLionel Sambuc   };
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc   typedef SmallVector<SharingMapTy, 64> StackTy;
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc   /// \brief Stack of used declaration and their data-sharing attributes.
111f4a2713aSLionel Sambuc   StackTy Stack;
112*0a6a1f1dSLionel Sambuc   Sema &SemaRef;
113f4a2713aSLionel Sambuc 
114f4a2713aSLionel Sambuc   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc   DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc   /// \brief Checks if the variable is a local for OpenMP region.
119*0a6a1f1dSLionel Sambuc   bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
120*0a6a1f1dSLionel Sambuc 
121f4a2713aSLionel Sambuc public:
DSAStackTy(Sema & S)122*0a6a1f1dSLionel Sambuc   explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
123f4a2713aSLionel Sambuc 
push(OpenMPDirectiveKind DKind,const DeclarationNameInfo & DirName,Scope * CurScope,SourceLocation Loc)124f4a2713aSLionel Sambuc   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
125*0a6a1f1dSLionel Sambuc             Scope *CurScope, SourceLocation Loc) {
126*0a6a1f1dSLionel Sambuc     Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
127*0a6a1f1dSLionel Sambuc     Stack.back().DefaultAttrLoc = Loc;
128f4a2713aSLionel Sambuc   }
129f4a2713aSLionel Sambuc 
pop()130f4a2713aSLionel Sambuc   void pop() {
131f4a2713aSLionel Sambuc     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
132f4a2713aSLionel Sambuc     Stack.pop_back();
133f4a2713aSLionel Sambuc   }
134f4a2713aSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc   /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
136*0a6a1f1dSLionel Sambuc   /// add it and return NULL; otherwise return previous occurrence's expression
137*0a6a1f1dSLionel Sambuc   /// for diagnostics.
138*0a6a1f1dSLionel Sambuc   DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
139*0a6a1f1dSLionel Sambuc 
140f4a2713aSLionel Sambuc   /// \brief Adds explicit data sharing attribute to the specified declaration.
141f4a2713aSLionel Sambuc   void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc   /// \brief Returns data sharing attributes from top of the stack for the
144f4a2713aSLionel Sambuc   /// specified declaration.
145*0a6a1f1dSLionel Sambuc   DSAVarData getTopDSA(VarDecl *D, bool FromParent);
146f4a2713aSLionel Sambuc   /// \brief Returns data-sharing attributes for the specified declaration.
147*0a6a1f1dSLionel Sambuc   DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
148*0a6a1f1dSLionel Sambuc   /// \brief Checks if the specified variables has data-sharing attributes which
149*0a6a1f1dSLionel Sambuc   /// match specified \a CPred predicate in any directive which matches \a DPred
150*0a6a1f1dSLionel Sambuc   /// predicate.
151*0a6a1f1dSLionel Sambuc   template <class ClausesPredicate, class DirectivesPredicate>
152*0a6a1f1dSLionel Sambuc   DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
153*0a6a1f1dSLionel Sambuc                     DirectivesPredicate DPred, bool FromParent);
154*0a6a1f1dSLionel Sambuc   /// \brief Checks if the specified variables has data-sharing attributes which
155*0a6a1f1dSLionel Sambuc   /// match specified \a CPred predicate in any innermost directive which
156*0a6a1f1dSLionel Sambuc   /// matches \a DPred predicate.
157*0a6a1f1dSLionel Sambuc   template <class ClausesPredicate, class DirectivesPredicate>
158*0a6a1f1dSLionel Sambuc   DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
159*0a6a1f1dSLionel Sambuc                              DirectivesPredicate DPred,
160*0a6a1f1dSLionel Sambuc                              bool FromParent);
161*0a6a1f1dSLionel Sambuc   /// \brief Finds a directive which matches specified \a DPred predicate.
162*0a6a1f1dSLionel Sambuc   template <class NamedDirectivesPredicate>
163*0a6a1f1dSLionel Sambuc   bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc   /// \brief Returns currently analyzed directive.
getCurrentDirective() const166f4a2713aSLionel Sambuc   OpenMPDirectiveKind getCurrentDirective() const {
167f4a2713aSLionel Sambuc     return Stack.back().Directive;
168f4a2713aSLionel Sambuc   }
169*0a6a1f1dSLionel Sambuc   /// \brief Returns parent directive.
getParentDirective() const170*0a6a1f1dSLionel Sambuc   OpenMPDirectiveKind getParentDirective() const {
171*0a6a1f1dSLionel Sambuc     if (Stack.size() > 2)
172*0a6a1f1dSLionel Sambuc       return Stack[Stack.size() - 2].Directive;
173*0a6a1f1dSLionel Sambuc     return OMPD_unknown;
174*0a6a1f1dSLionel Sambuc   }
175f4a2713aSLionel Sambuc 
176f4a2713aSLionel Sambuc   /// \brief Set default data sharing attribute to none.
setDefaultDSANone(SourceLocation Loc)177*0a6a1f1dSLionel Sambuc   void setDefaultDSANone(SourceLocation Loc) {
178*0a6a1f1dSLionel Sambuc     Stack.back().DefaultAttr = DSA_none;
179*0a6a1f1dSLionel Sambuc     Stack.back().DefaultAttrLoc = Loc;
180*0a6a1f1dSLionel Sambuc   }
181f4a2713aSLionel Sambuc   /// \brief Set default data sharing attribute to shared.
setDefaultDSAShared(SourceLocation Loc)182*0a6a1f1dSLionel Sambuc   void setDefaultDSAShared(SourceLocation Loc) {
183*0a6a1f1dSLionel Sambuc     Stack.back().DefaultAttr = DSA_shared;
184*0a6a1f1dSLionel Sambuc     Stack.back().DefaultAttrLoc = Loc;
185*0a6a1f1dSLionel Sambuc   }
186f4a2713aSLionel Sambuc 
getDefaultDSA() const187f4a2713aSLionel Sambuc   DefaultDataSharingAttributes getDefaultDSA() const {
188f4a2713aSLionel Sambuc     return Stack.back().DefaultAttr;
189f4a2713aSLionel Sambuc   }
getDefaultDSALocation() const190*0a6a1f1dSLionel Sambuc   SourceLocation getDefaultDSALocation() const {
191*0a6a1f1dSLionel Sambuc     return Stack.back().DefaultAttrLoc;
192*0a6a1f1dSLionel Sambuc   }
193f4a2713aSLionel Sambuc 
194*0a6a1f1dSLionel Sambuc   /// \brief Checks if the specified variable is a threadprivate.
isThreadPrivate(VarDecl * D)195*0a6a1f1dSLionel Sambuc   bool isThreadPrivate(VarDecl *D) {
196*0a6a1f1dSLionel Sambuc     DSAVarData DVar = getTopDSA(D, false);
197*0a6a1f1dSLionel Sambuc     return isOpenMPThreadPrivate(DVar.CKind);
198*0a6a1f1dSLionel Sambuc   }
199*0a6a1f1dSLionel Sambuc 
200*0a6a1f1dSLionel Sambuc   /// \brief Marks current region as ordered (it has an 'ordered' clause).
setOrderedRegion(bool IsOrdered=true)201*0a6a1f1dSLionel Sambuc   void setOrderedRegion(bool IsOrdered = true) {
202*0a6a1f1dSLionel Sambuc     Stack.back().OrderedRegion = IsOrdered;
203*0a6a1f1dSLionel Sambuc   }
204*0a6a1f1dSLionel Sambuc   /// \brief Returns true, if parent region is ordered (has associated
205*0a6a1f1dSLionel Sambuc   /// 'ordered' clause), false - otherwise.
isParentOrderedRegion() const206*0a6a1f1dSLionel Sambuc   bool isParentOrderedRegion() const {
207*0a6a1f1dSLionel Sambuc     if (Stack.size() > 2)
208*0a6a1f1dSLionel Sambuc       return Stack[Stack.size() - 2].OrderedRegion;
209*0a6a1f1dSLionel Sambuc     return false;
210*0a6a1f1dSLionel Sambuc   }
211*0a6a1f1dSLionel Sambuc 
212*0a6a1f1dSLionel Sambuc   /// \brief Marks current target region as one with closely nested teams
213*0a6a1f1dSLionel Sambuc   /// region.
setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc)214*0a6a1f1dSLionel Sambuc   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
215*0a6a1f1dSLionel Sambuc     if (Stack.size() > 2)
216*0a6a1f1dSLionel Sambuc       Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
217*0a6a1f1dSLionel Sambuc   }
218*0a6a1f1dSLionel Sambuc   /// \brief Returns true, if current region has closely nested teams region.
hasInnerTeamsRegion() const219*0a6a1f1dSLionel Sambuc   bool hasInnerTeamsRegion() const {
220*0a6a1f1dSLionel Sambuc     return getInnerTeamsRegionLoc().isValid();
221*0a6a1f1dSLionel Sambuc   }
222*0a6a1f1dSLionel Sambuc   /// \brief Returns location of the nested teams region (if any).
getInnerTeamsRegionLoc() const223*0a6a1f1dSLionel Sambuc   SourceLocation getInnerTeamsRegionLoc() const {
224*0a6a1f1dSLionel Sambuc     if (Stack.size() > 1)
225*0a6a1f1dSLionel Sambuc       return Stack.back().InnerTeamsRegionLoc;
226*0a6a1f1dSLionel Sambuc     return SourceLocation();
227*0a6a1f1dSLionel Sambuc   }
228*0a6a1f1dSLionel Sambuc 
getCurScope() const229*0a6a1f1dSLionel Sambuc   Scope *getCurScope() const { return Stack.back().CurScope; }
getCurScope()230f4a2713aSLionel Sambuc   Scope *getCurScope() { return Stack.back().CurScope; }
getConstructLoc()231*0a6a1f1dSLionel Sambuc   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
232f4a2713aSLionel Sambuc };
isParallelOrTaskRegion(OpenMPDirectiveKind DKind)233*0a6a1f1dSLionel Sambuc bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
234*0a6a1f1dSLionel Sambuc   return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
235*0a6a1f1dSLionel Sambuc          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
236*0a6a1f1dSLionel Sambuc }
237*0a6a1f1dSLionel Sambuc } // namespace
238f4a2713aSLionel Sambuc 
getDSA(StackTy::reverse_iterator Iter,VarDecl * D)239f4a2713aSLionel Sambuc DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
240f4a2713aSLionel Sambuc                                           VarDecl *D) {
241f4a2713aSLionel Sambuc   DSAVarData DVar;
242*0a6a1f1dSLionel Sambuc   if (Iter == std::prev(Stack.rend())) {
243f4a2713aSLionel Sambuc     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
244f4a2713aSLionel Sambuc     // in a region but not in construct]
245f4a2713aSLionel Sambuc     //  File-scope or namespace-scope variables referenced in called routines
246f4a2713aSLionel Sambuc     //  in the region are shared unless they appear in a threadprivate
247f4a2713aSLionel Sambuc     //  directive.
248*0a6a1f1dSLionel Sambuc     if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
249f4a2713aSLionel Sambuc       DVar.CKind = OMPC_shared;
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
252f4a2713aSLionel Sambuc     // in a region but not in construct]
253f4a2713aSLionel Sambuc     //  Variables with static storage duration that are declared in called
254f4a2713aSLionel Sambuc     //  routines in the region are shared.
255f4a2713aSLionel Sambuc     if (D->hasGlobalStorage())
256f4a2713aSLionel Sambuc       DVar.CKind = OMPC_shared;
257f4a2713aSLionel Sambuc 
258f4a2713aSLionel Sambuc     return DVar;
259f4a2713aSLionel Sambuc   }
260*0a6a1f1dSLionel Sambuc 
261f4a2713aSLionel Sambuc   DVar.DKind = Iter->Directive;
262*0a6a1f1dSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263*0a6a1f1dSLionel Sambuc   // in a Construct, C/C++, predetermined, p.1]
264*0a6a1f1dSLionel Sambuc   // Variables with automatic storage duration that are declared in a scope
265*0a6a1f1dSLionel Sambuc   // inside the construct are private.
266*0a6a1f1dSLionel Sambuc   if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
267*0a6a1f1dSLionel Sambuc       (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
268*0a6a1f1dSLionel Sambuc     DVar.CKind = OMPC_private;
269*0a6a1f1dSLionel Sambuc     return DVar;
270*0a6a1f1dSLionel Sambuc   }
271*0a6a1f1dSLionel Sambuc 
272f4a2713aSLionel Sambuc   // Explicitly specified attributes and local variables with predetermined
273f4a2713aSLionel Sambuc   // attributes.
274f4a2713aSLionel Sambuc   if (Iter->SharingMap.count(D)) {
275f4a2713aSLionel Sambuc     DVar.RefExpr = Iter->SharingMap[D].RefExpr;
276f4a2713aSLionel Sambuc     DVar.CKind = Iter->SharingMap[D].Attributes;
277*0a6a1f1dSLionel Sambuc     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
278f4a2713aSLionel Sambuc     return DVar;
279f4a2713aSLionel Sambuc   }
280f4a2713aSLionel Sambuc 
281f4a2713aSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
282f4a2713aSLionel Sambuc   // in a Construct, C/C++, implicitly determined, p.1]
283f4a2713aSLionel Sambuc   //  In a parallel or task construct, the data-sharing attributes of these
284f4a2713aSLionel Sambuc   //  variables are determined by the default clause, if present.
285f4a2713aSLionel Sambuc   switch (Iter->DefaultAttr) {
286f4a2713aSLionel Sambuc   case DSA_shared:
287f4a2713aSLionel Sambuc     DVar.CKind = OMPC_shared;
288*0a6a1f1dSLionel Sambuc     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
289f4a2713aSLionel Sambuc     return DVar;
290f4a2713aSLionel Sambuc   case DSA_none:
291f4a2713aSLionel Sambuc     return DVar;
292f4a2713aSLionel Sambuc   case DSA_unspecified:
293f4a2713aSLionel Sambuc     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
294f4a2713aSLionel Sambuc     // in a Construct, implicitly determined, p.2]
295f4a2713aSLionel Sambuc     //  In a parallel construct, if no default clause is present, these
296f4a2713aSLionel Sambuc     //  variables are shared.
297*0a6a1f1dSLionel Sambuc     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
298*0a6a1f1dSLionel Sambuc     if (isOpenMPParallelDirective(DVar.DKind) ||
299*0a6a1f1dSLionel Sambuc         isOpenMPTeamsDirective(DVar.DKind)) {
300f4a2713aSLionel Sambuc       DVar.CKind = OMPC_shared;
301f4a2713aSLionel Sambuc       return DVar;
302f4a2713aSLionel Sambuc     }
303f4a2713aSLionel Sambuc 
304f4a2713aSLionel Sambuc     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
305f4a2713aSLionel Sambuc     // in a Construct, implicitly determined, p.4]
306f4a2713aSLionel Sambuc     //  In a task construct, if no default clause is present, a variable that in
307f4a2713aSLionel Sambuc     //  the enclosing context is determined to be shared by all implicit tasks
308f4a2713aSLionel Sambuc     //  bound to the current team is shared.
309f4a2713aSLionel Sambuc     if (DVar.DKind == OMPD_task) {
310f4a2713aSLionel Sambuc       DSAVarData DVarTemp;
311*0a6a1f1dSLionel Sambuc       for (StackTy::reverse_iterator I = std::next(Iter),
312*0a6a1f1dSLionel Sambuc                                      EE = std::prev(Stack.rend());
313f4a2713aSLionel Sambuc            I != EE; ++I) {
314*0a6a1f1dSLionel Sambuc         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
315*0a6a1f1dSLionel Sambuc         // Referenced
316f4a2713aSLionel Sambuc         // in a Construct, implicitly determined, p.6]
317f4a2713aSLionel Sambuc         //  In a task construct, if no default clause is present, a variable
318f4a2713aSLionel Sambuc         //  whose data-sharing attribute is not determined by the rules above is
319f4a2713aSLionel Sambuc         //  firstprivate.
320f4a2713aSLionel Sambuc         DVarTemp = getDSA(I, D);
321f4a2713aSLionel Sambuc         if (DVarTemp.CKind != OMPC_shared) {
322*0a6a1f1dSLionel Sambuc           DVar.RefExpr = nullptr;
323f4a2713aSLionel Sambuc           DVar.DKind = OMPD_task;
324f4a2713aSLionel Sambuc           DVar.CKind = OMPC_firstprivate;
325f4a2713aSLionel Sambuc           return DVar;
326f4a2713aSLionel Sambuc         }
327*0a6a1f1dSLionel Sambuc         if (isParallelOrTaskRegion(I->Directive))
328*0a6a1f1dSLionel Sambuc           break;
329f4a2713aSLionel Sambuc       }
330f4a2713aSLionel Sambuc       DVar.DKind = OMPD_task;
331f4a2713aSLionel Sambuc       DVar.CKind =
332f4a2713aSLionel Sambuc           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
333f4a2713aSLionel Sambuc       return DVar;
334f4a2713aSLionel Sambuc     }
335f4a2713aSLionel Sambuc   }
336f4a2713aSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
337f4a2713aSLionel Sambuc   // in a Construct, implicitly determined, p.3]
338f4a2713aSLionel Sambuc   //  For constructs other than task, if no default clause is present, these
339f4a2713aSLionel Sambuc   //  variables inherit their data-sharing attributes from the enclosing
340f4a2713aSLionel Sambuc   //  context.
341*0a6a1f1dSLionel Sambuc   return getDSA(std::next(Iter), D);
342*0a6a1f1dSLionel Sambuc }
343*0a6a1f1dSLionel Sambuc 
addUniqueAligned(VarDecl * D,DeclRefExpr * NewDE)344*0a6a1f1dSLionel Sambuc DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
345*0a6a1f1dSLionel Sambuc   assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
346*0a6a1f1dSLionel Sambuc   auto It = Stack.back().AlignedMap.find(D);
347*0a6a1f1dSLionel Sambuc   if (It == Stack.back().AlignedMap.end()) {
348*0a6a1f1dSLionel Sambuc     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
349*0a6a1f1dSLionel Sambuc     Stack.back().AlignedMap[D] = NewDE;
350*0a6a1f1dSLionel Sambuc     return nullptr;
351*0a6a1f1dSLionel Sambuc   } else {
352*0a6a1f1dSLionel Sambuc     assert(It->second && "Unexpected nullptr expr in the aligned map");
353*0a6a1f1dSLionel Sambuc     return It->second;
354*0a6a1f1dSLionel Sambuc   }
355*0a6a1f1dSLionel Sambuc   return nullptr;
356f4a2713aSLionel Sambuc }
357f4a2713aSLionel Sambuc 
addDSA(VarDecl * D,DeclRefExpr * E,OpenMPClauseKind A)358f4a2713aSLionel Sambuc void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
359f4a2713aSLionel Sambuc   if (A == OMPC_threadprivate) {
360f4a2713aSLionel Sambuc     Stack[0].SharingMap[D].Attributes = A;
361f4a2713aSLionel Sambuc     Stack[0].SharingMap[D].RefExpr = E;
362f4a2713aSLionel Sambuc   } else {
363f4a2713aSLionel Sambuc     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
364f4a2713aSLionel Sambuc     Stack.back().SharingMap[D].Attributes = A;
365f4a2713aSLionel Sambuc     Stack.back().SharingMap[D].RefExpr = E;
366f4a2713aSLionel Sambuc   }
367f4a2713aSLionel Sambuc }
368f4a2713aSLionel Sambuc 
isOpenMPLocal(VarDecl * D,StackTy::reverse_iterator Iter)369*0a6a1f1dSLionel Sambuc bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
370*0a6a1f1dSLionel Sambuc   if (Stack.size() > 2) {
371*0a6a1f1dSLionel Sambuc     reverse_iterator I = Iter, E = std::prev(Stack.rend());
372*0a6a1f1dSLionel Sambuc     Scope *TopScope = nullptr;
373*0a6a1f1dSLionel Sambuc     while (I != E && !isParallelOrTaskRegion(I->Directive)) {
374*0a6a1f1dSLionel Sambuc       ++I;
375f4a2713aSLionel Sambuc     }
376*0a6a1f1dSLionel Sambuc     if (I == E)
377*0a6a1f1dSLionel Sambuc       return false;
378*0a6a1f1dSLionel Sambuc     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
379*0a6a1f1dSLionel Sambuc     Scope *CurScope = getCurScope();
380*0a6a1f1dSLionel Sambuc     while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
381*0a6a1f1dSLionel Sambuc       CurScope = CurScope->getParent();
382*0a6a1f1dSLionel Sambuc     }
383*0a6a1f1dSLionel Sambuc     return CurScope != TopScope;
384*0a6a1f1dSLionel Sambuc   }
385*0a6a1f1dSLionel Sambuc   return false;
386f4a2713aSLionel Sambuc }
387f4a2713aSLionel Sambuc 
getTopDSA(VarDecl * D,bool FromParent)388*0a6a1f1dSLionel Sambuc DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
389f4a2713aSLionel Sambuc   DSAVarData DVar;
390f4a2713aSLionel Sambuc 
391f4a2713aSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
392f4a2713aSLionel Sambuc   // in a Construct, C/C++, predetermined, p.1]
393f4a2713aSLionel Sambuc   //  Variables appearing in threadprivate directives are threadprivate.
394*0a6a1f1dSLionel Sambuc   if (D->getTLSKind() != VarDecl::TLS_None ||
395*0a6a1f1dSLionel Sambuc       D->getStorageClass() == SC_Register) {
396f4a2713aSLionel Sambuc     DVar.CKind = OMPC_threadprivate;
397f4a2713aSLionel Sambuc     return DVar;
398f4a2713aSLionel Sambuc   }
399f4a2713aSLionel Sambuc   if (Stack[0].SharingMap.count(D)) {
400f4a2713aSLionel Sambuc     DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
401f4a2713aSLionel Sambuc     DVar.CKind = OMPC_threadprivate;
402f4a2713aSLionel Sambuc     return DVar;
403f4a2713aSLionel Sambuc   }
404f4a2713aSLionel Sambuc 
405f4a2713aSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
406f4a2713aSLionel Sambuc   // in a Construct, C/C++, predetermined, p.1]
407f4a2713aSLionel Sambuc   // Variables with automatic storage duration that are declared in a scope
408f4a2713aSLionel Sambuc   // inside the construct are private.
409*0a6a1f1dSLionel Sambuc   OpenMPDirectiveKind Kind =
410*0a6a1f1dSLionel Sambuc       FromParent ? getParentDirective() : getCurrentDirective();
411*0a6a1f1dSLionel Sambuc   auto StartI = std::next(Stack.rbegin());
412*0a6a1f1dSLionel Sambuc   auto EndI = std::prev(Stack.rend());
413*0a6a1f1dSLionel Sambuc   if (FromParent && StartI != EndI) {
414*0a6a1f1dSLionel Sambuc     StartI = std::next(StartI);
415*0a6a1f1dSLionel Sambuc   }
416*0a6a1f1dSLionel Sambuc   if (!isParallelOrTaskRegion(Kind)) {
417*0a6a1f1dSLionel Sambuc     if (isOpenMPLocal(D, StartI) &&
418*0a6a1f1dSLionel Sambuc         ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
419*0a6a1f1dSLionel Sambuc                                   D->getStorageClass() == SC_None)) ||
420*0a6a1f1dSLionel Sambuc          isa<ParmVarDecl>(D))) {
421f4a2713aSLionel Sambuc       DVar.CKind = OMPC_private;
422f4a2713aSLionel Sambuc       return DVar;
423f4a2713aSLionel Sambuc     }
424*0a6a1f1dSLionel Sambuc   }
425f4a2713aSLionel Sambuc 
426f4a2713aSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
427f4a2713aSLionel Sambuc   // in a Construct, C/C++, predetermined, p.4]
428*0a6a1f1dSLionel Sambuc   //  Static data members are shared.
429f4a2713aSLionel Sambuc   if (D->isStaticDataMember()) {
430*0a6a1f1dSLionel Sambuc     // Variables with const-qualified type having no mutable member may be
431*0a6a1f1dSLionel Sambuc     // listed in a firstprivate clause, even if they are static data members.
432*0a6a1f1dSLionel Sambuc     DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
433*0a6a1f1dSLionel Sambuc                                  MatchesAlways(), FromParent);
434f4a2713aSLionel Sambuc     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
435f4a2713aSLionel Sambuc       return DVar;
436f4a2713aSLionel Sambuc 
437f4a2713aSLionel Sambuc     DVar.CKind = OMPC_shared;
438f4a2713aSLionel Sambuc     return DVar;
439f4a2713aSLionel Sambuc   }
440f4a2713aSLionel Sambuc 
441f4a2713aSLionel Sambuc   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
442*0a6a1f1dSLionel Sambuc   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
443f4a2713aSLionel Sambuc   while (Type->isArrayType()) {
444f4a2713aSLionel Sambuc     QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
445f4a2713aSLionel Sambuc     Type = ElemType.getNonReferenceType().getCanonicalType();
446f4a2713aSLionel Sambuc   }
447f4a2713aSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
448f4a2713aSLionel Sambuc   // in a Construct, C/C++, predetermined, p.6]
449f4a2713aSLionel Sambuc   //  Variables with const qualified type having no mutable member are
450f4a2713aSLionel Sambuc   //  shared.
451*0a6a1f1dSLionel Sambuc   CXXRecordDecl *RD =
452*0a6a1f1dSLionel Sambuc       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
453f4a2713aSLionel Sambuc   if (IsConstant &&
454*0a6a1f1dSLionel Sambuc       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
455f4a2713aSLionel Sambuc     // Variables with const-qualified type having no mutable member may be
456f4a2713aSLionel Sambuc     // listed in a firstprivate clause, even if they are static data members.
457*0a6a1f1dSLionel Sambuc     DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
458*0a6a1f1dSLionel Sambuc                                  MatchesAlways(), FromParent);
459f4a2713aSLionel Sambuc     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
460f4a2713aSLionel Sambuc       return DVar;
461f4a2713aSLionel Sambuc 
462f4a2713aSLionel Sambuc     DVar.CKind = OMPC_shared;
463f4a2713aSLionel Sambuc     return DVar;
464f4a2713aSLionel Sambuc   }
465f4a2713aSLionel Sambuc 
466f4a2713aSLionel Sambuc   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
467f4a2713aSLionel Sambuc   // in a Construct, C/C++, predetermined, p.7]
468f4a2713aSLionel Sambuc   //  Variables with static storage duration that are declared in a scope
469f4a2713aSLionel Sambuc   //  inside the construct are shared.
470*0a6a1f1dSLionel Sambuc   if (D->isStaticLocal()) {
471f4a2713aSLionel Sambuc     DVar.CKind = OMPC_shared;
472f4a2713aSLionel Sambuc     return DVar;
473f4a2713aSLionel Sambuc   }
474f4a2713aSLionel Sambuc 
475f4a2713aSLionel Sambuc   // Explicitly specified attributes and local variables with predetermined
476f4a2713aSLionel Sambuc   // attributes.
477*0a6a1f1dSLionel Sambuc   auto I = std::prev(StartI);
478*0a6a1f1dSLionel Sambuc   if (I->SharingMap.count(D)) {
479*0a6a1f1dSLionel Sambuc     DVar.RefExpr = I->SharingMap[D].RefExpr;
480*0a6a1f1dSLionel Sambuc     DVar.CKind = I->SharingMap[D].Attributes;
481*0a6a1f1dSLionel Sambuc     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
482f4a2713aSLionel Sambuc   }
483f4a2713aSLionel Sambuc 
484f4a2713aSLionel Sambuc   return DVar;
485f4a2713aSLionel Sambuc }
486f4a2713aSLionel Sambuc 
getImplicitDSA(VarDecl * D,bool FromParent)487*0a6a1f1dSLionel Sambuc DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
488*0a6a1f1dSLionel Sambuc   auto StartI = Stack.rbegin();
489*0a6a1f1dSLionel Sambuc   auto EndI = std::prev(Stack.rend());
490*0a6a1f1dSLionel Sambuc   if (FromParent && StartI != EndI) {
491*0a6a1f1dSLionel Sambuc     StartI = std::next(StartI);
492*0a6a1f1dSLionel Sambuc   }
493*0a6a1f1dSLionel Sambuc   return getDSA(StartI, D);
494f4a2713aSLionel Sambuc }
495f4a2713aSLionel Sambuc 
496*0a6a1f1dSLionel Sambuc template <class ClausesPredicate, class DirectivesPredicate>
hasDSA(VarDecl * D,ClausesPredicate CPred,DirectivesPredicate DPred,bool FromParent)497*0a6a1f1dSLionel Sambuc DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
498*0a6a1f1dSLionel Sambuc                                           DirectivesPredicate DPred,
499*0a6a1f1dSLionel Sambuc                                           bool FromParent) {
500*0a6a1f1dSLionel Sambuc   auto StartI = std::next(Stack.rbegin());
501*0a6a1f1dSLionel Sambuc   auto EndI = std::prev(Stack.rend());
502*0a6a1f1dSLionel Sambuc   if (FromParent && StartI != EndI) {
503*0a6a1f1dSLionel Sambuc     StartI = std::next(StartI);
504*0a6a1f1dSLionel Sambuc   }
505*0a6a1f1dSLionel Sambuc   for (auto I = StartI, EE = EndI; I != EE; ++I) {
506*0a6a1f1dSLionel Sambuc     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
507*0a6a1f1dSLionel Sambuc       continue;
508f4a2713aSLionel Sambuc     DSAVarData DVar = getDSA(I, D);
509*0a6a1f1dSLionel Sambuc     if (CPred(DVar.CKind))
510f4a2713aSLionel Sambuc       return DVar;
511f4a2713aSLionel Sambuc   }
512f4a2713aSLionel Sambuc   return DSAVarData();
513f4a2713aSLionel Sambuc }
514f4a2713aSLionel Sambuc 
515*0a6a1f1dSLionel Sambuc template <class ClausesPredicate, class DirectivesPredicate>
516*0a6a1f1dSLionel Sambuc DSAStackTy::DSAVarData
hasInnermostDSA(VarDecl * D,ClausesPredicate CPred,DirectivesPredicate DPred,bool FromParent)517*0a6a1f1dSLionel Sambuc DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
518*0a6a1f1dSLionel Sambuc                             DirectivesPredicate DPred, bool FromParent) {
519*0a6a1f1dSLionel Sambuc   auto StartI = std::next(Stack.rbegin());
520*0a6a1f1dSLionel Sambuc   auto EndI = std::prev(Stack.rend());
521*0a6a1f1dSLionel Sambuc   if (FromParent && StartI != EndI) {
522*0a6a1f1dSLionel Sambuc     StartI = std::next(StartI);
523*0a6a1f1dSLionel Sambuc   }
524*0a6a1f1dSLionel Sambuc   for (auto I = StartI, EE = EndI; I != EE; ++I) {
525*0a6a1f1dSLionel Sambuc     if (!DPred(I->Directive))
526*0a6a1f1dSLionel Sambuc       break;
527*0a6a1f1dSLionel Sambuc     DSAVarData DVar = getDSA(I, D);
528*0a6a1f1dSLionel Sambuc     if (CPred(DVar.CKind))
529*0a6a1f1dSLionel Sambuc       return DVar;
530*0a6a1f1dSLionel Sambuc     return DSAVarData();
531*0a6a1f1dSLionel Sambuc   }
532*0a6a1f1dSLionel Sambuc   return DSAVarData();
533*0a6a1f1dSLionel Sambuc }
534*0a6a1f1dSLionel Sambuc 
535*0a6a1f1dSLionel Sambuc template <class NamedDirectivesPredicate>
hasDirective(NamedDirectivesPredicate DPred,bool FromParent)536*0a6a1f1dSLionel Sambuc bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
537*0a6a1f1dSLionel Sambuc   auto StartI = std::next(Stack.rbegin());
538*0a6a1f1dSLionel Sambuc   auto EndI = std::prev(Stack.rend());
539*0a6a1f1dSLionel Sambuc   if (FromParent && StartI != EndI) {
540*0a6a1f1dSLionel Sambuc     StartI = std::next(StartI);
541*0a6a1f1dSLionel Sambuc   }
542*0a6a1f1dSLionel Sambuc   for (auto I = StartI, EE = EndI; I != EE; ++I) {
543*0a6a1f1dSLionel Sambuc     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
544*0a6a1f1dSLionel Sambuc       return true;
545*0a6a1f1dSLionel Sambuc   }
546*0a6a1f1dSLionel Sambuc   return false;
547*0a6a1f1dSLionel Sambuc }
548*0a6a1f1dSLionel Sambuc 
InitDataSharingAttributesStack()549f4a2713aSLionel Sambuc void Sema::InitDataSharingAttributesStack() {
550f4a2713aSLionel Sambuc   VarDataSharingAttributesStack = new DSAStackTy(*this);
551f4a2713aSLionel Sambuc }
552f4a2713aSLionel Sambuc 
553f4a2713aSLionel Sambuc #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
554f4a2713aSLionel Sambuc 
IsOpenMPCapturedVar(VarDecl * VD)555*0a6a1f1dSLionel Sambuc bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
556*0a6a1f1dSLionel Sambuc   assert(LangOpts.OpenMP && "OpenMP is not allowed");
557*0a6a1f1dSLionel Sambuc   if (DSAStack->getCurrentDirective() != OMPD_unknown) {
558*0a6a1f1dSLionel Sambuc     auto DVarPrivate = DSAStack->getTopDSA(VD, /*FromParent=*/false);
559*0a6a1f1dSLionel Sambuc     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
560*0a6a1f1dSLionel Sambuc       return true;
561*0a6a1f1dSLionel Sambuc     DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
562*0a6a1f1dSLionel Sambuc                                    /*FromParent=*/false);
563*0a6a1f1dSLionel Sambuc     return DVarPrivate.CKind != OMPC_unknown;
564f4a2713aSLionel Sambuc   }
565*0a6a1f1dSLionel Sambuc   return false;
566*0a6a1f1dSLionel Sambuc }
567*0a6a1f1dSLionel Sambuc 
DestroyDataSharingAttributesStack()568*0a6a1f1dSLionel Sambuc void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
569f4a2713aSLionel Sambuc 
StartOpenMPDSABlock(OpenMPDirectiveKind DKind,const DeclarationNameInfo & DirName,Scope * CurScope,SourceLocation Loc)570f4a2713aSLionel Sambuc void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
571f4a2713aSLionel Sambuc                                const DeclarationNameInfo &DirName,
572*0a6a1f1dSLionel Sambuc                                Scope *CurScope, SourceLocation Loc) {
573*0a6a1f1dSLionel Sambuc   DSAStack->push(DKind, DirName, CurScope, Loc);
574f4a2713aSLionel Sambuc   PushExpressionEvaluationContext(PotentiallyEvaluated);
575f4a2713aSLionel Sambuc }
576f4a2713aSLionel Sambuc 
EndOpenMPDSABlock(Stmt * CurDirective)577f4a2713aSLionel Sambuc void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
578*0a6a1f1dSLionel Sambuc   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
579*0a6a1f1dSLionel Sambuc   //  A variable of class type (or array thereof) that appears in a lastprivate
580*0a6a1f1dSLionel Sambuc   //  clause requires an accessible, unambiguous default constructor for the
581*0a6a1f1dSLionel Sambuc   //  class type, unless the list item is also specified in a firstprivate
582*0a6a1f1dSLionel Sambuc   //  clause.
583*0a6a1f1dSLionel Sambuc   if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
584*0a6a1f1dSLionel Sambuc     for (auto C : D->clauses()) {
585*0a6a1f1dSLionel Sambuc       if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
586*0a6a1f1dSLionel Sambuc         for (auto VarRef : Clause->varlists()) {
587*0a6a1f1dSLionel Sambuc           if (VarRef->isValueDependent() || VarRef->isTypeDependent())
588*0a6a1f1dSLionel Sambuc             continue;
589*0a6a1f1dSLionel Sambuc           auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
590*0a6a1f1dSLionel Sambuc           auto DVar = DSAStack->getTopDSA(VD, false);
591*0a6a1f1dSLionel Sambuc           if (DVar.CKind == OMPC_lastprivate) {
592*0a6a1f1dSLionel Sambuc             SourceLocation ELoc = VarRef->getExprLoc();
593*0a6a1f1dSLionel Sambuc             auto Type = VarRef->getType();
594*0a6a1f1dSLionel Sambuc             if (Type->isArrayType())
595*0a6a1f1dSLionel Sambuc               Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
596*0a6a1f1dSLionel Sambuc             CXXRecordDecl *RD =
597*0a6a1f1dSLionel Sambuc                 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
598*0a6a1f1dSLionel Sambuc             // FIXME This code must be replaced by actual constructing of the
599*0a6a1f1dSLionel Sambuc             // lastprivate variable.
600*0a6a1f1dSLionel Sambuc             if (RD) {
601*0a6a1f1dSLionel Sambuc               CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
602*0a6a1f1dSLionel Sambuc               PartialDiagnostic PD =
603*0a6a1f1dSLionel Sambuc                   PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
604*0a6a1f1dSLionel Sambuc               if (!CD ||
605*0a6a1f1dSLionel Sambuc                   CheckConstructorAccess(
606*0a6a1f1dSLionel Sambuc                       ELoc, CD, InitializedEntity::InitializeTemporary(Type),
607*0a6a1f1dSLionel Sambuc                       CD->getAccess(), PD) == AR_inaccessible ||
608*0a6a1f1dSLionel Sambuc                   CD->isDeleted()) {
609*0a6a1f1dSLionel Sambuc                 Diag(ELoc, diag::err_omp_required_method)
610*0a6a1f1dSLionel Sambuc                     << getOpenMPClauseName(OMPC_lastprivate) << 0;
611*0a6a1f1dSLionel Sambuc                 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
612*0a6a1f1dSLionel Sambuc                               VarDecl::DeclarationOnly;
613*0a6a1f1dSLionel Sambuc                 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
614*0a6a1f1dSLionel Sambuc                                                : diag::note_defined_here)
615*0a6a1f1dSLionel Sambuc                     << VD;
616*0a6a1f1dSLionel Sambuc                 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
617*0a6a1f1dSLionel Sambuc                 continue;
618*0a6a1f1dSLionel Sambuc               }
619*0a6a1f1dSLionel Sambuc               MarkFunctionReferenced(ELoc, CD);
620*0a6a1f1dSLionel Sambuc               DiagnoseUseOfDecl(CD, ELoc);
621*0a6a1f1dSLionel Sambuc             }
622*0a6a1f1dSLionel Sambuc           }
623*0a6a1f1dSLionel Sambuc         }
624*0a6a1f1dSLionel Sambuc       }
625*0a6a1f1dSLionel Sambuc     }
626*0a6a1f1dSLionel Sambuc   }
627*0a6a1f1dSLionel Sambuc 
628f4a2713aSLionel Sambuc   DSAStack->pop();
629f4a2713aSLionel Sambuc   DiscardCleanupsInEvaluationContext();
630f4a2713aSLionel Sambuc   PopExpressionEvaluationContext();
631f4a2713aSLionel Sambuc }
632f4a2713aSLionel Sambuc 
633f4a2713aSLionel Sambuc namespace {
634f4a2713aSLionel Sambuc 
635f4a2713aSLionel Sambuc class VarDeclFilterCCC : public CorrectionCandidateCallback {
636f4a2713aSLionel Sambuc private:
637*0a6a1f1dSLionel Sambuc   Sema &SemaRef;
638*0a6a1f1dSLionel Sambuc 
639f4a2713aSLionel Sambuc public:
VarDeclFilterCCC(Sema & S)640*0a6a1f1dSLionel Sambuc   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
ValidateCandidate(const TypoCorrection & Candidate)641*0a6a1f1dSLionel Sambuc   bool ValidateCandidate(const TypoCorrection &Candidate) override {
642f4a2713aSLionel Sambuc     NamedDecl *ND = Candidate.getCorrectionDecl();
643f4a2713aSLionel Sambuc     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
644f4a2713aSLionel Sambuc       return VD->hasGlobalStorage() &&
645*0a6a1f1dSLionel Sambuc              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
646*0a6a1f1dSLionel Sambuc                                    SemaRef.getCurScope());
647f4a2713aSLionel Sambuc     }
648f4a2713aSLionel Sambuc     return false;
649f4a2713aSLionel Sambuc   }
650f4a2713aSLionel Sambuc };
651*0a6a1f1dSLionel Sambuc } // namespace
652f4a2713aSLionel Sambuc 
ActOnOpenMPIdExpression(Scope * CurScope,CXXScopeSpec & ScopeSpec,const DeclarationNameInfo & Id)653f4a2713aSLionel Sambuc ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
654f4a2713aSLionel Sambuc                                          CXXScopeSpec &ScopeSpec,
655f4a2713aSLionel Sambuc                                          const DeclarationNameInfo &Id) {
656f4a2713aSLionel Sambuc   LookupResult Lookup(*this, Id, LookupOrdinaryName);
657f4a2713aSLionel Sambuc   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
658f4a2713aSLionel Sambuc 
659f4a2713aSLionel Sambuc   if (Lookup.isAmbiguous())
660f4a2713aSLionel Sambuc     return ExprError();
661f4a2713aSLionel Sambuc 
662f4a2713aSLionel Sambuc   VarDecl *VD;
663f4a2713aSLionel Sambuc   if (!Lookup.isSingleResult()) {
664*0a6a1f1dSLionel Sambuc     if (TypoCorrection Corrected = CorrectTypo(
665*0a6a1f1dSLionel Sambuc             Id, LookupOrdinaryName, CurScope, nullptr,
666*0a6a1f1dSLionel Sambuc             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
667f4a2713aSLionel Sambuc       diagnoseTypo(Corrected,
668*0a6a1f1dSLionel Sambuc                    PDiag(Lookup.empty()
669*0a6a1f1dSLionel Sambuc                              ? diag::err_undeclared_var_use_suggest
670f4a2713aSLionel Sambuc                              : diag::err_omp_expected_var_arg_suggest)
671f4a2713aSLionel Sambuc                        << Id.getName());
672f4a2713aSLionel Sambuc       VD = Corrected.getCorrectionDeclAs<VarDecl>();
673f4a2713aSLionel Sambuc     } else {
674f4a2713aSLionel Sambuc       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
675f4a2713aSLionel Sambuc                                        : diag::err_omp_expected_var_arg)
676f4a2713aSLionel Sambuc           << Id.getName();
677f4a2713aSLionel Sambuc       return ExprError();
678f4a2713aSLionel Sambuc     }
679f4a2713aSLionel Sambuc   } else {
680f4a2713aSLionel Sambuc     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
681*0a6a1f1dSLionel Sambuc       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
682f4a2713aSLionel Sambuc       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
683f4a2713aSLionel Sambuc       return ExprError();
684f4a2713aSLionel Sambuc     }
685f4a2713aSLionel Sambuc   }
686f4a2713aSLionel Sambuc   Lookup.suppressDiagnostics();
687f4a2713aSLionel Sambuc 
688f4a2713aSLionel Sambuc   // OpenMP [2.9.2, Syntax, C/C++]
689f4a2713aSLionel Sambuc   //   Variables must be file-scope, namespace-scope, or static block-scope.
690f4a2713aSLionel Sambuc   if (!VD->hasGlobalStorage()) {
691f4a2713aSLionel Sambuc     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
692*0a6a1f1dSLionel Sambuc         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
693*0a6a1f1dSLionel Sambuc     bool IsDecl =
694*0a6a1f1dSLionel Sambuc         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
695f4a2713aSLionel Sambuc     Diag(VD->getLocation(),
696*0a6a1f1dSLionel Sambuc          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
697*0a6a1f1dSLionel Sambuc         << VD;
698f4a2713aSLionel Sambuc     return ExprError();
699f4a2713aSLionel Sambuc   }
700f4a2713aSLionel Sambuc 
701f4a2713aSLionel Sambuc   VarDecl *CanonicalVD = VD->getCanonicalDecl();
702f4a2713aSLionel Sambuc   NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
703f4a2713aSLionel Sambuc   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
704f4a2713aSLionel Sambuc   //   A threadprivate directive for file-scope variables must appear outside
705f4a2713aSLionel Sambuc   //   any definition or declaration.
706f4a2713aSLionel Sambuc   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
707f4a2713aSLionel Sambuc       !getCurLexicalContext()->isTranslationUnit()) {
708f4a2713aSLionel Sambuc     Diag(Id.getLoc(), diag::err_omp_var_scope)
709f4a2713aSLionel Sambuc         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
710*0a6a1f1dSLionel Sambuc     bool IsDecl =
711*0a6a1f1dSLionel Sambuc         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
712*0a6a1f1dSLionel Sambuc     Diag(VD->getLocation(),
713*0a6a1f1dSLionel Sambuc          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
714*0a6a1f1dSLionel Sambuc         << VD;
715f4a2713aSLionel Sambuc     return ExprError();
716f4a2713aSLionel Sambuc   }
717f4a2713aSLionel Sambuc   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
718f4a2713aSLionel Sambuc   //   A threadprivate directive for static class member variables must appear
719f4a2713aSLionel Sambuc   //   in the class definition, in the same scope in which the member
720f4a2713aSLionel Sambuc   //   variables are declared.
721f4a2713aSLionel Sambuc   if (CanonicalVD->isStaticDataMember() &&
722f4a2713aSLionel Sambuc       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
723f4a2713aSLionel Sambuc     Diag(Id.getLoc(), diag::err_omp_var_scope)
724f4a2713aSLionel Sambuc         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
725*0a6a1f1dSLionel Sambuc     bool IsDecl =
726*0a6a1f1dSLionel Sambuc         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
727*0a6a1f1dSLionel Sambuc     Diag(VD->getLocation(),
728*0a6a1f1dSLionel Sambuc          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
729*0a6a1f1dSLionel Sambuc         << VD;
730f4a2713aSLionel Sambuc     return ExprError();
731f4a2713aSLionel Sambuc   }
732f4a2713aSLionel Sambuc   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
733f4a2713aSLionel Sambuc   //   A threadprivate directive for namespace-scope variables must appear
734f4a2713aSLionel Sambuc   //   outside any definition or declaration other than the namespace
735f4a2713aSLionel Sambuc   //   definition itself.
736f4a2713aSLionel Sambuc   if (CanonicalVD->getDeclContext()->isNamespace() &&
737f4a2713aSLionel Sambuc       (!getCurLexicalContext()->isFileContext() ||
738f4a2713aSLionel Sambuc        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
739f4a2713aSLionel Sambuc     Diag(Id.getLoc(), diag::err_omp_var_scope)
740f4a2713aSLionel Sambuc         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
741*0a6a1f1dSLionel Sambuc     bool IsDecl =
742*0a6a1f1dSLionel Sambuc         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
743*0a6a1f1dSLionel Sambuc     Diag(VD->getLocation(),
744*0a6a1f1dSLionel Sambuc          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
745*0a6a1f1dSLionel Sambuc         << VD;
746f4a2713aSLionel Sambuc     return ExprError();
747f4a2713aSLionel Sambuc   }
748f4a2713aSLionel Sambuc   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
749f4a2713aSLionel Sambuc   //   A threadprivate directive for static block-scope variables must appear
750f4a2713aSLionel Sambuc   //   in the scope of the variable and not in a nested scope.
751f4a2713aSLionel Sambuc   if (CanonicalVD->isStaticLocal() && CurScope &&
752f4a2713aSLionel Sambuc       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
753f4a2713aSLionel Sambuc     Diag(Id.getLoc(), diag::err_omp_var_scope)
754f4a2713aSLionel Sambuc         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
755*0a6a1f1dSLionel Sambuc     bool IsDecl =
756*0a6a1f1dSLionel Sambuc         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
757*0a6a1f1dSLionel Sambuc     Diag(VD->getLocation(),
758*0a6a1f1dSLionel Sambuc          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
759*0a6a1f1dSLionel Sambuc         << VD;
760f4a2713aSLionel Sambuc     return ExprError();
761f4a2713aSLionel Sambuc   }
762f4a2713aSLionel Sambuc 
763f4a2713aSLionel Sambuc   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
764f4a2713aSLionel Sambuc   //   A threadprivate directive must lexically precede all references to any
765f4a2713aSLionel Sambuc   //   of the variables in its list.
766f4a2713aSLionel Sambuc   if (VD->isUsed()) {
767f4a2713aSLionel Sambuc     Diag(Id.getLoc(), diag::err_omp_var_used)
768f4a2713aSLionel Sambuc         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
769f4a2713aSLionel Sambuc     return ExprError();
770f4a2713aSLionel Sambuc   }
771f4a2713aSLionel Sambuc 
772f4a2713aSLionel Sambuc   QualType ExprType = VD->getType().getNonReferenceType();
773*0a6a1f1dSLionel Sambuc   ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
774f4a2713aSLionel Sambuc   return DE;
775f4a2713aSLionel Sambuc }
776f4a2713aSLionel Sambuc 
777*0a6a1f1dSLionel Sambuc Sema::DeclGroupPtrTy
ActOnOpenMPThreadprivateDirective(SourceLocation Loc,ArrayRef<Expr * > VarList)778*0a6a1f1dSLionel Sambuc Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
779f4a2713aSLionel Sambuc                                         ArrayRef<Expr *> VarList) {
780f4a2713aSLionel Sambuc   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
781f4a2713aSLionel Sambuc     CurContext->addDecl(D);
782f4a2713aSLionel Sambuc     return DeclGroupPtrTy::make(DeclGroupRef(D));
783f4a2713aSLionel Sambuc   }
784f4a2713aSLionel Sambuc   return DeclGroupPtrTy();
785f4a2713aSLionel Sambuc }
786f4a2713aSLionel Sambuc 
787*0a6a1f1dSLionel Sambuc namespace {
788*0a6a1f1dSLionel Sambuc class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
789*0a6a1f1dSLionel Sambuc   Sema &SemaRef;
790*0a6a1f1dSLionel Sambuc 
791*0a6a1f1dSLionel Sambuc public:
VisitDeclRefExpr(const DeclRefExpr * E)792*0a6a1f1dSLionel Sambuc   bool VisitDeclRefExpr(const DeclRefExpr *E) {
793*0a6a1f1dSLionel Sambuc     if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
794*0a6a1f1dSLionel Sambuc       if (VD->hasLocalStorage()) {
795*0a6a1f1dSLionel Sambuc         SemaRef.Diag(E->getLocStart(),
796*0a6a1f1dSLionel Sambuc                      diag::err_omp_local_var_in_threadprivate_init)
797*0a6a1f1dSLionel Sambuc             << E->getSourceRange();
798*0a6a1f1dSLionel Sambuc         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
799*0a6a1f1dSLionel Sambuc             << VD << VD->getSourceRange();
800*0a6a1f1dSLionel Sambuc         return true;
801*0a6a1f1dSLionel Sambuc       }
802*0a6a1f1dSLionel Sambuc     }
803*0a6a1f1dSLionel Sambuc     return false;
804*0a6a1f1dSLionel Sambuc   }
VisitStmt(const Stmt * S)805*0a6a1f1dSLionel Sambuc   bool VisitStmt(const Stmt *S) {
806*0a6a1f1dSLionel Sambuc     for (auto Child : S->children()) {
807*0a6a1f1dSLionel Sambuc       if (Child && Visit(Child))
808*0a6a1f1dSLionel Sambuc         return true;
809*0a6a1f1dSLionel Sambuc     }
810*0a6a1f1dSLionel Sambuc     return false;
811*0a6a1f1dSLionel Sambuc   }
LocalVarRefChecker(Sema & SemaRef)812*0a6a1f1dSLionel Sambuc   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
813*0a6a1f1dSLionel Sambuc };
814*0a6a1f1dSLionel Sambuc } // namespace
815*0a6a1f1dSLionel Sambuc 
816*0a6a1f1dSLionel Sambuc OMPThreadPrivateDecl *
CheckOMPThreadPrivateDecl(SourceLocation Loc,ArrayRef<Expr * > VarList)817*0a6a1f1dSLionel Sambuc Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
818f4a2713aSLionel Sambuc   SmallVector<Expr *, 8> Vars;
819*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
820*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
821f4a2713aSLionel Sambuc     VarDecl *VD = cast<VarDecl>(DE->getDecl());
822f4a2713aSLionel Sambuc     SourceLocation ILoc = DE->getExprLoc();
823f4a2713aSLionel Sambuc 
824f4a2713aSLionel Sambuc     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
825f4a2713aSLionel Sambuc     //   A threadprivate variable must not have an incomplete type.
826f4a2713aSLionel Sambuc     if (RequireCompleteType(ILoc, VD->getType(),
827f4a2713aSLionel Sambuc                             diag::err_omp_threadprivate_incomplete_type)) {
828f4a2713aSLionel Sambuc       continue;
829f4a2713aSLionel Sambuc     }
830f4a2713aSLionel Sambuc 
831f4a2713aSLionel Sambuc     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
832f4a2713aSLionel Sambuc     //   A threadprivate variable must not have a reference type.
833f4a2713aSLionel Sambuc     if (VD->getType()->isReferenceType()) {
834f4a2713aSLionel Sambuc       Diag(ILoc, diag::err_omp_ref_type_arg)
835*0a6a1f1dSLionel Sambuc           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
836*0a6a1f1dSLionel Sambuc       bool IsDecl =
837*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
838*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
839*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
840*0a6a1f1dSLionel Sambuc           << VD;
841f4a2713aSLionel Sambuc       continue;
842f4a2713aSLionel Sambuc     }
843f4a2713aSLionel Sambuc 
844f4a2713aSLionel Sambuc     // Check if this is a TLS variable.
845*0a6a1f1dSLionel Sambuc     if (VD->getTLSKind() != VarDecl::TLS_None ||
846*0a6a1f1dSLionel Sambuc         VD->getStorageClass() == SC_Register) {
847*0a6a1f1dSLionel Sambuc       Diag(ILoc, diag::err_omp_var_thread_local)
848*0a6a1f1dSLionel Sambuc           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
849*0a6a1f1dSLionel Sambuc       bool IsDecl =
850*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
851*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
852*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
853*0a6a1f1dSLionel Sambuc           << VD;
854f4a2713aSLionel Sambuc       continue;
855f4a2713aSLionel Sambuc     }
856f4a2713aSLionel Sambuc 
857*0a6a1f1dSLionel Sambuc     // Check if initial value of threadprivate variable reference variable with
858*0a6a1f1dSLionel Sambuc     // local storage (it is not supported by runtime).
859*0a6a1f1dSLionel Sambuc     if (auto Init = VD->getAnyInitializer()) {
860*0a6a1f1dSLionel Sambuc       LocalVarRefChecker Checker(*this);
861*0a6a1f1dSLionel Sambuc       if (Checker.Visit(Init))
862*0a6a1f1dSLionel Sambuc         continue;
863f4a2713aSLionel Sambuc     }
864*0a6a1f1dSLionel Sambuc 
865*0a6a1f1dSLionel Sambuc     Vars.push_back(RefExpr);
866*0a6a1f1dSLionel Sambuc     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
867*0a6a1f1dSLionel Sambuc     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
868*0a6a1f1dSLionel Sambuc         Context, SourceRange(Loc, Loc)));
869*0a6a1f1dSLionel Sambuc     if (auto *ML = Context.getASTMutationListener())
870*0a6a1f1dSLionel Sambuc       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
871*0a6a1f1dSLionel Sambuc   }
872*0a6a1f1dSLionel Sambuc   OMPThreadPrivateDecl *D = nullptr;
873*0a6a1f1dSLionel Sambuc   if (!Vars.empty()) {
874*0a6a1f1dSLionel Sambuc     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
875*0a6a1f1dSLionel Sambuc                                      Vars);
876*0a6a1f1dSLionel Sambuc     D->setAccess(AS_public);
877*0a6a1f1dSLionel Sambuc   }
878*0a6a1f1dSLionel Sambuc   return D;
879*0a6a1f1dSLionel Sambuc }
880*0a6a1f1dSLionel Sambuc 
ReportOriginalDSA(Sema & SemaRef,DSAStackTy * Stack,const VarDecl * VD,DSAStackTy::DSAVarData DVar,bool IsLoopIterVar=false)881*0a6a1f1dSLionel Sambuc static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
882*0a6a1f1dSLionel Sambuc                               const VarDecl *VD, DSAStackTy::DSAVarData DVar,
883*0a6a1f1dSLionel Sambuc                               bool IsLoopIterVar = false) {
884*0a6a1f1dSLionel Sambuc   if (DVar.RefExpr) {
885*0a6a1f1dSLionel Sambuc     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
886*0a6a1f1dSLionel Sambuc         << getOpenMPClauseName(DVar.CKind);
887*0a6a1f1dSLionel Sambuc     return;
888*0a6a1f1dSLionel Sambuc   }
889*0a6a1f1dSLionel Sambuc   enum {
890*0a6a1f1dSLionel Sambuc     PDSA_StaticMemberShared,
891*0a6a1f1dSLionel Sambuc     PDSA_StaticLocalVarShared,
892*0a6a1f1dSLionel Sambuc     PDSA_LoopIterVarPrivate,
893*0a6a1f1dSLionel Sambuc     PDSA_LoopIterVarLinear,
894*0a6a1f1dSLionel Sambuc     PDSA_LoopIterVarLastprivate,
895*0a6a1f1dSLionel Sambuc     PDSA_ConstVarShared,
896*0a6a1f1dSLionel Sambuc     PDSA_GlobalVarShared,
897*0a6a1f1dSLionel Sambuc     PDSA_TaskVarFirstprivate,
898*0a6a1f1dSLionel Sambuc     PDSA_LocalVarPrivate,
899*0a6a1f1dSLionel Sambuc     PDSA_Implicit
900*0a6a1f1dSLionel Sambuc   } Reason = PDSA_Implicit;
901*0a6a1f1dSLionel Sambuc   bool ReportHint = false;
902*0a6a1f1dSLionel Sambuc   auto ReportLoc = VD->getLocation();
903*0a6a1f1dSLionel Sambuc   if (IsLoopIterVar) {
904*0a6a1f1dSLionel Sambuc     if (DVar.CKind == OMPC_private)
905*0a6a1f1dSLionel Sambuc       Reason = PDSA_LoopIterVarPrivate;
906*0a6a1f1dSLionel Sambuc     else if (DVar.CKind == OMPC_lastprivate)
907*0a6a1f1dSLionel Sambuc       Reason = PDSA_LoopIterVarLastprivate;
908*0a6a1f1dSLionel Sambuc     else
909*0a6a1f1dSLionel Sambuc       Reason = PDSA_LoopIterVarLinear;
910*0a6a1f1dSLionel Sambuc   } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
911*0a6a1f1dSLionel Sambuc     Reason = PDSA_TaskVarFirstprivate;
912*0a6a1f1dSLionel Sambuc     ReportLoc = DVar.ImplicitDSALoc;
913*0a6a1f1dSLionel Sambuc   } else if (VD->isStaticLocal())
914*0a6a1f1dSLionel Sambuc     Reason = PDSA_StaticLocalVarShared;
915*0a6a1f1dSLionel Sambuc   else if (VD->isStaticDataMember())
916*0a6a1f1dSLionel Sambuc     Reason = PDSA_StaticMemberShared;
917*0a6a1f1dSLionel Sambuc   else if (VD->isFileVarDecl())
918*0a6a1f1dSLionel Sambuc     Reason = PDSA_GlobalVarShared;
919*0a6a1f1dSLionel Sambuc   else if (VD->getType().isConstant(SemaRef.getASTContext()))
920*0a6a1f1dSLionel Sambuc     Reason = PDSA_ConstVarShared;
921*0a6a1f1dSLionel Sambuc   else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
922*0a6a1f1dSLionel Sambuc     ReportHint = true;
923*0a6a1f1dSLionel Sambuc     Reason = PDSA_LocalVarPrivate;
924*0a6a1f1dSLionel Sambuc   }
925*0a6a1f1dSLionel Sambuc   if (Reason != PDSA_Implicit) {
926*0a6a1f1dSLionel Sambuc     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
927*0a6a1f1dSLionel Sambuc         << Reason << ReportHint
928*0a6a1f1dSLionel Sambuc         << getOpenMPDirectiveName(Stack->getCurrentDirective());
929*0a6a1f1dSLionel Sambuc   } else if (DVar.ImplicitDSALoc.isValid()) {
930*0a6a1f1dSLionel Sambuc     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
931*0a6a1f1dSLionel Sambuc         << getOpenMPClauseName(DVar.CKind);
932*0a6a1f1dSLionel Sambuc   }
933f4a2713aSLionel Sambuc }
934f4a2713aSLionel Sambuc 
935f4a2713aSLionel Sambuc namespace {
936f4a2713aSLionel Sambuc class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
937f4a2713aSLionel Sambuc   DSAStackTy *Stack;
938*0a6a1f1dSLionel Sambuc   Sema &SemaRef;
939f4a2713aSLionel Sambuc   bool ErrorFound;
940f4a2713aSLionel Sambuc   CapturedStmt *CS;
941f4a2713aSLionel Sambuc   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
942*0a6a1f1dSLionel Sambuc   llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
943*0a6a1f1dSLionel Sambuc 
944f4a2713aSLionel Sambuc public:
VisitDeclRefExpr(DeclRefExpr * E)945f4a2713aSLionel Sambuc   void VisitDeclRefExpr(DeclRefExpr *E) {
946*0a6a1f1dSLionel Sambuc     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
947f4a2713aSLionel Sambuc       // Skip internally declared variables.
948*0a6a1f1dSLionel Sambuc       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
949f4a2713aSLionel Sambuc         return;
950*0a6a1f1dSLionel Sambuc 
951*0a6a1f1dSLionel Sambuc       auto DVar = Stack->getTopDSA(VD, false);
952*0a6a1f1dSLionel Sambuc       // Check if the variable has explicit DSA set and stop analysis if it so.
953*0a6a1f1dSLionel Sambuc       if (DVar.RefExpr) return;
954*0a6a1f1dSLionel Sambuc 
955*0a6a1f1dSLionel Sambuc       auto ELoc = E->getExprLoc();
956*0a6a1f1dSLionel Sambuc       auto DKind = Stack->getCurrentDirective();
957f4a2713aSLionel Sambuc       // The default(none) clause requires that each variable that is referenced
958f4a2713aSLionel Sambuc       // in the construct, and does not have a predetermined data-sharing
959f4a2713aSLionel Sambuc       // attribute, must have its data-sharing attribute explicitly determined
960f4a2713aSLionel Sambuc       // by being listed in a data-sharing attribute clause.
961f4a2713aSLionel Sambuc       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
962*0a6a1f1dSLionel Sambuc           isParallelOrTaskRegion(DKind) &&
963*0a6a1f1dSLionel Sambuc           VarsWithInheritedDSA.count(VD) == 0) {
964*0a6a1f1dSLionel Sambuc         VarsWithInheritedDSA[VD] = E;
965f4a2713aSLionel Sambuc         return;
966f4a2713aSLionel Sambuc       }
967f4a2713aSLionel Sambuc 
968f4a2713aSLionel Sambuc       // OpenMP [2.9.3.6, Restrictions, p.2]
969f4a2713aSLionel Sambuc       //  A list item that appears in a reduction clause of the innermost
970f4a2713aSLionel Sambuc       //  enclosing worksharing or parallel construct may not be accessed in an
971f4a2713aSLionel Sambuc       //  explicit task.
972*0a6a1f1dSLionel Sambuc       DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
973*0a6a1f1dSLionel Sambuc                                     [](OpenMPDirectiveKind K) -> bool {
974*0a6a1f1dSLionel Sambuc                                       return isOpenMPParallelDirective(K) ||
975*0a6a1f1dSLionel Sambuc                                              isOpenMPWorksharingDirective(K) ||
976*0a6a1f1dSLionel Sambuc                                              isOpenMPTeamsDirective(K);
977*0a6a1f1dSLionel Sambuc                                     },
978*0a6a1f1dSLionel Sambuc                                     false);
979*0a6a1f1dSLionel Sambuc       if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
980*0a6a1f1dSLionel Sambuc         ErrorFound = true;
981*0a6a1f1dSLionel Sambuc         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
982*0a6a1f1dSLionel Sambuc         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
983*0a6a1f1dSLionel Sambuc         return;
984*0a6a1f1dSLionel Sambuc       }
985f4a2713aSLionel Sambuc 
986f4a2713aSLionel Sambuc       // Define implicit data-sharing attributes for task.
987*0a6a1f1dSLionel Sambuc       DVar = Stack->getImplicitDSA(VD, false);
988f4a2713aSLionel Sambuc       if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
989*0a6a1f1dSLionel Sambuc         ImplicitFirstprivate.push_back(E);
990f4a2713aSLionel Sambuc     }
991f4a2713aSLionel Sambuc   }
VisitOMPExecutableDirective(OMPExecutableDirective * S)992f4a2713aSLionel Sambuc   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
993*0a6a1f1dSLionel Sambuc     for (auto *C : S->clauses()) {
994*0a6a1f1dSLionel Sambuc       // Skip analysis of arguments of implicitly defined firstprivate clause
995*0a6a1f1dSLionel Sambuc       // for task directives.
996*0a6a1f1dSLionel Sambuc       if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
997*0a6a1f1dSLionel Sambuc         for (auto *CC : C->children()) {
998*0a6a1f1dSLionel Sambuc           if (CC)
999*0a6a1f1dSLionel Sambuc             Visit(CC);
1000*0a6a1f1dSLionel Sambuc         }
1001*0a6a1f1dSLionel Sambuc     }
1002f4a2713aSLionel Sambuc   }
VisitStmt(Stmt * S)1003f4a2713aSLionel Sambuc   void VisitStmt(Stmt *S) {
1004*0a6a1f1dSLionel Sambuc     for (auto *C : S->children()) {
1005*0a6a1f1dSLionel Sambuc       if (C && !isa<OMPExecutableDirective>(C))
1006*0a6a1f1dSLionel Sambuc         Visit(C);
1007*0a6a1f1dSLionel Sambuc     }
1008f4a2713aSLionel Sambuc   }
1009f4a2713aSLionel Sambuc 
isErrorFound()1010f4a2713aSLionel Sambuc   bool isErrorFound() { return ErrorFound; }
getImplicitFirstprivate()1011f4a2713aSLionel Sambuc   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
getVarsWithInheritedDSA()1012*0a6a1f1dSLionel Sambuc   llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1013*0a6a1f1dSLionel Sambuc     return VarsWithInheritedDSA;
1014*0a6a1f1dSLionel Sambuc   }
1015f4a2713aSLionel Sambuc 
DSAAttrChecker(DSAStackTy * S,Sema & SemaRef,CapturedStmt * CS)1016*0a6a1f1dSLionel Sambuc   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1017*0a6a1f1dSLionel Sambuc       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1018f4a2713aSLionel Sambuc };
1019*0a6a1f1dSLionel Sambuc } // namespace
1020*0a6a1f1dSLionel Sambuc 
ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind,Scope * CurScope)1021*0a6a1f1dSLionel Sambuc void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1022*0a6a1f1dSLionel Sambuc   switch (DKind) {
1023*0a6a1f1dSLionel Sambuc   case OMPD_parallel: {
1024*0a6a1f1dSLionel Sambuc     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1025*0a6a1f1dSLionel Sambuc     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1026*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1027*0a6a1f1dSLionel Sambuc         std::make_pair(".global_tid.", KmpInt32PtrTy),
1028*0a6a1f1dSLionel Sambuc         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1029*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1030*0a6a1f1dSLionel Sambuc     };
1031*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1032*0a6a1f1dSLionel Sambuc                              Params);
1033*0a6a1f1dSLionel Sambuc     break;
1034*0a6a1f1dSLionel Sambuc   }
1035*0a6a1f1dSLionel Sambuc   case OMPD_simd: {
1036*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1037*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1038*0a6a1f1dSLionel Sambuc     };
1039*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1040*0a6a1f1dSLionel Sambuc                              Params);
1041*0a6a1f1dSLionel Sambuc     break;
1042*0a6a1f1dSLionel Sambuc   }
1043*0a6a1f1dSLionel Sambuc   case OMPD_for: {
1044*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1045*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1046*0a6a1f1dSLionel Sambuc     };
1047*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1048*0a6a1f1dSLionel Sambuc                              Params);
1049*0a6a1f1dSLionel Sambuc     break;
1050*0a6a1f1dSLionel Sambuc   }
1051*0a6a1f1dSLionel Sambuc   case OMPD_for_simd: {
1052*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1053*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1054*0a6a1f1dSLionel Sambuc     };
1055*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1056*0a6a1f1dSLionel Sambuc                              Params);
1057*0a6a1f1dSLionel Sambuc     break;
1058*0a6a1f1dSLionel Sambuc   }
1059*0a6a1f1dSLionel Sambuc   case OMPD_sections: {
1060*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1061*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1062*0a6a1f1dSLionel Sambuc     };
1063*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1064*0a6a1f1dSLionel Sambuc                              Params);
1065*0a6a1f1dSLionel Sambuc     break;
1066*0a6a1f1dSLionel Sambuc   }
1067*0a6a1f1dSLionel Sambuc   case OMPD_section: {
1068*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1069*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1070*0a6a1f1dSLionel Sambuc     };
1071*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1072*0a6a1f1dSLionel Sambuc                              Params);
1073*0a6a1f1dSLionel Sambuc     break;
1074*0a6a1f1dSLionel Sambuc   }
1075*0a6a1f1dSLionel Sambuc   case OMPD_single: {
1076*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1077*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1078*0a6a1f1dSLionel Sambuc     };
1079*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1080*0a6a1f1dSLionel Sambuc                              Params);
1081*0a6a1f1dSLionel Sambuc     break;
1082*0a6a1f1dSLionel Sambuc   }
1083*0a6a1f1dSLionel Sambuc   case OMPD_master: {
1084*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1085*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1086*0a6a1f1dSLionel Sambuc     };
1087*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1088*0a6a1f1dSLionel Sambuc                              Params);
1089*0a6a1f1dSLionel Sambuc     break;
1090*0a6a1f1dSLionel Sambuc   }
1091*0a6a1f1dSLionel Sambuc   case OMPD_critical: {
1092*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1093*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1094*0a6a1f1dSLionel Sambuc     };
1095*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1096*0a6a1f1dSLionel Sambuc                              Params);
1097*0a6a1f1dSLionel Sambuc     break;
1098*0a6a1f1dSLionel Sambuc   }
1099*0a6a1f1dSLionel Sambuc   case OMPD_parallel_for: {
1100*0a6a1f1dSLionel Sambuc     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1101*0a6a1f1dSLionel Sambuc     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1102*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1103*0a6a1f1dSLionel Sambuc         std::make_pair(".global_tid.", KmpInt32PtrTy),
1104*0a6a1f1dSLionel Sambuc         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1105*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1106*0a6a1f1dSLionel Sambuc     };
1107*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1108*0a6a1f1dSLionel Sambuc                              Params);
1109*0a6a1f1dSLionel Sambuc     break;
1110*0a6a1f1dSLionel Sambuc   }
1111*0a6a1f1dSLionel Sambuc   case OMPD_parallel_for_simd: {
1112*0a6a1f1dSLionel Sambuc     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1113*0a6a1f1dSLionel Sambuc     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1114*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1115*0a6a1f1dSLionel Sambuc         std::make_pair(".global_tid.", KmpInt32PtrTy),
1116*0a6a1f1dSLionel Sambuc         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1117*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1118*0a6a1f1dSLionel Sambuc     };
1119*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1120*0a6a1f1dSLionel Sambuc                              Params);
1121*0a6a1f1dSLionel Sambuc     break;
1122*0a6a1f1dSLionel Sambuc   }
1123*0a6a1f1dSLionel Sambuc   case OMPD_parallel_sections: {
1124*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1125*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1126*0a6a1f1dSLionel Sambuc     };
1127*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1128*0a6a1f1dSLionel Sambuc                              Params);
1129*0a6a1f1dSLionel Sambuc     break;
1130*0a6a1f1dSLionel Sambuc   }
1131*0a6a1f1dSLionel Sambuc   case OMPD_task: {
1132*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1133*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1134*0a6a1f1dSLionel Sambuc     };
1135*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1136*0a6a1f1dSLionel Sambuc                              Params);
1137*0a6a1f1dSLionel Sambuc     break;
1138*0a6a1f1dSLionel Sambuc   }
1139*0a6a1f1dSLionel Sambuc   case OMPD_ordered: {
1140*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1141*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1142*0a6a1f1dSLionel Sambuc     };
1143*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1144*0a6a1f1dSLionel Sambuc                              Params);
1145*0a6a1f1dSLionel Sambuc     break;
1146*0a6a1f1dSLionel Sambuc   }
1147*0a6a1f1dSLionel Sambuc   case OMPD_atomic: {
1148*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1149*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1150*0a6a1f1dSLionel Sambuc     };
1151*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1152*0a6a1f1dSLionel Sambuc                              Params);
1153*0a6a1f1dSLionel Sambuc     break;
1154*0a6a1f1dSLionel Sambuc   }
1155*0a6a1f1dSLionel Sambuc   case OMPD_target: {
1156*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1157*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1158*0a6a1f1dSLionel Sambuc     };
1159*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1160*0a6a1f1dSLionel Sambuc                              Params);
1161*0a6a1f1dSLionel Sambuc     break;
1162*0a6a1f1dSLionel Sambuc   }
1163*0a6a1f1dSLionel Sambuc   case OMPD_teams: {
1164*0a6a1f1dSLionel Sambuc     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1165*0a6a1f1dSLionel Sambuc     QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1166*0a6a1f1dSLionel Sambuc     Sema::CapturedParamNameType Params[] = {
1167*0a6a1f1dSLionel Sambuc         std::make_pair(".global_tid.", KmpInt32PtrTy),
1168*0a6a1f1dSLionel Sambuc         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1169*0a6a1f1dSLionel Sambuc         std::make_pair(StringRef(), QualType()) // __context with shared vars
1170*0a6a1f1dSLionel Sambuc     };
1171*0a6a1f1dSLionel Sambuc     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1172*0a6a1f1dSLionel Sambuc                              Params);
1173*0a6a1f1dSLionel Sambuc     break;
1174*0a6a1f1dSLionel Sambuc   }
1175*0a6a1f1dSLionel Sambuc   case OMPD_threadprivate:
1176*0a6a1f1dSLionel Sambuc   case OMPD_taskyield:
1177*0a6a1f1dSLionel Sambuc   case OMPD_barrier:
1178*0a6a1f1dSLionel Sambuc   case OMPD_taskwait:
1179*0a6a1f1dSLionel Sambuc   case OMPD_flush:
1180*0a6a1f1dSLionel Sambuc     llvm_unreachable("OpenMP Directive is not allowed");
1181*0a6a1f1dSLionel Sambuc   case OMPD_unknown:
1182*0a6a1f1dSLionel Sambuc     llvm_unreachable("Unknown OpenMP directive");
1183*0a6a1f1dSLionel Sambuc   }
1184*0a6a1f1dSLionel Sambuc }
1185*0a6a1f1dSLionel Sambuc 
CheckNestingOfRegions(Sema & SemaRef,DSAStackTy * Stack,OpenMPDirectiveKind CurrentRegion,const DeclarationNameInfo & CurrentName,SourceLocation StartLoc)1186*0a6a1f1dSLionel Sambuc static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1187*0a6a1f1dSLionel Sambuc                                   OpenMPDirectiveKind CurrentRegion,
1188*0a6a1f1dSLionel Sambuc                                   const DeclarationNameInfo &CurrentName,
1189*0a6a1f1dSLionel Sambuc                                   SourceLocation StartLoc) {
1190*0a6a1f1dSLionel Sambuc   // Allowed nesting of constructs
1191*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1192*0a6a1f1dSLionel Sambuc   // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1193*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1194*0a6a1f1dSLionel Sambuc   // | parallel         | parallel        | *                                  |
1195*0a6a1f1dSLionel Sambuc   // | parallel         | for             | *                                  |
1196*0a6a1f1dSLionel Sambuc   // | parallel         | for simd        | *                                  |
1197*0a6a1f1dSLionel Sambuc   // | parallel         | master          | *                                  |
1198*0a6a1f1dSLionel Sambuc   // | parallel         | critical        | *                                  |
1199*0a6a1f1dSLionel Sambuc   // | parallel         | simd            | *                                  |
1200*0a6a1f1dSLionel Sambuc   // | parallel         | sections        | *                                  |
1201*0a6a1f1dSLionel Sambuc   // | parallel         | section         | +                                  |
1202*0a6a1f1dSLionel Sambuc   // | parallel         | single          | *                                  |
1203*0a6a1f1dSLionel Sambuc   // | parallel         | parallel for    | *                                  |
1204*0a6a1f1dSLionel Sambuc   // | parallel         |parallel for simd| *                                  |
1205*0a6a1f1dSLionel Sambuc   // | parallel         |parallel sections| *                                  |
1206*0a6a1f1dSLionel Sambuc   // | parallel         | task            | *                                  |
1207*0a6a1f1dSLionel Sambuc   // | parallel         | taskyield       | *                                  |
1208*0a6a1f1dSLionel Sambuc   // | parallel         | barrier         | *                                  |
1209*0a6a1f1dSLionel Sambuc   // | parallel         | taskwait        | *                                  |
1210*0a6a1f1dSLionel Sambuc   // | parallel         | flush           | *                                  |
1211*0a6a1f1dSLionel Sambuc   // | parallel         | ordered         | +                                  |
1212*0a6a1f1dSLionel Sambuc   // | parallel         | atomic          | *                                  |
1213*0a6a1f1dSLionel Sambuc   // | parallel         | target          | *                                  |
1214*0a6a1f1dSLionel Sambuc   // | parallel         | teams           | +                                  |
1215*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1216*0a6a1f1dSLionel Sambuc   // | for              | parallel        | *                                  |
1217*0a6a1f1dSLionel Sambuc   // | for              | for             | +                                  |
1218*0a6a1f1dSLionel Sambuc   // | for              | for simd        | +                                  |
1219*0a6a1f1dSLionel Sambuc   // | for              | master          | +                                  |
1220*0a6a1f1dSLionel Sambuc   // | for              | critical        | *                                  |
1221*0a6a1f1dSLionel Sambuc   // | for              | simd            | *                                  |
1222*0a6a1f1dSLionel Sambuc   // | for              | sections        | +                                  |
1223*0a6a1f1dSLionel Sambuc   // | for              | section         | +                                  |
1224*0a6a1f1dSLionel Sambuc   // | for              | single          | +                                  |
1225*0a6a1f1dSLionel Sambuc   // | for              | parallel for    | *                                  |
1226*0a6a1f1dSLionel Sambuc   // | for              |parallel for simd| *                                  |
1227*0a6a1f1dSLionel Sambuc   // | for              |parallel sections| *                                  |
1228*0a6a1f1dSLionel Sambuc   // | for              | task            | *                                  |
1229*0a6a1f1dSLionel Sambuc   // | for              | taskyield       | *                                  |
1230*0a6a1f1dSLionel Sambuc   // | for              | barrier         | +                                  |
1231*0a6a1f1dSLionel Sambuc   // | for              | taskwait        | *                                  |
1232*0a6a1f1dSLionel Sambuc   // | for              | flush           | *                                  |
1233*0a6a1f1dSLionel Sambuc   // | for              | ordered         | * (if construct is ordered)        |
1234*0a6a1f1dSLionel Sambuc   // | for              | atomic          | *                                  |
1235*0a6a1f1dSLionel Sambuc   // | for              | target          | *                                  |
1236*0a6a1f1dSLionel Sambuc   // | for              | teams           | +                                  |
1237*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1238*0a6a1f1dSLionel Sambuc   // | master           | parallel        | *                                  |
1239*0a6a1f1dSLionel Sambuc   // | master           | for             | +                                  |
1240*0a6a1f1dSLionel Sambuc   // | master           | for simd        | +                                  |
1241*0a6a1f1dSLionel Sambuc   // | master           | master          | *                                  |
1242*0a6a1f1dSLionel Sambuc   // | master           | critical        | *                                  |
1243*0a6a1f1dSLionel Sambuc   // | master           | simd            | *                                  |
1244*0a6a1f1dSLionel Sambuc   // | master           | sections        | +                                  |
1245*0a6a1f1dSLionel Sambuc   // | master           | section         | +                                  |
1246*0a6a1f1dSLionel Sambuc   // | master           | single          | +                                  |
1247*0a6a1f1dSLionel Sambuc   // | master           | parallel for    | *                                  |
1248*0a6a1f1dSLionel Sambuc   // | master           |parallel for simd| *                                  |
1249*0a6a1f1dSLionel Sambuc   // | master           |parallel sections| *                                  |
1250*0a6a1f1dSLionel Sambuc   // | master           | task            | *                                  |
1251*0a6a1f1dSLionel Sambuc   // | master           | taskyield       | *                                  |
1252*0a6a1f1dSLionel Sambuc   // | master           | barrier         | +                                  |
1253*0a6a1f1dSLionel Sambuc   // | master           | taskwait        | *                                  |
1254*0a6a1f1dSLionel Sambuc   // | master           | flush           | *                                  |
1255*0a6a1f1dSLionel Sambuc   // | master           | ordered         | +                                  |
1256*0a6a1f1dSLionel Sambuc   // | master           | atomic          | *                                  |
1257*0a6a1f1dSLionel Sambuc   // | master           | target          | *                                  |
1258*0a6a1f1dSLionel Sambuc   // | master           | teams           | +                                  |
1259*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1260*0a6a1f1dSLionel Sambuc   // | critical         | parallel        | *                                  |
1261*0a6a1f1dSLionel Sambuc   // | critical         | for             | +                                  |
1262*0a6a1f1dSLionel Sambuc   // | critical         | for simd        | +                                  |
1263*0a6a1f1dSLionel Sambuc   // | critical         | master          | *                                  |
1264*0a6a1f1dSLionel Sambuc   // | critical         | critical        | * (should have different names)    |
1265*0a6a1f1dSLionel Sambuc   // | critical         | simd            | *                                  |
1266*0a6a1f1dSLionel Sambuc   // | critical         | sections        | +                                  |
1267*0a6a1f1dSLionel Sambuc   // | critical         | section         | +                                  |
1268*0a6a1f1dSLionel Sambuc   // | critical         | single          | +                                  |
1269*0a6a1f1dSLionel Sambuc   // | critical         | parallel for    | *                                  |
1270*0a6a1f1dSLionel Sambuc   // | critical         |parallel for simd| *                                  |
1271*0a6a1f1dSLionel Sambuc   // | critical         |parallel sections| *                                  |
1272*0a6a1f1dSLionel Sambuc   // | critical         | task            | *                                  |
1273*0a6a1f1dSLionel Sambuc   // | critical         | taskyield       | *                                  |
1274*0a6a1f1dSLionel Sambuc   // | critical         | barrier         | +                                  |
1275*0a6a1f1dSLionel Sambuc   // | critical         | taskwait        | *                                  |
1276*0a6a1f1dSLionel Sambuc   // | critical         | ordered         | +                                  |
1277*0a6a1f1dSLionel Sambuc   // | critical         | atomic          | *                                  |
1278*0a6a1f1dSLionel Sambuc   // | critical         | target          | *                                  |
1279*0a6a1f1dSLionel Sambuc   // | critical         | teams           | +                                  |
1280*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1281*0a6a1f1dSLionel Sambuc   // | simd             | parallel        |                                    |
1282*0a6a1f1dSLionel Sambuc   // | simd             | for             |                                    |
1283*0a6a1f1dSLionel Sambuc   // | simd             | for simd        |                                    |
1284*0a6a1f1dSLionel Sambuc   // | simd             | master          |                                    |
1285*0a6a1f1dSLionel Sambuc   // | simd             | critical        |                                    |
1286*0a6a1f1dSLionel Sambuc   // | simd             | simd            |                                    |
1287*0a6a1f1dSLionel Sambuc   // | simd             | sections        |                                    |
1288*0a6a1f1dSLionel Sambuc   // | simd             | section         |                                    |
1289*0a6a1f1dSLionel Sambuc   // | simd             | single          |                                    |
1290*0a6a1f1dSLionel Sambuc   // | simd             | parallel for    |                                    |
1291*0a6a1f1dSLionel Sambuc   // | simd             |parallel for simd|                                    |
1292*0a6a1f1dSLionel Sambuc   // | simd             |parallel sections|                                    |
1293*0a6a1f1dSLionel Sambuc   // | simd             | task            |                                    |
1294*0a6a1f1dSLionel Sambuc   // | simd             | taskyield       |                                    |
1295*0a6a1f1dSLionel Sambuc   // | simd             | barrier         |                                    |
1296*0a6a1f1dSLionel Sambuc   // | simd             | taskwait        |                                    |
1297*0a6a1f1dSLionel Sambuc   // | simd             | flush           |                                    |
1298*0a6a1f1dSLionel Sambuc   // | simd             | ordered         |                                    |
1299*0a6a1f1dSLionel Sambuc   // | simd             | atomic          |                                    |
1300*0a6a1f1dSLionel Sambuc   // | simd             | target          |                                    |
1301*0a6a1f1dSLionel Sambuc   // | simd             | teams           |                                    |
1302*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1303*0a6a1f1dSLionel Sambuc   // | for simd         | parallel        |                                    |
1304*0a6a1f1dSLionel Sambuc   // | for simd         | for             |                                    |
1305*0a6a1f1dSLionel Sambuc   // | for simd         | for simd        |                                    |
1306*0a6a1f1dSLionel Sambuc   // | for simd         | master          |                                    |
1307*0a6a1f1dSLionel Sambuc   // | for simd         | critical        |                                    |
1308*0a6a1f1dSLionel Sambuc   // | for simd         | simd            |                                    |
1309*0a6a1f1dSLionel Sambuc   // | for simd         | sections        |                                    |
1310*0a6a1f1dSLionel Sambuc   // | for simd         | section         |                                    |
1311*0a6a1f1dSLionel Sambuc   // | for simd         | single          |                                    |
1312*0a6a1f1dSLionel Sambuc   // | for simd         | parallel for    |                                    |
1313*0a6a1f1dSLionel Sambuc   // | for simd         |parallel for simd|                                    |
1314*0a6a1f1dSLionel Sambuc   // | for simd         |parallel sections|                                    |
1315*0a6a1f1dSLionel Sambuc   // | for simd         | task            |                                    |
1316*0a6a1f1dSLionel Sambuc   // | for simd         | taskyield       |                                    |
1317*0a6a1f1dSLionel Sambuc   // | for simd         | barrier         |                                    |
1318*0a6a1f1dSLionel Sambuc   // | for simd         | taskwait        |                                    |
1319*0a6a1f1dSLionel Sambuc   // | for simd         | flush           |                                    |
1320*0a6a1f1dSLionel Sambuc   // | for simd         | ordered         |                                    |
1321*0a6a1f1dSLionel Sambuc   // | for simd         | atomic          |                                    |
1322*0a6a1f1dSLionel Sambuc   // | for simd         | target          |                                    |
1323*0a6a1f1dSLionel Sambuc   // | for simd         | teams           |                                    |
1324*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1325*0a6a1f1dSLionel Sambuc   // | parallel for simd| parallel        |                                    |
1326*0a6a1f1dSLionel Sambuc   // | parallel for simd| for             |                                    |
1327*0a6a1f1dSLionel Sambuc   // | parallel for simd| for simd        |                                    |
1328*0a6a1f1dSLionel Sambuc   // | parallel for simd| master          |                                    |
1329*0a6a1f1dSLionel Sambuc   // | parallel for simd| critical        |                                    |
1330*0a6a1f1dSLionel Sambuc   // | parallel for simd| simd            |                                    |
1331*0a6a1f1dSLionel Sambuc   // | parallel for simd| sections        |                                    |
1332*0a6a1f1dSLionel Sambuc   // | parallel for simd| section         |                                    |
1333*0a6a1f1dSLionel Sambuc   // | parallel for simd| single          |                                    |
1334*0a6a1f1dSLionel Sambuc   // | parallel for simd| parallel for    |                                    |
1335*0a6a1f1dSLionel Sambuc   // | parallel for simd|parallel for simd|                                    |
1336*0a6a1f1dSLionel Sambuc   // | parallel for simd|parallel sections|                                    |
1337*0a6a1f1dSLionel Sambuc   // | parallel for simd| task            |                                    |
1338*0a6a1f1dSLionel Sambuc   // | parallel for simd| taskyield       |                                    |
1339*0a6a1f1dSLionel Sambuc   // | parallel for simd| barrier         |                                    |
1340*0a6a1f1dSLionel Sambuc   // | parallel for simd| taskwait        |                                    |
1341*0a6a1f1dSLionel Sambuc   // | parallel for simd| flush           |                                    |
1342*0a6a1f1dSLionel Sambuc   // | parallel for simd| ordered         |                                    |
1343*0a6a1f1dSLionel Sambuc   // | parallel for simd| atomic          |                                    |
1344*0a6a1f1dSLionel Sambuc   // | parallel for simd| target          |                                    |
1345*0a6a1f1dSLionel Sambuc   // | parallel for simd| teams           |                                    |
1346*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1347*0a6a1f1dSLionel Sambuc   // | sections         | parallel        | *                                  |
1348*0a6a1f1dSLionel Sambuc   // | sections         | for             | +                                  |
1349*0a6a1f1dSLionel Sambuc   // | sections         | for simd        | +                                  |
1350*0a6a1f1dSLionel Sambuc   // | sections         | master          | +                                  |
1351*0a6a1f1dSLionel Sambuc   // | sections         | critical        | *                                  |
1352*0a6a1f1dSLionel Sambuc   // | sections         | simd            | *                                  |
1353*0a6a1f1dSLionel Sambuc   // | sections         | sections        | +                                  |
1354*0a6a1f1dSLionel Sambuc   // | sections         | section         | *                                  |
1355*0a6a1f1dSLionel Sambuc   // | sections         | single          | +                                  |
1356*0a6a1f1dSLionel Sambuc   // | sections         | parallel for    | *                                  |
1357*0a6a1f1dSLionel Sambuc   // | sections         |parallel for simd| *                                  |
1358*0a6a1f1dSLionel Sambuc   // | sections         |parallel sections| *                                  |
1359*0a6a1f1dSLionel Sambuc   // | sections         | task            | *                                  |
1360*0a6a1f1dSLionel Sambuc   // | sections         | taskyield       | *                                  |
1361*0a6a1f1dSLionel Sambuc   // | sections         | barrier         | +                                  |
1362*0a6a1f1dSLionel Sambuc   // | sections         | taskwait        | *                                  |
1363*0a6a1f1dSLionel Sambuc   // | sections         | flush           | *                                  |
1364*0a6a1f1dSLionel Sambuc   // | sections         | ordered         | +                                  |
1365*0a6a1f1dSLionel Sambuc   // | sections         | atomic          | *                                  |
1366*0a6a1f1dSLionel Sambuc   // | sections         | target          | *                                  |
1367*0a6a1f1dSLionel Sambuc   // | sections         | teams           | +                                  |
1368*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1369*0a6a1f1dSLionel Sambuc   // | section          | parallel        | *                                  |
1370*0a6a1f1dSLionel Sambuc   // | section          | for             | +                                  |
1371*0a6a1f1dSLionel Sambuc   // | section          | for simd        | +                                  |
1372*0a6a1f1dSLionel Sambuc   // | section          | master          | +                                  |
1373*0a6a1f1dSLionel Sambuc   // | section          | critical        | *                                  |
1374*0a6a1f1dSLionel Sambuc   // | section          | simd            | *                                  |
1375*0a6a1f1dSLionel Sambuc   // | section          | sections        | +                                  |
1376*0a6a1f1dSLionel Sambuc   // | section          | section         | +                                  |
1377*0a6a1f1dSLionel Sambuc   // | section          | single          | +                                  |
1378*0a6a1f1dSLionel Sambuc   // | section          | parallel for    | *                                  |
1379*0a6a1f1dSLionel Sambuc   // | section          |parallel for simd| *                                  |
1380*0a6a1f1dSLionel Sambuc   // | section          |parallel sections| *                                  |
1381*0a6a1f1dSLionel Sambuc   // | section          | task            | *                                  |
1382*0a6a1f1dSLionel Sambuc   // | section          | taskyield       | *                                  |
1383*0a6a1f1dSLionel Sambuc   // | section          | barrier         | +                                  |
1384*0a6a1f1dSLionel Sambuc   // | section          | taskwait        | *                                  |
1385*0a6a1f1dSLionel Sambuc   // | section          | flush           | *                                  |
1386*0a6a1f1dSLionel Sambuc   // | section          | ordered         | +                                  |
1387*0a6a1f1dSLionel Sambuc   // | section          | atomic          | *                                  |
1388*0a6a1f1dSLionel Sambuc   // | section          | target          | *                                  |
1389*0a6a1f1dSLionel Sambuc   // | section          | teams           | +                                  |
1390*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1391*0a6a1f1dSLionel Sambuc   // | single           | parallel        | *                                  |
1392*0a6a1f1dSLionel Sambuc   // | single           | for             | +                                  |
1393*0a6a1f1dSLionel Sambuc   // | single           | for simd        | +                                  |
1394*0a6a1f1dSLionel Sambuc   // | single           | master          | +                                  |
1395*0a6a1f1dSLionel Sambuc   // | single           | critical        | *                                  |
1396*0a6a1f1dSLionel Sambuc   // | single           | simd            | *                                  |
1397*0a6a1f1dSLionel Sambuc   // | single           | sections        | +                                  |
1398*0a6a1f1dSLionel Sambuc   // | single           | section         | +                                  |
1399*0a6a1f1dSLionel Sambuc   // | single           | single          | +                                  |
1400*0a6a1f1dSLionel Sambuc   // | single           | parallel for    | *                                  |
1401*0a6a1f1dSLionel Sambuc   // | single           |parallel for simd| *                                  |
1402*0a6a1f1dSLionel Sambuc   // | single           |parallel sections| *                                  |
1403*0a6a1f1dSLionel Sambuc   // | single           | task            | *                                  |
1404*0a6a1f1dSLionel Sambuc   // | single           | taskyield       | *                                  |
1405*0a6a1f1dSLionel Sambuc   // | single           | barrier         | +                                  |
1406*0a6a1f1dSLionel Sambuc   // | single           | taskwait        | *                                  |
1407*0a6a1f1dSLionel Sambuc   // | single           | flush           | *                                  |
1408*0a6a1f1dSLionel Sambuc   // | single           | ordered         | +                                  |
1409*0a6a1f1dSLionel Sambuc   // | single           | atomic          | *                                  |
1410*0a6a1f1dSLionel Sambuc   // | single           | target          | *                                  |
1411*0a6a1f1dSLionel Sambuc   // | single           | teams           | +                                  |
1412*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1413*0a6a1f1dSLionel Sambuc   // | parallel for     | parallel        | *                                  |
1414*0a6a1f1dSLionel Sambuc   // | parallel for     | for             | +                                  |
1415*0a6a1f1dSLionel Sambuc   // | parallel for     | for simd        | +                                  |
1416*0a6a1f1dSLionel Sambuc   // | parallel for     | master          | +                                  |
1417*0a6a1f1dSLionel Sambuc   // | parallel for     | critical        | *                                  |
1418*0a6a1f1dSLionel Sambuc   // | parallel for     | simd            | *                                  |
1419*0a6a1f1dSLionel Sambuc   // | parallel for     | sections        | +                                  |
1420*0a6a1f1dSLionel Sambuc   // | parallel for     | section         | +                                  |
1421*0a6a1f1dSLionel Sambuc   // | parallel for     | single          | +                                  |
1422*0a6a1f1dSLionel Sambuc   // | parallel for     | parallel for    | *                                  |
1423*0a6a1f1dSLionel Sambuc   // | parallel for     |parallel for simd| *                                  |
1424*0a6a1f1dSLionel Sambuc   // | parallel for     |parallel sections| *                                  |
1425*0a6a1f1dSLionel Sambuc   // | parallel for     | task            | *                                  |
1426*0a6a1f1dSLionel Sambuc   // | parallel for     | taskyield       | *                                  |
1427*0a6a1f1dSLionel Sambuc   // | parallel for     | barrier         | +                                  |
1428*0a6a1f1dSLionel Sambuc   // | parallel for     | taskwait        | *                                  |
1429*0a6a1f1dSLionel Sambuc   // | parallel for     | flush           | *                                  |
1430*0a6a1f1dSLionel Sambuc   // | parallel for     | ordered         | * (if construct is ordered)        |
1431*0a6a1f1dSLionel Sambuc   // | parallel for     | atomic          | *                                  |
1432*0a6a1f1dSLionel Sambuc   // | parallel for     | target          | *                                  |
1433*0a6a1f1dSLionel Sambuc   // | parallel for     | teams           | +                                  |
1434*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1435*0a6a1f1dSLionel Sambuc   // | parallel sections| parallel        | *                                  |
1436*0a6a1f1dSLionel Sambuc   // | parallel sections| for             | +                                  |
1437*0a6a1f1dSLionel Sambuc   // | parallel sections| for simd        | +                                  |
1438*0a6a1f1dSLionel Sambuc   // | parallel sections| master          | +                                  |
1439*0a6a1f1dSLionel Sambuc   // | parallel sections| critical        | +                                  |
1440*0a6a1f1dSLionel Sambuc   // | parallel sections| simd            | *                                  |
1441*0a6a1f1dSLionel Sambuc   // | parallel sections| sections        | +                                  |
1442*0a6a1f1dSLionel Sambuc   // | parallel sections| section         | *                                  |
1443*0a6a1f1dSLionel Sambuc   // | parallel sections| single          | +                                  |
1444*0a6a1f1dSLionel Sambuc   // | parallel sections| parallel for    | *                                  |
1445*0a6a1f1dSLionel Sambuc   // | parallel sections|parallel for simd| *                                  |
1446*0a6a1f1dSLionel Sambuc   // | parallel sections|parallel sections| *                                  |
1447*0a6a1f1dSLionel Sambuc   // | parallel sections| task            | *                                  |
1448*0a6a1f1dSLionel Sambuc   // | parallel sections| taskyield       | *                                  |
1449*0a6a1f1dSLionel Sambuc   // | parallel sections| barrier         | +                                  |
1450*0a6a1f1dSLionel Sambuc   // | parallel sections| taskwait        | *                                  |
1451*0a6a1f1dSLionel Sambuc   // | parallel sections| flush           | *                                  |
1452*0a6a1f1dSLionel Sambuc   // | parallel sections| ordered         | +                                  |
1453*0a6a1f1dSLionel Sambuc   // | parallel sections| atomic          | *                                  |
1454*0a6a1f1dSLionel Sambuc   // | parallel sections| target          | *                                  |
1455*0a6a1f1dSLionel Sambuc   // | parallel sections| teams           | +                                  |
1456*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1457*0a6a1f1dSLionel Sambuc   // | task             | parallel        | *                                  |
1458*0a6a1f1dSLionel Sambuc   // | task             | for             | +                                  |
1459*0a6a1f1dSLionel Sambuc   // | task             | for simd        | +                                  |
1460*0a6a1f1dSLionel Sambuc   // | task             | master          | +                                  |
1461*0a6a1f1dSLionel Sambuc   // | task             | critical        | *                                  |
1462*0a6a1f1dSLionel Sambuc   // | task             | simd            | *                                  |
1463*0a6a1f1dSLionel Sambuc   // | task             | sections        | +                                  |
1464*0a6a1f1dSLionel Sambuc   // | task             | section         | +                                  |
1465*0a6a1f1dSLionel Sambuc   // | task             | single          | +                                  |
1466*0a6a1f1dSLionel Sambuc   // | task             | parallel for    | *                                  |
1467*0a6a1f1dSLionel Sambuc   // | task             |parallel for simd| *                                  |
1468*0a6a1f1dSLionel Sambuc   // | task             |parallel sections| *                                  |
1469*0a6a1f1dSLionel Sambuc   // | task             | task            | *                                  |
1470*0a6a1f1dSLionel Sambuc   // | task             | taskyield       | *                                  |
1471*0a6a1f1dSLionel Sambuc   // | task             | barrier         | +                                  |
1472*0a6a1f1dSLionel Sambuc   // | task             | taskwait        | *                                  |
1473*0a6a1f1dSLionel Sambuc   // | task             | flush           | *                                  |
1474*0a6a1f1dSLionel Sambuc   // | task             | ordered         | +                                  |
1475*0a6a1f1dSLionel Sambuc   // | task             | atomic          | *                                  |
1476*0a6a1f1dSLionel Sambuc   // | task             | target          | *                                  |
1477*0a6a1f1dSLionel Sambuc   // | task             | teams           | +                                  |
1478*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1479*0a6a1f1dSLionel Sambuc   // | ordered          | parallel        | *                                  |
1480*0a6a1f1dSLionel Sambuc   // | ordered          | for             | +                                  |
1481*0a6a1f1dSLionel Sambuc   // | ordered          | for simd        | +                                  |
1482*0a6a1f1dSLionel Sambuc   // | ordered          | master          | *                                  |
1483*0a6a1f1dSLionel Sambuc   // | ordered          | critical        | *                                  |
1484*0a6a1f1dSLionel Sambuc   // | ordered          | simd            | *                                  |
1485*0a6a1f1dSLionel Sambuc   // | ordered          | sections        | +                                  |
1486*0a6a1f1dSLionel Sambuc   // | ordered          | section         | +                                  |
1487*0a6a1f1dSLionel Sambuc   // | ordered          | single          | +                                  |
1488*0a6a1f1dSLionel Sambuc   // | ordered          | parallel for    | *                                  |
1489*0a6a1f1dSLionel Sambuc   // | ordered          |parallel for simd| *                                  |
1490*0a6a1f1dSLionel Sambuc   // | ordered          |parallel sections| *                                  |
1491*0a6a1f1dSLionel Sambuc   // | ordered          | task            | *                                  |
1492*0a6a1f1dSLionel Sambuc   // | ordered          | taskyield       | *                                  |
1493*0a6a1f1dSLionel Sambuc   // | ordered          | barrier         | +                                  |
1494*0a6a1f1dSLionel Sambuc   // | ordered          | taskwait        | *                                  |
1495*0a6a1f1dSLionel Sambuc   // | ordered          | flush           | *                                  |
1496*0a6a1f1dSLionel Sambuc   // | ordered          | ordered         | +                                  |
1497*0a6a1f1dSLionel Sambuc   // | ordered          | atomic          | *                                  |
1498*0a6a1f1dSLionel Sambuc   // | ordered          | target          | *                                  |
1499*0a6a1f1dSLionel Sambuc   // | ordered          | teams           | +                                  |
1500*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1501*0a6a1f1dSLionel Sambuc   // | atomic           | parallel        |                                    |
1502*0a6a1f1dSLionel Sambuc   // | atomic           | for             |                                    |
1503*0a6a1f1dSLionel Sambuc   // | atomic           | for simd        |                                    |
1504*0a6a1f1dSLionel Sambuc   // | atomic           | master          |                                    |
1505*0a6a1f1dSLionel Sambuc   // | atomic           | critical        |                                    |
1506*0a6a1f1dSLionel Sambuc   // | atomic           | simd            |                                    |
1507*0a6a1f1dSLionel Sambuc   // | atomic           | sections        |                                    |
1508*0a6a1f1dSLionel Sambuc   // | atomic           | section         |                                    |
1509*0a6a1f1dSLionel Sambuc   // | atomic           | single          |                                    |
1510*0a6a1f1dSLionel Sambuc   // | atomic           | parallel for    |                                    |
1511*0a6a1f1dSLionel Sambuc   // | atomic           |parallel for simd|                                    |
1512*0a6a1f1dSLionel Sambuc   // | atomic           |parallel sections|                                    |
1513*0a6a1f1dSLionel Sambuc   // | atomic           | task            |                                    |
1514*0a6a1f1dSLionel Sambuc   // | atomic           | taskyield       |                                    |
1515*0a6a1f1dSLionel Sambuc   // | atomic           | barrier         |                                    |
1516*0a6a1f1dSLionel Sambuc   // | atomic           | taskwait        |                                    |
1517*0a6a1f1dSLionel Sambuc   // | atomic           | flush           |                                    |
1518*0a6a1f1dSLionel Sambuc   // | atomic           | ordered         |                                    |
1519*0a6a1f1dSLionel Sambuc   // | atomic           | atomic          |                                    |
1520*0a6a1f1dSLionel Sambuc   // | atomic           | target          |                                    |
1521*0a6a1f1dSLionel Sambuc   // | atomic           | teams           |                                    |
1522*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1523*0a6a1f1dSLionel Sambuc   // | target           | parallel        | *                                  |
1524*0a6a1f1dSLionel Sambuc   // | target           | for             | *                                  |
1525*0a6a1f1dSLionel Sambuc   // | target           | for simd        | *                                  |
1526*0a6a1f1dSLionel Sambuc   // | target           | master          | *                                  |
1527*0a6a1f1dSLionel Sambuc   // | target           | critical        | *                                  |
1528*0a6a1f1dSLionel Sambuc   // | target           | simd            | *                                  |
1529*0a6a1f1dSLionel Sambuc   // | target           | sections        | *                                  |
1530*0a6a1f1dSLionel Sambuc   // | target           | section         | *                                  |
1531*0a6a1f1dSLionel Sambuc   // | target           | single          | *                                  |
1532*0a6a1f1dSLionel Sambuc   // | target           | parallel for    | *                                  |
1533*0a6a1f1dSLionel Sambuc   // | target           |parallel for simd| *                                  |
1534*0a6a1f1dSLionel Sambuc   // | target           |parallel sections| *                                  |
1535*0a6a1f1dSLionel Sambuc   // | target           | task            | *                                  |
1536*0a6a1f1dSLionel Sambuc   // | target           | taskyield       | *                                  |
1537*0a6a1f1dSLionel Sambuc   // | target           | barrier         | *                                  |
1538*0a6a1f1dSLionel Sambuc   // | target           | taskwait        | *                                  |
1539*0a6a1f1dSLionel Sambuc   // | target           | flush           | *                                  |
1540*0a6a1f1dSLionel Sambuc   // | target           | ordered         | *                                  |
1541*0a6a1f1dSLionel Sambuc   // | target           | atomic          | *                                  |
1542*0a6a1f1dSLionel Sambuc   // | target           | target          | *                                  |
1543*0a6a1f1dSLionel Sambuc   // | target           | teams           | *                                  |
1544*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1545*0a6a1f1dSLionel Sambuc   // | teams            | parallel        | *                                  |
1546*0a6a1f1dSLionel Sambuc   // | teams            | for             | +                                  |
1547*0a6a1f1dSLionel Sambuc   // | teams            | for simd        | +                                  |
1548*0a6a1f1dSLionel Sambuc   // | teams            | master          | +                                  |
1549*0a6a1f1dSLionel Sambuc   // | teams            | critical        | +                                  |
1550*0a6a1f1dSLionel Sambuc   // | teams            | simd            | +                                  |
1551*0a6a1f1dSLionel Sambuc   // | teams            | sections        | +                                  |
1552*0a6a1f1dSLionel Sambuc   // | teams            | section         | +                                  |
1553*0a6a1f1dSLionel Sambuc   // | teams            | single          | +                                  |
1554*0a6a1f1dSLionel Sambuc   // | teams            | parallel for    | *                                  |
1555*0a6a1f1dSLionel Sambuc   // | teams            |parallel for simd| *                                  |
1556*0a6a1f1dSLionel Sambuc   // | teams            |parallel sections| *                                  |
1557*0a6a1f1dSLionel Sambuc   // | teams            | task            | +                                  |
1558*0a6a1f1dSLionel Sambuc   // | teams            | taskyield       | +                                  |
1559*0a6a1f1dSLionel Sambuc   // | teams            | barrier         | +                                  |
1560*0a6a1f1dSLionel Sambuc   // | teams            | taskwait        | +                                  |
1561*0a6a1f1dSLionel Sambuc   // | teams            | flush           | +                                  |
1562*0a6a1f1dSLionel Sambuc   // | teams            | ordered         | +                                  |
1563*0a6a1f1dSLionel Sambuc   // | teams            | atomic          | +                                  |
1564*0a6a1f1dSLionel Sambuc   // | teams            | target          | +                                  |
1565*0a6a1f1dSLionel Sambuc   // | teams            | teams           | +                                  |
1566*0a6a1f1dSLionel Sambuc   // +------------------+-----------------+------------------------------------+
1567*0a6a1f1dSLionel Sambuc   if (Stack->getCurScope()) {
1568*0a6a1f1dSLionel Sambuc     auto ParentRegion = Stack->getParentDirective();
1569*0a6a1f1dSLionel Sambuc     bool NestingProhibited = false;
1570*0a6a1f1dSLionel Sambuc     bool CloseNesting = true;
1571*0a6a1f1dSLionel Sambuc     enum {
1572*0a6a1f1dSLionel Sambuc       NoRecommend,
1573*0a6a1f1dSLionel Sambuc       ShouldBeInParallelRegion,
1574*0a6a1f1dSLionel Sambuc       ShouldBeInOrderedRegion,
1575*0a6a1f1dSLionel Sambuc       ShouldBeInTargetRegion
1576*0a6a1f1dSLionel Sambuc     } Recommend = NoRecommend;
1577*0a6a1f1dSLionel Sambuc     if (isOpenMPSimdDirective(ParentRegion)) {
1578*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1579*0a6a1f1dSLionel Sambuc       // OpenMP constructs may not be nested inside a simd region.
1580*0a6a1f1dSLionel Sambuc       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1581*0a6a1f1dSLionel Sambuc       return true;
1582*0a6a1f1dSLionel Sambuc     }
1583*0a6a1f1dSLionel Sambuc     if (ParentRegion == OMPD_atomic) {
1584*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1585*0a6a1f1dSLionel Sambuc       // OpenMP constructs may not be nested inside an atomic region.
1586*0a6a1f1dSLionel Sambuc       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1587*0a6a1f1dSLionel Sambuc       return true;
1588*0a6a1f1dSLionel Sambuc     }
1589*0a6a1f1dSLionel Sambuc     if (CurrentRegion == OMPD_section) {
1590*0a6a1f1dSLionel Sambuc       // OpenMP [2.7.2, sections Construct, Restrictions]
1591*0a6a1f1dSLionel Sambuc       // Orphaned section directives are prohibited. That is, the section
1592*0a6a1f1dSLionel Sambuc       // directives must appear within the sections construct and must not be
1593*0a6a1f1dSLionel Sambuc       // encountered elsewhere in the sections region.
1594*0a6a1f1dSLionel Sambuc       if (ParentRegion != OMPD_sections &&
1595*0a6a1f1dSLionel Sambuc           ParentRegion != OMPD_parallel_sections) {
1596*0a6a1f1dSLionel Sambuc         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1597*0a6a1f1dSLionel Sambuc             << (ParentRegion != OMPD_unknown)
1598*0a6a1f1dSLionel Sambuc             << getOpenMPDirectiveName(ParentRegion);
1599*0a6a1f1dSLionel Sambuc         return true;
1600*0a6a1f1dSLionel Sambuc       }
1601*0a6a1f1dSLionel Sambuc       return false;
1602*0a6a1f1dSLionel Sambuc     }
1603*0a6a1f1dSLionel Sambuc     // Allow some constructs to be orphaned (they could be used in functions,
1604*0a6a1f1dSLionel Sambuc     // called from OpenMP regions with the required preconditions).
1605*0a6a1f1dSLionel Sambuc     if (ParentRegion == OMPD_unknown)
1606*0a6a1f1dSLionel Sambuc       return false;
1607*0a6a1f1dSLionel Sambuc     if (CurrentRegion == OMPD_master) {
1608*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1609*0a6a1f1dSLionel Sambuc       // A master region may not be closely nested inside a worksharing,
1610*0a6a1f1dSLionel Sambuc       // atomic, or explicit task region.
1611*0a6a1f1dSLionel Sambuc       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1612*0a6a1f1dSLionel Sambuc                           ParentRegion == OMPD_task;
1613*0a6a1f1dSLionel Sambuc     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1614*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1615*0a6a1f1dSLionel Sambuc       // A critical region may not be nested (closely or otherwise) inside a
1616*0a6a1f1dSLionel Sambuc       // critical region with the same name. Note that this restriction is not
1617*0a6a1f1dSLionel Sambuc       // sufficient to prevent deadlock.
1618*0a6a1f1dSLionel Sambuc       SourceLocation PreviousCriticalLoc;
1619*0a6a1f1dSLionel Sambuc       bool DeadLock =
1620*0a6a1f1dSLionel Sambuc           Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1621*0a6a1f1dSLionel Sambuc                                   OpenMPDirectiveKind K,
1622*0a6a1f1dSLionel Sambuc                                   const DeclarationNameInfo &DNI,
1623*0a6a1f1dSLionel Sambuc                                   SourceLocation Loc)
1624*0a6a1f1dSLionel Sambuc                                   ->bool {
1625*0a6a1f1dSLionel Sambuc                                 if (K == OMPD_critical &&
1626*0a6a1f1dSLionel Sambuc                                     DNI.getName() == CurrentName.getName()) {
1627*0a6a1f1dSLionel Sambuc                                   PreviousCriticalLoc = Loc;
1628*0a6a1f1dSLionel Sambuc                                   return true;
1629*0a6a1f1dSLionel Sambuc                                 } else
1630*0a6a1f1dSLionel Sambuc                                   return false;
1631*0a6a1f1dSLionel Sambuc                               },
1632*0a6a1f1dSLionel Sambuc                               false /* skip top directive */);
1633*0a6a1f1dSLionel Sambuc       if (DeadLock) {
1634*0a6a1f1dSLionel Sambuc         SemaRef.Diag(StartLoc,
1635*0a6a1f1dSLionel Sambuc                      diag::err_omp_prohibited_region_critical_same_name)
1636*0a6a1f1dSLionel Sambuc             << CurrentName.getName();
1637*0a6a1f1dSLionel Sambuc         if (PreviousCriticalLoc.isValid())
1638*0a6a1f1dSLionel Sambuc           SemaRef.Diag(PreviousCriticalLoc,
1639*0a6a1f1dSLionel Sambuc                        diag::note_omp_previous_critical_region);
1640*0a6a1f1dSLionel Sambuc         return true;
1641*0a6a1f1dSLionel Sambuc       }
1642*0a6a1f1dSLionel Sambuc     } else if (CurrentRegion == OMPD_barrier) {
1643*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1644*0a6a1f1dSLionel Sambuc       // A barrier region may not be closely nested inside a worksharing,
1645*0a6a1f1dSLionel Sambuc       // explicit task, critical, ordered, atomic, or master region.
1646*0a6a1f1dSLionel Sambuc       NestingProhibited =
1647*0a6a1f1dSLionel Sambuc           isOpenMPWorksharingDirective(ParentRegion) ||
1648*0a6a1f1dSLionel Sambuc           ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1649*0a6a1f1dSLionel Sambuc           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1650*0a6a1f1dSLionel Sambuc     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1651*0a6a1f1dSLionel Sambuc                !isOpenMPParallelDirective(CurrentRegion)) {
1652*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1653*0a6a1f1dSLionel Sambuc       // A worksharing region may not be closely nested inside a worksharing,
1654*0a6a1f1dSLionel Sambuc       // explicit task, critical, ordered, atomic, or master region.
1655*0a6a1f1dSLionel Sambuc       NestingProhibited =
1656*0a6a1f1dSLionel Sambuc           isOpenMPWorksharingDirective(ParentRegion) ||
1657*0a6a1f1dSLionel Sambuc           ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1658*0a6a1f1dSLionel Sambuc           ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1659*0a6a1f1dSLionel Sambuc       Recommend = ShouldBeInParallelRegion;
1660*0a6a1f1dSLionel Sambuc     } else if (CurrentRegion == OMPD_ordered) {
1661*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1662*0a6a1f1dSLionel Sambuc       // An ordered region may not be closely nested inside a critical,
1663*0a6a1f1dSLionel Sambuc       // atomic, or explicit task region.
1664*0a6a1f1dSLionel Sambuc       // An ordered region must be closely nested inside a loop region (or
1665*0a6a1f1dSLionel Sambuc       // parallel loop region) with an ordered clause.
1666*0a6a1f1dSLionel Sambuc       NestingProhibited = ParentRegion == OMPD_critical ||
1667*0a6a1f1dSLionel Sambuc                           ParentRegion == OMPD_task ||
1668*0a6a1f1dSLionel Sambuc                           !Stack->isParentOrderedRegion();
1669*0a6a1f1dSLionel Sambuc       Recommend = ShouldBeInOrderedRegion;
1670*0a6a1f1dSLionel Sambuc     } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1671*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1672*0a6a1f1dSLionel Sambuc       // If specified, a teams construct must be contained within a target
1673*0a6a1f1dSLionel Sambuc       // construct.
1674*0a6a1f1dSLionel Sambuc       NestingProhibited = ParentRegion != OMPD_target;
1675*0a6a1f1dSLionel Sambuc       Recommend = ShouldBeInTargetRegion;
1676*0a6a1f1dSLionel Sambuc       Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1677*0a6a1f1dSLionel Sambuc     }
1678*0a6a1f1dSLionel Sambuc     if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1679*0a6a1f1dSLionel Sambuc       // OpenMP [2.16, Nesting of Regions]
1680*0a6a1f1dSLionel Sambuc       // distribute, parallel, parallel sections, parallel workshare, and the
1681*0a6a1f1dSLionel Sambuc       // parallel loop and parallel loop SIMD constructs are the only OpenMP
1682*0a6a1f1dSLionel Sambuc       // constructs that can be closely nested in the teams region.
1683*0a6a1f1dSLionel Sambuc       // TODO: add distribute directive.
1684*0a6a1f1dSLionel Sambuc       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1685*0a6a1f1dSLionel Sambuc       Recommend = ShouldBeInParallelRegion;
1686*0a6a1f1dSLionel Sambuc     }
1687*0a6a1f1dSLionel Sambuc     if (NestingProhibited) {
1688*0a6a1f1dSLionel Sambuc       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
1689*0a6a1f1dSLionel Sambuc           << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1690*0a6a1f1dSLionel Sambuc           << getOpenMPDirectiveName(CurrentRegion);
1691*0a6a1f1dSLionel Sambuc       return true;
1692*0a6a1f1dSLionel Sambuc     }
1693*0a6a1f1dSLionel Sambuc   }
1694*0a6a1f1dSLionel Sambuc   return false;
1695f4a2713aSLionel Sambuc }
1696f4a2713aSLionel Sambuc 
ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,const DeclarationNameInfo & DirName,ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)1697f4a2713aSLionel Sambuc StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1698*0a6a1f1dSLionel Sambuc                                                 const DeclarationNameInfo &DirName,
1699f4a2713aSLionel Sambuc                                                 ArrayRef<OMPClause *> Clauses,
1700f4a2713aSLionel Sambuc                                                 Stmt *AStmt,
1701f4a2713aSLionel Sambuc                                                 SourceLocation StartLoc,
1702f4a2713aSLionel Sambuc                                                 SourceLocation EndLoc) {
1703f4a2713aSLionel Sambuc   StmtResult Res = StmtError();
1704*0a6a1f1dSLionel Sambuc   if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
1705*0a6a1f1dSLionel Sambuc     return StmtError();
1706*0a6a1f1dSLionel Sambuc 
1707*0a6a1f1dSLionel Sambuc   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1708*0a6a1f1dSLionel Sambuc   llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
1709*0a6a1f1dSLionel Sambuc   bool ErrorFound = false;
1710*0a6a1f1dSLionel Sambuc   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1711*0a6a1f1dSLionel Sambuc   if (AStmt) {
1712*0a6a1f1dSLionel Sambuc     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1713f4a2713aSLionel Sambuc 
1714f4a2713aSLionel Sambuc     // Check default data sharing attributes for referenced variables.
1715f4a2713aSLionel Sambuc     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1716f4a2713aSLionel Sambuc     DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1717f4a2713aSLionel Sambuc     if (DSAChecker.isErrorFound())
1718f4a2713aSLionel Sambuc       return StmtError();
1719f4a2713aSLionel Sambuc     // Generate list of implicitly defined firstprivate variables.
1720*0a6a1f1dSLionel Sambuc     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
1721f4a2713aSLionel Sambuc 
1722f4a2713aSLionel Sambuc     if (!DSAChecker.getImplicitFirstprivate().empty()) {
1723*0a6a1f1dSLionel Sambuc       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1724*0a6a1f1dSLionel Sambuc               DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1725*0a6a1f1dSLionel Sambuc               SourceLocation(), SourceLocation())) {
1726f4a2713aSLionel Sambuc         ClausesWithImplicit.push_back(Implicit);
1727f4a2713aSLionel Sambuc         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1728f4a2713aSLionel Sambuc                      DSAChecker.getImplicitFirstprivate().size();
1729f4a2713aSLionel Sambuc       } else
1730f4a2713aSLionel Sambuc         ErrorFound = true;
1731f4a2713aSLionel Sambuc     }
1732*0a6a1f1dSLionel Sambuc   }
1733f4a2713aSLionel Sambuc 
1734f4a2713aSLionel Sambuc   switch (Kind) {
1735f4a2713aSLionel Sambuc   case OMPD_parallel:
1736*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1737*0a6a1f1dSLionel Sambuc                                        EndLoc);
1738*0a6a1f1dSLionel Sambuc     break;
1739*0a6a1f1dSLionel Sambuc   case OMPD_simd:
1740*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1741*0a6a1f1dSLionel Sambuc                                    VarsWithInheritedDSA);
1742*0a6a1f1dSLionel Sambuc     break;
1743*0a6a1f1dSLionel Sambuc   case OMPD_for:
1744*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1745*0a6a1f1dSLionel Sambuc                                   VarsWithInheritedDSA);
1746*0a6a1f1dSLionel Sambuc     break;
1747*0a6a1f1dSLionel Sambuc   case OMPD_for_simd:
1748*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1749*0a6a1f1dSLionel Sambuc                                       EndLoc, VarsWithInheritedDSA);
1750*0a6a1f1dSLionel Sambuc     break;
1751*0a6a1f1dSLionel Sambuc   case OMPD_sections:
1752*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1753*0a6a1f1dSLionel Sambuc                                        EndLoc);
1754*0a6a1f1dSLionel Sambuc     break;
1755*0a6a1f1dSLionel Sambuc   case OMPD_section:
1756*0a6a1f1dSLionel Sambuc     assert(ClausesWithImplicit.empty() &&
1757*0a6a1f1dSLionel Sambuc            "No clauses are allowed for 'omp section' directive");
1758*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1759*0a6a1f1dSLionel Sambuc     break;
1760*0a6a1f1dSLionel Sambuc   case OMPD_single:
1761*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1762*0a6a1f1dSLionel Sambuc                                      EndLoc);
1763*0a6a1f1dSLionel Sambuc     break;
1764*0a6a1f1dSLionel Sambuc   case OMPD_master:
1765*0a6a1f1dSLionel Sambuc     assert(ClausesWithImplicit.empty() &&
1766*0a6a1f1dSLionel Sambuc            "No clauses are allowed for 'omp master' directive");
1767*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1768*0a6a1f1dSLionel Sambuc     break;
1769*0a6a1f1dSLionel Sambuc   case OMPD_critical:
1770*0a6a1f1dSLionel Sambuc     assert(ClausesWithImplicit.empty() &&
1771*0a6a1f1dSLionel Sambuc            "No clauses are allowed for 'omp critical' directive");
1772*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1773*0a6a1f1dSLionel Sambuc     break;
1774*0a6a1f1dSLionel Sambuc   case OMPD_parallel_for:
1775*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1776*0a6a1f1dSLionel Sambuc                                           EndLoc, VarsWithInheritedDSA);
1777*0a6a1f1dSLionel Sambuc     break;
1778*0a6a1f1dSLionel Sambuc   case OMPD_parallel_for_simd:
1779*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPParallelForSimdDirective(
1780*0a6a1f1dSLionel Sambuc         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1781*0a6a1f1dSLionel Sambuc     break;
1782*0a6a1f1dSLionel Sambuc   case OMPD_parallel_sections:
1783*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1784f4a2713aSLionel Sambuc                                                StartLoc, EndLoc);
1785f4a2713aSLionel Sambuc     break;
1786f4a2713aSLionel Sambuc   case OMPD_task:
1787*0a6a1f1dSLionel Sambuc     Res =
1788*0a6a1f1dSLionel Sambuc         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1789*0a6a1f1dSLionel Sambuc     break;
1790*0a6a1f1dSLionel Sambuc   case OMPD_taskyield:
1791*0a6a1f1dSLionel Sambuc     assert(ClausesWithImplicit.empty() &&
1792*0a6a1f1dSLionel Sambuc            "No clauses are allowed for 'omp taskyield' directive");
1793*0a6a1f1dSLionel Sambuc     assert(AStmt == nullptr &&
1794*0a6a1f1dSLionel Sambuc            "No associated statement allowed for 'omp taskyield' directive");
1795*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1796*0a6a1f1dSLionel Sambuc     break;
1797*0a6a1f1dSLionel Sambuc   case OMPD_barrier:
1798*0a6a1f1dSLionel Sambuc     assert(ClausesWithImplicit.empty() &&
1799*0a6a1f1dSLionel Sambuc            "No clauses are allowed for 'omp barrier' directive");
1800*0a6a1f1dSLionel Sambuc     assert(AStmt == nullptr &&
1801*0a6a1f1dSLionel Sambuc            "No associated statement allowed for 'omp barrier' directive");
1802*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1803*0a6a1f1dSLionel Sambuc     break;
1804*0a6a1f1dSLionel Sambuc   case OMPD_taskwait:
1805*0a6a1f1dSLionel Sambuc     assert(ClausesWithImplicit.empty() &&
1806*0a6a1f1dSLionel Sambuc            "No clauses are allowed for 'omp taskwait' directive");
1807*0a6a1f1dSLionel Sambuc     assert(AStmt == nullptr &&
1808*0a6a1f1dSLionel Sambuc            "No associated statement allowed for 'omp taskwait' directive");
1809*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1810*0a6a1f1dSLionel Sambuc     break;
1811*0a6a1f1dSLionel Sambuc   case OMPD_flush:
1812*0a6a1f1dSLionel Sambuc     assert(AStmt == nullptr &&
1813*0a6a1f1dSLionel Sambuc            "No associated statement allowed for 'omp flush' directive");
1814*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1815*0a6a1f1dSLionel Sambuc     break;
1816*0a6a1f1dSLionel Sambuc   case OMPD_ordered:
1817*0a6a1f1dSLionel Sambuc     assert(ClausesWithImplicit.empty() &&
1818*0a6a1f1dSLionel Sambuc            "No clauses are allowed for 'omp ordered' directive");
1819*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1820*0a6a1f1dSLionel Sambuc     break;
1821*0a6a1f1dSLionel Sambuc   case OMPD_atomic:
1822*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1823*0a6a1f1dSLionel Sambuc                                      EndLoc);
1824*0a6a1f1dSLionel Sambuc     break;
1825*0a6a1f1dSLionel Sambuc   case OMPD_teams:
1826*0a6a1f1dSLionel Sambuc     Res =
1827*0a6a1f1dSLionel Sambuc         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1828*0a6a1f1dSLionel Sambuc     break;
1829*0a6a1f1dSLionel Sambuc   case OMPD_target:
1830*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1831*0a6a1f1dSLionel Sambuc                                      EndLoc);
1832*0a6a1f1dSLionel Sambuc     break;
1833*0a6a1f1dSLionel Sambuc   case OMPD_threadprivate:
1834f4a2713aSLionel Sambuc     llvm_unreachable("OpenMP Directive is not allowed");
1835f4a2713aSLionel Sambuc   case OMPD_unknown:
1836f4a2713aSLionel Sambuc     llvm_unreachable("Unknown OpenMP directive");
1837f4a2713aSLionel Sambuc   }
1838f4a2713aSLionel Sambuc 
1839*0a6a1f1dSLionel Sambuc   for (auto P : VarsWithInheritedDSA) {
1840*0a6a1f1dSLionel Sambuc     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1841*0a6a1f1dSLionel Sambuc         << P.first << P.second->getSourceRange();
1842*0a6a1f1dSLionel Sambuc   }
1843*0a6a1f1dSLionel Sambuc   if (!VarsWithInheritedDSA.empty())
1844*0a6a1f1dSLionel Sambuc     return StmtError();
1845*0a6a1f1dSLionel Sambuc 
1846*0a6a1f1dSLionel Sambuc   if (ErrorFound)
1847*0a6a1f1dSLionel Sambuc     return StmtError();
1848f4a2713aSLionel Sambuc   return Res;
1849f4a2713aSLionel Sambuc }
1850f4a2713aSLionel Sambuc 
ActOnOpenMPParallelDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)1851f4a2713aSLionel Sambuc StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1852f4a2713aSLionel Sambuc                                               Stmt *AStmt,
1853f4a2713aSLionel Sambuc                                               SourceLocation StartLoc,
1854f4a2713aSLionel Sambuc                                               SourceLocation EndLoc) {
1855*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1856*0a6a1f1dSLionel Sambuc   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1857*0a6a1f1dSLionel Sambuc   // 1.2.2 OpenMP Language Terminology
1858*0a6a1f1dSLionel Sambuc   // Structured block - An executable statement with a single entry at the
1859*0a6a1f1dSLionel Sambuc   // top and a single exit at the bottom.
1860*0a6a1f1dSLionel Sambuc   // The point of exit cannot be a branch out of the structured block.
1861*0a6a1f1dSLionel Sambuc   // longjmp() and throw() must not violate the entry/exit criteria.
1862*0a6a1f1dSLionel Sambuc   CS->getCapturedDecl()->setNothrow();
1863*0a6a1f1dSLionel Sambuc 
1864f4a2713aSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
1865f4a2713aSLionel Sambuc 
1866*0a6a1f1dSLionel Sambuc   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1867*0a6a1f1dSLionel Sambuc                                       AStmt);
1868f4a2713aSLionel Sambuc }
1869f4a2713aSLionel Sambuc 
1870*0a6a1f1dSLionel Sambuc namespace {
1871*0a6a1f1dSLionel Sambuc /// \brief Helper class for checking canonical form of the OpenMP loops and
1872*0a6a1f1dSLionel Sambuc /// extracting iteration space of each loop in the loop nest, that will be used
1873*0a6a1f1dSLionel Sambuc /// for IR generation.
1874*0a6a1f1dSLionel Sambuc class OpenMPIterationSpaceChecker {
1875*0a6a1f1dSLionel Sambuc   /// \brief Reference to Sema.
1876*0a6a1f1dSLionel Sambuc   Sema &SemaRef;
1877*0a6a1f1dSLionel Sambuc   /// \brief A location for diagnostics (when there is no some better location).
1878*0a6a1f1dSLionel Sambuc   SourceLocation DefaultLoc;
1879*0a6a1f1dSLionel Sambuc   /// \brief A location for diagnostics (when increment is not compatible).
1880*0a6a1f1dSLionel Sambuc   SourceLocation ConditionLoc;
1881*0a6a1f1dSLionel Sambuc   /// \brief A source location for referring to loop init later.
1882*0a6a1f1dSLionel Sambuc   SourceRange InitSrcRange;
1883*0a6a1f1dSLionel Sambuc   /// \brief A source location for referring to condition later.
1884*0a6a1f1dSLionel Sambuc   SourceRange ConditionSrcRange;
1885*0a6a1f1dSLionel Sambuc   /// \brief A source location for referring to increment later.
1886*0a6a1f1dSLionel Sambuc   SourceRange IncrementSrcRange;
1887*0a6a1f1dSLionel Sambuc   /// \brief Loop variable.
1888*0a6a1f1dSLionel Sambuc   VarDecl *Var;
1889*0a6a1f1dSLionel Sambuc   /// \brief Reference to loop variable.
1890*0a6a1f1dSLionel Sambuc   DeclRefExpr *VarRef;
1891*0a6a1f1dSLionel Sambuc   /// \brief Lower bound (initializer for the var).
1892*0a6a1f1dSLionel Sambuc   Expr *LB;
1893*0a6a1f1dSLionel Sambuc   /// \brief Upper bound.
1894*0a6a1f1dSLionel Sambuc   Expr *UB;
1895*0a6a1f1dSLionel Sambuc   /// \brief Loop step (increment).
1896*0a6a1f1dSLionel Sambuc   Expr *Step;
1897*0a6a1f1dSLionel Sambuc   /// \brief This flag is true when condition is one of:
1898*0a6a1f1dSLionel Sambuc   ///   Var <  UB
1899*0a6a1f1dSLionel Sambuc   ///   Var <= UB
1900*0a6a1f1dSLionel Sambuc   ///   UB  >  Var
1901*0a6a1f1dSLionel Sambuc   ///   UB  >= Var
1902*0a6a1f1dSLionel Sambuc   bool TestIsLessOp;
1903*0a6a1f1dSLionel Sambuc   /// \brief This flag is true when condition is strict ( < or > ).
1904*0a6a1f1dSLionel Sambuc   bool TestIsStrictOp;
1905*0a6a1f1dSLionel Sambuc   /// \brief This flag is true when step is subtracted on each iteration.
1906*0a6a1f1dSLionel Sambuc   bool SubtractStep;
1907*0a6a1f1dSLionel Sambuc 
1908*0a6a1f1dSLionel Sambuc public:
OpenMPIterationSpaceChecker(Sema & SemaRef,SourceLocation DefaultLoc)1909*0a6a1f1dSLionel Sambuc   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1910*0a6a1f1dSLionel Sambuc       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1911*0a6a1f1dSLionel Sambuc         InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1912*0a6a1f1dSLionel Sambuc         IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
1913*0a6a1f1dSLionel Sambuc         LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1914*0a6a1f1dSLionel Sambuc         TestIsStrictOp(false), SubtractStep(false) {}
1915*0a6a1f1dSLionel Sambuc   /// \brief Check init-expr for canonical loop form and save loop counter
1916*0a6a1f1dSLionel Sambuc   /// variable - #Var and its initialization value - #LB.
1917*0a6a1f1dSLionel Sambuc   bool CheckInit(Stmt *S);
1918*0a6a1f1dSLionel Sambuc   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1919*0a6a1f1dSLionel Sambuc   /// for less/greater and for strict/non-strict comparison.
1920*0a6a1f1dSLionel Sambuc   bool CheckCond(Expr *S);
1921*0a6a1f1dSLionel Sambuc   /// \brief Check incr-expr for canonical loop form and return true if it
1922*0a6a1f1dSLionel Sambuc   /// does not conform, otherwise save loop step (#Step).
1923*0a6a1f1dSLionel Sambuc   bool CheckInc(Expr *S);
1924*0a6a1f1dSLionel Sambuc   /// \brief Return the loop counter variable.
GetLoopVar() const1925*0a6a1f1dSLionel Sambuc   VarDecl *GetLoopVar() const { return Var; }
1926*0a6a1f1dSLionel Sambuc   /// \brief Return the reference expression to loop counter variable.
GetLoopVarRefExpr() const1927*0a6a1f1dSLionel Sambuc   DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
1928*0a6a1f1dSLionel Sambuc   /// \brief Source range of the loop init.
GetInitSrcRange() const1929*0a6a1f1dSLionel Sambuc   SourceRange GetInitSrcRange() const { return InitSrcRange; }
1930*0a6a1f1dSLionel Sambuc   /// \brief Source range of the loop condition.
GetConditionSrcRange() const1931*0a6a1f1dSLionel Sambuc   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1932*0a6a1f1dSLionel Sambuc   /// \brief Source range of the loop increment.
GetIncrementSrcRange() const1933*0a6a1f1dSLionel Sambuc   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1934*0a6a1f1dSLionel Sambuc   /// \brief True if the step should be subtracted.
ShouldSubtractStep() const1935*0a6a1f1dSLionel Sambuc   bool ShouldSubtractStep() const { return SubtractStep; }
1936*0a6a1f1dSLionel Sambuc   /// \brief Build the expression to calculate the number of iterations.
1937*0a6a1f1dSLionel Sambuc   Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
1938*0a6a1f1dSLionel Sambuc   /// \brief Build reference expression to the counter be used for codegen.
1939*0a6a1f1dSLionel Sambuc   Expr *BuildCounterVar() const;
1940*0a6a1f1dSLionel Sambuc   /// \brief Build initization of the counter be used for codegen.
1941*0a6a1f1dSLionel Sambuc   Expr *BuildCounterInit() const;
1942*0a6a1f1dSLionel Sambuc   /// \brief Build step of the counter be used for codegen.
1943*0a6a1f1dSLionel Sambuc   Expr *BuildCounterStep() const;
1944*0a6a1f1dSLionel Sambuc   /// \brief Return true if any expression is dependent.
1945*0a6a1f1dSLionel Sambuc   bool Dependent() const;
1946*0a6a1f1dSLionel Sambuc 
1947*0a6a1f1dSLionel Sambuc private:
1948*0a6a1f1dSLionel Sambuc   /// \brief Check the right-hand side of an assignment in the increment
1949*0a6a1f1dSLionel Sambuc   /// expression.
1950*0a6a1f1dSLionel Sambuc   bool CheckIncRHS(Expr *RHS);
1951*0a6a1f1dSLionel Sambuc   /// \brief Helper to set loop counter variable and its initializer.
1952*0a6a1f1dSLionel Sambuc   bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
1953*0a6a1f1dSLionel Sambuc   /// \brief Helper to set upper bound.
1954*0a6a1f1dSLionel Sambuc   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1955*0a6a1f1dSLionel Sambuc              const SourceLocation &SL);
1956*0a6a1f1dSLionel Sambuc   /// \brief Helper to set loop increment.
1957*0a6a1f1dSLionel Sambuc   bool SetStep(Expr *NewStep, bool Subtract);
1958*0a6a1f1dSLionel Sambuc };
1959*0a6a1f1dSLionel Sambuc 
Dependent() const1960*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::Dependent() const {
1961*0a6a1f1dSLionel Sambuc   if (!Var) {
1962*0a6a1f1dSLionel Sambuc     assert(!LB && !UB && !Step);
1963*0a6a1f1dSLionel Sambuc     return false;
1964*0a6a1f1dSLionel Sambuc   }
1965*0a6a1f1dSLionel Sambuc   return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1966*0a6a1f1dSLionel Sambuc          (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1967*0a6a1f1dSLionel Sambuc }
1968*0a6a1f1dSLionel Sambuc 
SetVarAndLB(VarDecl * NewVar,DeclRefExpr * NewVarRefExpr,Expr * NewLB)1969*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1970*0a6a1f1dSLionel Sambuc                                               DeclRefExpr *NewVarRefExpr,
1971*0a6a1f1dSLionel Sambuc                                               Expr *NewLB) {
1972*0a6a1f1dSLionel Sambuc   // State consistency checking to ensure correct usage.
1973*0a6a1f1dSLionel Sambuc   assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1974*0a6a1f1dSLionel Sambuc          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
1975*0a6a1f1dSLionel Sambuc   if (!NewVar || !NewLB)
1976*0a6a1f1dSLionel Sambuc     return true;
1977*0a6a1f1dSLionel Sambuc   Var = NewVar;
1978*0a6a1f1dSLionel Sambuc   VarRef = NewVarRefExpr;
1979*0a6a1f1dSLionel Sambuc   LB = NewLB;
1980*0a6a1f1dSLionel Sambuc   return false;
1981*0a6a1f1dSLionel Sambuc }
1982*0a6a1f1dSLionel Sambuc 
SetUB(Expr * NewUB,bool LessOp,bool StrictOp,const SourceRange & SR,const SourceLocation & SL)1983*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1984*0a6a1f1dSLionel Sambuc                                         const SourceRange &SR,
1985*0a6a1f1dSLionel Sambuc                                         const SourceLocation &SL) {
1986*0a6a1f1dSLionel Sambuc   // State consistency checking to ensure correct usage.
1987*0a6a1f1dSLionel Sambuc   assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1988*0a6a1f1dSLionel Sambuc          !TestIsLessOp && !TestIsStrictOp);
1989*0a6a1f1dSLionel Sambuc   if (!NewUB)
1990*0a6a1f1dSLionel Sambuc     return true;
1991*0a6a1f1dSLionel Sambuc   UB = NewUB;
1992*0a6a1f1dSLionel Sambuc   TestIsLessOp = LessOp;
1993*0a6a1f1dSLionel Sambuc   TestIsStrictOp = StrictOp;
1994*0a6a1f1dSLionel Sambuc   ConditionSrcRange = SR;
1995*0a6a1f1dSLionel Sambuc   ConditionLoc = SL;
1996*0a6a1f1dSLionel Sambuc   return false;
1997*0a6a1f1dSLionel Sambuc }
1998*0a6a1f1dSLionel Sambuc 
SetStep(Expr * NewStep,bool Subtract)1999*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2000*0a6a1f1dSLionel Sambuc   // State consistency checking to ensure correct usage.
2001*0a6a1f1dSLionel Sambuc   assert(Var != nullptr && LB != nullptr && Step == nullptr);
2002*0a6a1f1dSLionel Sambuc   if (!NewStep)
2003*0a6a1f1dSLionel Sambuc     return true;
2004*0a6a1f1dSLionel Sambuc   if (!NewStep->isValueDependent()) {
2005*0a6a1f1dSLionel Sambuc     // Check that the step is integer expression.
2006*0a6a1f1dSLionel Sambuc     SourceLocation StepLoc = NewStep->getLocStart();
2007*0a6a1f1dSLionel Sambuc     ExprResult Val =
2008*0a6a1f1dSLionel Sambuc         SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2009*0a6a1f1dSLionel Sambuc     if (Val.isInvalid())
2010*0a6a1f1dSLionel Sambuc       return true;
2011*0a6a1f1dSLionel Sambuc     NewStep = Val.get();
2012*0a6a1f1dSLionel Sambuc 
2013*0a6a1f1dSLionel Sambuc     // OpenMP [2.6, Canonical Loop Form, Restrictions]
2014*0a6a1f1dSLionel Sambuc     //  If test-expr is of form var relational-op b and relational-op is < or
2015*0a6a1f1dSLionel Sambuc     //  <= then incr-expr must cause var to increase on each iteration of the
2016*0a6a1f1dSLionel Sambuc     //  loop. If test-expr is of form var relational-op b and relational-op is
2017*0a6a1f1dSLionel Sambuc     //  > or >= then incr-expr must cause var to decrease on each iteration of
2018*0a6a1f1dSLionel Sambuc     //  the loop.
2019*0a6a1f1dSLionel Sambuc     //  If test-expr is of form b relational-op var and relational-op is < or
2020*0a6a1f1dSLionel Sambuc     //  <= then incr-expr must cause var to decrease on each iteration of the
2021*0a6a1f1dSLionel Sambuc     //  loop. If test-expr is of form b relational-op var and relational-op is
2022*0a6a1f1dSLionel Sambuc     //  > or >= then incr-expr must cause var to increase on each iteration of
2023*0a6a1f1dSLionel Sambuc     //  the loop.
2024*0a6a1f1dSLionel Sambuc     llvm::APSInt Result;
2025*0a6a1f1dSLionel Sambuc     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2026*0a6a1f1dSLionel Sambuc     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2027*0a6a1f1dSLionel Sambuc     bool IsConstNeg =
2028*0a6a1f1dSLionel Sambuc         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
2029*0a6a1f1dSLionel Sambuc     bool IsConstPos =
2030*0a6a1f1dSLionel Sambuc         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
2031*0a6a1f1dSLionel Sambuc     bool IsConstZero = IsConstant && !Result.getBoolValue();
2032*0a6a1f1dSLionel Sambuc     if (UB && (IsConstZero ||
2033*0a6a1f1dSLionel Sambuc                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
2034*0a6a1f1dSLionel Sambuc                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
2035*0a6a1f1dSLionel Sambuc       SemaRef.Diag(NewStep->getExprLoc(),
2036*0a6a1f1dSLionel Sambuc                    diag::err_omp_loop_incr_not_compatible)
2037*0a6a1f1dSLionel Sambuc           << Var << TestIsLessOp << NewStep->getSourceRange();
2038*0a6a1f1dSLionel Sambuc       SemaRef.Diag(ConditionLoc,
2039*0a6a1f1dSLionel Sambuc                    diag::note_omp_loop_cond_requres_compatible_incr)
2040*0a6a1f1dSLionel Sambuc           << TestIsLessOp << ConditionSrcRange;
2041*0a6a1f1dSLionel Sambuc       return true;
2042*0a6a1f1dSLionel Sambuc     }
2043*0a6a1f1dSLionel Sambuc     if (TestIsLessOp == Subtract) {
2044*0a6a1f1dSLionel Sambuc       NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2045*0a6a1f1dSLionel Sambuc                                              NewStep).get();
2046*0a6a1f1dSLionel Sambuc       Subtract = !Subtract;
2047*0a6a1f1dSLionel Sambuc     }
2048*0a6a1f1dSLionel Sambuc   }
2049*0a6a1f1dSLionel Sambuc 
2050*0a6a1f1dSLionel Sambuc   Step = NewStep;
2051*0a6a1f1dSLionel Sambuc   SubtractStep = Subtract;
2052*0a6a1f1dSLionel Sambuc   return false;
2053*0a6a1f1dSLionel Sambuc }
2054*0a6a1f1dSLionel Sambuc 
CheckInit(Stmt * S)2055*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2056*0a6a1f1dSLionel Sambuc   // Check init-expr for canonical loop form and save loop counter
2057*0a6a1f1dSLionel Sambuc   // variable - #Var and its initialization value - #LB.
2058*0a6a1f1dSLionel Sambuc   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2059*0a6a1f1dSLionel Sambuc   //   var = lb
2060*0a6a1f1dSLionel Sambuc   //   integer-type var = lb
2061*0a6a1f1dSLionel Sambuc   //   random-access-iterator-type var = lb
2062*0a6a1f1dSLionel Sambuc   //   pointer-type var = lb
2063*0a6a1f1dSLionel Sambuc   //
2064*0a6a1f1dSLionel Sambuc   if (!S) {
2065*0a6a1f1dSLionel Sambuc     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2066*0a6a1f1dSLionel Sambuc     return true;
2067*0a6a1f1dSLionel Sambuc   }
2068*0a6a1f1dSLionel Sambuc   InitSrcRange = S->getSourceRange();
2069*0a6a1f1dSLionel Sambuc   if (Expr *E = dyn_cast<Expr>(S))
2070*0a6a1f1dSLionel Sambuc     S = E->IgnoreParens();
2071*0a6a1f1dSLionel Sambuc   if (auto BO = dyn_cast<BinaryOperator>(S)) {
2072*0a6a1f1dSLionel Sambuc     if (BO->getOpcode() == BO_Assign)
2073*0a6a1f1dSLionel Sambuc       if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
2074*0a6a1f1dSLionel Sambuc         return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2075*0a6a1f1dSLionel Sambuc                            BO->getRHS());
2076*0a6a1f1dSLionel Sambuc   } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2077*0a6a1f1dSLionel Sambuc     if (DS->isSingleDecl()) {
2078*0a6a1f1dSLionel Sambuc       if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2079*0a6a1f1dSLionel Sambuc         if (Var->hasInit()) {
2080*0a6a1f1dSLionel Sambuc           // Accept non-canonical init form here but emit ext. warning.
2081*0a6a1f1dSLionel Sambuc           if (Var->getInitStyle() != VarDecl::CInit)
2082*0a6a1f1dSLionel Sambuc             SemaRef.Diag(S->getLocStart(),
2083*0a6a1f1dSLionel Sambuc                          diag::ext_omp_loop_not_canonical_init)
2084*0a6a1f1dSLionel Sambuc                 << S->getSourceRange();
2085*0a6a1f1dSLionel Sambuc           return SetVarAndLB(Var, nullptr, Var->getInit());
2086*0a6a1f1dSLionel Sambuc         }
2087*0a6a1f1dSLionel Sambuc       }
2088*0a6a1f1dSLionel Sambuc     }
2089*0a6a1f1dSLionel Sambuc   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2090*0a6a1f1dSLionel Sambuc     if (CE->getOperator() == OO_Equal)
2091*0a6a1f1dSLionel Sambuc       if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
2092*0a6a1f1dSLionel Sambuc         return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2093*0a6a1f1dSLionel Sambuc                            CE->getArg(1));
2094*0a6a1f1dSLionel Sambuc 
2095*0a6a1f1dSLionel Sambuc   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2096*0a6a1f1dSLionel Sambuc       << S->getSourceRange();
2097*0a6a1f1dSLionel Sambuc   return true;
2098*0a6a1f1dSLionel Sambuc }
2099*0a6a1f1dSLionel Sambuc 
2100*0a6a1f1dSLionel Sambuc /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
2101*0a6a1f1dSLionel Sambuc /// variable (which may be the loop variable) if possible.
GetInitVarDecl(const Expr * E)2102*0a6a1f1dSLionel Sambuc static const VarDecl *GetInitVarDecl(const Expr *E) {
2103*0a6a1f1dSLionel Sambuc   if (!E)
2104*0a6a1f1dSLionel Sambuc     return nullptr;
2105*0a6a1f1dSLionel Sambuc   E = E->IgnoreParenImpCasts();
2106*0a6a1f1dSLionel Sambuc   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2107*0a6a1f1dSLionel Sambuc     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2108*0a6a1f1dSLionel Sambuc       if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2109*0a6a1f1dSLionel Sambuc           CE->getArg(0) != nullptr)
2110*0a6a1f1dSLionel Sambuc         E = CE->getArg(0)->IgnoreParenImpCasts();
2111*0a6a1f1dSLionel Sambuc   auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2112*0a6a1f1dSLionel Sambuc   if (!DRE)
2113*0a6a1f1dSLionel Sambuc     return nullptr;
2114*0a6a1f1dSLionel Sambuc   return dyn_cast<VarDecl>(DRE->getDecl());
2115*0a6a1f1dSLionel Sambuc }
2116*0a6a1f1dSLionel Sambuc 
CheckCond(Expr * S)2117*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2118*0a6a1f1dSLionel Sambuc   // Check test-expr for canonical form, save upper-bound UB, flags for
2119*0a6a1f1dSLionel Sambuc   // less/greater and for strict/non-strict comparison.
2120*0a6a1f1dSLionel Sambuc   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2121*0a6a1f1dSLionel Sambuc   //   var relational-op b
2122*0a6a1f1dSLionel Sambuc   //   b relational-op var
2123*0a6a1f1dSLionel Sambuc   //
2124*0a6a1f1dSLionel Sambuc   if (!S) {
2125*0a6a1f1dSLionel Sambuc     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2126*0a6a1f1dSLionel Sambuc     return true;
2127*0a6a1f1dSLionel Sambuc   }
2128*0a6a1f1dSLionel Sambuc   S = S->IgnoreParenImpCasts();
2129*0a6a1f1dSLionel Sambuc   SourceLocation CondLoc = S->getLocStart();
2130*0a6a1f1dSLionel Sambuc   if (auto BO = dyn_cast<BinaryOperator>(S)) {
2131*0a6a1f1dSLionel Sambuc     if (BO->isRelationalOp()) {
2132*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(BO->getLHS()) == Var)
2133*0a6a1f1dSLionel Sambuc         return SetUB(BO->getRHS(),
2134*0a6a1f1dSLionel Sambuc                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2135*0a6a1f1dSLionel Sambuc                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2136*0a6a1f1dSLionel Sambuc                      BO->getSourceRange(), BO->getOperatorLoc());
2137*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(BO->getRHS()) == Var)
2138*0a6a1f1dSLionel Sambuc         return SetUB(BO->getLHS(),
2139*0a6a1f1dSLionel Sambuc                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2140*0a6a1f1dSLionel Sambuc                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2141*0a6a1f1dSLionel Sambuc                      BO->getSourceRange(), BO->getOperatorLoc());
2142*0a6a1f1dSLionel Sambuc     }
2143*0a6a1f1dSLionel Sambuc   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2144*0a6a1f1dSLionel Sambuc     if (CE->getNumArgs() == 2) {
2145*0a6a1f1dSLionel Sambuc       auto Op = CE->getOperator();
2146*0a6a1f1dSLionel Sambuc       switch (Op) {
2147*0a6a1f1dSLionel Sambuc       case OO_Greater:
2148*0a6a1f1dSLionel Sambuc       case OO_GreaterEqual:
2149*0a6a1f1dSLionel Sambuc       case OO_Less:
2150*0a6a1f1dSLionel Sambuc       case OO_LessEqual:
2151*0a6a1f1dSLionel Sambuc         if (GetInitVarDecl(CE->getArg(0)) == Var)
2152*0a6a1f1dSLionel Sambuc           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2153*0a6a1f1dSLionel Sambuc                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2154*0a6a1f1dSLionel Sambuc                        CE->getOperatorLoc());
2155*0a6a1f1dSLionel Sambuc         if (GetInitVarDecl(CE->getArg(1)) == Var)
2156*0a6a1f1dSLionel Sambuc           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2157*0a6a1f1dSLionel Sambuc                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2158*0a6a1f1dSLionel Sambuc                        CE->getOperatorLoc());
2159*0a6a1f1dSLionel Sambuc         break;
2160*0a6a1f1dSLionel Sambuc       default:
2161*0a6a1f1dSLionel Sambuc         break;
2162*0a6a1f1dSLionel Sambuc       }
2163*0a6a1f1dSLionel Sambuc     }
2164*0a6a1f1dSLionel Sambuc   }
2165*0a6a1f1dSLionel Sambuc   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2166*0a6a1f1dSLionel Sambuc       << S->getSourceRange() << Var;
2167*0a6a1f1dSLionel Sambuc   return true;
2168*0a6a1f1dSLionel Sambuc }
2169*0a6a1f1dSLionel Sambuc 
CheckIncRHS(Expr * RHS)2170*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2171*0a6a1f1dSLionel Sambuc   // RHS of canonical loop form increment can be:
2172*0a6a1f1dSLionel Sambuc   //   var + incr
2173*0a6a1f1dSLionel Sambuc   //   incr + var
2174*0a6a1f1dSLionel Sambuc   //   var - incr
2175*0a6a1f1dSLionel Sambuc   //
2176*0a6a1f1dSLionel Sambuc   RHS = RHS->IgnoreParenImpCasts();
2177*0a6a1f1dSLionel Sambuc   if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2178*0a6a1f1dSLionel Sambuc     if (BO->isAdditiveOp()) {
2179*0a6a1f1dSLionel Sambuc       bool IsAdd = BO->getOpcode() == BO_Add;
2180*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(BO->getLHS()) == Var)
2181*0a6a1f1dSLionel Sambuc         return SetStep(BO->getRHS(), !IsAdd);
2182*0a6a1f1dSLionel Sambuc       if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2183*0a6a1f1dSLionel Sambuc         return SetStep(BO->getLHS(), false);
2184*0a6a1f1dSLionel Sambuc     }
2185*0a6a1f1dSLionel Sambuc   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2186*0a6a1f1dSLionel Sambuc     bool IsAdd = CE->getOperator() == OO_Plus;
2187*0a6a1f1dSLionel Sambuc     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2188*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(CE->getArg(0)) == Var)
2189*0a6a1f1dSLionel Sambuc         return SetStep(CE->getArg(1), !IsAdd);
2190*0a6a1f1dSLionel Sambuc       if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2191*0a6a1f1dSLionel Sambuc         return SetStep(CE->getArg(0), false);
2192*0a6a1f1dSLionel Sambuc     }
2193*0a6a1f1dSLionel Sambuc   }
2194*0a6a1f1dSLionel Sambuc   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2195*0a6a1f1dSLionel Sambuc       << RHS->getSourceRange() << Var;
2196*0a6a1f1dSLionel Sambuc   return true;
2197*0a6a1f1dSLionel Sambuc }
2198*0a6a1f1dSLionel Sambuc 
CheckInc(Expr * S)2199*0a6a1f1dSLionel Sambuc bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2200*0a6a1f1dSLionel Sambuc   // Check incr-expr for canonical loop form and return true if it
2201*0a6a1f1dSLionel Sambuc   // does not conform.
2202*0a6a1f1dSLionel Sambuc   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2203*0a6a1f1dSLionel Sambuc   //   ++var
2204*0a6a1f1dSLionel Sambuc   //   var++
2205*0a6a1f1dSLionel Sambuc   //   --var
2206*0a6a1f1dSLionel Sambuc   //   var--
2207*0a6a1f1dSLionel Sambuc   //   var += incr
2208*0a6a1f1dSLionel Sambuc   //   var -= incr
2209*0a6a1f1dSLionel Sambuc   //   var = var + incr
2210*0a6a1f1dSLionel Sambuc   //   var = incr + var
2211*0a6a1f1dSLionel Sambuc   //   var = var - incr
2212*0a6a1f1dSLionel Sambuc   //
2213*0a6a1f1dSLionel Sambuc   if (!S) {
2214*0a6a1f1dSLionel Sambuc     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2215*0a6a1f1dSLionel Sambuc     return true;
2216*0a6a1f1dSLionel Sambuc   }
2217*0a6a1f1dSLionel Sambuc   IncrementSrcRange = S->getSourceRange();
2218*0a6a1f1dSLionel Sambuc   S = S->IgnoreParens();
2219*0a6a1f1dSLionel Sambuc   if (auto UO = dyn_cast<UnaryOperator>(S)) {
2220*0a6a1f1dSLionel Sambuc     if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2221*0a6a1f1dSLionel Sambuc       return SetStep(
2222*0a6a1f1dSLionel Sambuc           SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2223*0a6a1f1dSLionel Sambuc                                        (UO->isDecrementOp() ? -1 : 1)).get(),
2224*0a6a1f1dSLionel Sambuc           false);
2225*0a6a1f1dSLionel Sambuc   } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2226*0a6a1f1dSLionel Sambuc     switch (BO->getOpcode()) {
2227*0a6a1f1dSLionel Sambuc     case BO_AddAssign:
2228*0a6a1f1dSLionel Sambuc     case BO_SubAssign:
2229*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(BO->getLHS()) == Var)
2230*0a6a1f1dSLionel Sambuc         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2231*0a6a1f1dSLionel Sambuc       break;
2232*0a6a1f1dSLionel Sambuc     case BO_Assign:
2233*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(BO->getLHS()) == Var)
2234*0a6a1f1dSLionel Sambuc         return CheckIncRHS(BO->getRHS());
2235*0a6a1f1dSLionel Sambuc       break;
2236*0a6a1f1dSLionel Sambuc     default:
2237*0a6a1f1dSLionel Sambuc       break;
2238*0a6a1f1dSLionel Sambuc     }
2239*0a6a1f1dSLionel Sambuc   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2240*0a6a1f1dSLionel Sambuc     switch (CE->getOperator()) {
2241*0a6a1f1dSLionel Sambuc     case OO_PlusPlus:
2242*0a6a1f1dSLionel Sambuc     case OO_MinusMinus:
2243*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(CE->getArg(0)) == Var)
2244*0a6a1f1dSLionel Sambuc         return SetStep(
2245*0a6a1f1dSLionel Sambuc             SemaRef.ActOnIntegerConstant(
2246*0a6a1f1dSLionel Sambuc                         CE->getLocStart(),
2247*0a6a1f1dSLionel Sambuc                         ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2248*0a6a1f1dSLionel Sambuc             false);
2249*0a6a1f1dSLionel Sambuc       break;
2250*0a6a1f1dSLionel Sambuc     case OO_PlusEqual:
2251*0a6a1f1dSLionel Sambuc     case OO_MinusEqual:
2252*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(CE->getArg(0)) == Var)
2253*0a6a1f1dSLionel Sambuc         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2254*0a6a1f1dSLionel Sambuc       break;
2255*0a6a1f1dSLionel Sambuc     case OO_Equal:
2256*0a6a1f1dSLionel Sambuc       if (GetInitVarDecl(CE->getArg(0)) == Var)
2257*0a6a1f1dSLionel Sambuc         return CheckIncRHS(CE->getArg(1));
2258*0a6a1f1dSLionel Sambuc       break;
2259*0a6a1f1dSLionel Sambuc     default:
2260*0a6a1f1dSLionel Sambuc       break;
2261*0a6a1f1dSLionel Sambuc     }
2262*0a6a1f1dSLionel Sambuc   }
2263*0a6a1f1dSLionel Sambuc   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2264*0a6a1f1dSLionel Sambuc       << S->getSourceRange() << Var;
2265*0a6a1f1dSLionel Sambuc   return true;
2266*0a6a1f1dSLionel Sambuc }
2267*0a6a1f1dSLionel Sambuc 
2268*0a6a1f1dSLionel Sambuc /// \brief Build the expression to calculate the number of iterations.
2269*0a6a1f1dSLionel Sambuc Expr *
BuildNumIterations(Scope * S,const bool LimitedType) const2270*0a6a1f1dSLionel Sambuc OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2271*0a6a1f1dSLionel Sambuc                                                 const bool LimitedType) const {
2272*0a6a1f1dSLionel Sambuc   ExprResult Diff;
2273*0a6a1f1dSLionel Sambuc   if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2274*0a6a1f1dSLionel Sambuc       SemaRef.getLangOpts().CPlusPlus) {
2275*0a6a1f1dSLionel Sambuc     // Upper - Lower
2276*0a6a1f1dSLionel Sambuc     Expr *Upper = TestIsLessOp ? UB : LB;
2277*0a6a1f1dSLionel Sambuc     Expr *Lower = TestIsLessOp ? LB : UB;
2278*0a6a1f1dSLionel Sambuc 
2279*0a6a1f1dSLionel Sambuc     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2280*0a6a1f1dSLionel Sambuc 
2281*0a6a1f1dSLionel Sambuc     if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2282*0a6a1f1dSLionel Sambuc       // BuildBinOp already emitted error, this one is to point user to upper
2283*0a6a1f1dSLionel Sambuc       // and lower bound, and to tell what is passed to 'operator-'.
2284*0a6a1f1dSLionel Sambuc       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2285*0a6a1f1dSLionel Sambuc           << Upper->getSourceRange() << Lower->getSourceRange();
2286*0a6a1f1dSLionel Sambuc       return nullptr;
2287*0a6a1f1dSLionel Sambuc     }
2288*0a6a1f1dSLionel Sambuc   }
2289*0a6a1f1dSLionel Sambuc 
2290*0a6a1f1dSLionel Sambuc   if (!Diff.isUsable())
2291*0a6a1f1dSLionel Sambuc     return nullptr;
2292*0a6a1f1dSLionel Sambuc 
2293*0a6a1f1dSLionel Sambuc   // Upper - Lower [- 1]
2294*0a6a1f1dSLionel Sambuc   if (TestIsStrictOp)
2295*0a6a1f1dSLionel Sambuc     Diff = SemaRef.BuildBinOp(
2296*0a6a1f1dSLionel Sambuc         S, DefaultLoc, BO_Sub, Diff.get(),
2297*0a6a1f1dSLionel Sambuc         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2298*0a6a1f1dSLionel Sambuc   if (!Diff.isUsable())
2299*0a6a1f1dSLionel Sambuc     return nullptr;
2300*0a6a1f1dSLionel Sambuc 
2301*0a6a1f1dSLionel Sambuc   // Upper - Lower [- 1] + Step
2302*0a6a1f1dSLionel Sambuc   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2303*0a6a1f1dSLionel Sambuc                             Step->IgnoreImplicit());
2304*0a6a1f1dSLionel Sambuc   if (!Diff.isUsable())
2305*0a6a1f1dSLionel Sambuc     return nullptr;
2306*0a6a1f1dSLionel Sambuc 
2307*0a6a1f1dSLionel Sambuc   // Parentheses (for dumping/debugging purposes only).
2308*0a6a1f1dSLionel Sambuc   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2309*0a6a1f1dSLionel Sambuc   if (!Diff.isUsable())
2310*0a6a1f1dSLionel Sambuc     return nullptr;
2311*0a6a1f1dSLionel Sambuc 
2312*0a6a1f1dSLionel Sambuc   // (Upper - Lower [- 1] + Step) / Step
2313*0a6a1f1dSLionel Sambuc   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2314*0a6a1f1dSLionel Sambuc                             Step->IgnoreImplicit());
2315*0a6a1f1dSLionel Sambuc   if (!Diff.isUsable())
2316*0a6a1f1dSLionel Sambuc     return nullptr;
2317*0a6a1f1dSLionel Sambuc 
2318*0a6a1f1dSLionel Sambuc   // OpenMP runtime requires 32-bit or 64-bit loop variables.
2319*0a6a1f1dSLionel Sambuc   if (LimitedType) {
2320*0a6a1f1dSLionel Sambuc     auto &C = SemaRef.Context;
2321*0a6a1f1dSLionel Sambuc     QualType Type = Diff.get()->getType();
2322*0a6a1f1dSLionel Sambuc     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2323*0a6a1f1dSLionel Sambuc     if (NewSize != C.getTypeSize(Type)) {
2324*0a6a1f1dSLionel Sambuc       if (NewSize < C.getTypeSize(Type)) {
2325*0a6a1f1dSLionel Sambuc         assert(NewSize == 64 && "incorrect loop var size");
2326*0a6a1f1dSLionel Sambuc         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2327*0a6a1f1dSLionel Sambuc             << InitSrcRange << ConditionSrcRange;
2328*0a6a1f1dSLionel Sambuc       }
2329*0a6a1f1dSLionel Sambuc       QualType NewType = C.getIntTypeForBitwidth(
2330*0a6a1f1dSLionel Sambuc           NewSize, Type->hasSignedIntegerRepresentation());
2331*0a6a1f1dSLionel Sambuc       Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2332*0a6a1f1dSLionel Sambuc                                                Sema::AA_Converting, true);
2333*0a6a1f1dSLionel Sambuc       if (!Diff.isUsable())
2334*0a6a1f1dSLionel Sambuc         return nullptr;
2335*0a6a1f1dSLionel Sambuc     }
2336*0a6a1f1dSLionel Sambuc   }
2337*0a6a1f1dSLionel Sambuc 
2338*0a6a1f1dSLionel Sambuc   return Diff.get();
2339*0a6a1f1dSLionel Sambuc }
2340*0a6a1f1dSLionel Sambuc 
2341*0a6a1f1dSLionel Sambuc /// \brief Build reference expression to the counter be used for codegen.
BuildCounterVar() const2342*0a6a1f1dSLionel Sambuc Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2343*0a6a1f1dSLionel Sambuc   return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2344*0a6a1f1dSLionel Sambuc                              GetIncrementSrcRange().getBegin(), Var, false,
2345*0a6a1f1dSLionel Sambuc                              DefaultLoc, Var->getType(), VK_LValue);
2346*0a6a1f1dSLionel Sambuc }
2347*0a6a1f1dSLionel Sambuc 
2348*0a6a1f1dSLionel Sambuc /// \brief Build initization of the counter be used for codegen.
BuildCounterInit() const2349*0a6a1f1dSLionel Sambuc Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2350*0a6a1f1dSLionel Sambuc 
2351*0a6a1f1dSLionel Sambuc /// \brief Build step of the counter be used for codegen.
BuildCounterStep() const2352*0a6a1f1dSLionel Sambuc Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2353*0a6a1f1dSLionel Sambuc 
2354*0a6a1f1dSLionel Sambuc /// \brief Iteration space of a single for loop.
2355*0a6a1f1dSLionel Sambuc struct LoopIterationSpace {
2356*0a6a1f1dSLionel Sambuc   /// \brief This expression calculates the number of iterations in the loop.
2357*0a6a1f1dSLionel Sambuc   /// It is always possible to calculate it before starting the loop.
2358*0a6a1f1dSLionel Sambuc   Expr *NumIterations;
2359*0a6a1f1dSLionel Sambuc   /// \brief The loop counter variable.
2360*0a6a1f1dSLionel Sambuc   Expr *CounterVar;
2361*0a6a1f1dSLionel Sambuc   /// \brief This is initializer for the initial value of #CounterVar.
2362*0a6a1f1dSLionel Sambuc   Expr *CounterInit;
2363*0a6a1f1dSLionel Sambuc   /// \brief This is step for the #CounterVar used to generate its update:
2364*0a6a1f1dSLionel Sambuc   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2365*0a6a1f1dSLionel Sambuc   Expr *CounterStep;
2366*0a6a1f1dSLionel Sambuc   /// \brief Should step be subtracted?
2367*0a6a1f1dSLionel Sambuc   bool Subtract;
2368*0a6a1f1dSLionel Sambuc   /// \brief Source range of the loop init.
2369*0a6a1f1dSLionel Sambuc   SourceRange InitSrcRange;
2370*0a6a1f1dSLionel Sambuc   /// \brief Source range of the loop condition.
2371*0a6a1f1dSLionel Sambuc   SourceRange CondSrcRange;
2372*0a6a1f1dSLionel Sambuc   /// \brief Source range of the loop increment.
2373*0a6a1f1dSLionel Sambuc   SourceRange IncSrcRange;
2374*0a6a1f1dSLionel Sambuc };
2375*0a6a1f1dSLionel Sambuc 
2376*0a6a1f1dSLionel Sambuc } // namespace
2377*0a6a1f1dSLionel Sambuc 
2378*0a6a1f1dSLionel Sambuc /// \brief Called on a for stmt to check and extract its iteration space
2379*0a6a1f1dSLionel Sambuc /// for further processing (such as collapsing).
CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind,Stmt * S,Sema & SemaRef,DSAStackTy & DSA,unsigned CurrentNestedLoopCount,unsigned NestedLoopCount,Expr * NestedLoopCountExpr,llvm::DenseMap<VarDecl *,Expr * > & VarsWithImplicitDSA,LoopIterationSpace & ResultIterSpace)2380*0a6a1f1dSLionel Sambuc static bool CheckOpenMPIterationSpace(
2381*0a6a1f1dSLionel Sambuc     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2382*0a6a1f1dSLionel Sambuc     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2383*0a6a1f1dSLionel Sambuc     Expr *NestedLoopCountExpr,
2384*0a6a1f1dSLionel Sambuc     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2385*0a6a1f1dSLionel Sambuc     LoopIterationSpace &ResultIterSpace) {
2386*0a6a1f1dSLionel Sambuc   // OpenMP [2.6, Canonical Loop Form]
2387*0a6a1f1dSLionel Sambuc   //   for (init-expr; test-expr; incr-expr) structured-block
2388*0a6a1f1dSLionel Sambuc   auto For = dyn_cast_or_null<ForStmt>(S);
2389*0a6a1f1dSLionel Sambuc   if (!For) {
2390*0a6a1f1dSLionel Sambuc     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
2391*0a6a1f1dSLionel Sambuc         << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2392*0a6a1f1dSLionel Sambuc         << NestedLoopCount << (CurrentNestedLoopCount > 0)
2393*0a6a1f1dSLionel Sambuc         << CurrentNestedLoopCount;
2394*0a6a1f1dSLionel Sambuc     if (NestedLoopCount > 1)
2395*0a6a1f1dSLionel Sambuc       SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2396*0a6a1f1dSLionel Sambuc                    diag::note_omp_collapse_expr)
2397*0a6a1f1dSLionel Sambuc           << NestedLoopCountExpr->getSourceRange();
2398*0a6a1f1dSLionel Sambuc     return true;
2399*0a6a1f1dSLionel Sambuc   }
2400*0a6a1f1dSLionel Sambuc   assert(For->getBody());
2401*0a6a1f1dSLionel Sambuc 
2402*0a6a1f1dSLionel Sambuc   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2403*0a6a1f1dSLionel Sambuc 
2404*0a6a1f1dSLionel Sambuc   // Check init.
2405*0a6a1f1dSLionel Sambuc   auto Init = For->getInit();
2406*0a6a1f1dSLionel Sambuc   if (ISC.CheckInit(Init)) {
2407*0a6a1f1dSLionel Sambuc     return true;
2408*0a6a1f1dSLionel Sambuc   }
2409*0a6a1f1dSLionel Sambuc 
2410*0a6a1f1dSLionel Sambuc   bool HasErrors = false;
2411*0a6a1f1dSLionel Sambuc 
2412*0a6a1f1dSLionel Sambuc   // Check loop variable's type.
2413*0a6a1f1dSLionel Sambuc   auto Var = ISC.GetLoopVar();
2414*0a6a1f1dSLionel Sambuc 
2415*0a6a1f1dSLionel Sambuc   // OpenMP [2.6, Canonical Loop Form]
2416*0a6a1f1dSLionel Sambuc   // Var is one of the following:
2417*0a6a1f1dSLionel Sambuc   //   A variable of signed or unsigned integer type.
2418*0a6a1f1dSLionel Sambuc   //   For C++, a variable of a random access iterator type.
2419*0a6a1f1dSLionel Sambuc   //   For C, a variable of a pointer type.
2420*0a6a1f1dSLionel Sambuc   auto VarType = Var->getType();
2421*0a6a1f1dSLionel Sambuc   if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2422*0a6a1f1dSLionel Sambuc       !VarType->isPointerType() &&
2423*0a6a1f1dSLionel Sambuc       !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2424*0a6a1f1dSLionel Sambuc     SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2425*0a6a1f1dSLionel Sambuc         << SemaRef.getLangOpts().CPlusPlus;
2426*0a6a1f1dSLionel Sambuc     HasErrors = true;
2427*0a6a1f1dSLionel Sambuc   }
2428*0a6a1f1dSLionel Sambuc 
2429*0a6a1f1dSLionel Sambuc   // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2430*0a6a1f1dSLionel Sambuc   // Construct
2431*0a6a1f1dSLionel Sambuc   // The loop iteration variable(s) in the associated for-loop(s) of a for or
2432*0a6a1f1dSLionel Sambuc   // parallel for construct is (are) private.
2433*0a6a1f1dSLionel Sambuc   // The loop iteration variable in the associated for-loop of a simd construct
2434*0a6a1f1dSLionel Sambuc   // with just one associated for-loop is linear with a constant-linear-step
2435*0a6a1f1dSLionel Sambuc   // that is the increment of the associated for-loop.
2436*0a6a1f1dSLionel Sambuc   // Exclude loop var from the list of variables with implicitly defined data
2437*0a6a1f1dSLionel Sambuc   // sharing attributes.
2438*0a6a1f1dSLionel Sambuc   VarsWithImplicitDSA.erase(Var);
2439*0a6a1f1dSLionel Sambuc 
2440*0a6a1f1dSLionel Sambuc   // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2441*0a6a1f1dSLionel Sambuc   // a Construct, C/C++].
2442*0a6a1f1dSLionel Sambuc   // The loop iteration variable in the associated for-loop of a simd construct
2443*0a6a1f1dSLionel Sambuc   // with just one associated for-loop may be listed in a linear clause with a
2444*0a6a1f1dSLionel Sambuc   // constant-linear-step that is the increment of the associated for-loop.
2445*0a6a1f1dSLionel Sambuc   // The loop iteration variable(s) in the associated for-loop(s) of a for or
2446*0a6a1f1dSLionel Sambuc   // parallel for construct may be listed in a private or lastprivate clause.
2447*0a6a1f1dSLionel Sambuc   DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
2448*0a6a1f1dSLionel Sambuc   auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2449*0a6a1f1dSLionel Sambuc   // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2450*0a6a1f1dSLionel Sambuc   // declared in the loop and it is predetermined as a private.
2451*0a6a1f1dSLionel Sambuc   auto PredeterminedCKind =
2452*0a6a1f1dSLionel Sambuc       isOpenMPSimdDirective(DKind)
2453*0a6a1f1dSLionel Sambuc           ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2454*0a6a1f1dSLionel Sambuc           : OMPC_private;
2455*0a6a1f1dSLionel Sambuc   if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
2456*0a6a1f1dSLionel Sambuc         DVar.CKind != PredeterminedCKind) ||
2457*0a6a1f1dSLionel Sambuc        (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2458*0a6a1f1dSLionel Sambuc         DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2459*0a6a1f1dSLionel Sambuc         DVar.CKind != OMPC_lastprivate)) &&
2460*0a6a1f1dSLionel Sambuc       (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2461*0a6a1f1dSLionel Sambuc     SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
2462*0a6a1f1dSLionel Sambuc         << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2463*0a6a1f1dSLionel Sambuc         << getOpenMPClauseName(PredeterminedCKind);
2464*0a6a1f1dSLionel Sambuc     ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
2465*0a6a1f1dSLionel Sambuc     HasErrors = true;
2466*0a6a1f1dSLionel Sambuc   } else if (LoopVarRefExpr != nullptr) {
2467*0a6a1f1dSLionel Sambuc     // Make the loop iteration variable private (for worksharing constructs),
2468*0a6a1f1dSLionel Sambuc     // linear (for simd directives with the only one associated loop) or
2469*0a6a1f1dSLionel Sambuc     // lastprivate (for simd directives with several collapsed loops).
2470*0a6a1f1dSLionel Sambuc     // FIXME: the next check and error message must be removed once the
2471*0a6a1f1dSLionel Sambuc     // capturing of global variables in loops is fixed.
2472*0a6a1f1dSLionel Sambuc     if (DVar.CKind == OMPC_unknown)
2473*0a6a1f1dSLionel Sambuc       DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2474*0a6a1f1dSLionel Sambuc                         /*FromParent=*/false);
2475*0a6a1f1dSLionel Sambuc     if (!Var->hasLocalStorage() && DVar.CKind == OMPC_unknown) {
2476*0a6a1f1dSLionel Sambuc       SemaRef.Diag(Init->getLocStart(), diag::err_omp_global_loop_var_dsa)
2477*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(PredeterminedCKind)
2478*0a6a1f1dSLionel Sambuc           << getOpenMPDirectiveName(DKind);
2479*0a6a1f1dSLionel Sambuc       HasErrors = true;
2480*0a6a1f1dSLionel Sambuc     } else
2481*0a6a1f1dSLionel Sambuc       DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
2482*0a6a1f1dSLionel Sambuc   }
2483*0a6a1f1dSLionel Sambuc 
2484*0a6a1f1dSLionel Sambuc   assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
2485*0a6a1f1dSLionel Sambuc 
2486*0a6a1f1dSLionel Sambuc   // Check test-expr.
2487*0a6a1f1dSLionel Sambuc   HasErrors |= ISC.CheckCond(For->getCond());
2488*0a6a1f1dSLionel Sambuc 
2489*0a6a1f1dSLionel Sambuc   // Check incr-expr.
2490*0a6a1f1dSLionel Sambuc   HasErrors |= ISC.CheckInc(For->getInc());
2491*0a6a1f1dSLionel Sambuc 
2492*0a6a1f1dSLionel Sambuc   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
2493*0a6a1f1dSLionel Sambuc     return HasErrors;
2494*0a6a1f1dSLionel Sambuc 
2495*0a6a1f1dSLionel Sambuc   // Build the loop's iteration space representation.
2496*0a6a1f1dSLionel Sambuc   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2497*0a6a1f1dSLionel Sambuc       DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
2498*0a6a1f1dSLionel Sambuc   ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2499*0a6a1f1dSLionel Sambuc   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2500*0a6a1f1dSLionel Sambuc   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2501*0a6a1f1dSLionel Sambuc   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2502*0a6a1f1dSLionel Sambuc   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2503*0a6a1f1dSLionel Sambuc   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2504*0a6a1f1dSLionel Sambuc   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2505*0a6a1f1dSLionel Sambuc 
2506*0a6a1f1dSLionel Sambuc   HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2507*0a6a1f1dSLionel Sambuc                 ResultIterSpace.CounterVar == nullptr ||
2508*0a6a1f1dSLionel Sambuc                 ResultIterSpace.CounterInit == nullptr ||
2509*0a6a1f1dSLionel Sambuc                 ResultIterSpace.CounterStep == nullptr);
2510*0a6a1f1dSLionel Sambuc 
2511*0a6a1f1dSLionel Sambuc   return HasErrors;
2512*0a6a1f1dSLionel Sambuc }
2513*0a6a1f1dSLionel Sambuc 
2514*0a6a1f1dSLionel Sambuc /// \brief Build a variable declaration for OpenMP loop iteration variable.
BuildVarDecl(Sema & SemaRef,SourceLocation Loc,QualType Type,StringRef Name)2515*0a6a1f1dSLionel Sambuc static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2516*0a6a1f1dSLionel Sambuc                              StringRef Name) {
2517*0a6a1f1dSLionel Sambuc   DeclContext *DC = SemaRef.CurContext;
2518*0a6a1f1dSLionel Sambuc   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2519*0a6a1f1dSLionel Sambuc   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2520*0a6a1f1dSLionel Sambuc   VarDecl *Decl =
2521*0a6a1f1dSLionel Sambuc       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2522*0a6a1f1dSLionel Sambuc   Decl->setImplicit();
2523*0a6a1f1dSLionel Sambuc   return Decl;
2524*0a6a1f1dSLionel Sambuc }
2525*0a6a1f1dSLionel Sambuc 
2526*0a6a1f1dSLionel Sambuc /// \brief Build 'VarRef = Start + Iter * Step'.
BuildCounterUpdate(Sema & SemaRef,Scope * S,SourceLocation Loc,ExprResult VarRef,ExprResult Start,ExprResult Iter,ExprResult Step,bool Subtract)2527*0a6a1f1dSLionel Sambuc static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2528*0a6a1f1dSLionel Sambuc                                      SourceLocation Loc, ExprResult VarRef,
2529*0a6a1f1dSLionel Sambuc                                      ExprResult Start, ExprResult Iter,
2530*0a6a1f1dSLionel Sambuc                                      ExprResult Step, bool Subtract) {
2531*0a6a1f1dSLionel Sambuc   // Add parentheses (for debugging purposes only).
2532*0a6a1f1dSLionel Sambuc   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2533*0a6a1f1dSLionel Sambuc   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2534*0a6a1f1dSLionel Sambuc       !Step.isUsable())
2535*0a6a1f1dSLionel Sambuc     return ExprError();
2536*0a6a1f1dSLionel Sambuc 
2537*0a6a1f1dSLionel Sambuc   ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2538*0a6a1f1dSLionel Sambuc                                          Step.get()->IgnoreImplicit());
2539*0a6a1f1dSLionel Sambuc   if (!Update.isUsable())
2540*0a6a1f1dSLionel Sambuc     return ExprError();
2541*0a6a1f1dSLionel Sambuc 
2542*0a6a1f1dSLionel Sambuc   // Build 'VarRef = Start + Iter * Step'.
2543*0a6a1f1dSLionel Sambuc   Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2544*0a6a1f1dSLionel Sambuc                               Start.get()->IgnoreImplicit(), Update.get());
2545*0a6a1f1dSLionel Sambuc   if (!Update.isUsable())
2546*0a6a1f1dSLionel Sambuc     return ExprError();
2547*0a6a1f1dSLionel Sambuc 
2548*0a6a1f1dSLionel Sambuc   Update = SemaRef.PerformImplicitConversion(
2549*0a6a1f1dSLionel Sambuc       Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2550*0a6a1f1dSLionel Sambuc   if (!Update.isUsable())
2551*0a6a1f1dSLionel Sambuc     return ExprError();
2552*0a6a1f1dSLionel Sambuc 
2553*0a6a1f1dSLionel Sambuc   Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2554*0a6a1f1dSLionel Sambuc   return Update;
2555*0a6a1f1dSLionel Sambuc }
2556*0a6a1f1dSLionel Sambuc 
2557*0a6a1f1dSLionel Sambuc /// \brief Convert integer expression \a E to make it have at least \a Bits
2558*0a6a1f1dSLionel Sambuc /// bits.
WidenIterationCount(unsigned Bits,Expr * E,Sema & SemaRef)2559*0a6a1f1dSLionel Sambuc static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2560*0a6a1f1dSLionel Sambuc                                       Sema &SemaRef) {
2561*0a6a1f1dSLionel Sambuc   if (E == nullptr)
2562*0a6a1f1dSLionel Sambuc     return ExprError();
2563*0a6a1f1dSLionel Sambuc   auto &C = SemaRef.Context;
2564*0a6a1f1dSLionel Sambuc   QualType OldType = E->getType();
2565*0a6a1f1dSLionel Sambuc   unsigned HasBits = C.getTypeSize(OldType);
2566*0a6a1f1dSLionel Sambuc   if (HasBits >= Bits)
2567*0a6a1f1dSLionel Sambuc     return ExprResult(E);
2568*0a6a1f1dSLionel Sambuc   // OK to convert to signed, because new type has more bits than old.
2569*0a6a1f1dSLionel Sambuc   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2570*0a6a1f1dSLionel Sambuc   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2571*0a6a1f1dSLionel Sambuc                                            true);
2572*0a6a1f1dSLionel Sambuc }
2573*0a6a1f1dSLionel Sambuc 
2574*0a6a1f1dSLionel Sambuc /// \brief Check if the given expression \a E is a constant integer that fits
2575*0a6a1f1dSLionel Sambuc /// into \a Bits bits.
FitsInto(unsigned Bits,bool Signed,Expr * E,Sema & SemaRef)2576*0a6a1f1dSLionel Sambuc static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2577*0a6a1f1dSLionel Sambuc   if (E == nullptr)
2578*0a6a1f1dSLionel Sambuc     return false;
2579*0a6a1f1dSLionel Sambuc   llvm::APSInt Result;
2580*0a6a1f1dSLionel Sambuc   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2581*0a6a1f1dSLionel Sambuc     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2582*0a6a1f1dSLionel Sambuc   return false;
2583*0a6a1f1dSLionel Sambuc }
2584*0a6a1f1dSLionel Sambuc 
2585*0a6a1f1dSLionel Sambuc /// \brief Called on a for stmt to check itself and nested loops (if any).
2586*0a6a1f1dSLionel Sambuc /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2587*0a6a1f1dSLionel Sambuc /// number of collapsed loops otherwise.
2588*0a6a1f1dSLionel Sambuc static unsigned
CheckOpenMPLoop(OpenMPDirectiveKind DKind,Expr * NestedLoopCountExpr,Stmt * AStmt,Sema & SemaRef,DSAStackTy & DSA,llvm::DenseMap<VarDecl *,Expr * > & VarsWithImplicitDSA,OMPLoopDirective::HelperExprs & Built)2589*0a6a1f1dSLionel Sambuc CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2590*0a6a1f1dSLionel Sambuc                 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2591*0a6a1f1dSLionel Sambuc                 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2592*0a6a1f1dSLionel Sambuc                 OMPLoopDirective::HelperExprs &Built) {
2593*0a6a1f1dSLionel Sambuc   unsigned NestedLoopCount = 1;
2594*0a6a1f1dSLionel Sambuc   if (NestedLoopCountExpr) {
2595*0a6a1f1dSLionel Sambuc     // Found 'collapse' clause - calculate collapse number.
2596*0a6a1f1dSLionel Sambuc     llvm::APSInt Result;
2597*0a6a1f1dSLionel Sambuc     if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2598*0a6a1f1dSLionel Sambuc       NestedLoopCount = Result.getLimitedValue();
2599*0a6a1f1dSLionel Sambuc   }
2600*0a6a1f1dSLionel Sambuc   // This is helper routine for loop directives (e.g., 'for', 'simd',
2601*0a6a1f1dSLionel Sambuc   // 'for simd', etc.).
2602*0a6a1f1dSLionel Sambuc   SmallVector<LoopIterationSpace, 4> IterSpaces;
2603*0a6a1f1dSLionel Sambuc   IterSpaces.resize(NestedLoopCount);
2604*0a6a1f1dSLionel Sambuc   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
2605*0a6a1f1dSLionel Sambuc   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
2606*0a6a1f1dSLionel Sambuc     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
2607*0a6a1f1dSLionel Sambuc                                   NestedLoopCount, NestedLoopCountExpr,
2608*0a6a1f1dSLionel Sambuc                                   VarsWithImplicitDSA, IterSpaces[Cnt]))
2609*0a6a1f1dSLionel Sambuc       return 0;
2610*0a6a1f1dSLionel Sambuc     // Move on to the next nested for loop, or to the loop body.
2611*0a6a1f1dSLionel Sambuc     // OpenMP [2.8.1, simd construct, Restrictions]
2612*0a6a1f1dSLionel Sambuc     // All loops associated with the construct must be perfectly nested; that
2613*0a6a1f1dSLionel Sambuc     // is, there must be no intervening code nor any OpenMP directive between
2614*0a6a1f1dSLionel Sambuc     // any two loops.
2615*0a6a1f1dSLionel Sambuc     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
2616*0a6a1f1dSLionel Sambuc   }
2617*0a6a1f1dSLionel Sambuc 
2618*0a6a1f1dSLionel Sambuc   Built.clear(/* size */ NestedLoopCount);
2619*0a6a1f1dSLionel Sambuc 
2620*0a6a1f1dSLionel Sambuc   if (SemaRef.CurContext->isDependentContext())
2621*0a6a1f1dSLionel Sambuc     return NestedLoopCount;
2622*0a6a1f1dSLionel Sambuc 
2623*0a6a1f1dSLionel Sambuc   // An example of what is generated for the following code:
2624*0a6a1f1dSLionel Sambuc   //
2625*0a6a1f1dSLionel Sambuc   //   #pragma omp simd collapse(2)
2626*0a6a1f1dSLionel Sambuc   //   for (i = 0; i < NI; ++i)
2627*0a6a1f1dSLionel Sambuc   //     for (j = J0; j < NJ; j+=2) {
2628*0a6a1f1dSLionel Sambuc   //     <loop body>
2629*0a6a1f1dSLionel Sambuc   //   }
2630*0a6a1f1dSLionel Sambuc   //
2631*0a6a1f1dSLionel Sambuc   // We generate the code below.
2632*0a6a1f1dSLionel Sambuc   // Note: the loop body may be outlined in CodeGen.
2633*0a6a1f1dSLionel Sambuc   // Note: some counters may be C++ classes, operator- is used to find number of
2634*0a6a1f1dSLionel Sambuc   // iterations and operator+= to calculate counter value.
2635*0a6a1f1dSLionel Sambuc   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2636*0a6a1f1dSLionel Sambuc   // or i64 is currently supported).
2637*0a6a1f1dSLionel Sambuc   //
2638*0a6a1f1dSLionel Sambuc   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2639*0a6a1f1dSLionel Sambuc   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2640*0a6a1f1dSLionel Sambuc   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2641*0a6a1f1dSLionel Sambuc   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2642*0a6a1f1dSLionel Sambuc   //     // similar updates for vars in clauses (e.g. 'linear')
2643*0a6a1f1dSLionel Sambuc   //     <loop body (using local i and j)>
2644*0a6a1f1dSLionel Sambuc   //   }
2645*0a6a1f1dSLionel Sambuc   //   i = NI; // assign final values of counters
2646*0a6a1f1dSLionel Sambuc   //   j = NJ;
2647*0a6a1f1dSLionel Sambuc   //
2648*0a6a1f1dSLionel Sambuc 
2649*0a6a1f1dSLionel Sambuc   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2650*0a6a1f1dSLionel Sambuc   // the iteration counts of the collapsed for loops.
2651*0a6a1f1dSLionel Sambuc   auto N0 = IterSpaces[0].NumIterations;
2652*0a6a1f1dSLionel Sambuc   ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2653*0a6a1f1dSLionel Sambuc   ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2654*0a6a1f1dSLionel Sambuc 
2655*0a6a1f1dSLionel Sambuc   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2656*0a6a1f1dSLionel Sambuc     return NestedLoopCount;
2657*0a6a1f1dSLionel Sambuc 
2658*0a6a1f1dSLionel Sambuc   auto &C = SemaRef.Context;
2659*0a6a1f1dSLionel Sambuc   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2660*0a6a1f1dSLionel Sambuc 
2661*0a6a1f1dSLionel Sambuc   Scope *CurScope = DSA.getCurScope();
2662*0a6a1f1dSLionel Sambuc   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2663*0a6a1f1dSLionel Sambuc     auto N = IterSpaces[Cnt].NumIterations;
2664*0a6a1f1dSLionel Sambuc     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2665*0a6a1f1dSLionel Sambuc     if (LastIteration32.isUsable())
2666*0a6a1f1dSLionel Sambuc       LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2667*0a6a1f1dSLionel Sambuc                                            LastIteration32.get(), N);
2668*0a6a1f1dSLionel Sambuc     if (LastIteration64.isUsable())
2669*0a6a1f1dSLionel Sambuc       LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2670*0a6a1f1dSLionel Sambuc                                            LastIteration64.get(), N);
2671*0a6a1f1dSLionel Sambuc   }
2672*0a6a1f1dSLionel Sambuc 
2673*0a6a1f1dSLionel Sambuc   // Choose either the 32-bit or 64-bit version.
2674*0a6a1f1dSLionel Sambuc   ExprResult LastIteration = LastIteration64;
2675*0a6a1f1dSLionel Sambuc   if (LastIteration32.isUsable() &&
2676*0a6a1f1dSLionel Sambuc       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2677*0a6a1f1dSLionel Sambuc       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2678*0a6a1f1dSLionel Sambuc        FitsInto(
2679*0a6a1f1dSLionel Sambuc            32 /* Bits */,
2680*0a6a1f1dSLionel Sambuc            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2681*0a6a1f1dSLionel Sambuc            LastIteration64.get(), SemaRef)))
2682*0a6a1f1dSLionel Sambuc     LastIteration = LastIteration32;
2683*0a6a1f1dSLionel Sambuc 
2684*0a6a1f1dSLionel Sambuc   if (!LastIteration.isUsable())
2685*0a6a1f1dSLionel Sambuc     return 0;
2686*0a6a1f1dSLionel Sambuc 
2687*0a6a1f1dSLionel Sambuc   // Save the number of iterations.
2688*0a6a1f1dSLionel Sambuc   ExprResult NumIterations = LastIteration;
2689*0a6a1f1dSLionel Sambuc   {
2690*0a6a1f1dSLionel Sambuc     LastIteration = SemaRef.BuildBinOp(
2691*0a6a1f1dSLionel Sambuc         CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2692*0a6a1f1dSLionel Sambuc         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2693*0a6a1f1dSLionel Sambuc     if (!LastIteration.isUsable())
2694*0a6a1f1dSLionel Sambuc       return 0;
2695*0a6a1f1dSLionel Sambuc   }
2696*0a6a1f1dSLionel Sambuc 
2697*0a6a1f1dSLionel Sambuc   // Calculate the last iteration number beforehand instead of doing this on
2698*0a6a1f1dSLionel Sambuc   // each iteration. Do not do this if the number of iterations may be kfold-ed.
2699*0a6a1f1dSLionel Sambuc   llvm::APSInt Result;
2700*0a6a1f1dSLionel Sambuc   bool IsConstant =
2701*0a6a1f1dSLionel Sambuc       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2702*0a6a1f1dSLionel Sambuc   ExprResult CalcLastIteration;
2703*0a6a1f1dSLionel Sambuc   if (!IsConstant) {
2704*0a6a1f1dSLionel Sambuc     SourceLocation SaveLoc;
2705*0a6a1f1dSLionel Sambuc     VarDecl *SaveVar =
2706*0a6a1f1dSLionel Sambuc         BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2707*0a6a1f1dSLionel Sambuc                      ".omp.last.iteration");
2708*0a6a1f1dSLionel Sambuc     ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2709*0a6a1f1dSLionel Sambuc         SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2710*0a6a1f1dSLionel Sambuc     CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2711*0a6a1f1dSLionel Sambuc                                            SaveRef.get(), LastIteration.get());
2712*0a6a1f1dSLionel Sambuc     LastIteration = SaveRef;
2713*0a6a1f1dSLionel Sambuc 
2714*0a6a1f1dSLionel Sambuc     // Prepare SaveRef + 1.
2715*0a6a1f1dSLionel Sambuc     NumIterations = SemaRef.BuildBinOp(
2716*0a6a1f1dSLionel Sambuc         CurScope, SaveLoc, BO_Add, SaveRef.get(),
2717*0a6a1f1dSLionel Sambuc         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2718*0a6a1f1dSLionel Sambuc     if (!NumIterations.isUsable())
2719*0a6a1f1dSLionel Sambuc       return 0;
2720*0a6a1f1dSLionel Sambuc   }
2721*0a6a1f1dSLionel Sambuc 
2722*0a6a1f1dSLionel Sambuc   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2723*0a6a1f1dSLionel Sambuc 
2724*0a6a1f1dSLionel Sambuc   // Precondition tests if there is at least one iteration (LastIteration > 0).
2725*0a6a1f1dSLionel Sambuc   ExprResult PreCond = SemaRef.BuildBinOp(
2726*0a6a1f1dSLionel Sambuc       CurScope, InitLoc, BO_GT, LastIteration.get(),
2727*0a6a1f1dSLionel Sambuc       SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2728*0a6a1f1dSLionel Sambuc 
2729*0a6a1f1dSLionel Sambuc   QualType VType = LastIteration.get()->getType();
2730*0a6a1f1dSLionel Sambuc   // Build variables passed into runtime, nesessary for worksharing directives.
2731*0a6a1f1dSLionel Sambuc   ExprResult LB, UB, IL, ST, EUB;
2732*0a6a1f1dSLionel Sambuc   if (isOpenMPWorksharingDirective(DKind)) {
2733*0a6a1f1dSLionel Sambuc     // Lower bound variable, initialized with zero.
2734*0a6a1f1dSLionel Sambuc     VarDecl *LBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
2735*0a6a1f1dSLionel Sambuc     LB = SemaRef.BuildDeclRefExpr(LBDecl, VType, VK_LValue, InitLoc);
2736*0a6a1f1dSLionel Sambuc     SemaRef.AddInitializerToDecl(
2737*0a6a1f1dSLionel Sambuc         LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2738*0a6a1f1dSLionel Sambuc         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2739*0a6a1f1dSLionel Sambuc 
2740*0a6a1f1dSLionel Sambuc     // Upper bound variable, initialized with last iteration number.
2741*0a6a1f1dSLionel Sambuc     VarDecl *UBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
2742*0a6a1f1dSLionel Sambuc     UB = SemaRef.BuildDeclRefExpr(UBDecl, VType, VK_LValue, InitLoc);
2743*0a6a1f1dSLionel Sambuc     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
2744*0a6a1f1dSLionel Sambuc                                  /*DirectInit*/ false,
2745*0a6a1f1dSLionel Sambuc                                  /*TypeMayContainAuto*/ false);
2746*0a6a1f1dSLionel Sambuc 
2747*0a6a1f1dSLionel Sambuc     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
2748*0a6a1f1dSLionel Sambuc     // This will be used to implement clause 'lastprivate'.
2749*0a6a1f1dSLionel Sambuc     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
2750*0a6a1f1dSLionel Sambuc     VarDecl *ILDecl = BuildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
2751*0a6a1f1dSLionel Sambuc     IL = SemaRef.BuildDeclRefExpr(ILDecl, Int32Ty, VK_LValue, InitLoc);
2752*0a6a1f1dSLionel Sambuc     SemaRef.AddInitializerToDecl(
2753*0a6a1f1dSLionel Sambuc         ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2754*0a6a1f1dSLionel Sambuc         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2755*0a6a1f1dSLionel Sambuc 
2756*0a6a1f1dSLionel Sambuc     // Stride variable returned by runtime (we initialize it to 1 by default).
2757*0a6a1f1dSLionel Sambuc     VarDecl *STDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
2758*0a6a1f1dSLionel Sambuc     ST = SemaRef.BuildDeclRefExpr(STDecl, VType, VK_LValue, InitLoc);
2759*0a6a1f1dSLionel Sambuc     SemaRef.AddInitializerToDecl(
2760*0a6a1f1dSLionel Sambuc         STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
2761*0a6a1f1dSLionel Sambuc         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2762*0a6a1f1dSLionel Sambuc 
2763*0a6a1f1dSLionel Sambuc     // Build expression: UB = min(UB, LastIteration)
2764*0a6a1f1dSLionel Sambuc     // It is nesessary for CodeGen of directives with static scheduling.
2765*0a6a1f1dSLionel Sambuc     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
2766*0a6a1f1dSLionel Sambuc                                                 UB.get(), LastIteration.get());
2767*0a6a1f1dSLionel Sambuc     ExprResult CondOp = SemaRef.ActOnConditionalOp(
2768*0a6a1f1dSLionel Sambuc         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
2769*0a6a1f1dSLionel Sambuc     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
2770*0a6a1f1dSLionel Sambuc                              CondOp.get());
2771*0a6a1f1dSLionel Sambuc     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
2772*0a6a1f1dSLionel Sambuc   }
2773*0a6a1f1dSLionel Sambuc 
2774*0a6a1f1dSLionel Sambuc   // Build the iteration variable and its initialization before loop.
2775*0a6a1f1dSLionel Sambuc   ExprResult IV;
2776*0a6a1f1dSLionel Sambuc   ExprResult Init;
2777*0a6a1f1dSLionel Sambuc   {
2778*0a6a1f1dSLionel Sambuc     VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
2779*0a6a1f1dSLionel Sambuc     IV = SemaRef.BuildDeclRefExpr(IVDecl, VType, VK_LValue, InitLoc);
2780*0a6a1f1dSLionel Sambuc     Expr *RHS = isOpenMPWorksharingDirective(DKind)
2781*0a6a1f1dSLionel Sambuc                     ? LB.get()
2782*0a6a1f1dSLionel Sambuc                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
2783*0a6a1f1dSLionel Sambuc     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
2784*0a6a1f1dSLionel Sambuc     Init = SemaRef.ActOnFinishFullExpr(Init.get());
2785*0a6a1f1dSLionel Sambuc   }
2786*0a6a1f1dSLionel Sambuc 
2787*0a6a1f1dSLionel Sambuc   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
2788*0a6a1f1dSLionel Sambuc   SourceLocation CondLoc;
2789*0a6a1f1dSLionel Sambuc   ExprResult Cond =
2790*0a6a1f1dSLionel Sambuc       isOpenMPWorksharingDirective(DKind)
2791*0a6a1f1dSLionel Sambuc           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
2792*0a6a1f1dSLionel Sambuc           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2793*0a6a1f1dSLionel Sambuc                                NumIterations.get());
2794*0a6a1f1dSLionel Sambuc   // Loop condition with 1 iteration separated (IV < LastIteration)
2795*0a6a1f1dSLionel Sambuc   ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2796*0a6a1f1dSLionel Sambuc                                                 IV.get(), LastIteration.get());
2797*0a6a1f1dSLionel Sambuc 
2798*0a6a1f1dSLionel Sambuc   // Loop increment (IV = IV + 1)
2799*0a6a1f1dSLionel Sambuc   SourceLocation IncLoc;
2800*0a6a1f1dSLionel Sambuc   ExprResult Inc =
2801*0a6a1f1dSLionel Sambuc       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2802*0a6a1f1dSLionel Sambuc                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2803*0a6a1f1dSLionel Sambuc   if (!Inc.isUsable())
2804*0a6a1f1dSLionel Sambuc     return 0;
2805*0a6a1f1dSLionel Sambuc   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
2806*0a6a1f1dSLionel Sambuc   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
2807*0a6a1f1dSLionel Sambuc   if (!Inc.isUsable())
2808*0a6a1f1dSLionel Sambuc     return 0;
2809*0a6a1f1dSLionel Sambuc 
2810*0a6a1f1dSLionel Sambuc   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
2811*0a6a1f1dSLionel Sambuc   // Used for directives with static scheduling.
2812*0a6a1f1dSLionel Sambuc   ExprResult NextLB, NextUB;
2813*0a6a1f1dSLionel Sambuc   if (isOpenMPWorksharingDirective(DKind)) {
2814*0a6a1f1dSLionel Sambuc     // LB + ST
2815*0a6a1f1dSLionel Sambuc     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
2816*0a6a1f1dSLionel Sambuc     if (!NextLB.isUsable())
2817*0a6a1f1dSLionel Sambuc       return 0;
2818*0a6a1f1dSLionel Sambuc     // LB = LB + ST
2819*0a6a1f1dSLionel Sambuc     NextLB =
2820*0a6a1f1dSLionel Sambuc         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
2821*0a6a1f1dSLionel Sambuc     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
2822*0a6a1f1dSLionel Sambuc     if (!NextLB.isUsable())
2823*0a6a1f1dSLionel Sambuc       return 0;
2824*0a6a1f1dSLionel Sambuc     // UB + ST
2825*0a6a1f1dSLionel Sambuc     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
2826*0a6a1f1dSLionel Sambuc     if (!NextUB.isUsable())
2827*0a6a1f1dSLionel Sambuc       return 0;
2828*0a6a1f1dSLionel Sambuc     // UB = UB + ST
2829*0a6a1f1dSLionel Sambuc     NextUB =
2830*0a6a1f1dSLionel Sambuc         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
2831*0a6a1f1dSLionel Sambuc     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
2832*0a6a1f1dSLionel Sambuc     if (!NextUB.isUsable())
2833*0a6a1f1dSLionel Sambuc       return 0;
2834*0a6a1f1dSLionel Sambuc   }
2835*0a6a1f1dSLionel Sambuc 
2836*0a6a1f1dSLionel Sambuc   // Build updates and final values of the loop counters.
2837*0a6a1f1dSLionel Sambuc   bool HasErrors = false;
2838*0a6a1f1dSLionel Sambuc   Built.Counters.resize(NestedLoopCount);
2839*0a6a1f1dSLionel Sambuc   Built.Updates.resize(NestedLoopCount);
2840*0a6a1f1dSLionel Sambuc   Built.Finals.resize(NestedLoopCount);
2841*0a6a1f1dSLionel Sambuc   {
2842*0a6a1f1dSLionel Sambuc     ExprResult Div;
2843*0a6a1f1dSLionel Sambuc     // Go from inner nested loop to outer.
2844*0a6a1f1dSLionel Sambuc     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2845*0a6a1f1dSLionel Sambuc       LoopIterationSpace &IS = IterSpaces[Cnt];
2846*0a6a1f1dSLionel Sambuc       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2847*0a6a1f1dSLionel Sambuc       // Build: Iter = (IV / Div) % IS.NumIters
2848*0a6a1f1dSLionel Sambuc       // where Div is product of previous iterations' IS.NumIters.
2849*0a6a1f1dSLionel Sambuc       ExprResult Iter;
2850*0a6a1f1dSLionel Sambuc       if (Div.isUsable()) {
2851*0a6a1f1dSLionel Sambuc         Iter =
2852*0a6a1f1dSLionel Sambuc             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2853*0a6a1f1dSLionel Sambuc       } else {
2854*0a6a1f1dSLionel Sambuc         Iter = IV;
2855*0a6a1f1dSLionel Sambuc         assert((Cnt == (int)NestedLoopCount - 1) &&
2856*0a6a1f1dSLionel Sambuc                "unusable div expected on first iteration only");
2857*0a6a1f1dSLionel Sambuc       }
2858*0a6a1f1dSLionel Sambuc 
2859*0a6a1f1dSLionel Sambuc       if (Cnt != 0 && Iter.isUsable())
2860*0a6a1f1dSLionel Sambuc         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2861*0a6a1f1dSLionel Sambuc                                   IS.NumIterations);
2862*0a6a1f1dSLionel Sambuc       if (!Iter.isUsable()) {
2863*0a6a1f1dSLionel Sambuc         HasErrors = true;
2864*0a6a1f1dSLionel Sambuc         break;
2865*0a6a1f1dSLionel Sambuc       }
2866*0a6a1f1dSLionel Sambuc 
2867*0a6a1f1dSLionel Sambuc       // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2868*0a6a1f1dSLionel Sambuc       ExprResult Update =
2869*0a6a1f1dSLionel Sambuc           BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2870*0a6a1f1dSLionel Sambuc                              IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2871*0a6a1f1dSLionel Sambuc       if (!Update.isUsable()) {
2872*0a6a1f1dSLionel Sambuc         HasErrors = true;
2873*0a6a1f1dSLionel Sambuc         break;
2874*0a6a1f1dSLionel Sambuc       }
2875*0a6a1f1dSLionel Sambuc 
2876*0a6a1f1dSLionel Sambuc       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2877*0a6a1f1dSLionel Sambuc       ExprResult Final = BuildCounterUpdate(
2878*0a6a1f1dSLionel Sambuc           SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2879*0a6a1f1dSLionel Sambuc           IS.NumIterations, IS.CounterStep, IS.Subtract);
2880*0a6a1f1dSLionel Sambuc       if (!Final.isUsable()) {
2881*0a6a1f1dSLionel Sambuc         HasErrors = true;
2882*0a6a1f1dSLionel Sambuc         break;
2883*0a6a1f1dSLionel Sambuc       }
2884*0a6a1f1dSLionel Sambuc 
2885*0a6a1f1dSLionel Sambuc       // Build Div for the next iteration: Div <- Div * IS.NumIters
2886*0a6a1f1dSLionel Sambuc       if (Cnt != 0) {
2887*0a6a1f1dSLionel Sambuc         if (Div.isUnset())
2888*0a6a1f1dSLionel Sambuc           Div = IS.NumIterations;
2889*0a6a1f1dSLionel Sambuc         else
2890*0a6a1f1dSLionel Sambuc           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2891*0a6a1f1dSLionel Sambuc                                    IS.NumIterations);
2892*0a6a1f1dSLionel Sambuc 
2893*0a6a1f1dSLionel Sambuc         // Add parentheses (for debugging purposes only).
2894*0a6a1f1dSLionel Sambuc         if (Div.isUsable())
2895*0a6a1f1dSLionel Sambuc           Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2896*0a6a1f1dSLionel Sambuc         if (!Div.isUsable()) {
2897*0a6a1f1dSLionel Sambuc           HasErrors = true;
2898*0a6a1f1dSLionel Sambuc           break;
2899*0a6a1f1dSLionel Sambuc         }
2900*0a6a1f1dSLionel Sambuc       }
2901*0a6a1f1dSLionel Sambuc       if (!Update.isUsable() || !Final.isUsable()) {
2902*0a6a1f1dSLionel Sambuc         HasErrors = true;
2903*0a6a1f1dSLionel Sambuc         break;
2904*0a6a1f1dSLionel Sambuc       }
2905*0a6a1f1dSLionel Sambuc       // Save results
2906*0a6a1f1dSLionel Sambuc       Built.Counters[Cnt] = IS.CounterVar;
2907*0a6a1f1dSLionel Sambuc       Built.Updates[Cnt] = Update.get();
2908*0a6a1f1dSLionel Sambuc       Built.Finals[Cnt] = Final.get();
2909*0a6a1f1dSLionel Sambuc     }
2910*0a6a1f1dSLionel Sambuc   }
2911*0a6a1f1dSLionel Sambuc 
2912*0a6a1f1dSLionel Sambuc   if (HasErrors)
2913*0a6a1f1dSLionel Sambuc     return 0;
2914*0a6a1f1dSLionel Sambuc 
2915*0a6a1f1dSLionel Sambuc   // Save results
2916*0a6a1f1dSLionel Sambuc   Built.IterationVarRef = IV.get();
2917*0a6a1f1dSLionel Sambuc   Built.LastIteration = LastIteration.get();
2918*0a6a1f1dSLionel Sambuc   Built.CalcLastIteration = CalcLastIteration.get();
2919*0a6a1f1dSLionel Sambuc   Built.PreCond = PreCond.get();
2920*0a6a1f1dSLionel Sambuc   Built.Cond = Cond.get();
2921*0a6a1f1dSLionel Sambuc   Built.SeparatedCond = SeparatedCond.get();
2922*0a6a1f1dSLionel Sambuc   Built.Init = Init.get();
2923*0a6a1f1dSLionel Sambuc   Built.Inc = Inc.get();
2924*0a6a1f1dSLionel Sambuc   Built.LB = LB.get();
2925*0a6a1f1dSLionel Sambuc   Built.UB = UB.get();
2926*0a6a1f1dSLionel Sambuc   Built.IL = IL.get();
2927*0a6a1f1dSLionel Sambuc   Built.ST = ST.get();
2928*0a6a1f1dSLionel Sambuc   Built.EUB = EUB.get();
2929*0a6a1f1dSLionel Sambuc   Built.NLB = NextLB.get();
2930*0a6a1f1dSLionel Sambuc   Built.NUB = NextUB.get();
2931*0a6a1f1dSLionel Sambuc 
2932*0a6a1f1dSLionel Sambuc   return NestedLoopCount;
2933*0a6a1f1dSLionel Sambuc }
2934*0a6a1f1dSLionel Sambuc 
GetCollapseNumberExpr(ArrayRef<OMPClause * > Clauses)2935*0a6a1f1dSLionel Sambuc static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
2936*0a6a1f1dSLionel Sambuc   auto CollapseFilter = [](const OMPClause *C) -> bool {
2937*0a6a1f1dSLionel Sambuc     return C->getClauseKind() == OMPC_collapse;
2938*0a6a1f1dSLionel Sambuc   };
2939*0a6a1f1dSLionel Sambuc   OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2940*0a6a1f1dSLionel Sambuc       Clauses, CollapseFilter);
2941*0a6a1f1dSLionel Sambuc   if (I)
2942*0a6a1f1dSLionel Sambuc     return cast<OMPCollapseClause>(*I)->getNumForLoops();
2943*0a6a1f1dSLionel Sambuc   return nullptr;
2944*0a6a1f1dSLionel Sambuc }
2945*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<VarDecl *,Expr * > & VarsWithImplicitDSA)2946*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPSimdDirective(
2947*0a6a1f1dSLionel Sambuc     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2948*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc,
2949*0a6a1f1dSLionel Sambuc     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2950*0a6a1f1dSLionel Sambuc   OMPLoopDirective::HelperExprs B;
2951*0a6a1f1dSLionel Sambuc   // In presence of clause 'collapse', it will define the nested loops number.
2952*0a6a1f1dSLionel Sambuc   unsigned NestedLoopCount =
2953*0a6a1f1dSLionel Sambuc       CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2954*0a6a1f1dSLionel Sambuc                       *DSAStack, VarsWithImplicitDSA, B);
2955*0a6a1f1dSLionel Sambuc   if (NestedLoopCount == 0)
2956*0a6a1f1dSLionel Sambuc     return StmtError();
2957*0a6a1f1dSLionel Sambuc 
2958*0a6a1f1dSLionel Sambuc   assert((CurContext->isDependentContext() || B.builtAll()) &&
2959*0a6a1f1dSLionel Sambuc          "omp simd loop exprs were not built");
2960*0a6a1f1dSLionel Sambuc 
2961*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
2962*0a6a1f1dSLionel Sambuc   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2963*0a6a1f1dSLionel Sambuc                                   Clauses, AStmt, B);
2964*0a6a1f1dSLionel Sambuc }
2965*0a6a1f1dSLionel Sambuc 
ActOnOpenMPForDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<VarDecl *,Expr * > & VarsWithImplicitDSA)2966*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPForDirective(
2967*0a6a1f1dSLionel Sambuc     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2968*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc,
2969*0a6a1f1dSLionel Sambuc     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2970*0a6a1f1dSLionel Sambuc   OMPLoopDirective::HelperExprs B;
2971*0a6a1f1dSLionel Sambuc   // In presence of clause 'collapse', it will define the nested loops number.
2972*0a6a1f1dSLionel Sambuc   unsigned NestedLoopCount =
2973*0a6a1f1dSLionel Sambuc       CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2974*0a6a1f1dSLionel Sambuc                       *DSAStack, VarsWithImplicitDSA, B);
2975*0a6a1f1dSLionel Sambuc   if (NestedLoopCount == 0)
2976*0a6a1f1dSLionel Sambuc     return StmtError();
2977*0a6a1f1dSLionel Sambuc 
2978*0a6a1f1dSLionel Sambuc   assert((CurContext->isDependentContext() || B.builtAll()) &&
2979*0a6a1f1dSLionel Sambuc          "omp for loop exprs were not built");
2980*0a6a1f1dSLionel Sambuc 
2981*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
2982*0a6a1f1dSLionel Sambuc   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2983*0a6a1f1dSLionel Sambuc                                  Clauses, AStmt, B);
2984*0a6a1f1dSLionel Sambuc }
2985*0a6a1f1dSLionel Sambuc 
ActOnOpenMPForSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<VarDecl *,Expr * > & VarsWithImplicitDSA)2986*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPForSimdDirective(
2987*0a6a1f1dSLionel Sambuc     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2988*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc,
2989*0a6a1f1dSLionel Sambuc     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2990*0a6a1f1dSLionel Sambuc   OMPLoopDirective::HelperExprs B;
2991*0a6a1f1dSLionel Sambuc   // In presence of clause 'collapse', it will define the nested loops number.
2992*0a6a1f1dSLionel Sambuc   unsigned NestedLoopCount =
2993*0a6a1f1dSLionel Sambuc       CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
2994*0a6a1f1dSLionel Sambuc                       *this, *DSAStack, VarsWithImplicitDSA, B);
2995*0a6a1f1dSLionel Sambuc   if (NestedLoopCount == 0)
2996*0a6a1f1dSLionel Sambuc     return StmtError();
2997*0a6a1f1dSLionel Sambuc 
2998*0a6a1f1dSLionel Sambuc   assert((CurContext->isDependentContext() || B.builtAll()) &&
2999*0a6a1f1dSLionel Sambuc          "omp for simd loop exprs were not built");
3000*0a6a1f1dSLionel Sambuc 
3001*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3002*0a6a1f1dSLionel Sambuc   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3003*0a6a1f1dSLionel Sambuc                                      Clauses, AStmt, B);
3004*0a6a1f1dSLionel Sambuc }
3005*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSectionsDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3006*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3007*0a6a1f1dSLionel Sambuc                                               Stmt *AStmt,
3008*0a6a1f1dSLionel Sambuc                                               SourceLocation StartLoc,
3009*0a6a1f1dSLionel Sambuc                                               SourceLocation EndLoc) {
3010*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3011*0a6a1f1dSLionel Sambuc   auto BaseStmt = AStmt;
3012*0a6a1f1dSLionel Sambuc   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3013*0a6a1f1dSLionel Sambuc     BaseStmt = CS->getCapturedStmt();
3014*0a6a1f1dSLionel Sambuc   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3015*0a6a1f1dSLionel Sambuc     auto S = C->children();
3016*0a6a1f1dSLionel Sambuc     if (!S)
3017*0a6a1f1dSLionel Sambuc       return StmtError();
3018*0a6a1f1dSLionel Sambuc     // All associated statements must be '#pragma omp section' except for
3019*0a6a1f1dSLionel Sambuc     // the first one.
3020*0a6a1f1dSLionel Sambuc     for (++S; S; ++S) {
3021*0a6a1f1dSLionel Sambuc       auto SectionStmt = *S;
3022*0a6a1f1dSLionel Sambuc       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3023*0a6a1f1dSLionel Sambuc         if (SectionStmt)
3024*0a6a1f1dSLionel Sambuc           Diag(SectionStmt->getLocStart(),
3025*0a6a1f1dSLionel Sambuc                diag::err_omp_sections_substmt_not_section);
3026*0a6a1f1dSLionel Sambuc         return StmtError();
3027*0a6a1f1dSLionel Sambuc       }
3028*0a6a1f1dSLionel Sambuc     }
3029*0a6a1f1dSLionel Sambuc   } else {
3030*0a6a1f1dSLionel Sambuc     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3031*0a6a1f1dSLionel Sambuc     return StmtError();
3032*0a6a1f1dSLionel Sambuc   }
3033*0a6a1f1dSLionel Sambuc 
3034*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3035*0a6a1f1dSLionel Sambuc 
3036*0a6a1f1dSLionel Sambuc   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3037*0a6a1f1dSLionel Sambuc                                       AStmt);
3038*0a6a1f1dSLionel Sambuc }
3039*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSectionDirective(Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3040*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3041*0a6a1f1dSLionel Sambuc                                              SourceLocation StartLoc,
3042*0a6a1f1dSLionel Sambuc                                              SourceLocation EndLoc) {
3043*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3044*0a6a1f1dSLionel Sambuc 
3045*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3046*0a6a1f1dSLionel Sambuc 
3047*0a6a1f1dSLionel Sambuc   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3048*0a6a1f1dSLionel Sambuc }
3049*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSingleDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3050*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3051*0a6a1f1dSLionel Sambuc                                             Stmt *AStmt,
3052*0a6a1f1dSLionel Sambuc                                             SourceLocation StartLoc,
3053*0a6a1f1dSLionel Sambuc                                             SourceLocation EndLoc) {
3054*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3055*0a6a1f1dSLionel Sambuc 
3056*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3057*0a6a1f1dSLionel Sambuc 
3058*0a6a1f1dSLionel Sambuc   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3059*0a6a1f1dSLionel Sambuc }
3060*0a6a1f1dSLionel Sambuc 
ActOnOpenMPMasterDirective(Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3061*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3062*0a6a1f1dSLionel Sambuc                                             SourceLocation StartLoc,
3063*0a6a1f1dSLionel Sambuc                                             SourceLocation EndLoc) {
3064*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3065*0a6a1f1dSLionel Sambuc 
3066*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3067*0a6a1f1dSLionel Sambuc 
3068*0a6a1f1dSLionel Sambuc   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3069*0a6a1f1dSLionel Sambuc }
3070*0a6a1f1dSLionel Sambuc 
3071*0a6a1f1dSLionel Sambuc StmtResult
ActOnOpenMPCriticalDirective(const DeclarationNameInfo & DirName,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3072*0a6a1f1dSLionel Sambuc Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3073*0a6a1f1dSLionel Sambuc                                    Stmt *AStmt, SourceLocation StartLoc,
3074*0a6a1f1dSLionel Sambuc                                    SourceLocation EndLoc) {
3075*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3076*0a6a1f1dSLionel Sambuc 
3077*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3078*0a6a1f1dSLionel Sambuc 
3079*0a6a1f1dSLionel Sambuc   return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3080*0a6a1f1dSLionel Sambuc                                       AStmt);
3081*0a6a1f1dSLionel Sambuc }
3082*0a6a1f1dSLionel Sambuc 
ActOnOpenMPParallelForDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<VarDecl *,Expr * > & VarsWithImplicitDSA)3083*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPParallelForDirective(
3084*0a6a1f1dSLionel Sambuc     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3085*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc,
3086*0a6a1f1dSLionel Sambuc     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3087*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3088*0a6a1f1dSLionel Sambuc   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3089*0a6a1f1dSLionel Sambuc   // 1.2.2 OpenMP Language Terminology
3090*0a6a1f1dSLionel Sambuc   // Structured block - An executable statement with a single entry at the
3091*0a6a1f1dSLionel Sambuc   // top and a single exit at the bottom.
3092*0a6a1f1dSLionel Sambuc   // The point of exit cannot be a branch out of the structured block.
3093*0a6a1f1dSLionel Sambuc   // longjmp() and throw() must not violate the entry/exit criteria.
3094*0a6a1f1dSLionel Sambuc   CS->getCapturedDecl()->setNothrow();
3095*0a6a1f1dSLionel Sambuc 
3096*0a6a1f1dSLionel Sambuc   OMPLoopDirective::HelperExprs B;
3097*0a6a1f1dSLionel Sambuc   // In presence of clause 'collapse', it will define the nested loops number.
3098*0a6a1f1dSLionel Sambuc   unsigned NestedLoopCount =
3099*0a6a1f1dSLionel Sambuc       CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
3100*0a6a1f1dSLionel Sambuc                       *this, *DSAStack, VarsWithImplicitDSA, B);
3101*0a6a1f1dSLionel Sambuc   if (NestedLoopCount == 0)
3102*0a6a1f1dSLionel Sambuc     return StmtError();
3103*0a6a1f1dSLionel Sambuc 
3104*0a6a1f1dSLionel Sambuc   assert((CurContext->isDependentContext() || B.builtAll()) &&
3105*0a6a1f1dSLionel Sambuc          "omp parallel for loop exprs were not built");
3106*0a6a1f1dSLionel Sambuc 
3107*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3108*0a6a1f1dSLionel Sambuc   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3109*0a6a1f1dSLionel Sambuc                                          NestedLoopCount, Clauses, AStmt, B);
3110*0a6a1f1dSLionel Sambuc }
3111*0a6a1f1dSLionel Sambuc 
ActOnOpenMPParallelForSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<VarDecl *,Expr * > & VarsWithImplicitDSA)3112*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3113*0a6a1f1dSLionel Sambuc     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3114*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc,
3115*0a6a1f1dSLionel Sambuc     llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3116*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3117*0a6a1f1dSLionel Sambuc   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3118*0a6a1f1dSLionel Sambuc   // 1.2.2 OpenMP Language Terminology
3119*0a6a1f1dSLionel Sambuc   // Structured block - An executable statement with a single entry at the
3120*0a6a1f1dSLionel Sambuc   // top and a single exit at the bottom.
3121*0a6a1f1dSLionel Sambuc   // The point of exit cannot be a branch out of the structured block.
3122*0a6a1f1dSLionel Sambuc   // longjmp() and throw() must not violate the entry/exit criteria.
3123*0a6a1f1dSLionel Sambuc   CS->getCapturedDecl()->setNothrow();
3124*0a6a1f1dSLionel Sambuc 
3125*0a6a1f1dSLionel Sambuc   OMPLoopDirective::HelperExprs B;
3126*0a6a1f1dSLionel Sambuc   // In presence of clause 'collapse', it will define the nested loops number.
3127*0a6a1f1dSLionel Sambuc   unsigned NestedLoopCount =
3128*0a6a1f1dSLionel Sambuc       CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
3129*0a6a1f1dSLionel Sambuc                       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
3130*0a6a1f1dSLionel Sambuc   if (NestedLoopCount == 0)
3131*0a6a1f1dSLionel Sambuc     return StmtError();
3132*0a6a1f1dSLionel Sambuc 
3133*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3134*0a6a1f1dSLionel Sambuc   return OMPParallelForSimdDirective::Create(
3135*0a6a1f1dSLionel Sambuc       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
3136*0a6a1f1dSLionel Sambuc }
3137*0a6a1f1dSLionel Sambuc 
3138*0a6a1f1dSLionel Sambuc StmtResult
ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3139*0a6a1f1dSLionel Sambuc Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3140*0a6a1f1dSLionel Sambuc                                            Stmt *AStmt, SourceLocation StartLoc,
3141*0a6a1f1dSLionel Sambuc                                            SourceLocation EndLoc) {
3142*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3143*0a6a1f1dSLionel Sambuc   auto BaseStmt = AStmt;
3144*0a6a1f1dSLionel Sambuc   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3145*0a6a1f1dSLionel Sambuc     BaseStmt = CS->getCapturedStmt();
3146*0a6a1f1dSLionel Sambuc   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3147*0a6a1f1dSLionel Sambuc     auto S = C->children();
3148*0a6a1f1dSLionel Sambuc     if (!S)
3149*0a6a1f1dSLionel Sambuc       return StmtError();
3150*0a6a1f1dSLionel Sambuc     // All associated statements must be '#pragma omp section' except for
3151*0a6a1f1dSLionel Sambuc     // the first one.
3152*0a6a1f1dSLionel Sambuc     for (++S; S; ++S) {
3153*0a6a1f1dSLionel Sambuc       auto SectionStmt = *S;
3154*0a6a1f1dSLionel Sambuc       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3155*0a6a1f1dSLionel Sambuc         if (SectionStmt)
3156*0a6a1f1dSLionel Sambuc           Diag(SectionStmt->getLocStart(),
3157*0a6a1f1dSLionel Sambuc                diag::err_omp_parallel_sections_substmt_not_section);
3158*0a6a1f1dSLionel Sambuc         return StmtError();
3159*0a6a1f1dSLionel Sambuc       }
3160*0a6a1f1dSLionel Sambuc     }
3161*0a6a1f1dSLionel Sambuc   } else {
3162*0a6a1f1dSLionel Sambuc     Diag(AStmt->getLocStart(),
3163*0a6a1f1dSLionel Sambuc          diag::err_omp_parallel_sections_not_compound_stmt);
3164*0a6a1f1dSLionel Sambuc     return StmtError();
3165*0a6a1f1dSLionel Sambuc   }
3166*0a6a1f1dSLionel Sambuc 
3167*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3168*0a6a1f1dSLionel Sambuc 
3169*0a6a1f1dSLionel Sambuc   return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3170*0a6a1f1dSLionel Sambuc                                               Clauses, AStmt);
3171*0a6a1f1dSLionel Sambuc }
3172*0a6a1f1dSLionel Sambuc 
ActOnOpenMPTaskDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3173*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3174*0a6a1f1dSLionel Sambuc                                           Stmt *AStmt, SourceLocation StartLoc,
3175*0a6a1f1dSLionel Sambuc                                           SourceLocation EndLoc) {
3176*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3177*0a6a1f1dSLionel Sambuc   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3178*0a6a1f1dSLionel Sambuc   // 1.2.2 OpenMP Language Terminology
3179*0a6a1f1dSLionel Sambuc   // Structured block - An executable statement with a single entry at the
3180*0a6a1f1dSLionel Sambuc   // top and a single exit at the bottom.
3181*0a6a1f1dSLionel Sambuc   // The point of exit cannot be a branch out of the structured block.
3182*0a6a1f1dSLionel Sambuc   // longjmp() and throw() must not violate the entry/exit criteria.
3183*0a6a1f1dSLionel Sambuc   CS->getCapturedDecl()->setNothrow();
3184*0a6a1f1dSLionel Sambuc 
3185*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3186*0a6a1f1dSLionel Sambuc 
3187*0a6a1f1dSLionel Sambuc   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3188*0a6a1f1dSLionel Sambuc }
3189*0a6a1f1dSLionel Sambuc 
ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,SourceLocation EndLoc)3190*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3191*0a6a1f1dSLionel Sambuc                                                SourceLocation EndLoc) {
3192*0a6a1f1dSLionel Sambuc   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3193*0a6a1f1dSLionel Sambuc }
3194*0a6a1f1dSLionel Sambuc 
ActOnOpenMPBarrierDirective(SourceLocation StartLoc,SourceLocation EndLoc)3195*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3196*0a6a1f1dSLionel Sambuc                                              SourceLocation EndLoc) {
3197*0a6a1f1dSLionel Sambuc   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3198*0a6a1f1dSLionel Sambuc }
3199*0a6a1f1dSLionel Sambuc 
ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,SourceLocation EndLoc)3200*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3201*0a6a1f1dSLionel Sambuc                                               SourceLocation EndLoc) {
3202*0a6a1f1dSLionel Sambuc   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3203*0a6a1f1dSLionel Sambuc }
3204*0a6a1f1dSLionel Sambuc 
ActOnOpenMPFlushDirective(ArrayRef<OMPClause * > Clauses,SourceLocation StartLoc,SourceLocation EndLoc)3205*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3206*0a6a1f1dSLionel Sambuc                                            SourceLocation StartLoc,
3207*0a6a1f1dSLionel Sambuc                                            SourceLocation EndLoc) {
3208*0a6a1f1dSLionel Sambuc   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3209*0a6a1f1dSLionel Sambuc   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3210*0a6a1f1dSLionel Sambuc }
3211*0a6a1f1dSLionel Sambuc 
ActOnOpenMPOrderedDirective(Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3212*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3213*0a6a1f1dSLionel Sambuc                                              SourceLocation StartLoc,
3214*0a6a1f1dSLionel Sambuc                                              SourceLocation EndLoc) {
3215*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3216*0a6a1f1dSLionel Sambuc 
3217*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3218*0a6a1f1dSLionel Sambuc 
3219*0a6a1f1dSLionel Sambuc   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3220*0a6a1f1dSLionel Sambuc }
3221*0a6a1f1dSLionel Sambuc 
ActOnOpenMPAtomicDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3222*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3223*0a6a1f1dSLionel Sambuc                                             Stmt *AStmt,
3224*0a6a1f1dSLionel Sambuc                                             SourceLocation StartLoc,
3225*0a6a1f1dSLionel Sambuc                                             SourceLocation EndLoc) {
3226*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3227*0a6a1f1dSLionel Sambuc   auto CS = cast<CapturedStmt>(AStmt);
3228*0a6a1f1dSLionel Sambuc   // 1.2.2 OpenMP Language Terminology
3229*0a6a1f1dSLionel Sambuc   // Structured block - An executable statement with a single entry at the
3230*0a6a1f1dSLionel Sambuc   // top and a single exit at the bottom.
3231*0a6a1f1dSLionel Sambuc   // The point of exit cannot be a branch out of the structured block.
3232*0a6a1f1dSLionel Sambuc   // longjmp() and throw() must not violate the entry/exit criteria.
3233*0a6a1f1dSLionel Sambuc   // TODO further analysis of associated statements and clauses.
3234*0a6a1f1dSLionel Sambuc   OpenMPClauseKind AtomicKind = OMPC_unknown;
3235*0a6a1f1dSLionel Sambuc   SourceLocation AtomicKindLoc;
3236*0a6a1f1dSLionel Sambuc   for (auto *C : Clauses) {
3237*0a6a1f1dSLionel Sambuc     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
3238*0a6a1f1dSLionel Sambuc         C->getClauseKind() == OMPC_update ||
3239*0a6a1f1dSLionel Sambuc         C->getClauseKind() == OMPC_capture) {
3240*0a6a1f1dSLionel Sambuc       if (AtomicKind != OMPC_unknown) {
3241*0a6a1f1dSLionel Sambuc         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3242*0a6a1f1dSLionel Sambuc             << SourceRange(C->getLocStart(), C->getLocEnd());
3243*0a6a1f1dSLionel Sambuc         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3244*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(AtomicKind);
3245*0a6a1f1dSLionel Sambuc       } else {
3246*0a6a1f1dSLionel Sambuc         AtomicKind = C->getClauseKind();
3247*0a6a1f1dSLionel Sambuc         AtomicKindLoc = C->getLocStart();
3248*0a6a1f1dSLionel Sambuc       }
3249*0a6a1f1dSLionel Sambuc     }
3250*0a6a1f1dSLionel Sambuc   }
3251*0a6a1f1dSLionel Sambuc 
3252*0a6a1f1dSLionel Sambuc   auto Body = CS->getCapturedStmt();
3253*0a6a1f1dSLionel Sambuc   Expr *X = nullptr;
3254*0a6a1f1dSLionel Sambuc   Expr *V = nullptr;
3255*0a6a1f1dSLionel Sambuc   Expr *E = nullptr;
3256*0a6a1f1dSLionel Sambuc   // OpenMP [2.12.6, atomic Construct]
3257*0a6a1f1dSLionel Sambuc   // In the next expressions:
3258*0a6a1f1dSLionel Sambuc   // * x and v (as applicable) are both l-value expressions with scalar type.
3259*0a6a1f1dSLionel Sambuc   // * During the execution of an atomic region, multiple syntactic
3260*0a6a1f1dSLionel Sambuc   // occurrences of x must designate the same storage location.
3261*0a6a1f1dSLionel Sambuc   // * Neither of v and expr (as applicable) may access the storage location
3262*0a6a1f1dSLionel Sambuc   // designated by x.
3263*0a6a1f1dSLionel Sambuc   // * Neither of x and expr (as applicable) may access the storage location
3264*0a6a1f1dSLionel Sambuc   // designated by v.
3265*0a6a1f1dSLionel Sambuc   // * expr is an expression with scalar type.
3266*0a6a1f1dSLionel Sambuc   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3267*0a6a1f1dSLionel Sambuc   // * binop, binop=, ++, and -- are not overloaded operators.
3268*0a6a1f1dSLionel Sambuc   // * The expression x binop expr must be numerically equivalent to x binop
3269*0a6a1f1dSLionel Sambuc   // (expr). This requirement is satisfied if the operators in expr have
3270*0a6a1f1dSLionel Sambuc   // precedence greater than binop, or by using parentheses around expr or
3271*0a6a1f1dSLionel Sambuc   // subexpressions of expr.
3272*0a6a1f1dSLionel Sambuc   // * The expression expr binop x must be numerically equivalent to (expr)
3273*0a6a1f1dSLionel Sambuc   // binop x. This requirement is satisfied if the operators in expr have
3274*0a6a1f1dSLionel Sambuc   // precedence equal to or greater than binop, or by using parentheses around
3275*0a6a1f1dSLionel Sambuc   // expr or subexpressions of expr.
3276*0a6a1f1dSLionel Sambuc   // * For forms that allow multiple occurrences of x, the number of times
3277*0a6a1f1dSLionel Sambuc   // that x is evaluated is unspecified.
3278*0a6a1f1dSLionel Sambuc   enum {
3279*0a6a1f1dSLionel Sambuc     NotAnExpression,
3280*0a6a1f1dSLionel Sambuc     NotAnAssignmentOp,
3281*0a6a1f1dSLionel Sambuc     NotAScalarType,
3282*0a6a1f1dSLionel Sambuc     NotAnLValue,
3283*0a6a1f1dSLionel Sambuc     NoError
3284*0a6a1f1dSLionel Sambuc   } ErrorFound = NoError;
3285*0a6a1f1dSLionel Sambuc   if (AtomicKind == OMPC_read) {
3286*0a6a1f1dSLionel Sambuc     SourceLocation ErrorLoc, NoteLoc;
3287*0a6a1f1dSLionel Sambuc     SourceRange ErrorRange, NoteRange;
3288*0a6a1f1dSLionel Sambuc     // If clause is read:
3289*0a6a1f1dSLionel Sambuc     //  v = x;
3290*0a6a1f1dSLionel Sambuc     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3291*0a6a1f1dSLionel Sambuc       auto AtomicBinOp =
3292*0a6a1f1dSLionel Sambuc           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3293*0a6a1f1dSLionel Sambuc       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3294*0a6a1f1dSLionel Sambuc         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3295*0a6a1f1dSLionel Sambuc         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3296*0a6a1f1dSLionel Sambuc         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3297*0a6a1f1dSLionel Sambuc             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3298*0a6a1f1dSLionel Sambuc           if (!X->isLValue() || !V->isLValue()) {
3299*0a6a1f1dSLionel Sambuc             auto NotLValueExpr = X->isLValue() ? V : X;
3300*0a6a1f1dSLionel Sambuc             ErrorFound = NotAnLValue;
3301*0a6a1f1dSLionel Sambuc             ErrorLoc = AtomicBinOp->getExprLoc();
3302*0a6a1f1dSLionel Sambuc             ErrorRange = AtomicBinOp->getSourceRange();
3303*0a6a1f1dSLionel Sambuc             NoteLoc = NotLValueExpr->getExprLoc();
3304*0a6a1f1dSLionel Sambuc             NoteRange = NotLValueExpr->getSourceRange();
3305*0a6a1f1dSLionel Sambuc           }
3306*0a6a1f1dSLionel Sambuc         } else if (!X->isInstantiationDependent() ||
3307*0a6a1f1dSLionel Sambuc                    !V->isInstantiationDependent()) {
3308*0a6a1f1dSLionel Sambuc           auto NotScalarExpr =
3309*0a6a1f1dSLionel Sambuc               (X->isInstantiationDependent() || X->getType()->isScalarType())
3310*0a6a1f1dSLionel Sambuc                   ? V
3311*0a6a1f1dSLionel Sambuc                   : X;
3312*0a6a1f1dSLionel Sambuc           ErrorFound = NotAScalarType;
3313*0a6a1f1dSLionel Sambuc           ErrorLoc = AtomicBinOp->getExprLoc();
3314*0a6a1f1dSLionel Sambuc           ErrorRange = AtomicBinOp->getSourceRange();
3315*0a6a1f1dSLionel Sambuc           NoteLoc = NotScalarExpr->getExprLoc();
3316*0a6a1f1dSLionel Sambuc           NoteRange = NotScalarExpr->getSourceRange();
3317*0a6a1f1dSLionel Sambuc         }
3318*0a6a1f1dSLionel Sambuc       } else {
3319*0a6a1f1dSLionel Sambuc         ErrorFound = NotAnAssignmentOp;
3320*0a6a1f1dSLionel Sambuc         ErrorLoc = AtomicBody->getExprLoc();
3321*0a6a1f1dSLionel Sambuc         ErrorRange = AtomicBody->getSourceRange();
3322*0a6a1f1dSLionel Sambuc         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3323*0a6a1f1dSLionel Sambuc                               : AtomicBody->getExprLoc();
3324*0a6a1f1dSLionel Sambuc         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3325*0a6a1f1dSLionel Sambuc                                 : AtomicBody->getSourceRange();
3326*0a6a1f1dSLionel Sambuc       }
3327*0a6a1f1dSLionel Sambuc     } else {
3328*0a6a1f1dSLionel Sambuc       ErrorFound = NotAnExpression;
3329*0a6a1f1dSLionel Sambuc       NoteLoc = ErrorLoc = Body->getLocStart();
3330*0a6a1f1dSLionel Sambuc       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3331*0a6a1f1dSLionel Sambuc     }
3332*0a6a1f1dSLionel Sambuc     if (ErrorFound != NoError) {
3333*0a6a1f1dSLionel Sambuc       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3334*0a6a1f1dSLionel Sambuc           << ErrorRange;
3335*0a6a1f1dSLionel Sambuc       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3336*0a6a1f1dSLionel Sambuc                                                       << NoteRange;
3337*0a6a1f1dSLionel Sambuc       return StmtError();
3338*0a6a1f1dSLionel Sambuc     } else if (CurContext->isDependentContext())
3339*0a6a1f1dSLionel Sambuc       V = X = nullptr;
3340*0a6a1f1dSLionel Sambuc   } else if (AtomicKind == OMPC_write) {
3341*0a6a1f1dSLionel Sambuc     SourceLocation ErrorLoc, NoteLoc;
3342*0a6a1f1dSLionel Sambuc     SourceRange ErrorRange, NoteRange;
3343*0a6a1f1dSLionel Sambuc     // If clause is write:
3344*0a6a1f1dSLionel Sambuc     //  x = expr;
3345*0a6a1f1dSLionel Sambuc     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3346*0a6a1f1dSLionel Sambuc       auto AtomicBinOp =
3347*0a6a1f1dSLionel Sambuc           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3348*0a6a1f1dSLionel Sambuc       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3349*0a6a1f1dSLionel Sambuc         X = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3350*0a6a1f1dSLionel Sambuc         E = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3351*0a6a1f1dSLionel Sambuc         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3352*0a6a1f1dSLionel Sambuc             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3353*0a6a1f1dSLionel Sambuc           if (!X->isLValue()) {
3354*0a6a1f1dSLionel Sambuc             ErrorFound = NotAnLValue;
3355*0a6a1f1dSLionel Sambuc             ErrorLoc = AtomicBinOp->getExprLoc();
3356*0a6a1f1dSLionel Sambuc             ErrorRange = AtomicBinOp->getSourceRange();
3357*0a6a1f1dSLionel Sambuc             NoteLoc = X->getExprLoc();
3358*0a6a1f1dSLionel Sambuc             NoteRange = X->getSourceRange();
3359*0a6a1f1dSLionel Sambuc           }
3360*0a6a1f1dSLionel Sambuc         } else if (!X->isInstantiationDependent() ||
3361*0a6a1f1dSLionel Sambuc                    !E->isInstantiationDependent()) {
3362*0a6a1f1dSLionel Sambuc           auto NotScalarExpr =
3363*0a6a1f1dSLionel Sambuc               (X->isInstantiationDependent() || X->getType()->isScalarType())
3364*0a6a1f1dSLionel Sambuc                   ? E
3365*0a6a1f1dSLionel Sambuc                   : X;
3366*0a6a1f1dSLionel Sambuc           ErrorFound = NotAScalarType;
3367*0a6a1f1dSLionel Sambuc           ErrorLoc = AtomicBinOp->getExprLoc();
3368*0a6a1f1dSLionel Sambuc           ErrorRange = AtomicBinOp->getSourceRange();
3369*0a6a1f1dSLionel Sambuc           NoteLoc = NotScalarExpr->getExprLoc();
3370*0a6a1f1dSLionel Sambuc           NoteRange = NotScalarExpr->getSourceRange();
3371*0a6a1f1dSLionel Sambuc         }
3372*0a6a1f1dSLionel Sambuc       } else {
3373*0a6a1f1dSLionel Sambuc         ErrorFound = NotAnAssignmentOp;
3374*0a6a1f1dSLionel Sambuc         ErrorLoc = AtomicBody->getExprLoc();
3375*0a6a1f1dSLionel Sambuc         ErrorRange = AtomicBody->getSourceRange();
3376*0a6a1f1dSLionel Sambuc         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3377*0a6a1f1dSLionel Sambuc                               : AtomicBody->getExprLoc();
3378*0a6a1f1dSLionel Sambuc         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3379*0a6a1f1dSLionel Sambuc                                 : AtomicBody->getSourceRange();
3380*0a6a1f1dSLionel Sambuc       }
3381*0a6a1f1dSLionel Sambuc     } else {
3382*0a6a1f1dSLionel Sambuc       ErrorFound = NotAnExpression;
3383*0a6a1f1dSLionel Sambuc       NoteLoc = ErrorLoc = Body->getLocStart();
3384*0a6a1f1dSLionel Sambuc       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3385*0a6a1f1dSLionel Sambuc     }
3386*0a6a1f1dSLionel Sambuc     if (ErrorFound != NoError) {
3387*0a6a1f1dSLionel Sambuc       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
3388*0a6a1f1dSLionel Sambuc           << ErrorRange;
3389*0a6a1f1dSLionel Sambuc       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3390*0a6a1f1dSLionel Sambuc                                                       << NoteRange;
3391*0a6a1f1dSLionel Sambuc       return StmtError();
3392*0a6a1f1dSLionel Sambuc     } else if (CurContext->isDependentContext())
3393*0a6a1f1dSLionel Sambuc       E = X = nullptr;
3394*0a6a1f1dSLionel Sambuc   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
3395*0a6a1f1dSLionel Sambuc     if (!isa<Expr>(Body)) {
3396*0a6a1f1dSLionel Sambuc       Diag(Body->getLocStart(),
3397*0a6a1f1dSLionel Sambuc            diag::err_omp_atomic_update_not_expression_statement)
3398*0a6a1f1dSLionel Sambuc           << (AtomicKind == OMPC_update);
3399*0a6a1f1dSLionel Sambuc       return StmtError();
3400*0a6a1f1dSLionel Sambuc     }
3401*0a6a1f1dSLionel Sambuc   } else if (AtomicKind == OMPC_capture) {
3402*0a6a1f1dSLionel Sambuc     if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3403*0a6a1f1dSLionel Sambuc       Diag(Body->getLocStart(),
3404*0a6a1f1dSLionel Sambuc            diag::err_omp_atomic_capture_not_expression_statement);
3405*0a6a1f1dSLionel Sambuc       return StmtError();
3406*0a6a1f1dSLionel Sambuc     } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3407*0a6a1f1dSLionel Sambuc       Diag(Body->getLocStart(),
3408*0a6a1f1dSLionel Sambuc            diag::err_omp_atomic_capture_not_compound_statement);
3409*0a6a1f1dSLionel Sambuc       return StmtError();
3410*0a6a1f1dSLionel Sambuc     }
3411*0a6a1f1dSLionel Sambuc   }
3412*0a6a1f1dSLionel Sambuc 
3413*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3414*0a6a1f1dSLionel Sambuc 
3415*0a6a1f1dSLionel Sambuc   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3416*0a6a1f1dSLionel Sambuc                                     X, V, E);
3417*0a6a1f1dSLionel Sambuc }
3418*0a6a1f1dSLionel Sambuc 
ActOnOpenMPTargetDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3419*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3420*0a6a1f1dSLionel Sambuc                                             Stmt *AStmt,
3421*0a6a1f1dSLionel Sambuc                                             SourceLocation StartLoc,
3422*0a6a1f1dSLionel Sambuc                                             SourceLocation EndLoc) {
3423*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3424*0a6a1f1dSLionel Sambuc 
3425*0a6a1f1dSLionel Sambuc   // OpenMP [2.16, Nesting of Regions]
3426*0a6a1f1dSLionel Sambuc   // If specified, a teams construct must be contained within a target
3427*0a6a1f1dSLionel Sambuc   // construct. That target construct must contain no statements or directives
3428*0a6a1f1dSLionel Sambuc   // outside of the teams construct.
3429*0a6a1f1dSLionel Sambuc   if (DSAStack->hasInnerTeamsRegion()) {
3430*0a6a1f1dSLionel Sambuc     auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3431*0a6a1f1dSLionel Sambuc     bool OMPTeamsFound = true;
3432*0a6a1f1dSLionel Sambuc     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3433*0a6a1f1dSLionel Sambuc       auto I = CS->body_begin();
3434*0a6a1f1dSLionel Sambuc       while (I != CS->body_end()) {
3435*0a6a1f1dSLionel Sambuc         auto OED = dyn_cast<OMPExecutableDirective>(*I);
3436*0a6a1f1dSLionel Sambuc         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3437*0a6a1f1dSLionel Sambuc           OMPTeamsFound = false;
3438*0a6a1f1dSLionel Sambuc           break;
3439*0a6a1f1dSLionel Sambuc         }
3440*0a6a1f1dSLionel Sambuc         ++I;
3441*0a6a1f1dSLionel Sambuc       }
3442*0a6a1f1dSLionel Sambuc       assert(I != CS->body_end() && "Not found statement");
3443*0a6a1f1dSLionel Sambuc       S = *I;
3444*0a6a1f1dSLionel Sambuc     }
3445*0a6a1f1dSLionel Sambuc     if (!OMPTeamsFound) {
3446*0a6a1f1dSLionel Sambuc       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3447*0a6a1f1dSLionel Sambuc       Diag(DSAStack->getInnerTeamsRegionLoc(),
3448*0a6a1f1dSLionel Sambuc            diag::note_omp_nested_teams_construct_here);
3449*0a6a1f1dSLionel Sambuc       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3450*0a6a1f1dSLionel Sambuc           << isa<OMPExecutableDirective>(S);
3451*0a6a1f1dSLionel Sambuc       return StmtError();
3452*0a6a1f1dSLionel Sambuc     }
3453*0a6a1f1dSLionel Sambuc   }
3454*0a6a1f1dSLionel Sambuc 
3455*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3456*0a6a1f1dSLionel Sambuc 
3457*0a6a1f1dSLionel Sambuc   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3458*0a6a1f1dSLionel Sambuc }
3459*0a6a1f1dSLionel Sambuc 
ActOnOpenMPTeamsDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3460*0a6a1f1dSLionel Sambuc StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3461*0a6a1f1dSLionel Sambuc                                            Stmt *AStmt, SourceLocation StartLoc,
3462*0a6a1f1dSLionel Sambuc                                            SourceLocation EndLoc) {
3463*0a6a1f1dSLionel Sambuc   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3464*0a6a1f1dSLionel Sambuc   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3465*0a6a1f1dSLionel Sambuc   // 1.2.2 OpenMP Language Terminology
3466*0a6a1f1dSLionel Sambuc   // Structured block - An executable statement with a single entry at the
3467*0a6a1f1dSLionel Sambuc   // top and a single exit at the bottom.
3468*0a6a1f1dSLionel Sambuc   // The point of exit cannot be a branch out of the structured block.
3469*0a6a1f1dSLionel Sambuc   // longjmp() and throw() must not violate the entry/exit criteria.
3470*0a6a1f1dSLionel Sambuc   CS->getCapturedDecl()->setNothrow();
3471*0a6a1f1dSLionel Sambuc 
3472*0a6a1f1dSLionel Sambuc   getCurFunction()->setHasBranchProtectedScope();
3473*0a6a1f1dSLionel Sambuc 
3474*0a6a1f1dSLionel Sambuc   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3475*0a6a1f1dSLionel Sambuc }
3476*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,Expr * Expr,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3477*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
3478f4a2713aSLionel Sambuc                                              SourceLocation StartLoc,
3479f4a2713aSLionel Sambuc                                              SourceLocation LParenLoc,
3480f4a2713aSLionel Sambuc                                              SourceLocation EndLoc) {
3481*0a6a1f1dSLionel Sambuc   OMPClause *Res = nullptr;
3482*0a6a1f1dSLionel Sambuc   switch (Kind) {
3483*0a6a1f1dSLionel Sambuc   case OMPC_if:
3484*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3485*0a6a1f1dSLionel Sambuc     break;
3486*0a6a1f1dSLionel Sambuc   case OMPC_final:
3487*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3488*0a6a1f1dSLionel Sambuc     break;
3489*0a6a1f1dSLionel Sambuc   case OMPC_num_threads:
3490*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3491*0a6a1f1dSLionel Sambuc     break;
3492*0a6a1f1dSLionel Sambuc   case OMPC_safelen:
3493*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3494*0a6a1f1dSLionel Sambuc     break;
3495*0a6a1f1dSLionel Sambuc   case OMPC_collapse:
3496*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3497*0a6a1f1dSLionel Sambuc     break;
3498*0a6a1f1dSLionel Sambuc   case OMPC_default:
3499*0a6a1f1dSLionel Sambuc   case OMPC_proc_bind:
3500*0a6a1f1dSLionel Sambuc   case OMPC_schedule:
3501*0a6a1f1dSLionel Sambuc   case OMPC_private:
3502*0a6a1f1dSLionel Sambuc   case OMPC_firstprivate:
3503*0a6a1f1dSLionel Sambuc   case OMPC_lastprivate:
3504*0a6a1f1dSLionel Sambuc   case OMPC_shared:
3505*0a6a1f1dSLionel Sambuc   case OMPC_reduction:
3506*0a6a1f1dSLionel Sambuc   case OMPC_linear:
3507*0a6a1f1dSLionel Sambuc   case OMPC_aligned:
3508*0a6a1f1dSLionel Sambuc   case OMPC_copyin:
3509*0a6a1f1dSLionel Sambuc   case OMPC_copyprivate:
3510*0a6a1f1dSLionel Sambuc   case OMPC_ordered:
3511*0a6a1f1dSLionel Sambuc   case OMPC_nowait:
3512*0a6a1f1dSLionel Sambuc   case OMPC_untied:
3513*0a6a1f1dSLionel Sambuc   case OMPC_mergeable:
3514*0a6a1f1dSLionel Sambuc   case OMPC_threadprivate:
3515*0a6a1f1dSLionel Sambuc   case OMPC_flush:
3516*0a6a1f1dSLionel Sambuc   case OMPC_read:
3517*0a6a1f1dSLionel Sambuc   case OMPC_write:
3518*0a6a1f1dSLionel Sambuc   case OMPC_update:
3519*0a6a1f1dSLionel Sambuc   case OMPC_capture:
3520*0a6a1f1dSLionel Sambuc   case OMPC_seq_cst:
3521*0a6a1f1dSLionel Sambuc   case OMPC_unknown:
3522*0a6a1f1dSLionel Sambuc     llvm_unreachable("Clause is not allowed.");
3523*0a6a1f1dSLionel Sambuc   }
3524*0a6a1f1dSLionel Sambuc   return Res;
3525*0a6a1f1dSLionel Sambuc }
3526*0a6a1f1dSLionel Sambuc 
ActOnOpenMPIfClause(Expr * Condition,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3527*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
3528*0a6a1f1dSLionel Sambuc                                      SourceLocation LParenLoc,
3529*0a6a1f1dSLionel Sambuc                                      SourceLocation EndLoc) {
3530*0a6a1f1dSLionel Sambuc   Expr *ValExpr = Condition;
3531*0a6a1f1dSLionel Sambuc   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3532*0a6a1f1dSLionel Sambuc       !Condition->isInstantiationDependent() &&
3533*0a6a1f1dSLionel Sambuc       !Condition->containsUnexpandedParameterPack()) {
3534*0a6a1f1dSLionel Sambuc     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3535*0a6a1f1dSLionel Sambuc                                            Condition->getExprLoc(), Condition);
3536*0a6a1f1dSLionel Sambuc     if (Val.isInvalid())
3537*0a6a1f1dSLionel Sambuc       return nullptr;
3538*0a6a1f1dSLionel Sambuc 
3539*0a6a1f1dSLionel Sambuc     ValExpr = Val.get();
3540*0a6a1f1dSLionel Sambuc   }
3541*0a6a1f1dSLionel Sambuc 
3542*0a6a1f1dSLionel Sambuc   return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3543*0a6a1f1dSLionel Sambuc }
3544*0a6a1f1dSLionel Sambuc 
ActOnOpenMPFinalClause(Expr * Condition,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3545*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3546*0a6a1f1dSLionel Sambuc                                         SourceLocation StartLoc,
3547*0a6a1f1dSLionel Sambuc                                         SourceLocation LParenLoc,
3548*0a6a1f1dSLionel Sambuc                                         SourceLocation EndLoc) {
3549*0a6a1f1dSLionel Sambuc   Expr *ValExpr = Condition;
3550*0a6a1f1dSLionel Sambuc   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3551*0a6a1f1dSLionel Sambuc       !Condition->isInstantiationDependent() &&
3552*0a6a1f1dSLionel Sambuc       !Condition->containsUnexpandedParameterPack()) {
3553*0a6a1f1dSLionel Sambuc     ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3554*0a6a1f1dSLionel Sambuc                                            Condition->getExprLoc(), Condition);
3555*0a6a1f1dSLionel Sambuc     if (Val.isInvalid())
3556*0a6a1f1dSLionel Sambuc       return nullptr;
3557*0a6a1f1dSLionel Sambuc 
3558*0a6a1f1dSLionel Sambuc     ValExpr = Val.get();
3559*0a6a1f1dSLionel Sambuc   }
3560*0a6a1f1dSLionel Sambuc 
3561*0a6a1f1dSLionel Sambuc   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3562*0a6a1f1dSLionel Sambuc }
PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,Expr * Op)3563*0a6a1f1dSLionel Sambuc ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3564*0a6a1f1dSLionel Sambuc                                                         Expr *Op) {
3565*0a6a1f1dSLionel Sambuc   if (!Op)
3566*0a6a1f1dSLionel Sambuc     return ExprError();
3567*0a6a1f1dSLionel Sambuc 
3568*0a6a1f1dSLionel Sambuc   class IntConvertDiagnoser : public ICEConvertDiagnoser {
3569*0a6a1f1dSLionel Sambuc   public:
3570*0a6a1f1dSLionel Sambuc     IntConvertDiagnoser()
3571*0a6a1f1dSLionel Sambuc         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
3572*0a6a1f1dSLionel Sambuc     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3573*0a6a1f1dSLionel Sambuc                                          QualType T) override {
3574*0a6a1f1dSLionel Sambuc       return S.Diag(Loc, diag::err_omp_not_integral) << T;
3575*0a6a1f1dSLionel Sambuc     }
3576*0a6a1f1dSLionel Sambuc     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3577*0a6a1f1dSLionel Sambuc                                              QualType T) override {
3578*0a6a1f1dSLionel Sambuc       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3579*0a6a1f1dSLionel Sambuc     }
3580*0a6a1f1dSLionel Sambuc     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3581*0a6a1f1dSLionel Sambuc                                                QualType T,
3582*0a6a1f1dSLionel Sambuc                                                QualType ConvTy) override {
3583*0a6a1f1dSLionel Sambuc       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3584*0a6a1f1dSLionel Sambuc     }
3585*0a6a1f1dSLionel Sambuc     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3586*0a6a1f1dSLionel Sambuc                                            QualType ConvTy) override {
3587*0a6a1f1dSLionel Sambuc       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
3588*0a6a1f1dSLionel Sambuc              << ConvTy->isEnumeralType() << ConvTy;
3589*0a6a1f1dSLionel Sambuc     }
3590*0a6a1f1dSLionel Sambuc     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3591*0a6a1f1dSLionel Sambuc                                             QualType T) override {
3592*0a6a1f1dSLionel Sambuc       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3593*0a6a1f1dSLionel Sambuc     }
3594*0a6a1f1dSLionel Sambuc     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3595*0a6a1f1dSLionel Sambuc                                         QualType ConvTy) override {
3596*0a6a1f1dSLionel Sambuc       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
3597*0a6a1f1dSLionel Sambuc              << ConvTy->isEnumeralType() << ConvTy;
3598*0a6a1f1dSLionel Sambuc     }
3599*0a6a1f1dSLionel Sambuc     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3600*0a6a1f1dSLionel Sambuc                                              QualType) override {
3601*0a6a1f1dSLionel Sambuc       llvm_unreachable("conversion functions are permitted");
3602*0a6a1f1dSLionel Sambuc     }
3603*0a6a1f1dSLionel Sambuc   } ConvertDiagnoser;
3604*0a6a1f1dSLionel Sambuc   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3605*0a6a1f1dSLionel Sambuc }
3606*0a6a1f1dSLionel Sambuc 
ActOnOpenMPNumThreadsClause(Expr * NumThreads,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3607*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3608*0a6a1f1dSLionel Sambuc                                              SourceLocation StartLoc,
3609*0a6a1f1dSLionel Sambuc                                              SourceLocation LParenLoc,
3610*0a6a1f1dSLionel Sambuc                                              SourceLocation EndLoc) {
3611*0a6a1f1dSLionel Sambuc   Expr *ValExpr = NumThreads;
3612*0a6a1f1dSLionel Sambuc   if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
3613*0a6a1f1dSLionel Sambuc       !NumThreads->containsUnexpandedParameterPack()) {
3614*0a6a1f1dSLionel Sambuc     SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3615*0a6a1f1dSLionel Sambuc     ExprResult Val =
3616*0a6a1f1dSLionel Sambuc         PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
3617*0a6a1f1dSLionel Sambuc     if (Val.isInvalid())
3618*0a6a1f1dSLionel Sambuc       return nullptr;
3619*0a6a1f1dSLionel Sambuc 
3620*0a6a1f1dSLionel Sambuc     ValExpr = Val.get();
3621*0a6a1f1dSLionel Sambuc 
3622*0a6a1f1dSLionel Sambuc     // OpenMP [2.5, Restrictions]
3623*0a6a1f1dSLionel Sambuc     //  The num_threads expression must evaluate to a positive integer value.
3624*0a6a1f1dSLionel Sambuc     llvm::APSInt Result;
3625*0a6a1f1dSLionel Sambuc     if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3626*0a6a1f1dSLionel Sambuc         !Result.isStrictlyPositive()) {
3627*0a6a1f1dSLionel Sambuc       Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3628*0a6a1f1dSLionel Sambuc           << "num_threads" << NumThreads->getSourceRange();
3629*0a6a1f1dSLionel Sambuc       return nullptr;
3630*0a6a1f1dSLionel Sambuc     }
3631*0a6a1f1dSLionel Sambuc   }
3632*0a6a1f1dSLionel Sambuc 
3633*0a6a1f1dSLionel Sambuc   return new (Context)
3634*0a6a1f1dSLionel Sambuc       OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3635*0a6a1f1dSLionel Sambuc }
3636*0a6a1f1dSLionel Sambuc 
VerifyPositiveIntegerConstantInClause(Expr * E,OpenMPClauseKind CKind)3637*0a6a1f1dSLionel Sambuc ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3638*0a6a1f1dSLionel Sambuc                                                        OpenMPClauseKind CKind) {
3639*0a6a1f1dSLionel Sambuc   if (!E)
3640*0a6a1f1dSLionel Sambuc     return ExprError();
3641*0a6a1f1dSLionel Sambuc   if (E->isValueDependent() || E->isTypeDependent() ||
3642*0a6a1f1dSLionel Sambuc       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3643*0a6a1f1dSLionel Sambuc     return E;
3644*0a6a1f1dSLionel Sambuc   llvm::APSInt Result;
3645*0a6a1f1dSLionel Sambuc   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3646*0a6a1f1dSLionel Sambuc   if (ICE.isInvalid())
3647*0a6a1f1dSLionel Sambuc     return ExprError();
3648*0a6a1f1dSLionel Sambuc   if (!Result.isStrictlyPositive()) {
3649*0a6a1f1dSLionel Sambuc     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3650*0a6a1f1dSLionel Sambuc         << getOpenMPClauseName(CKind) << E->getSourceRange();
3651*0a6a1f1dSLionel Sambuc     return ExprError();
3652*0a6a1f1dSLionel Sambuc   }
3653*0a6a1f1dSLionel Sambuc   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3654*0a6a1f1dSLionel Sambuc     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3655*0a6a1f1dSLionel Sambuc         << E->getSourceRange();
3656*0a6a1f1dSLionel Sambuc     return ExprError();
3657*0a6a1f1dSLionel Sambuc   }
3658*0a6a1f1dSLionel Sambuc   return ICE;
3659*0a6a1f1dSLionel Sambuc }
3660*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSafelenClause(Expr * Len,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3661*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3662*0a6a1f1dSLionel Sambuc                                           SourceLocation LParenLoc,
3663*0a6a1f1dSLionel Sambuc                                           SourceLocation EndLoc) {
3664*0a6a1f1dSLionel Sambuc   // OpenMP [2.8.1, simd construct, Description]
3665*0a6a1f1dSLionel Sambuc   // The parameter of the safelen clause must be a constant
3666*0a6a1f1dSLionel Sambuc   // positive integer expression.
3667*0a6a1f1dSLionel Sambuc   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3668*0a6a1f1dSLionel Sambuc   if (Safelen.isInvalid())
3669*0a6a1f1dSLionel Sambuc     return nullptr;
3670*0a6a1f1dSLionel Sambuc   return new (Context)
3671*0a6a1f1dSLionel Sambuc       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
3672*0a6a1f1dSLionel Sambuc }
3673*0a6a1f1dSLionel Sambuc 
ActOnOpenMPCollapseClause(Expr * NumForLoops,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3674*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3675*0a6a1f1dSLionel Sambuc                                            SourceLocation StartLoc,
3676*0a6a1f1dSLionel Sambuc                                            SourceLocation LParenLoc,
3677*0a6a1f1dSLionel Sambuc                                            SourceLocation EndLoc) {
3678*0a6a1f1dSLionel Sambuc   // OpenMP [2.7.1, loop construct, Description]
3679*0a6a1f1dSLionel Sambuc   // OpenMP [2.8.1, simd construct, Description]
3680*0a6a1f1dSLionel Sambuc   // OpenMP [2.9.6, distribute construct, Description]
3681*0a6a1f1dSLionel Sambuc   // The parameter of the collapse clause must be a constant
3682*0a6a1f1dSLionel Sambuc   // positive integer expression.
3683*0a6a1f1dSLionel Sambuc   ExprResult NumForLoopsResult =
3684*0a6a1f1dSLionel Sambuc       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3685*0a6a1f1dSLionel Sambuc   if (NumForLoopsResult.isInvalid())
3686*0a6a1f1dSLionel Sambuc     return nullptr;
3687*0a6a1f1dSLionel Sambuc   return new (Context)
3688*0a6a1f1dSLionel Sambuc       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
3689*0a6a1f1dSLionel Sambuc }
3690*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,unsigned Argument,SourceLocation ArgumentLoc,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3691*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPSimpleClause(
3692*0a6a1f1dSLionel Sambuc     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3693*0a6a1f1dSLionel Sambuc     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
3694*0a6a1f1dSLionel Sambuc   OMPClause *Res = nullptr;
3695f4a2713aSLionel Sambuc   switch (Kind) {
3696f4a2713aSLionel Sambuc   case OMPC_default:
3697f4a2713aSLionel Sambuc     Res =
3698f4a2713aSLionel Sambuc         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3699f4a2713aSLionel Sambuc                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
3700f4a2713aSLionel Sambuc     break;
3701*0a6a1f1dSLionel Sambuc   case OMPC_proc_bind:
3702*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPProcBindClause(
3703*0a6a1f1dSLionel Sambuc         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3704*0a6a1f1dSLionel Sambuc         LParenLoc, EndLoc);
3705*0a6a1f1dSLionel Sambuc     break;
3706*0a6a1f1dSLionel Sambuc   case OMPC_if:
3707*0a6a1f1dSLionel Sambuc   case OMPC_final:
3708*0a6a1f1dSLionel Sambuc   case OMPC_num_threads:
3709*0a6a1f1dSLionel Sambuc   case OMPC_safelen:
3710*0a6a1f1dSLionel Sambuc   case OMPC_collapse:
3711*0a6a1f1dSLionel Sambuc   case OMPC_schedule:
3712f4a2713aSLionel Sambuc   case OMPC_private:
3713f4a2713aSLionel Sambuc   case OMPC_firstprivate:
3714*0a6a1f1dSLionel Sambuc   case OMPC_lastprivate:
3715f4a2713aSLionel Sambuc   case OMPC_shared:
3716*0a6a1f1dSLionel Sambuc   case OMPC_reduction:
3717*0a6a1f1dSLionel Sambuc   case OMPC_linear:
3718*0a6a1f1dSLionel Sambuc   case OMPC_aligned:
3719*0a6a1f1dSLionel Sambuc   case OMPC_copyin:
3720*0a6a1f1dSLionel Sambuc   case OMPC_copyprivate:
3721*0a6a1f1dSLionel Sambuc   case OMPC_ordered:
3722*0a6a1f1dSLionel Sambuc   case OMPC_nowait:
3723*0a6a1f1dSLionel Sambuc   case OMPC_untied:
3724*0a6a1f1dSLionel Sambuc   case OMPC_mergeable:
3725f4a2713aSLionel Sambuc   case OMPC_threadprivate:
3726*0a6a1f1dSLionel Sambuc   case OMPC_flush:
3727*0a6a1f1dSLionel Sambuc   case OMPC_read:
3728*0a6a1f1dSLionel Sambuc   case OMPC_write:
3729*0a6a1f1dSLionel Sambuc   case OMPC_update:
3730*0a6a1f1dSLionel Sambuc   case OMPC_capture:
3731*0a6a1f1dSLionel Sambuc   case OMPC_seq_cst:
3732f4a2713aSLionel Sambuc   case OMPC_unknown:
3733f4a2713aSLionel Sambuc     llvm_unreachable("Clause is not allowed.");
3734f4a2713aSLionel Sambuc   }
3735f4a2713aSLionel Sambuc   return Res;
3736f4a2713aSLionel Sambuc }
3737f4a2713aSLionel Sambuc 
ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,SourceLocation KindKwLoc,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3738f4a2713aSLionel Sambuc OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3739f4a2713aSLionel Sambuc                                           SourceLocation KindKwLoc,
3740f4a2713aSLionel Sambuc                                           SourceLocation StartLoc,
3741f4a2713aSLionel Sambuc                                           SourceLocation LParenLoc,
3742f4a2713aSLionel Sambuc                                           SourceLocation EndLoc) {
3743f4a2713aSLionel Sambuc   if (Kind == OMPC_DEFAULT_unknown) {
3744f4a2713aSLionel Sambuc     std::string Values;
3745*0a6a1f1dSLionel Sambuc     static_assert(OMPC_DEFAULT_unknown > 0,
3746*0a6a1f1dSLionel Sambuc                   "OMPC_DEFAULT_unknown not greater than 0");
3747*0a6a1f1dSLionel Sambuc     std::string Sep(", ");
3748*0a6a1f1dSLionel Sambuc     for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
3749f4a2713aSLionel Sambuc       Values += "'";
3750f4a2713aSLionel Sambuc       Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3751f4a2713aSLionel Sambuc       Values += "'";
3752f4a2713aSLionel Sambuc       switch (i) {
3753*0a6a1f1dSLionel Sambuc       case OMPC_DEFAULT_unknown - 2:
3754f4a2713aSLionel Sambuc         Values += " or ";
3755f4a2713aSLionel Sambuc         break;
3756*0a6a1f1dSLionel Sambuc       case OMPC_DEFAULT_unknown - 1:
3757f4a2713aSLionel Sambuc         break;
3758f4a2713aSLionel Sambuc       default:
3759f4a2713aSLionel Sambuc         Values += Sep;
3760f4a2713aSLionel Sambuc         break;
3761f4a2713aSLionel Sambuc       }
3762f4a2713aSLionel Sambuc     }
3763f4a2713aSLionel Sambuc     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
3764f4a2713aSLionel Sambuc         << Values << getOpenMPClauseName(OMPC_default);
3765*0a6a1f1dSLionel Sambuc     return nullptr;
3766f4a2713aSLionel Sambuc   }
3767f4a2713aSLionel Sambuc   switch (Kind) {
3768f4a2713aSLionel Sambuc   case OMPC_DEFAULT_none:
3769*0a6a1f1dSLionel Sambuc     DSAStack->setDefaultDSANone(KindKwLoc);
3770f4a2713aSLionel Sambuc     break;
3771f4a2713aSLionel Sambuc   case OMPC_DEFAULT_shared:
3772*0a6a1f1dSLionel Sambuc     DSAStack->setDefaultDSAShared(KindKwLoc);
3773f4a2713aSLionel Sambuc     break;
3774*0a6a1f1dSLionel Sambuc   case OMPC_DEFAULT_unknown:
3775*0a6a1f1dSLionel Sambuc     llvm_unreachable("Clause kind is not allowed.");
3776f4a2713aSLionel Sambuc     break;
3777f4a2713aSLionel Sambuc   }
3778*0a6a1f1dSLionel Sambuc   return new (Context)
3779*0a6a1f1dSLionel Sambuc       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
3780f4a2713aSLionel Sambuc }
3781f4a2713aSLionel Sambuc 
ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,SourceLocation KindKwLoc,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)3782*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3783*0a6a1f1dSLionel Sambuc                                            SourceLocation KindKwLoc,
3784f4a2713aSLionel Sambuc                                            SourceLocation StartLoc,
3785f4a2713aSLionel Sambuc                                            SourceLocation LParenLoc,
3786f4a2713aSLionel Sambuc                                            SourceLocation EndLoc) {
3787*0a6a1f1dSLionel Sambuc   if (Kind == OMPC_PROC_BIND_unknown) {
3788*0a6a1f1dSLionel Sambuc     std::string Values;
3789*0a6a1f1dSLionel Sambuc     std::string Sep(", ");
3790*0a6a1f1dSLionel Sambuc     for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3791*0a6a1f1dSLionel Sambuc       Values += "'";
3792*0a6a1f1dSLionel Sambuc       Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3793*0a6a1f1dSLionel Sambuc       Values += "'";
3794*0a6a1f1dSLionel Sambuc       switch (i) {
3795*0a6a1f1dSLionel Sambuc       case OMPC_PROC_BIND_unknown - 2:
3796*0a6a1f1dSLionel Sambuc         Values += " or ";
3797*0a6a1f1dSLionel Sambuc         break;
3798*0a6a1f1dSLionel Sambuc       case OMPC_PROC_BIND_unknown - 1:
3799*0a6a1f1dSLionel Sambuc         break;
3800*0a6a1f1dSLionel Sambuc       default:
3801*0a6a1f1dSLionel Sambuc         Values += Sep;
3802*0a6a1f1dSLionel Sambuc         break;
3803*0a6a1f1dSLionel Sambuc       }
3804*0a6a1f1dSLionel Sambuc     }
3805*0a6a1f1dSLionel Sambuc     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
3806*0a6a1f1dSLionel Sambuc         << Values << getOpenMPClauseName(OMPC_proc_bind);
3807*0a6a1f1dSLionel Sambuc     return nullptr;
3808*0a6a1f1dSLionel Sambuc   }
3809*0a6a1f1dSLionel Sambuc   return new (Context)
3810*0a6a1f1dSLionel Sambuc       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
3811*0a6a1f1dSLionel Sambuc }
3812*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,unsigned Argument,Expr * Expr,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ArgumentLoc,SourceLocation CommaLoc,SourceLocation EndLoc)3813*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3814*0a6a1f1dSLionel Sambuc     OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3815*0a6a1f1dSLionel Sambuc     SourceLocation StartLoc, SourceLocation LParenLoc,
3816*0a6a1f1dSLionel Sambuc     SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3817*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc) {
3818*0a6a1f1dSLionel Sambuc   OMPClause *Res = nullptr;
3819*0a6a1f1dSLionel Sambuc   switch (Kind) {
3820*0a6a1f1dSLionel Sambuc   case OMPC_schedule:
3821*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPScheduleClause(
3822*0a6a1f1dSLionel Sambuc         static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3823*0a6a1f1dSLionel Sambuc         LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3824*0a6a1f1dSLionel Sambuc     break;
3825*0a6a1f1dSLionel Sambuc   case OMPC_if:
3826*0a6a1f1dSLionel Sambuc   case OMPC_final:
3827*0a6a1f1dSLionel Sambuc   case OMPC_num_threads:
3828*0a6a1f1dSLionel Sambuc   case OMPC_safelen:
3829*0a6a1f1dSLionel Sambuc   case OMPC_collapse:
3830*0a6a1f1dSLionel Sambuc   case OMPC_default:
3831*0a6a1f1dSLionel Sambuc   case OMPC_proc_bind:
3832*0a6a1f1dSLionel Sambuc   case OMPC_private:
3833*0a6a1f1dSLionel Sambuc   case OMPC_firstprivate:
3834*0a6a1f1dSLionel Sambuc   case OMPC_lastprivate:
3835*0a6a1f1dSLionel Sambuc   case OMPC_shared:
3836*0a6a1f1dSLionel Sambuc   case OMPC_reduction:
3837*0a6a1f1dSLionel Sambuc   case OMPC_linear:
3838*0a6a1f1dSLionel Sambuc   case OMPC_aligned:
3839*0a6a1f1dSLionel Sambuc   case OMPC_copyin:
3840*0a6a1f1dSLionel Sambuc   case OMPC_copyprivate:
3841*0a6a1f1dSLionel Sambuc   case OMPC_ordered:
3842*0a6a1f1dSLionel Sambuc   case OMPC_nowait:
3843*0a6a1f1dSLionel Sambuc   case OMPC_untied:
3844*0a6a1f1dSLionel Sambuc   case OMPC_mergeable:
3845*0a6a1f1dSLionel Sambuc   case OMPC_threadprivate:
3846*0a6a1f1dSLionel Sambuc   case OMPC_flush:
3847*0a6a1f1dSLionel Sambuc   case OMPC_read:
3848*0a6a1f1dSLionel Sambuc   case OMPC_write:
3849*0a6a1f1dSLionel Sambuc   case OMPC_update:
3850*0a6a1f1dSLionel Sambuc   case OMPC_capture:
3851*0a6a1f1dSLionel Sambuc   case OMPC_seq_cst:
3852*0a6a1f1dSLionel Sambuc   case OMPC_unknown:
3853*0a6a1f1dSLionel Sambuc     llvm_unreachable("Clause is not allowed.");
3854*0a6a1f1dSLionel Sambuc   }
3855*0a6a1f1dSLionel Sambuc   return Res;
3856*0a6a1f1dSLionel Sambuc }
3857*0a6a1f1dSLionel Sambuc 
ActOnOpenMPScheduleClause(OpenMPScheduleClauseKind Kind,Expr * ChunkSize,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation KindLoc,SourceLocation CommaLoc,SourceLocation EndLoc)3858*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPScheduleClause(
3859*0a6a1f1dSLionel Sambuc     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3860*0a6a1f1dSLionel Sambuc     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3861*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc) {
3862*0a6a1f1dSLionel Sambuc   if (Kind == OMPC_SCHEDULE_unknown) {
3863*0a6a1f1dSLionel Sambuc     std::string Values;
3864*0a6a1f1dSLionel Sambuc     std::string Sep(", ");
3865*0a6a1f1dSLionel Sambuc     for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3866*0a6a1f1dSLionel Sambuc       Values += "'";
3867*0a6a1f1dSLionel Sambuc       Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3868*0a6a1f1dSLionel Sambuc       Values += "'";
3869*0a6a1f1dSLionel Sambuc       switch (i) {
3870*0a6a1f1dSLionel Sambuc       case OMPC_SCHEDULE_unknown - 2:
3871*0a6a1f1dSLionel Sambuc         Values += " or ";
3872*0a6a1f1dSLionel Sambuc         break;
3873*0a6a1f1dSLionel Sambuc       case OMPC_SCHEDULE_unknown - 1:
3874*0a6a1f1dSLionel Sambuc         break;
3875*0a6a1f1dSLionel Sambuc       default:
3876*0a6a1f1dSLionel Sambuc         Values += Sep;
3877*0a6a1f1dSLionel Sambuc         break;
3878*0a6a1f1dSLionel Sambuc       }
3879*0a6a1f1dSLionel Sambuc     }
3880*0a6a1f1dSLionel Sambuc     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3881*0a6a1f1dSLionel Sambuc         << Values << getOpenMPClauseName(OMPC_schedule);
3882*0a6a1f1dSLionel Sambuc     return nullptr;
3883*0a6a1f1dSLionel Sambuc   }
3884*0a6a1f1dSLionel Sambuc   Expr *ValExpr = ChunkSize;
3885*0a6a1f1dSLionel Sambuc   if (ChunkSize) {
3886*0a6a1f1dSLionel Sambuc     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3887*0a6a1f1dSLionel Sambuc         !ChunkSize->isInstantiationDependent() &&
3888*0a6a1f1dSLionel Sambuc         !ChunkSize->containsUnexpandedParameterPack()) {
3889*0a6a1f1dSLionel Sambuc       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3890*0a6a1f1dSLionel Sambuc       ExprResult Val =
3891*0a6a1f1dSLionel Sambuc           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3892*0a6a1f1dSLionel Sambuc       if (Val.isInvalid())
3893*0a6a1f1dSLionel Sambuc         return nullptr;
3894*0a6a1f1dSLionel Sambuc 
3895*0a6a1f1dSLionel Sambuc       ValExpr = Val.get();
3896*0a6a1f1dSLionel Sambuc 
3897*0a6a1f1dSLionel Sambuc       // OpenMP [2.7.1, Restrictions]
3898*0a6a1f1dSLionel Sambuc       //  chunk_size must be a loop invariant integer expression with a positive
3899*0a6a1f1dSLionel Sambuc       //  value.
3900*0a6a1f1dSLionel Sambuc       llvm::APSInt Result;
3901*0a6a1f1dSLionel Sambuc       if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3902*0a6a1f1dSLionel Sambuc           Result.isSigned() && !Result.isStrictlyPositive()) {
3903*0a6a1f1dSLionel Sambuc         Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3904*0a6a1f1dSLionel Sambuc             << "schedule" << ChunkSize->getSourceRange();
3905*0a6a1f1dSLionel Sambuc         return nullptr;
3906*0a6a1f1dSLionel Sambuc       }
3907*0a6a1f1dSLionel Sambuc     }
3908*0a6a1f1dSLionel Sambuc   }
3909*0a6a1f1dSLionel Sambuc 
3910*0a6a1f1dSLionel Sambuc   return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3911*0a6a1f1dSLionel Sambuc                                          EndLoc, Kind, ValExpr);
3912*0a6a1f1dSLionel Sambuc }
3913*0a6a1f1dSLionel Sambuc 
ActOnOpenMPClause(OpenMPClauseKind Kind,SourceLocation StartLoc,SourceLocation EndLoc)3914*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3915*0a6a1f1dSLionel Sambuc                                    SourceLocation StartLoc,
3916*0a6a1f1dSLionel Sambuc                                    SourceLocation EndLoc) {
3917*0a6a1f1dSLionel Sambuc   OMPClause *Res = nullptr;
3918*0a6a1f1dSLionel Sambuc   switch (Kind) {
3919*0a6a1f1dSLionel Sambuc   case OMPC_ordered:
3920*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3921*0a6a1f1dSLionel Sambuc     break;
3922*0a6a1f1dSLionel Sambuc   case OMPC_nowait:
3923*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3924*0a6a1f1dSLionel Sambuc     break;
3925*0a6a1f1dSLionel Sambuc   case OMPC_untied:
3926*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3927*0a6a1f1dSLionel Sambuc     break;
3928*0a6a1f1dSLionel Sambuc   case OMPC_mergeable:
3929*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3930*0a6a1f1dSLionel Sambuc     break;
3931*0a6a1f1dSLionel Sambuc   case OMPC_read:
3932*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3933*0a6a1f1dSLionel Sambuc     break;
3934*0a6a1f1dSLionel Sambuc   case OMPC_write:
3935*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3936*0a6a1f1dSLionel Sambuc     break;
3937*0a6a1f1dSLionel Sambuc   case OMPC_update:
3938*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3939*0a6a1f1dSLionel Sambuc     break;
3940*0a6a1f1dSLionel Sambuc   case OMPC_capture:
3941*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3942*0a6a1f1dSLionel Sambuc     break;
3943*0a6a1f1dSLionel Sambuc   case OMPC_seq_cst:
3944*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3945*0a6a1f1dSLionel Sambuc     break;
3946*0a6a1f1dSLionel Sambuc   case OMPC_if:
3947*0a6a1f1dSLionel Sambuc   case OMPC_final:
3948*0a6a1f1dSLionel Sambuc   case OMPC_num_threads:
3949*0a6a1f1dSLionel Sambuc   case OMPC_safelen:
3950*0a6a1f1dSLionel Sambuc   case OMPC_collapse:
3951*0a6a1f1dSLionel Sambuc   case OMPC_schedule:
3952*0a6a1f1dSLionel Sambuc   case OMPC_private:
3953*0a6a1f1dSLionel Sambuc   case OMPC_firstprivate:
3954*0a6a1f1dSLionel Sambuc   case OMPC_lastprivate:
3955*0a6a1f1dSLionel Sambuc   case OMPC_shared:
3956*0a6a1f1dSLionel Sambuc   case OMPC_reduction:
3957*0a6a1f1dSLionel Sambuc   case OMPC_linear:
3958*0a6a1f1dSLionel Sambuc   case OMPC_aligned:
3959*0a6a1f1dSLionel Sambuc   case OMPC_copyin:
3960*0a6a1f1dSLionel Sambuc   case OMPC_copyprivate:
3961*0a6a1f1dSLionel Sambuc   case OMPC_default:
3962*0a6a1f1dSLionel Sambuc   case OMPC_proc_bind:
3963*0a6a1f1dSLionel Sambuc   case OMPC_threadprivate:
3964*0a6a1f1dSLionel Sambuc   case OMPC_flush:
3965*0a6a1f1dSLionel Sambuc   case OMPC_unknown:
3966*0a6a1f1dSLionel Sambuc     llvm_unreachable("Clause is not allowed.");
3967*0a6a1f1dSLionel Sambuc   }
3968*0a6a1f1dSLionel Sambuc   return Res;
3969*0a6a1f1dSLionel Sambuc }
3970*0a6a1f1dSLionel Sambuc 
ActOnOpenMPOrderedClause(SourceLocation StartLoc,SourceLocation EndLoc)3971*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3972*0a6a1f1dSLionel Sambuc                                           SourceLocation EndLoc) {
3973*0a6a1f1dSLionel Sambuc   DSAStack->setOrderedRegion();
3974*0a6a1f1dSLionel Sambuc   return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3975*0a6a1f1dSLionel Sambuc }
3976*0a6a1f1dSLionel Sambuc 
ActOnOpenMPNowaitClause(SourceLocation StartLoc,SourceLocation EndLoc)3977*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3978*0a6a1f1dSLionel Sambuc                                          SourceLocation EndLoc) {
3979*0a6a1f1dSLionel Sambuc   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3980*0a6a1f1dSLionel Sambuc }
3981*0a6a1f1dSLionel Sambuc 
ActOnOpenMPUntiedClause(SourceLocation StartLoc,SourceLocation EndLoc)3982*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3983*0a6a1f1dSLionel Sambuc                                          SourceLocation EndLoc) {
3984*0a6a1f1dSLionel Sambuc   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3985*0a6a1f1dSLionel Sambuc }
3986*0a6a1f1dSLionel Sambuc 
ActOnOpenMPMergeableClause(SourceLocation StartLoc,SourceLocation EndLoc)3987*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3988*0a6a1f1dSLionel Sambuc                                             SourceLocation EndLoc) {
3989*0a6a1f1dSLionel Sambuc   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3990*0a6a1f1dSLionel Sambuc }
3991*0a6a1f1dSLionel Sambuc 
ActOnOpenMPReadClause(SourceLocation StartLoc,SourceLocation EndLoc)3992*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3993*0a6a1f1dSLionel Sambuc                                        SourceLocation EndLoc) {
3994*0a6a1f1dSLionel Sambuc   return new (Context) OMPReadClause(StartLoc, EndLoc);
3995*0a6a1f1dSLionel Sambuc }
3996*0a6a1f1dSLionel Sambuc 
ActOnOpenMPWriteClause(SourceLocation StartLoc,SourceLocation EndLoc)3997*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3998*0a6a1f1dSLionel Sambuc                                         SourceLocation EndLoc) {
3999*0a6a1f1dSLionel Sambuc   return new (Context) OMPWriteClause(StartLoc, EndLoc);
4000*0a6a1f1dSLionel Sambuc }
4001*0a6a1f1dSLionel Sambuc 
ActOnOpenMPUpdateClause(SourceLocation StartLoc,SourceLocation EndLoc)4002*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4003*0a6a1f1dSLionel Sambuc                                          SourceLocation EndLoc) {
4004*0a6a1f1dSLionel Sambuc   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4005*0a6a1f1dSLionel Sambuc }
4006*0a6a1f1dSLionel Sambuc 
ActOnOpenMPCaptureClause(SourceLocation StartLoc,SourceLocation EndLoc)4007*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4008*0a6a1f1dSLionel Sambuc                                           SourceLocation EndLoc) {
4009*0a6a1f1dSLionel Sambuc   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4010*0a6a1f1dSLionel Sambuc }
4011*0a6a1f1dSLionel Sambuc 
ActOnOpenMPSeqCstClause(SourceLocation StartLoc,SourceLocation EndLoc)4012*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4013*0a6a1f1dSLionel Sambuc                                          SourceLocation EndLoc) {
4014*0a6a1f1dSLionel Sambuc   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4015*0a6a1f1dSLionel Sambuc }
4016*0a6a1f1dSLionel Sambuc 
ActOnOpenMPVarListClause(OpenMPClauseKind Kind,ArrayRef<Expr * > VarList,Expr * TailExpr,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc,CXXScopeSpec & ReductionIdScopeSpec,const DeclarationNameInfo & ReductionId)4017*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPVarListClause(
4018*0a6a1f1dSLionel Sambuc     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4019*0a6a1f1dSLionel Sambuc     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4020*0a6a1f1dSLionel Sambuc     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
4021*0a6a1f1dSLionel Sambuc     const DeclarationNameInfo &ReductionId) {
4022*0a6a1f1dSLionel Sambuc   OMPClause *Res = nullptr;
4023f4a2713aSLionel Sambuc   switch (Kind) {
4024f4a2713aSLionel Sambuc   case OMPC_private:
4025f4a2713aSLionel Sambuc     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4026f4a2713aSLionel Sambuc     break;
4027f4a2713aSLionel Sambuc   case OMPC_firstprivate:
4028f4a2713aSLionel Sambuc     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4029f4a2713aSLionel Sambuc     break;
4030*0a6a1f1dSLionel Sambuc   case OMPC_lastprivate:
4031*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4032*0a6a1f1dSLionel Sambuc     break;
4033f4a2713aSLionel Sambuc   case OMPC_shared:
4034f4a2713aSLionel Sambuc     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4035f4a2713aSLionel Sambuc     break;
4036*0a6a1f1dSLionel Sambuc   case OMPC_reduction:
4037*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4038*0a6a1f1dSLionel Sambuc                                      EndLoc, ReductionIdScopeSpec, ReductionId);
4039*0a6a1f1dSLionel Sambuc     break;
4040*0a6a1f1dSLionel Sambuc   case OMPC_linear:
4041*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4042*0a6a1f1dSLionel Sambuc                                   ColonLoc, EndLoc);
4043*0a6a1f1dSLionel Sambuc     break;
4044*0a6a1f1dSLionel Sambuc   case OMPC_aligned:
4045*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4046*0a6a1f1dSLionel Sambuc                                    ColonLoc, EndLoc);
4047*0a6a1f1dSLionel Sambuc     break;
4048*0a6a1f1dSLionel Sambuc   case OMPC_copyin:
4049*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4050*0a6a1f1dSLionel Sambuc     break;
4051*0a6a1f1dSLionel Sambuc   case OMPC_copyprivate:
4052*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4053*0a6a1f1dSLionel Sambuc     break;
4054*0a6a1f1dSLionel Sambuc   case OMPC_flush:
4055*0a6a1f1dSLionel Sambuc     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4056*0a6a1f1dSLionel Sambuc     break;
4057*0a6a1f1dSLionel Sambuc   case OMPC_if:
4058*0a6a1f1dSLionel Sambuc   case OMPC_final:
4059*0a6a1f1dSLionel Sambuc   case OMPC_num_threads:
4060*0a6a1f1dSLionel Sambuc   case OMPC_safelen:
4061*0a6a1f1dSLionel Sambuc   case OMPC_collapse:
4062f4a2713aSLionel Sambuc   case OMPC_default:
4063*0a6a1f1dSLionel Sambuc   case OMPC_proc_bind:
4064*0a6a1f1dSLionel Sambuc   case OMPC_schedule:
4065*0a6a1f1dSLionel Sambuc   case OMPC_ordered:
4066*0a6a1f1dSLionel Sambuc   case OMPC_nowait:
4067*0a6a1f1dSLionel Sambuc   case OMPC_untied:
4068*0a6a1f1dSLionel Sambuc   case OMPC_mergeable:
4069f4a2713aSLionel Sambuc   case OMPC_threadprivate:
4070*0a6a1f1dSLionel Sambuc   case OMPC_read:
4071*0a6a1f1dSLionel Sambuc   case OMPC_write:
4072*0a6a1f1dSLionel Sambuc   case OMPC_update:
4073*0a6a1f1dSLionel Sambuc   case OMPC_capture:
4074*0a6a1f1dSLionel Sambuc   case OMPC_seq_cst:
4075f4a2713aSLionel Sambuc   case OMPC_unknown:
4076f4a2713aSLionel Sambuc     llvm_unreachable("Clause is not allowed.");
4077f4a2713aSLionel Sambuc   }
4078f4a2713aSLionel Sambuc   return Res;
4079f4a2713aSLionel Sambuc }
4080f4a2713aSLionel Sambuc 
ActOnOpenMPPrivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)4081f4a2713aSLionel Sambuc OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4082f4a2713aSLionel Sambuc                                           SourceLocation StartLoc,
4083f4a2713aSLionel Sambuc                                           SourceLocation LParenLoc,
4084f4a2713aSLionel Sambuc                                           SourceLocation EndLoc) {
4085f4a2713aSLionel Sambuc   SmallVector<Expr *, 8> Vars;
4086*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> PrivateCopies;
4087*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
4088*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP private clause.");
4089*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4090f4a2713aSLionel Sambuc       // It will be analyzed later.
4091*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
4092*0a6a1f1dSLionel Sambuc       PrivateCopies.push_back(nullptr);
4093f4a2713aSLionel Sambuc       continue;
4094f4a2713aSLionel Sambuc     }
4095f4a2713aSLionel Sambuc 
4096*0a6a1f1dSLionel Sambuc     SourceLocation ELoc = RefExpr->getExprLoc();
4097f4a2713aSLionel Sambuc     // OpenMP [2.1, C/C++]
4098f4a2713aSLionel Sambuc     //  A list item is a variable name.
4099f4a2713aSLionel Sambuc     // OpenMP  [2.9.3.3, Restrictions, p.1]
4100f4a2713aSLionel Sambuc     //  A variable that is part of another variable (as an array or
4101f4a2713aSLionel Sambuc     //  structure element) cannot appear in a private clause.
4102*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4103f4a2713aSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
4104*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4105f4a2713aSLionel Sambuc       continue;
4106f4a2713aSLionel Sambuc     }
4107f4a2713aSLionel Sambuc     Decl *D = DE->getDecl();
4108f4a2713aSLionel Sambuc     VarDecl *VD = cast<VarDecl>(D);
4109f4a2713aSLionel Sambuc 
4110f4a2713aSLionel Sambuc     QualType Type = VD->getType();
4111f4a2713aSLionel Sambuc     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4112f4a2713aSLionel Sambuc       // It will be analyzed later.
4113f4a2713aSLionel Sambuc       Vars.push_back(DE);
4114*0a6a1f1dSLionel Sambuc       PrivateCopies.push_back(nullptr);
4115f4a2713aSLionel Sambuc       continue;
4116f4a2713aSLionel Sambuc     }
4117f4a2713aSLionel Sambuc 
4118f4a2713aSLionel Sambuc     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4119f4a2713aSLionel Sambuc     //  A variable that appears in a private clause must not have an incomplete
4120f4a2713aSLionel Sambuc     //  type or a reference type.
4121f4a2713aSLionel Sambuc     if (RequireCompleteType(ELoc, Type,
4122f4a2713aSLionel Sambuc                             diag::err_omp_private_incomplete_type)) {
4123f4a2713aSLionel Sambuc       continue;
4124f4a2713aSLionel Sambuc     }
4125f4a2713aSLionel Sambuc     if (Type->isReferenceType()) {
4126f4a2713aSLionel Sambuc       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4127f4a2713aSLionel Sambuc           << getOpenMPClauseName(OMPC_private) << Type;
4128*0a6a1f1dSLionel Sambuc       bool IsDecl =
4129*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4130*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4131*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4132*0a6a1f1dSLionel Sambuc           << VD;
4133f4a2713aSLionel Sambuc       continue;
4134f4a2713aSLionel Sambuc     }
4135f4a2713aSLionel Sambuc 
4136f4a2713aSLionel Sambuc     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
4137f4a2713aSLionel Sambuc     //  A variable of class type (or array thereof) that appears in a private
4138*0a6a1f1dSLionel Sambuc     //  clause requires an accessible, unambiguous default constructor for the
4139f4a2713aSLionel Sambuc     //  class type.
4140*0a6a1f1dSLionel Sambuc     while (Type->isArrayType()) {
4141*0a6a1f1dSLionel Sambuc       Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
4142f4a2713aSLionel Sambuc     }
4143f4a2713aSLionel Sambuc 
4144f4a2713aSLionel Sambuc     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4145f4a2713aSLionel Sambuc     // in a Construct]
4146f4a2713aSLionel Sambuc     //  Variables with the predetermined data-sharing attributes may not be
4147f4a2713aSLionel Sambuc     //  listed in data-sharing attributes clauses, except for the cases
4148f4a2713aSLionel Sambuc     //  listed below. For these exceptions only, listing a predetermined
4149f4a2713aSLionel Sambuc     //  variable in a data-sharing attribute clause is allowed and overrides
4150f4a2713aSLionel Sambuc     //  the variable's predetermined data-sharing attributes.
4151*0a6a1f1dSLionel Sambuc     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
4152f4a2713aSLionel Sambuc     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
4153*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4154f4a2713aSLionel Sambuc                                           << getOpenMPClauseName(OMPC_private);
4155*0a6a1f1dSLionel Sambuc       ReportOriginalDSA(*this, DSAStack, VD, DVar);
4156f4a2713aSLionel Sambuc       continue;
4157f4a2713aSLionel Sambuc     }
4158f4a2713aSLionel Sambuc 
4159*0a6a1f1dSLionel Sambuc     // Generate helper private variable and initialize it with the default
4160*0a6a1f1dSLionel Sambuc     // value. The address of the original variable is replaced by the address of
4161*0a6a1f1dSLionel Sambuc     // the new private variable in CodeGen. This new variable is not added to
4162*0a6a1f1dSLionel Sambuc     // IdResolver, so the code in the OpenMP region uses original variable for
4163*0a6a1f1dSLionel Sambuc     // proper diagnostics.
4164*0a6a1f1dSLionel Sambuc     auto VDPrivate =
4165*0a6a1f1dSLionel Sambuc         VarDecl::Create(Context, CurContext, DE->getLocStart(),
4166*0a6a1f1dSLionel Sambuc                         DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
4167*0a6a1f1dSLionel Sambuc                         VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4168*0a6a1f1dSLionel Sambuc     ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4169*0a6a1f1dSLionel Sambuc     if (VDPrivate->isInvalidDecl())
4170*0a6a1f1dSLionel Sambuc       continue;
4171*0a6a1f1dSLionel Sambuc     CurContext->addDecl(VDPrivate);
4172*0a6a1f1dSLionel Sambuc     auto VDPrivateRefExpr =
4173*0a6a1f1dSLionel Sambuc         DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4174*0a6a1f1dSLionel Sambuc                             /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4175*0a6a1f1dSLionel Sambuc                             /*RefersToEnclosingVariableOrCapture*/ false,
4176*0a6a1f1dSLionel Sambuc                             /*NameLoc*/ SourceLocation(), DE->getType(),
4177*0a6a1f1dSLionel Sambuc                             /*VK*/ VK_LValue);
4178*0a6a1f1dSLionel Sambuc 
4179f4a2713aSLionel Sambuc     DSAStack->addDSA(VD, DE, OMPC_private);
4180f4a2713aSLionel Sambuc     Vars.push_back(DE);
4181*0a6a1f1dSLionel Sambuc     PrivateCopies.push_back(VDPrivateRefExpr);
4182f4a2713aSLionel Sambuc   }
4183f4a2713aSLionel Sambuc 
4184*0a6a1f1dSLionel Sambuc   if (Vars.empty())
4185*0a6a1f1dSLionel Sambuc     return nullptr;
4186f4a2713aSLionel Sambuc 
4187*0a6a1f1dSLionel Sambuc   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4188*0a6a1f1dSLionel Sambuc                                   PrivateCopies);
4189*0a6a1f1dSLionel Sambuc }
4190*0a6a1f1dSLionel Sambuc 
4191*0a6a1f1dSLionel Sambuc namespace {
4192*0a6a1f1dSLionel Sambuc class DiagsUninitializedSeveretyRAII {
4193*0a6a1f1dSLionel Sambuc private:
4194*0a6a1f1dSLionel Sambuc   DiagnosticsEngine &Diags;
4195*0a6a1f1dSLionel Sambuc   SourceLocation SavedLoc;
4196*0a6a1f1dSLionel Sambuc   bool IsIgnored;
4197*0a6a1f1dSLionel Sambuc 
4198*0a6a1f1dSLionel Sambuc public:
DiagsUninitializedSeveretyRAII(DiagnosticsEngine & Diags,SourceLocation Loc,bool IsIgnored)4199*0a6a1f1dSLionel Sambuc   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4200*0a6a1f1dSLionel Sambuc                                  bool IsIgnored)
4201*0a6a1f1dSLionel Sambuc       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4202*0a6a1f1dSLionel Sambuc     if (!IsIgnored) {
4203*0a6a1f1dSLionel Sambuc       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4204*0a6a1f1dSLionel Sambuc                         /*Map*/ diag::Severity::Ignored, Loc);
4205*0a6a1f1dSLionel Sambuc     }
4206*0a6a1f1dSLionel Sambuc   }
~DiagsUninitializedSeveretyRAII()4207*0a6a1f1dSLionel Sambuc   ~DiagsUninitializedSeveretyRAII() {
4208*0a6a1f1dSLionel Sambuc     if (!IsIgnored)
4209*0a6a1f1dSLionel Sambuc       Diags.popMappings(SavedLoc);
4210*0a6a1f1dSLionel Sambuc   }
4211*0a6a1f1dSLionel Sambuc };
4212f4a2713aSLionel Sambuc }
4213f4a2713aSLionel Sambuc 
ActOnOpenMPFirstprivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)4214f4a2713aSLionel Sambuc OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4215f4a2713aSLionel Sambuc                                                SourceLocation StartLoc,
4216f4a2713aSLionel Sambuc                                                SourceLocation LParenLoc,
4217f4a2713aSLionel Sambuc                                                SourceLocation EndLoc) {
4218f4a2713aSLionel Sambuc   SmallVector<Expr *, 8> Vars;
4219*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> PrivateCopies;
4220*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> Inits;
4221*0a6a1f1dSLionel Sambuc   bool IsImplicitClause =
4222*0a6a1f1dSLionel Sambuc       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4223*0a6a1f1dSLionel Sambuc   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4224*0a6a1f1dSLionel Sambuc 
4225*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
4226*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4227*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4228f4a2713aSLionel Sambuc       // It will be analyzed later.
4229*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
4230*0a6a1f1dSLionel Sambuc       PrivateCopies.push_back(nullptr);
4231*0a6a1f1dSLionel Sambuc       Inits.push_back(nullptr);
4232f4a2713aSLionel Sambuc       continue;
4233f4a2713aSLionel Sambuc     }
4234f4a2713aSLionel Sambuc 
4235*0a6a1f1dSLionel Sambuc     SourceLocation ELoc =
4236*0a6a1f1dSLionel Sambuc         IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
4237f4a2713aSLionel Sambuc     // OpenMP [2.1, C/C++]
4238f4a2713aSLionel Sambuc     //  A list item is a variable name.
4239f4a2713aSLionel Sambuc     // OpenMP  [2.9.3.3, Restrictions, p.1]
4240f4a2713aSLionel Sambuc     //  A variable that is part of another variable (as an array or
4241f4a2713aSLionel Sambuc     //  structure element) cannot appear in a private clause.
4242*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4243f4a2713aSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
4244*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4245f4a2713aSLionel Sambuc       continue;
4246f4a2713aSLionel Sambuc     }
4247f4a2713aSLionel Sambuc     Decl *D = DE->getDecl();
4248f4a2713aSLionel Sambuc     VarDecl *VD = cast<VarDecl>(D);
4249f4a2713aSLionel Sambuc 
4250f4a2713aSLionel Sambuc     QualType Type = VD->getType();
4251f4a2713aSLionel Sambuc     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4252f4a2713aSLionel Sambuc       // It will be analyzed later.
4253f4a2713aSLionel Sambuc       Vars.push_back(DE);
4254*0a6a1f1dSLionel Sambuc       PrivateCopies.push_back(nullptr);
4255*0a6a1f1dSLionel Sambuc       Inits.push_back(nullptr);
4256f4a2713aSLionel Sambuc       continue;
4257f4a2713aSLionel Sambuc     }
4258f4a2713aSLionel Sambuc 
4259f4a2713aSLionel Sambuc     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4260f4a2713aSLionel Sambuc     //  A variable that appears in a private clause must not have an incomplete
4261f4a2713aSLionel Sambuc     //  type or a reference type.
4262f4a2713aSLionel Sambuc     if (RequireCompleteType(ELoc, Type,
4263f4a2713aSLionel Sambuc                             diag::err_omp_firstprivate_incomplete_type)) {
4264f4a2713aSLionel Sambuc       continue;
4265f4a2713aSLionel Sambuc     }
4266f4a2713aSLionel Sambuc     if (Type->isReferenceType()) {
4267*0a6a1f1dSLionel Sambuc       if (IsImplicitClause) {
4268*0a6a1f1dSLionel Sambuc         Diag(ImplicitClauseLoc,
4269*0a6a1f1dSLionel Sambuc              diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4270*0a6a1f1dSLionel Sambuc             << Type;
4271*0a6a1f1dSLionel Sambuc         Diag(RefExpr->getExprLoc(), diag::note_used_here);
4272*0a6a1f1dSLionel Sambuc       } else {
4273f4a2713aSLionel Sambuc         Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4274f4a2713aSLionel Sambuc             << getOpenMPClauseName(OMPC_firstprivate) << Type;
4275*0a6a1f1dSLionel Sambuc       }
4276*0a6a1f1dSLionel Sambuc       bool IsDecl =
4277*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4278*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4279*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4280*0a6a1f1dSLionel Sambuc           << VD;
4281f4a2713aSLionel Sambuc       continue;
4282f4a2713aSLionel Sambuc     }
4283f4a2713aSLionel Sambuc 
4284f4a2713aSLionel Sambuc     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4285f4a2713aSLionel Sambuc     //  A variable of class type (or array thereof) that appears in a private
4286*0a6a1f1dSLionel Sambuc     //  clause requires an accessible, unambiguous copy constructor for the
4287f4a2713aSLionel Sambuc     //  class type.
4288f4a2713aSLionel Sambuc     Type = Context.getBaseElementType(Type);
4289f4a2713aSLionel Sambuc 
4290*0a6a1f1dSLionel Sambuc     // If an implicit firstprivate variable found it was checked already.
4291*0a6a1f1dSLionel Sambuc     if (!IsImplicitClause) {
4292*0a6a1f1dSLionel Sambuc       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
4293f4a2713aSLionel Sambuc       Type = Type.getNonReferenceType().getCanonicalType();
4294f4a2713aSLionel Sambuc       bool IsConstant = Type.isConstant(Context);
4295f4a2713aSLionel Sambuc       Type = Context.getBaseElementType(Type);
4296f4a2713aSLionel Sambuc       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4297f4a2713aSLionel Sambuc       //  A list item that specifies a given variable may not appear in more
4298f4a2713aSLionel Sambuc       // than one clause on the same directive, except that a variable may be
4299f4a2713aSLionel Sambuc       //  specified in both firstprivate and lastprivate clauses.
4300f4a2713aSLionel Sambuc       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
4301*0a6a1f1dSLionel Sambuc           DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
4302f4a2713aSLionel Sambuc         Diag(ELoc, diag::err_omp_wrong_dsa)
4303f4a2713aSLionel Sambuc             << getOpenMPClauseName(DVar.CKind)
4304f4a2713aSLionel Sambuc             << getOpenMPClauseName(OMPC_firstprivate);
4305*0a6a1f1dSLionel Sambuc         ReportOriginalDSA(*this, DSAStack, VD, DVar);
4306f4a2713aSLionel Sambuc         continue;
4307f4a2713aSLionel Sambuc       }
4308f4a2713aSLionel Sambuc 
4309f4a2713aSLionel Sambuc       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4310f4a2713aSLionel Sambuc       // in a Construct]
4311f4a2713aSLionel Sambuc       //  Variables with the predetermined data-sharing attributes may not be
4312f4a2713aSLionel Sambuc       //  listed in data-sharing attributes clauses, except for the cases
4313f4a2713aSLionel Sambuc       //  listed below. For these exceptions only, listing a predetermined
4314f4a2713aSLionel Sambuc       //  variable in a data-sharing attribute clause is allowed and overrides
4315f4a2713aSLionel Sambuc       //  the variable's predetermined data-sharing attributes.
4316f4a2713aSLionel Sambuc       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4317f4a2713aSLionel Sambuc       // in a Construct, C/C++, p.2]
4318f4a2713aSLionel Sambuc       //  Variables with const-qualified type having no mutable member may be
4319f4a2713aSLionel Sambuc       //  listed in a firstprivate clause, even if they are static data members.
4320f4a2713aSLionel Sambuc       if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4321f4a2713aSLionel Sambuc           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4322f4a2713aSLionel Sambuc         Diag(ELoc, diag::err_omp_wrong_dsa)
4323f4a2713aSLionel Sambuc             << getOpenMPClauseName(DVar.CKind)
4324f4a2713aSLionel Sambuc             << getOpenMPClauseName(OMPC_firstprivate);
4325*0a6a1f1dSLionel Sambuc         ReportOriginalDSA(*this, DSAStack, VD, DVar);
4326f4a2713aSLionel Sambuc         continue;
4327f4a2713aSLionel Sambuc       }
4328f4a2713aSLionel Sambuc 
4329*0a6a1f1dSLionel Sambuc       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4330f4a2713aSLionel Sambuc       // OpenMP [2.9.3.4, Restrictions, p.2]
4331f4a2713aSLionel Sambuc       //  A list item that is private within a parallel region must not appear
4332f4a2713aSLionel Sambuc       //  in a firstprivate clause on a worksharing construct if any of the
4333f4a2713aSLionel Sambuc       //  worksharing regions arising from the worksharing construct ever bind
4334f4a2713aSLionel Sambuc       //  to any of the parallel regions arising from the parallel construct.
4335*0a6a1f1dSLionel Sambuc       if (isOpenMPWorksharingDirective(CurrDir) &&
4336*0a6a1f1dSLionel Sambuc           !isOpenMPParallelDirective(CurrDir)) {
4337*0a6a1f1dSLionel Sambuc         DVar = DSAStack->getImplicitDSA(VD, true);
4338*0a6a1f1dSLionel Sambuc         if (DVar.CKind != OMPC_shared &&
4339*0a6a1f1dSLionel Sambuc             (isOpenMPParallelDirective(DVar.DKind) ||
4340*0a6a1f1dSLionel Sambuc              DVar.DKind == OMPD_unknown)) {
4341*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_required_access)
4342*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_firstprivate)
4343*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_shared);
4344*0a6a1f1dSLionel Sambuc           ReportOriginalDSA(*this, DSAStack, VD, DVar);
4345*0a6a1f1dSLionel Sambuc           continue;
4346*0a6a1f1dSLionel Sambuc         }
4347*0a6a1f1dSLionel Sambuc       }
4348f4a2713aSLionel Sambuc       // OpenMP [2.9.3.4, Restrictions, p.3]
4349f4a2713aSLionel Sambuc       //  A list item that appears in a reduction clause of a parallel construct
4350f4a2713aSLionel Sambuc       //  must not appear in a firstprivate clause on a worksharing or task
4351f4a2713aSLionel Sambuc       //  construct if any of the worksharing or task regions arising from the
4352f4a2713aSLionel Sambuc       //  worksharing or task construct ever bind to any of the parallel regions
4353f4a2713aSLionel Sambuc       //  arising from the parallel construct.
4354f4a2713aSLionel Sambuc       // OpenMP [2.9.3.4, Restrictions, p.4]
4355f4a2713aSLionel Sambuc       //  A list item that appears in a reduction clause in worksharing
4356f4a2713aSLionel Sambuc       //  construct must not appear in a firstprivate clause in a task construct
4357f4a2713aSLionel Sambuc       //  encountered during execution of any of the worksharing regions arising
4358f4a2713aSLionel Sambuc       //  from the worksharing construct.
4359*0a6a1f1dSLionel Sambuc       if (CurrDir == OMPD_task) {
4360*0a6a1f1dSLionel Sambuc         DVar =
4361*0a6a1f1dSLionel Sambuc             DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4362*0a6a1f1dSLionel Sambuc                                       [](OpenMPDirectiveKind K) -> bool {
4363*0a6a1f1dSLionel Sambuc                                         return isOpenMPParallelDirective(K) ||
4364*0a6a1f1dSLionel Sambuc                                                isOpenMPWorksharingDirective(K);
4365*0a6a1f1dSLionel Sambuc                                       },
4366*0a6a1f1dSLionel Sambuc                                       false);
4367*0a6a1f1dSLionel Sambuc         if (DVar.CKind == OMPC_reduction &&
4368*0a6a1f1dSLionel Sambuc             (isOpenMPParallelDirective(DVar.DKind) ||
4369*0a6a1f1dSLionel Sambuc              isOpenMPWorksharingDirective(DVar.DKind))) {
4370*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4371*0a6a1f1dSLionel Sambuc               << getOpenMPDirectiveName(DVar.DKind);
4372*0a6a1f1dSLionel Sambuc           ReportOriginalDSA(*this, DSAStack, VD, DVar);
4373*0a6a1f1dSLionel Sambuc           continue;
4374*0a6a1f1dSLionel Sambuc         }
4375*0a6a1f1dSLionel Sambuc       }
4376f4a2713aSLionel Sambuc     }
4377f4a2713aSLionel Sambuc 
4378*0a6a1f1dSLionel Sambuc     Type = Type.getUnqualifiedType();
4379*0a6a1f1dSLionel Sambuc     auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4380*0a6a1f1dSLionel Sambuc                                      ELoc, VD->getIdentifier(), VD->getType(),
4381*0a6a1f1dSLionel Sambuc                                      VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4382*0a6a1f1dSLionel Sambuc     // Generate helper private variable and initialize it with the value of the
4383*0a6a1f1dSLionel Sambuc     // original variable. The address of the original variable is replaced by
4384*0a6a1f1dSLionel Sambuc     // the address of the new private variable in the CodeGen. This new variable
4385*0a6a1f1dSLionel Sambuc     // is not added to IdResolver, so the code in the OpenMP region uses
4386*0a6a1f1dSLionel Sambuc     // original variable for proper diagnostics and variable capturing.
4387*0a6a1f1dSLionel Sambuc     Expr *VDInitRefExpr = nullptr;
4388*0a6a1f1dSLionel Sambuc     // For arrays generate initializer for single element and replace it by the
4389*0a6a1f1dSLionel Sambuc     // original array element in CodeGen.
4390*0a6a1f1dSLionel Sambuc     if (DE->getType()->isArrayType()) {
4391*0a6a1f1dSLionel Sambuc       auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4392*0a6a1f1dSLionel Sambuc                                     ELoc, VD->getIdentifier(), Type,
4393*0a6a1f1dSLionel Sambuc                                     VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4394*0a6a1f1dSLionel Sambuc       CurContext->addHiddenDecl(VDInit);
4395*0a6a1f1dSLionel Sambuc       VDInitRefExpr = DeclRefExpr::Create(
4396*0a6a1f1dSLionel Sambuc           Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4397*0a6a1f1dSLionel Sambuc           /*TemplateKWLoc*/ SourceLocation(), VDInit,
4398*0a6a1f1dSLionel Sambuc           /*RefersToEnclosingVariableOrCapture*/ true, ELoc, Type,
4399*0a6a1f1dSLionel Sambuc           /*VK*/ VK_LValue);
4400*0a6a1f1dSLionel Sambuc       VDInit->setIsUsed();
4401*0a6a1f1dSLionel Sambuc       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4402*0a6a1f1dSLionel Sambuc       InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4403*0a6a1f1dSLionel Sambuc       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4404*0a6a1f1dSLionel Sambuc 
4405*0a6a1f1dSLionel Sambuc       InitializationSequence InitSeq(*this, Entity, Kind, Init);
4406*0a6a1f1dSLionel Sambuc       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4407*0a6a1f1dSLionel Sambuc       if (Result.isInvalid())
4408*0a6a1f1dSLionel Sambuc         VDPrivate->setInvalidDecl();
4409*0a6a1f1dSLionel Sambuc       else
4410*0a6a1f1dSLionel Sambuc         VDPrivate->setInit(Result.getAs<Expr>());
4411*0a6a1f1dSLionel Sambuc     } else {
4412*0a6a1f1dSLionel Sambuc       AddInitializerToDecl(
4413*0a6a1f1dSLionel Sambuc           VDPrivate,
4414*0a6a1f1dSLionel Sambuc           DefaultLvalueConversion(
4415*0a6a1f1dSLionel Sambuc               DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
4416*0a6a1f1dSLionel Sambuc                                   SourceLocation(), DE->getDecl(),
4417*0a6a1f1dSLionel Sambuc                                   /*RefersToEnclosingVariableOrCapture=*/true,
4418*0a6a1f1dSLionel Sambuc                                   DE->getExprLoc(), DE->getType(),
4419*0a6a1f1dSLionel Sambuc                                   /*VK=*/VK_LValue)).get(),
4420*0a6a1f1dSLionel Sambuc           /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
4421*0a6a1f1dSLionel Sambuc     }
4422*0a6a1f1dSLionel Sambuc     if (VDPrivate->isInvalidDecl()) {
4423*0a6a1f1dSLionel Sambuc       if (IsImplicitClause) {
4424*0a6a1f1dSLionel Sambuc         Diag(DE->getExprLoc(),
4425*0a6a1f1dSLionel Sambuc              diag::note_omp_task_predetermined_firstprivate_here);
4426*0a6a1f1dSLionel Sambuc       }
4427*0a6a1f1dSLionel Sambuc       continue;
4428*0a6a1f1dSLionel Sambuc     }
4429*0a6a1f1dSLionel Sambuc     CurContext->addDecl(VDPrivate);
4430*0a6a1f1dSLionel Sambuc     auto VDPrivateRefExpr =
4431*0a6a1f1dSLionel Sambuc         DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4432*0a6a1f1dSLionel Sambuc                             /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4433*0a6a1f1dSLionel Sambuc                             /*RefersToEnclosingVariableOrCapture*/ false,
4434*0a6a1f1dSLionel Sambuc                             DE->getLocStart(), DE->getType(),
4435*0a6a1f1dSLionel Sambuc                             /*VK*/ VK_LValue);
4436f4a2713aSLionel Sambuc     DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4437f4a2713aSLionel Sambuc     Vars.push_back(DE);
4438*0a6a1f1dSLionel Sambuc     PrivateCopies.push_back(VDPrivateRefExpr);
4439*0a6a1f1dSLionel Sambuc     Inits.push_back(VDInitRefExpr);
4440*0a6a1f1dSLionel Sambuc   }
4441*0a6a1f1dSLionel Sambuc 
4442*0a6a1f1dSLionel Sambuc   if (Vars.empty())
4443*0a6a1f1dSLionel Sambuc     return nullptr;
4444*0a6a1f1dSLionel Sambuc 
4445*0a6a1f1dSLionel Sambuc   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4446*0a6a1f1dSLionel Sambuc                                        Vars, PrivateCopies, Inits);
4447*0a6a1f1dSLionel Sambuc }
4448*0a6a1f1dSLionel Sambuc 
ActOnOpenMPLastprivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)4449*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4450*0a6a1f1dSLionel Sambuc                                               SourceLocation StartLoc,
4451*0a6a1f1dSLionel Sambuc                                               SourceLocation LParenLoc,
4452*0a6a1f1dSLionel Sambuc                                               SourceLocation EndLoc) {
4453*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> Vars;
4454*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
4455*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4456*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4457*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
4458*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
4459*0a6a1f1dSLionel Sambuc       continue;
4460*0a6a1f1dSLionel Sambuc     }
4461*0a6a1f1dSLionel Sambuc 
4462*0a6a1f1dSLionel Sambuc     SourceLocation ELoc = RefExpr->getExprLoc();
4463*0a6a1f1dSLionel Sambuc     // OpenMP [2.1, C/C++]
4464*0a6a1f1dSLionel Sambuc     //  A list item is a variable name.
4465*0a6a1f1dSLionel Sambuc     // OpenMP  [2.14.3.5, Restrictions, p.1]
4466*0a6a1f1dSLionel Sambuc     //  A variable that is part of another variable (as an array or structure
4467*0a6a1f1dSLionel Sambuc     //  element) cannot appear in a lastprivate clause.
4468*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4469*0a6a1f1dSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
4470*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4471*0a6a1f1dSLionel Sambuc       continue;
4472*0a6a1f1dSLionel Sambuc     }
4473*0a6a1f1dSLionel Sambuc     Decl *D = DE->getDecl();
4474*0a6a1f1dSLionel Sambuc     VarDecl *VD = cast<VarDecl>(D);
4475*0a6a1f1dSLionel Sambuc 
4476*0a6a1f1dSLionel Sambuc     QualType Type = VD->getType();
4477*0a6a1f1dSLionel Sambuc     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4478*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
4479*0a6a1f1dSLionel Sambuc       Vars.push_back(DE);
4480*0a6a1f1dSLionel Sambuc       continue;
4481*0a6a1f1dSLionel Sambuc     }
4482*0a6a1f1dSLionel Sambuc 
4483*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4484*0a6a1f1dSLionel Sambuc     //  A variable that appears in a lastprivate clause must not have an
4485*0a6a1f1dSLionel Sambuc     //  incomplete type or a reference type.
4486*0a6a1f1dSLionel Sambuc     if (RequireCompleteType(ELoc, Type,
4487*0a6a1f1dSLionel Sambuc                             diag::err_omp_lastprivate_incomplete_type)) {
4488*0a6a1f1dSLionel Sambuc       continue;
4489*0a6a1f1dSLionel Sambuc     }
4490*0a6a1f1dSLionel Sambuc     if (Type->isReferenceType()) {
4491*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4492*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_lastprivate) << Type;
4493*0a6a1f1dSLionel Sambuc       bool IsDecl =
4494*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4495*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4496*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4497*0a6a1f1dSLionel Sambuc           << VD;
4498*0a6a1f1dSLionel Sambuc       continue;
4499*0a6a1f1dSLionel Sambuc     }
4500*0a6a1f1dSLionel Sambuc 
4501*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4502*0a6a1f1dSLionel Sambuc     // in a Construct]
4503*0a6a1f1dSLionel Sambuc     //  Variables with the predetermined data-sharing attributes may not be
4504*0a6a1f1dSLionel Sambuc     //  listed in data-sharing attributes clauses, except for the cases
4505*0a6a1f1dSLionel Sambuc     //  listed below.
4506*0a6a1f1dSLionel Sambuc     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
4507*0a6a1f1dSLionel Sambuc     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4508*0a6a1f1dSLionel Sambuc         DVar.CKind != OMPC_firstprivate &&
4509*0a6a1f1dSLionel Sambuc         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4510*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_wrong_dsa)
4511*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(DVar.CKind)
4512*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_lastprivate);
4513*0a6a1f1dSLionel Sambuc       ReportOriginalDSA(*this, DSAStack, VD, DVar);
4514*0a6a1f1dSLionel Sambuc       continue;
4515*0a6a1f1dSLionel Sambuc     }
4516*0a6a1f1dSLionel Sambuc 
4517*0a6a1f1dSLionel Sambuc     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4518*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.5, Restrictions, p.2]
4519*0a6a1f1dSLionel Sambuc     // A list item that is private within a parallel region, or that appears in
4520*0a6a1f1dSLionel Sambuc     // the reduction clause of a parallel construct, must not appear in a
4521*0a6a1f1dSLionel Sambuc     // lastprivate clause on a worksharing construct if any of the corresponding
4522*0a6a1f1dSLionel Sambuc     // worksharing regions ever binds to any of the corresponding parallel
4523*0a6a1f1dSLionel Sambuc     // regions.
4524*0a6a1f1dSLionel Sambuc     if (isOpenMPWorksharingDirective(CurrDir) &&
4525*0a6a1f1dSLionel Sambuc         !isOpenMPParallelDirective(CurrDir)) {
4526*0a6a1f1dSLionel Sambuc       DVar = DSAStack->getImplicitDSA(VD, true);
4527*0a6a1f1dSLionel Sambuc       if (DVar.CKind != OMPC_shared) {
4528*0a6a1f1dSLionel Sambuc         Diag(ELoc, diag::err_omp_required_access)
4529*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(OMPC_lastprivate)
4530*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(OMPC_shared);
4531*0a6a1f1dSLionel Sambuc         ReportOriginalDSA(*this, DSAStack, VD, DVar);
4532*0a6a1f1dSLionel Sambuc         continue;
4533*0a6a1f1dSLionel Sambuc       }
4534*0a6a1f1dSLionel Sambuc     }
4535*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
4536*0a6a1f1dSLionel Sambuc     //  A variable of class type (or array thereof) that appears in a
4537*0a6a1f1dSLionel Sambuc     //  lastprivate clause requires an accessible, unambiguous default
4538*0a6a1f1dSLionel Sambuc     //  constructor for the class type, unless the list item is also specified
4539*0a6a1f1dSLionel Sambuc     //  in a firstprivate clause.
4540*0a6a1f1dSLionel Sambuc     //  A variable of class type (or array thereof) that appears in a
4541*0a6a1f1dSLionel Sambuc     //  lastprivate clause requires an accessible, unambiguous copy assignment
4542*0a6a1f1dSLionel Sambuc     //  operator for the class type.
4543*0a6a1f1dSLionel Sambuc     while (Type.getNonReferenceType()->isArrayType())
4544*0a6a1f1dSLionel Sambuc       Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4545*0a6a1f1dSLionel Sambuc                  ->getElementType();
4546*0a6a1f1dSLionel Sambuc     CXXRecordDecl *RD = getLangOpts().CPlusPlus
4547*0a6a1f1dSLionel Sambuc                             ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4548*0a6a1f1dSLionel Sambuc                             : nullptr;
4549*0a6a1f1dSLionel Sambuc     // FIXME This code must be replaced by actual copying and destructing of the
4550*0a6a1f1dSLionel Sambuc     // lastprivate variable.
4551*0a6a1f1dSLionel Sambuc     if (RD) {
4552*0a6a1f1dSLionel Sambuc       CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4553*0a6a1f1dSLionel Sambuc       DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4554*0a6a1f1dSLionel Sambuc       if (MD) {
4555*0a6a1f1dSLionel Sambuc         if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4556*0a6a1f1dSLionel Sambuc             MD->isDeleted()) {
4557*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_required_method)
4558*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_lastprivate) << 2;
4559*0a6a1f1dSLionel Sambuc           bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4560*0a6a1f1dSLionel Sambuc                         VarDecl::DeclarationOnly;
4561*0a6a1f1dSLionel Sambuc           Diag(VD->getLocation(),
4562*0a6a1f1dSLionel Sambuc                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4563*0a6a1f1dSLionel Sambuc               << VD;
4564*0a6a1f1dSLionel Sambuc           Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4565*0a6a1f1dSLionel Sambuc           continue;
4566*0a6a1f1dSLionel Sambuc         }
4567*0a6a1f1dSLionel Sambuc         MarkFunctionReferenced(ELoc, MD);
4568*0a6a1f1dSLionel Sambuc         DiagnoseUseOfDecl(MD, ELoc);
4569*0a6a1f1dSLionel Sambuc       }
4570*0a6a1f1dSLionel Sambuc 
4571*0a6a1f1dSLionel Sambuc       CXXDestructorDecl *DD = RD->getDestructor();
4572*0a6a1f1dSLionel Sambuc       if (DD) {
4573*0a6a1f1dSLionel Sambuc         PartialDiagnostic PD =
4574*0a6a1f1dSLionel Sambuc             PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
4575*0a6a1f1dSLionel Sambuc         if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4576*0a6a1f1dSLionel Sambuc             DD->isDeleted()) {
4577*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_required_method)
4578*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_lastprivate) << 4;
4579*0a6a1f1dSLionel Sambuc           bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4580*0a6a1f1dSLionel Sambuc                         VarDecl::DeclarationOnly;
4581*0a6a1f1dSLionel Sambuc           Diag(VD->getLocation(),
4582*0a6a1f1dSLionel Sambuc                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4583*0a6a1f1dSLionel Sambuc               << VD;
4584*0a6a1f1dSLionel Sambuc           Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4585*0a6a1f1dSLionel Sambuc           continue;
4586*0a6a1f1dSLionel Sambuc         }
4587*0a6a1f1dSLionel Sambuc         MarkFunctionReferenced(ELoc, DD);
4588*0a6a1f1dSLionel Sambuc         DiagnoseUseOfDecl(DD, ELoc);
4589*0a6a1f1dSLionel Sambuc       }
4590*0a6a1f1dSLionel Sambuc     }
4591*0a6a1f1dSLionel Sambuc 
4592*0a6a1f1dSLionel Sambuc     if (DVar.CKind != OMPC_firstprivate)
4593*0a6a1f1dSLionel Sambuc       DSAStack->addDSA(VD, DE, OMPC_lastprivate);
4594*0a6a1f1dSLionel Sambuc     Vars.push_back(DE);
4595f4a2713aSLionel Sambuc   }
4596f4a2713aSLionel Sambuc 
4597*0a6a1f1dSLionel Sambuc   if (Vars.empty())
4598*0a6a1f1dSLionel Sambuc     return nullptr;
4599f4a2713aSLionel Sambuc 
4600*0a6a1f1dSLionel Sambuc   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4601f4a2713aSLionel Sambuc                                       Vars);
4602f4a2713aSLionel Sambuc }
4603f4a2713aSLionel Sambuc 
ActOnOpenMPSharedClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)4604f4a2713aSLionel Sambuc OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4605f4a2713aSLionel Sambuc                                          SourceLocation StartLoc,
4606f4a2713aSLionel Sambuc                                          SourceLocation LParenLoc,
4607f4a2713aSLionel Sambuc                                          SourceLocation EndLoc) {
4608f4a2713aSLionel Sambuc   SmallVector<Expr *, 8> Vars;
4609*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
4610*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP shared clause.");
4611*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4612f4a2713aSLionel Sambuc       // It will be analyzed later.
4613*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
4614f4a2713aSLionel Sambuc       continue;
4615f4a2713aSLionel Sambuc     }
4616f4a2713aSLionel Sambuc 
4617*0a6a1f1dSLionel Sambuc     SourceLocation ELoc = RefExpr->getExprLoc();
4618f4a2713aSLionel Sambuc     // OpenMP [2.1, C/C++]
4619f4a2713aSLionel Sambuc     //  A list item is a variable name.
4620*0a6a1f1dSLionel Sambuc     // OpenMP  [2.14.3.2, Restrictions, p.1]
4621*0a6a1f1dSLionel Sambuc     //  A variable that is part of another variable (as an array or structure
4622*0a6a1f1dSLionel Sambuc     //  element) cannot appear in a shared unless it is a static data member
4623*0a6a1f1dSLionel Sambuc     //  of a C++ class.
4624*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4625f4a2713aSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
4626*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4627f4a2713aSLionel Sambuc       continue;
4628f4a2713aSLionel Sambuc     }
4629f4a2713aSLionel Sambuc     Decl *D = DE->getDecl();
4630f4a2713aSLionel Sambuc     VarDecl *VD = cast<VarDecl>(D);
4631f4a2713aSLionel Sambuc 
4632f4a2713aSLionel Sambuc     QualType Type = VD->getType();
4633f4a2713aSLionel Sambuc     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4634f4a2713aSLionel Sambuc       // It will be analyzed later.
4635f4a2713aSLionel Sambuc       Vars.push_back(DE);
4636f4a2713aSLionel Sambuc       continue;
4637f4a2713aSLionel Sambuc     }
4638f4a2713aSLionel Sambuc 
4639f4a2713aSLionel Sambuc     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4640f4a2713aSLionel Sambuc     // in a Construct]
4641f4a2713aSLionel Sambuc     //  Variables with the predetermined data-sharing attributes may not be
4642f4a2713aSLionel Sambuc     //  listed in data-sharing attributes clauses, except for the cases
4643f4a2713aSLionel Sambuc     //  listed below. For these exceptions only, listing a predetermined
4644f4a2713aSLionel Sambuc     //  variable in a data-sharing attribute clause is allowed and overrides
4645f4a2713aSLionel Sambuc     //  the variable's predetermined data-sharing attributes.
4646*0a6a1f1dSLionel Sambuc     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
4647*0a6a1f1dSLionel Sambuc     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4648*0a6a1f1dSLionel Sambuc         DVar.RefExpr) {
4649*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4650f4a2713aSLionel Sambuc                                           << getOpenMPClauseName(OMPC_shared);
4651*0a6a1f1dSLionel Sambuc       ReportOriginalDSA(*this, DSAStack, VD, DVar);
4652f4a2713aSLionel Sambuc       continue;
4653f4a2713aSLionel Sambuc     }
4654f4a2713aSLionel Sambuc 
4655f4a2713aSLionel Sambuc     DSAStack->addDSA(VD, DE, OMPC_shared);
4656f4a2713aSLionel Sambuc     Vars.push_back(DE);
4657f4a2713aSLionel Sambuc   }
4658f4a2713aSLionel Sambuc 
4659*0a6a1f1dSLionel Sambuc   if (Vars.empty())
4660*0a6a1f1dSLionel Sambuc     return nullptr;
4661f4a2713aSLionel Sambuc 
4662f4a2713aSLionel Sambuc   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4663f4a2713aSLionel Sambuc }
4664f4a2713aSLionel Sambuc 
4665*0a6a1f1dSLionel Sambuc namespace {
4666*0a6a1f1dSLionel Sambuc class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4667*0a6a1f1dSLionel Sambuc   DSAStackTy *Stack;
4668*0a6a1f1dSLionel Sambuc 
4669*0a6a1f1dSLionel Sambuc public:
VisitDeclRefExpr(DeclRefExpr * E)4670*0a6a1f1dSLionel Sambuc   bool VisitDeclRefExpr(DeclRefExpr *E) {
4671*0a6a1f1dSLionel Sambuc     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
4672*0a6a1f1dSLionel Sambuc       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
4673*0a6a1f1dSLionel Sambuc       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4674*0a6a1f1dSLionel Sambuc         return false;
4675*0a6a1f1dSLionel Sambuc       if (DVar.CKind != OMPC_unknown)
4676*0a6a1f1dSLionel Sambuc         return true;
4677*0a6a1f1dSLionel Sambuc       DSAStackTy::DSAVarData DVarPrivate =
4678*0a6a1f1dSLionel Sambuc           Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
4679*0a6a1f1dSLionel Sambuc       if (DVarPrivate.CKind != OMPC_unknown)
4680*0a6a1f1dSLionel Sambuc         return true;
4681*0a6a1f1dSLionel Sambuc       return false;
4682*0a6a1f1dSLionel Sambuc     }
4683*0a6a1f1dSLionel Sambuc     return false;
4684*0a6a1f1dSLionel Sambuc   }
VisitStmt(Stmt * S)4685*0a6a1f1dSLionel Sambuc   bool VisitStmt(Stmt *S) {
4686*0a6a1f1dSLionel Sambuc     for (auto Child : S->children()) {
4687*0a6a1f1dSLionel Sambuc       if (Child && Visit(Child))
4688*0a6a1f1dSLionel Sambuc         return true;
4689*0a6a1f1dSLionel Sambuc     }
4690*0a6a1f1dSLionel Sambuc     return false;
4691*0a6a1f1dSLionel Sambuc   }
DSARefChecker(DSAStackTy * S)4692*0a6a1f1dSLionel Sambuc   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
4693*0a6a1f1dSLionel Sambuc };
4694*0a6a1f1dSLionel Sambuc } // namespace
4695*0a6a1f1dSLionel Sambuc 
ActOnOpenMPReductionClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc,CXXScopeSpec & ReductionIdScopeSpec,const DeclarationNameInfo & ReductionId)4696*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPReductionClause(
4697*0a6a1f1dSLionel Sambuc     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4698*0a6a1f1dSLionel Sambuc     SourceLocation ColonLoc, SourceLocation EndLoc,
4699*0a6a1f1dSLionel Sambuc     CXXScopeSpec &ReductionIdScopeSpec,
4700*0a6a1f1dSLionel Sambuc     const DeclarationNameInfo &ReductionId) {
4701*0a6a1f1dSLionel Sambuc   // TODO: Allow scope specification search when 'declare reduction' is
4702*0a6a1f1dSLionel Sambuc   // supported.
4703*0a6a1f1dSLionel Sambuc   assert(ReductionIdScopeSpec.isEmpty() &&
4704*0a6a1f1dSLionel Sambuc          "No support for scoped reduction identifiers yet.");
4705*0a6a1f1dSLionel Sambuc 
4706*0a6a1f1dSLionel Sambuc   auto DN = ReductionId.getName();
4707*0a6a1f1dSLionel Sambuc   auto OOK = DN.getCXXOverloadedOperator();
4708*0a6a1f1dSLionel Sambuc   BinaryOperatorKind BOK = BO_Comma;
4709*0a6a1f1dSLionel Sambuc 
4710*0a6a1f1dSLionel Sambuc   // OpenMP [2.14.3.6, reduction clause]
4711*0a6a1f1dSLionel Sambuc   // C
4712*0a6a1f1dSLionel Sambuc   // reduction-identifier is either an identifier or one of the following
4713*0a6a1f1dSLionel Sambuc   // operators: +, -, *,  &, |, ^, && and ||
4714*0a6a1f1dSLionel Sambuc   // C++
4715*0a6a1f1dSLionel Sambuc   // reduction-identifier is either an id-expression or one of the following
4716*0a6a1f1dSLionel Sambuc   // operators: +, -, *, &, |, ^, && and ||
4717*0a6a1f1dSLionel Sambuc   // FIXME: Only 'min' and 'max' identifiers are supported for now.
4718*0a6a1f1dSLionel Sambuc   switch (OOK) {
4719*0a6a1f1dSLionel Sambuc   case OO_Plus:
4720*0a6a1f1dSLionel Sambuc   case OO_Minus:
4721*0a6a1f1dSLionel Sambuc     BOK = BO_AddAssign;
4722*0a6a1f1dSLionel Sambuc     break;
4723*0a6a1f1dSLionel Sambuc   case OO_Star:
4724*0a6a1f1dSLionel Sambuc     BOK = BO_MulAssign;
4725*0a6a1f1dSLionel Sambuc     break;
4726*0a6a1f1dSLionel Sambuc   case OO_Amp:
4727*0a6a1f1dSLionel Sambuc     BOK = BO_AndAssign;
4728*0a6a1f1dSLionel Sambuc     break;
4729*0a6a1f1dSLionel Sambuc   case OO_Pipe:
4730*0a6a1f1dSLionel Sambuc     BOK = BO_OrAssign;
4731*0a6a1f1dSLionel Sambuc     break;
4732*0a6a1f1dSLionel Sambuc   case OO_Caret:
4733*0a6a1f1dSLionel Sambuc     BOK = BO_XorAssign;
4734*0a6a1f1dSLionel Sambuc     break;
4735*0a6a1f1dSLionel Sambuc   case OO_AmpAmp:
4736*0a6a1f1dSLionel Sambuc     BOK = BO_LAnd;
4737*0a6a1f1dSLionel Sambuc     break;
4738*0a6a1f1dSLionel Sambuc   case OO_PipePipe:
4739*0a6a1f1dSLionel Sambuc     BOK = BO_LOr;
4740*0a6a1f1dSLionel Sambuc     break;
4741*0a6a1f1dSLionel Sambuc   default:
4742*0a6a1f1dSLionel Sambuc     if (auto II = DN.getAsIdentifierInfo()) {
4743*0a6a1f1dSLionel Sambuc       if (II->isStr("max"))
4744*0a6a1f1dSLionel Sambuc         BOK = BO_GT;
4745*0a6a1f1dSLionel Sambuc       else if (II->isStr("min"))
4746*0a6a1f1dSLionel Sambuc         BOK = BO_LT;
4747*0a6a1f1dSLionel Sambuc     }
4748*0a6a1f1dSLionel Sambuc     break;
4749*0a6a1f1dSLionel Sambuc   }
4750*0a6a1f1dSLionel Sambuc   SourceRange ReductionIdRange;
4751*0a6a1f1dSLionel Sambuc   if (ReductionIdScopeSpec.isValid()) {
4752*0a6a1f1dSLionel Sambuc     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4753*0a6a1f1dSLionel Sambuc   }
4754*0a6a1f1dSLionel Sambuc   ReductionIdRange.setEnd(ReductionId.getEndLoc());
4755*0a6a1f1dSLionel Sambuc   if (BOK == BO_Comma) {
4756*0a6a1f1dSLionel Sambuc     // Not allowed reduction identifier is found.
4757*0a6a1f1dSLionel Sambuc     Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4758*0a6a1f1dSLionel Sambuc         << ReductionIdRange;
4759*0a6a1f1dSLionel Sambuc     return nullptr;
4760*0a6a1f1dSLionel Sambuc   }
4761*0a6a1f1dSLionel Sambuc 
4762*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> Vars;
4763*0a6a1f1dSLionel Sambuc   for (auto RefExpr : VarList) {
4764*0a6a1f1dSLionel Sambuc     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4765*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4766*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
4767*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
4768*0a6a1f1dSLionel Sambuc       continue;
4769*0a6a1f1dSLionel Sambuc     }
4770*0a6a1f1dSLionel Sambuc 
4771*0a6a1f1dSLionel Sambuc     if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4772*0a6a1f1dSLionel Sambuc         RefExpr->isInstantiationDependent() ||
4773*0a6a1f1dSLionel Sambuc         RefExpr->containsUnexpandedParameterPack()) {
4774*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
4775*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
4776*0a6a1f1dSLionel Sambuc       continue;
4777*0a6a1f1dSLionel Sambuc     }
4778*0a6a1f1dSLionel Sambuc 
4779*0a6a1f1dSLionel Sambuc     auto ELoc = RefExpr->getExprLoc();
4780*0a6a1f1dSLionel Sambuc     auto ERange = RefExpr->getSourceRange();
4781*0a6a1f1dSLionel Sambuc     // OpenMP [2.1, C/C++]
4782*0a6a1f1dSLionel Sambuc     //  A list item is a variable or array section, subject to the restrictions
4783*0a6a1f1dSLionel Sambuc     //  specified in Section 2.4 on page 42 and in each of the sections
4784*0a6a1f1dSLionel Sambuc     // describing clauses and directives for which a list appears.
4785*0a6a1f1dSLionel Sambuc     // OpenMP  [2.14.3.3, Restrictions, p.1]
4786*0a6a1f1dSLionel Sambuc     //  A variable that is part of another variable (as an array or
4787*0a6a1f1dSLionel Sambuc     //  structure element) cannot appear in a private clause.
4788*0a6a1f1dSLionel Sambuc     auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4789*0a6a1f1dSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
4790*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4791*0a6a1f1dSLionel Sambuc       continue;
4792*0a6a1f1dSLionel Sambuc     }
4793*0a6a1f1dSLionel Sambuc     auto D = DE->getDecl();
4794*0a6a1f1dSLionel Sambuc     auto VD = cast<VarDecl>(D);
4795*0a6a1f1dSLionel Sambuc     auto Type = VD->getType();
4796*0a6a1f1dSLionel Sambuc     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4797*0a6a1f1dSLionel Sambuc     //  A variable that appears in a private clause must not have an incomplete
4798*0a6a1f1dSLionel Sambuc     //  type or a reference type.
4799*0a6a1f1dSLionel Sambuc     if (RequireCompleteType(ELoc, Type,
4800*0a6a1f1dSLionel Sambuc                             diag::err_omp_reduction_incomplete_type))
4801*0a6a1f1dSLionel Sambuc       continue;
4802*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.6, reduction clause, Restrictions]
4803*0a6a1f1dSLionel Sambuc     // Arrays may not appear in a reduction clause.
4804*0a6a1f1dSLionel Sambuc     if (Type.getNonReferenceType()->isArrayType()) {
4805*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4806*0a6a1f1dSLionel Sambuc       bool IsDecl =
4807*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4808*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4809*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4810*0a6a1f1dSLionel Sambuc           << VD;
4811*0a6a1f1dSLionel Sambuc       continue;
4812*0a6a1f1dSLionel Sambuc     }
4813*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.6, reduction clause, Restrictions]
4814*0a6a1f1dSLionel Sambuc     // A list item that appears in a reduction clause must not be
4815*0a6a1f1dSLionel Sambuc     // const-qualified.
4816*0a6a1f1dSLionel Sambuc     if (Type.getNonReferenceType().isConstant(Context)) {
4817*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_const_variable)
4818*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4819*0a6a1f1dSLionel Sambuc       bool IsDecl =
4820*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4821*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4822*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4823*0a6a1f1dSLionel Sambuc           << VD;
4824*0a6a1f1dSLionel Sambuc       continue;
4825*0a6a1f1dSLionel Sambuc     }
4826*0a6a1f1dSLionel Sambuc     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4827*0a6a1f1dSLionel Sambuc     //  If a list-item is a reference type then it must bind to the same object
4828*0a6a1f1dSLionel Sambuc     //  for all threads of the team.
4829*0a6a1f1dSLionel Sambuc     VarDecl *VDDef = VD->getDefinition();
4830*0a6a1f1dSLionel Sambuc     if (Type->isReferenceType() && VDDef) {
4831*0a6a1f1dSLionel Sambuc       DSARefChecker Check(DSAStack);
4832*0a6a1f1dSLionel Sambuc       if (Check.Visit(VDDef->getInit())) {
4833*0a6a1f1dSLionel Sambuc         Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4834*0a6a1f1dSLionel Sambuc         Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4835*0a6a1f1dSLionel Sambuc         continue;
4836*0a6a1f1dSLionel Sambuc       }
4837*0a6a1f1dSLionel Sambuc     }
4838*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.6, reduction clause, Restrictions]
4839*0a6a1f1dSLionel Sambuc     // The type of a list item that appears in a reduction clause must be valid
4840*0a6a1f1dSLionel Sambuc     // for the reduction-identifier. For a max or min reduction in C, the type
4841*0a6a1f1dSLionel Sambuc     // of the list item must be an allowed arithmetic data type: char, int,
4842*0a6a1f1dSLionel Sambuc     // float, double, or _Bool, possibly modified with long, short, signed, or
4843*0a6a1f1dSLionel Sambuc     // unsigned. For a max or min reduction in C++, the type of the list item
4844*0a6a1f1dSLionel Sambuc     // must be an allowed arithmetic data type: char, wchar_t, int, float,
4845*0a6a1f1dSLionel Sambuc     // double, or bool, possibly modified with long, short, signed, or unsigned.
4846*0a6a1f1dSLionel Sambuc     if ((BOK == BO_GT || BOK == BO_LT) &&
4847*0a6a1f1dSLionel Sambuc         !(Type->isScalarType() ||
4848*0a6a1f1dSLionel Sambuc           (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4849*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4850*0a6a1f1dSLionel Sambuc           << getLangOpts().CPlusPlus;
4851*0a6a1f1dSLionel Sambuc       bool IsDecl =
4852*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4853*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4854*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4855*0a6a1f1dSLionel Sambuc           << VD;
4856*0a6a1f1dSLionel Sambuc       continue;
4857*0a6a1f1dSLionel Sambuc     }
4858*0a6a1f1dSLionel Sambuc     if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4859*0a6a1f1dSLionel Sambuc         !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4860*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4861*0a6a1f1dSLionel Sambuc       bool IsDecl =
4862*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4863*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4864*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4865*0a6a1f1dSLionel Sambuc           << VD;
4866*0a6a1f1dSLionel Sambuc       continue;
4867*0a6a1f1dSLionel Sambuc     }
4868*0a6a1f1dSLionel Sambuc     bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4869*0a6a1f1dSLionel Sambuc     getDiagnostics().setSuppressAllDiagnostics(true);
4870*0a6a1f1dSLionel Sambuc     ExprResult ReductionOp =
4871*0a6a1f1dSLionel Sambuc         BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4872*0a6a1f1dSLionel Sambuc                    RefExpr, RefExpr);
4873*0a6a1f1dSLionel Sambuc     getDiagnostics().setSuppressAllDiagnostics(Suppress);
4874*0a6a1f1dSLionel Sambuc     if (ReductionOp.isInvalid()) {
4875*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
4876*0a6a1f1dSLionel Sambuc                                                             << ReductionIdRange;
4877*0a6a1f1dSLionel Sambuc       bool IsDecl =
4878*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4879*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
4880*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4881*0a6a1f1dSLionel Sambuc           << VD;
4882*0a6a1f1dSLionel Sambuc       continue;
4883*0a6a1f1dSLionel Sambuc     }
4884*0a6a1f1dSLionel Sambuc 
4885*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4886*0a6a1f1dSLionel Sambuc     // in a Construct]
4887*0a6a1f1dSLionel Sambuc     //  Variables with the predetermined data-sharing attributes may not be
4888*0a6a1f1dSLionel Sambuc     //  listed in data-sharing attributes clauses, except for the cases
4889*0a6a1f1dSLionel Sambuc     //  listed below. For these exceptions only, listing a predetermined
4890*0a6a1f1dSLionel Sambuc     //  variable in a data-sharing attribute clause is allowed and overrides
4891*0a6a1f1dSLionel Sambuc     //  the variable's predetermined data-sharing attributes.
4892*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.6, Restrictions, p.3]
4893*0a6a1f1dSLionel Sambuc     //  Any number of reduction clauses can be specified on the directive,
4894*0a6a1f1dSLionel Sambuc     //  but a list item can appear only once in the reduction clauses for that
4895*0a6a1f1dSLionel Sambuc     //  directive.
4896*0a6a1f1dSLionel Sambuc     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
4897*0a6a1f1dSLionel Sambuc     if (DVar.CKind == OMPC_reduction) {
4898*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_once_referenced)
4899*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_reduction);
4900*0a6a1f1dSLionel Sambuc       if (DVar.RefExpr) {
4901*0a6a1f1dSLionel Sambuc         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4902*0a6a1f1dSLionel Sambuc       }
4903*0a6a1f1dSLionel Sambuc     } else if (DVar.CKind != OMPC_unknown) {
4904*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_wrong_dsa)
4905*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(DVar.CKind)
4906*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_reduction);
4907*0a6a1f1dSLionel Sambuc       ReportOriginalDSA(*this, DSAStack, VD, DVar);
4908*0a6a1f1dSLionel Sambuc       continue;
4909*0a6a1f1dSLionel Sambuc     }
4910*0a6a1f1dSLionel Sambuc 
4911*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.6, Restrictions, p.1]
4912*0a6a1f1dSLionel Sambuc     //  A list item that appears in a reduction clause of a worksharing
4913*0a6a1f1dSLionel Sambuc     //  construct must be shared in the parallel regions to which any of the
4914*0a6a1f1dSLionel Sambuc     //  worksharing regions arising from the worksharing construct bind.
4915*0a6a1f1dSLionel Sambuc     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4916*0a6a1f1dSLionel Sambuc     if (isOpenMPWorksharingDirective(CurrDir) &&
4917*0a6a1f1dSLionel Sambuc         !isOpenMPParallelDirective(CurrDir)) {
4918*0a6a1f1dSLionel Sambuc       DVar = DSAStack->getImplicitDSA(VD, true);
4919*0a6a1f1dSLionel Sambuc       if (DVar.CKind != OMPC_shared) {
4920*0a6a1f1dSLionel Sambuc         Diag(ELoc, diag::err_omp_required_access)
4921*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(OMPC_reduction)
4922*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(OMPC_shared);
4923*0a6a1f1dSLionel Sambuc         ReportOriginalDSA(*this, DSAStack, VD, DVar);
4924*0a6a1f1dSLionel Sambuc         continue;
4925*0a6a1f1dSLionel Sambuc       }
4926*0a6a1f1dSLionel Sambuc     }
4927*0a6a1f1dSLionel Sambuc 
4928*0a6a1f1dSLionel Sambuc     CXXRecordDecl *RD = getLangOpts().CPlusPlus
4929*0a6a1f1dSLionel Sambuc                             ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4930*0a6a1f1dSLionel Sambuc                             : nullptr;
4931*0a6a1f1dSLionel Sambuc     // FIXME This code must be replaced by actual constructing/destructing of
4932*0a6a1f1dSLionel Sambuc     // the reduction variable.
4933*0a6a1f1dSLionel Sambuc     if (RD) {
4934*0a6a1f1dSLionel Sambuc       CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4935*0a6a1f1dSLionel Sambuc       PartialDiagnostic PD =
4936*0a6a1f1dSLionel Sambuc           PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
4937*0a6a1f1dSLionel Sambuc       if (!CD ||
4938*0a6a1f1dSLionel Sambuc           CheckConstructorAccess(ELoc, CD,
4939*0a6a1f1dSLionel Sambuc                                  InitializedEntity::InitializeTemporary(Type),
4940*0a6a1f1dSLionel Sambuc                                  CD->getAccess(), PD) == AR_inaccessible ||
4941*0a6a1f1dSLionel Sambuc           CD->isDeleted()) {
4942*0a6a1f1dSLionel Sambuc         Diag(ELoc, diag::err_omp_required_method)
4943*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(OMPC_reduction) << 0;
4944*0a6a1f1dSLionel Sambuc         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4945*0a6a1f1dSLionel Sambuc                       VarDecl::DeclarationOnly;
4946*0a6a1f1dSLionel Sambuc         Diag(VD->getLocation(),
4947*0a6a1f1dSLionel Sambuc              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4948*0a6a1f1dSLionel Sambuc             << VD;
4949*0a6a1f1dSLionel Sambuc         Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4950*0a6a1f1dSLionel Sambuc         continue;
4951*0a6a1f1dSLionel Sambuc       }
4952*0a6a1f1dSLionel Sambuc       MarkFunctionReferenced(ELoc, CD);
4953*0a6a1f1dSLionel Sambuc       DiagnoseUseOfDecl(CD, ELoc);
4954*0a6a1f1dSLionel Sambuc 
4955*0a6a1f1dSLionel Sambuc       CXXDestructorDecl *DD = RD->getDestructor();
4956*0a6a1f1dSLionel Sambuc       if (DD) {
4957*0a6a1f1dSLionel Sambuc         if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4958*0a6a1f1dSLionel Sambuc             DD->isDeleted()) {
4959*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_required_method)
4960*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_reduction) << 4;
4961*0a6a1f1dSLionel Sambuc           bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4962*0a6a1f1dSLionel Sambuc                         VarDecl::DeclarationOnly;
4963*0a6a1f1dSLionel Sambuc           Diag(VD->getLocation(),
4964*0a6a1f1dSLionel Sambuc                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4965*0a6a1f1dSLionel Sambuc               << VD;
4966*0a6a1f1dSLionel Sambuc           Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4967*0a6a1f1dSLionel Sambuc           continue;
4968*0a6a1f1dSLionel Sambuc         }
4969*0a6a1f1dSLionel Sambuc         MarkFunctionReferenced(ELoc, DD);
4970*0a6a1f1dSLionel Sambuc         DiagnoseUseOfDecl(DD, ELoc);
4971*0a6a1f1dSLionel Sambuc       }
4972*0a6a1f1dSLionel Sambuc     }
4973*0a6a1f1dSLionel Sambuc 
4974*0a6a1f1dSLionel Sambuc     DSAStack->addDSA(VD, DE, OMPC_reduction);
4975*0a6a1f1dSLionel Sambuc     Vars.push_back(DE);
4976*0a6a1f1dSLionel Sambuc   }
4977*0a6a1f1dSLionel Sambuc 
4978*0a6a1f1dSLionel Sambuc   if (Vars.empty())
4979*0a6a1f1dSLionel Sambuc     return nullptr;
4980*0a6a1f1dSLionel Sambuc 
4981*0a6a1f1dSLionel Sambuc   return OMPReductionClause::Create(
4982*0a6a1f1dSLionel Sambuc       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4983*0a6a1f1dSLionel Sambuc       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4984*0a6a1f1dSLionel Sambuc }
4985*0a6a1f1dSLionel Sambuc 
ActOnOpenMPLinearClause(ArrayRef<Expr * > VarList,Expr * Step,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc)4986*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4987*0a6a1f1dSLionel Sambuc                                          SourceLocation StartLoc,
4988*0a6a1f1dSLionel Sambuc                                          SourceLocation LParenLoc,
4989*0a6a1f1dSLionel Sambuc                                          SourceLocation ColonLoc,
4990*0a6a1f1dSLionel Sambuc                                          SourceLocation EndLoc) {
4991*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> Vars;
4992*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
4993*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP linear clause.");
4994*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4995*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
4996*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
4997*0a6a1f1dSLionel Sambuc       continue;
4998*0a6a1f1dSLionel Sambuc     }
4999*0a6a1f1dSLionel Sambuc 
5000*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.7, linear clause]
5001*0a6a1f1dSLionel Sambuc     // A list item that appears in a linear clause is subject to the private
5002*0a6a1f1dSLionel Sambuc     // clause semantics described in Section 2.14.3.3 on page 159 except as
5003*0a6a1f1dSLionel Sambuc     // noted. In addition, the value of the new list item on each iteration
5004*0a6a1f1dSLionel Sambuc     // of the associated loop(s) corresponds to the value of the original
5005*0a6a1f1dSLionel Sambuc     // list item before entering the construct plus the logical number of
5006*0a6a1f1dSLionel Sambuc     // the iteration times linear-step.
5007*0a6a1f1dSLionel Sambuc 
5008*0a6a1f1dSLionel Sambuc     SourceLocation ELoc = RefExpr->getExprLoc();
5009*0a6a1f1dSLionel Sambuc     // OpenMP [2.1, C/C++]
5010*0a6a1f1dSLionel Sambuc     //  A list item is a variable name.
5011*0a6a1f1dSLionel Sambuc     // OpenMP  [2.14.3.3, Restrictions, p.1]
5012*0a6a1f1dSLionel Sambuc     //  A variable that is part of another variable (as an array or
5013*0a6a1f1dSLionel Sambuc     //  structure element) cannot appear in a private clause.
5014*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5015*0a6a1f1dSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5016*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5017*0a6a1f1dSLionel Sambuc       continue;
5018*0a6a1f1dSLionel Sambuc     }
5019*0a6a1f1dSLionel Sambuc 
5020*0a6a1f1dSLionel Sambuc     VarDecl *VD = cast<VarDecl>(DE->getDecl());
5021*0a6a1f1dSLionel Sambuc 
5022*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.3.7, linear clause]
5023*0a6a1f1dSLionel Sambuc     //  A list-item cannot appear in more than one linear clause.
5024*0a6a1f1dSLionel Sambuc     //  A list-item that appears in a linear clause cannot appear in any
5025*0a6a1f1dSLionel Sambuc     //  other data-sharing attribute clause.
5026*0a6a1f1dSLionel Sambuc     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
5027*0a6a1f1dSLionel Sambuc     if (DVar.RefExpr) {
5028*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5029*0a6a1f1dSLionel Sambuc                                           << getOpenMPClauseName(OMPC_linear);
5030*0a6a1f1dSLionel Sambuc       ReportOriginalDSA(*this, DSAStack, VD, DVar);
5031*0a6a1f1dSLionel Sambuc       continue;
5032*0a6a1f1dSLionel Sambuc     }
5033*0a6a1f1dSLionel Sambuc 
5034*0a6a1f1dSLionel Sambuc     QualType QType = VD->getType();
5035*0a6a1f1dSLionel Sambuc     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
5036*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
5037*0a6a1f1dSLionel Sambuc       Vars.push_back(DE);
5038*0a6a1f1dSLionel Sambuc       continue;
5039*0a6a1f1dSLionel Sambuc     }
5040*0a6a1f1dSLionel Sambuc 
5041*0a6a1f1dSLionel Sambuc     // A variable must not have an incomplete type or a reference type.
5042*0a6a1f1dSLionel Sambuc     if (RequireCompleteType(ELoc, QType,
5043*0a6a1f1dSLionel Sambuc                             diag::err_omp_linear_incomplete_type)) {
5044*0a6a1f1dSLionel Sambuc       continue;
5045*0a6a1f1dSLionel Sambuc     }
5046*0a6a1f1dSLionel Sambuc     if (QType->isReferenceType()) {
5047*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5048*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_linear) << QType;
5049*0a6a1f1dSLionel Sambuc       bool IsDecl =
5050*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5051*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
5052*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5053*0a6a1f1dSLionel Sambuc           << VD;
5054*0a6a1f1dSLionel Sambuc       continue;
5055*0a6a1f1dSLionel Sambuc     }
5056*0a6a1f1dSLionel Sambuc 
5057*0a6a1f1dSLionel Sambuc     // A list item must not be const-qualified.
5058*0a6a1f1dSLionel Sambuc     if (QType.isConstant(Context)) {
5059*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_const_variable)
5060*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_linear);
5061*0a6a1f1dSLionel Sambuc       bool IsDecl =
5062*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5063*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
5064*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5065*0a6a1f1dSLionel Sambuc           << VD;
5066*0a6a1f1dSLionel Sambuc       continue;
5067*0a6a1f1dSLionel Sambuc     }
5068*0a6a1f1dSLionel Sambuc 
5069*0a6a1f1dSLionel Sambuc     // A list item must be of integral or pointer type.
5070*0a6a1f1dSLionel Sambuc     QType = QType.getUnqualifiedType().getCanonicalType();
5071*0a6a1f1dSLionel Sambuc     const Type *Ty = QType.getTypePtrOrNull();
5072*0a6a1f1dSLionel Sambuc     if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
5073*0a6a1f1dSLionel Sambuc                 !Ty->isPointerType())) {
5074*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
5075*0a6a1f1dSLionel Sambuc       bool IsDecl =
5076*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5077*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
5078*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5079*0a6a1f1dSLionel Sambuc           << VD;
5080*0a6a1f1dSLionel Sambuc       continue;
5081*0a6a1f1dSLionel Sambuc     }
5082*0a6a1f1dSLionel Sambuc 
5083*0a6a1f1dSLionel Sambuc     DSAStack->addDSA(VD, DE, OMPC_linear);
5084*0a6a1f1dSLionel Sambuc     Vars.push_back(DE);
5085*0a6a1f1dSLionel Sambuc   }
5086*0a6a1f1dSLionel Sambuc 
5087*0a6a1f1dSLionel Sambuc   if (Vars.empty())
5088*0a6a1f1dSLionel Sambuc     return nullptr;
5089*0a6a1f1dSLionel Sambuc 
5090*0a6a1f1dSLionel Sambuc   Expr *StepExpr = Step;
5091*0a6a1f1dSLionel Sambuc   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5092*0a6a1f1dSLionel Sambuc       !Step->isInstantiationDependent() &&
5093*0a6a1f1dSLionel Sambuc       !Step->containsUnexpandedParameterPack()) {
5094*0a6a1f1dSLionel Sambuc     SourceLocation StepLoc = Step->getLocStart();
5095*0a6a1f1dSLionel Sambuc     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
5096*0a6a1f1dSLionel Sambuc     if (Val.isInvalid())
5097*0a6a1f1dSLionel Sambuc       return nullptr;
5098*0a6a1f1dSLionel Sambuc     StepExpr = Val.get();
5099*0a6a1f1dSLionel Sambuc 
5100*0a6a1f1dSLionel Sambuc     // Warn about zero linear step (it would be probably better specified as
5101*0a6a1f1dSLionel Sambuc     // making corresponding variables 'const').
5102*0a6a1f1dSLionel Sambuc     llvm::APSInt Result;
5103*0a6a1f1dSLionel Sambuc     if (StepExpr->isIntegerConstantExpr(Result, Context) &&
5104*0a6a1f1dSLionel Sambuc         !Result.isNegative() && !Result.isStrictlyPositive())
5105*0a6a1f1dSLionel Sambuc       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
5106*0a6a1f1dSLionel Sambuc                                                      << (Vars.size() > 1);
5107*0a6a1f1dSLionel Sambuc   }
5108*0a6a1f1dSLionel Sambuc 
5109*0a6a1f1dSLionel Sambuc   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
5110*0a6a1f1dSLionel Sambuc                                  Vars, StepExpr);
5111*0a6a1f1dSLionel Sambuc }
5112*0a6a1f1dSLionel Sambuc 
ActOnOpenMPAlignedClause(ArrayRef<Expr * > VarList,Expr * Alignment,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc)5113*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPAlignedClause(
5114*0a6a1f1dSLionel Sambuc     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
5115*0a6a1f1dSLionel Sambuc     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
5116*0a6a1f1dSLionel Sambuc 
5117*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> Vars;
5118*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
5119*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP aligned clause.");
5120*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5121*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
5122*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
5123*0a6a1f1dSLionel Sambuc       continue;
5124*0a6a1f1dSLionel Sambuc     }
5125*0a6a1f1dSLionel Sambuc 
5126*0a6a1f1dSLionel Sambuc     SourceLocation ELoc = RefExpr->getExprLoc();
5127*0a6a1f1dSLionel Sambuc     // OpenMP [2.1, C/C++]
5128*0a6a1f1dSLionel Sambuc     //  A list item is a variable name.
5129*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5130*0a6a1f1dSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5131*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5132*0a6a1f1dSLionel Sambuc       continue;
5133*0a6a1f1dSLionel Sambuc     }
5134*0a6a1f1dSLionel Sambuc 
5135*0a6a1f1dSLionel Sambuc     VarDecl *VD = cast<VarDecl>(DE->getDecl());
5136*0a6a1f1dSLionel Sambuc 
5137*0a6a1f1dSLionel Sambuc     // OpenMP  [2.8.1, simd construct, Restrictions]
5138*0a6a1f1dSLionel Sambuc     // The type of list items appearing in the aligned clause must be
5139*0a6a1f1dSLionel Sambuc     // array, pointer, reference to array, or reference to pointer.
5140*0a6a1f1dSLionel Sambuc     QualType QType = DE->getType()
5141*0a6a1f1dSLionel Sambuc                          .getNonReferenceType()
5142*0a6a1f1dSLionel Sambuc                          .getUnqualifiedType()
5143*0a6a1f1dSLionel Sambuc                          .getCanonicalType();
5144*0a6a1f1dSLionel Sambuc     const Type *Ty = QType.getTypePtrOrNull();
5145*0a6a1f1dSLionel Sambuc     if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
5146*0a6a1f1dSLionel Sambuc                 !Ty->isPointerType())) {
5147*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
5148*0a6a1f1dSLionel Sambuc           << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
5149*0a6a1f1dSLionel Sambuc       bool IsDecl =
5150*0a6a1f1dSLionel Sambuc           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5151*0a6a1f1dSLionel Sambuc       Diag(VD->getLocation(),
5152*0a6a1f1dSLionel Sambuc            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5153*0a6a1f1dSLionel Sambuc           << VD;
5154*0a6a1f1dSLionel Sambuc       continue;
5155*0a6a1f1dSLionel Sambuc     }
5156*0a6a1f1dSLionel Sambuc 
5157*0a6a1f1dSLionel Sambuc     // OpenMP  [2.8.1, simd construct, Restrictions]
5158*0a6a1f1dSLionel Sambuc     // A list-item cannot appear in more than one aligned clause.
5159*0a6a1f1dSLionel Sambuc     if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5160*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5161*0a6a1f1dSLionel Sambuc       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5162*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_aligned);
5163*0a6a1f1dSLionel Sambuc       continue;
5164*0a6a1f1dSLionel Sambuc     }
5165*0a6a1f1dSLionel Sambuc 
5166*0a6a1f1dSLionel Sambuc     Vars.push_back(DE);
5167*0a6a1f1dSLionel Sambuc   }
5168*0a6a1f1dSLionel Sambuc 
5169*0a6a1f1dSLionel Sambuc   // OpenMP [2.8.1, simd construct, Description]
5170*0a6a1f1dSLionel Sambuc   // The parameter of the aligned clause, alignment, must be a constant
5171*0a6a1f1dSLionel Sambuc   // positive integer expression.
5172*0a6a1f1dSLionel Sambuc   // If no optional parameter is specified, implementation-defined default
5173*0a6a1f1dSLionel Sambuc   // alignments for SIMD instructions on the target platforms are assumed.
5174*0a6a1f1dSLionel Sambuc   if (Alignment != nullptr) {
5175*0a6a1f1dSLionel Sambuc     ExprResult AlignResult =
5176*0a6a1f1dSLionel Sambuc         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5177*0a6a1f1dSLionel Sambuc     if (AlignResult.isInvalid())
5178*0a6a1f1dSLionel Sambuc       return nullptr;
5179*0a6a1f1dSLionel Sambuc     Alignment = AlignResult.get();
5180*0a6a1f1dSLionel Sambuc   }
5181*0a6a1f1dSLionel Sambuc   if (Vars.empty())
5182*0a6a1f1dSLionel Sambuc     return nullptr;
5183*0a6a1f1dSLionel Sambuc 
5184*0a6a1f1dSLionel Sambuc   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5185*0a6a1f1dSLionel Sambuc                                   EndLoc, Vars, Alignment);
5186*0a6a1f1dSLionel Sambuc }
5187*0a6a1f1dSLionel Sambuc 
ActOnOpenMPCopyinClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)5188*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5189*0a6a1f1dSLionel Sambuc                                          SourceLocation StartLoc,
5190*0a6a1f1dSLionel Sambuc                                          SourceLocation LParenLoc,
5191*0a6a1f1dSLionel Sambuc                                          SourceLocation EndLoc) {
5192*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> Vars;
5193*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
5194*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5195*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5196*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
5197*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
5198*0a6a1f1dSLionel Sambuc       continue;
5199*0a6a1f1dSLionel Sambuc     }
5200*0a6a1f1dSLionel Sambuc 
5201*0a6a1f1dSLionel Sambuc     SourceLocation ELoc = RefExpr->getExprLoc();
5202*0a6a1f1dSLionel Sambuc     // OpenMP [2.1, C/C++]
5203*0a6a1f1dSLionel Sambuc     //  A list item is a variable name.
5204*0a6a1f1dSLionel Sambuc     // OpenMP  [2.14.4.1, Restrictions, p.1]
5205*0a6a1f1dSLionel Sambuc     //  A list item that appears in a copyin clause must be threadprivate.
5206*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5207*0a6a1f1dSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5208*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5209*0a6a1f1dSLionel Sambuc       continue;
5210*0a6a1f1dSLionel Sambuc     }
5211*0a6a1f1dSLionel Sambuc 
5212*0a6a1f1dSLionel Sambuc     Decl *D = DE->getDecl();
5213*0a6a1f1dSLionel Sambuc     VarDecl *VD = cast<VarDecl>(D);
5214*0a6a1f1dSLionel Sambuc 
5215*0a6a1f1dSLionel Sambuc     QualType Type = VD->getType();
5216*0a6a1f1dSLionel Sambuc     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5217*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
5218*0a6a1f1dSLionel Sambuc       Vars.push_back(DE);
5219*0a6a1f1dSLionel Sambuc       continue;
5220*0a6a1f1dSLionel Sambuc     }
5221*0a6a1f1dSLionel Sambuc 
5222*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5223*0a6a1f1dSLionel Sambuc     //  A list item that appears in a copyin clause must be threadprivate.
5224*0a6a1f1dSLionel Sambuc     if (!DSAStack->isThreadPrivate(VD)) {
5225*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_required_access)
5226*0a6a1f1dSLionel Sambuc           << getOpenMPClauseName(OMPC_copyin)
5227*0a6a1f1dSLionel Sambuc           << getOpenMPDirectiveName(OMPD_threadprivate);
5228*0a6a1f1dSLionel Sambuc       continue;
5229*0a6a1f1dSLionel Sambuc     }
5230*0a6a1f1dSLionel Sambuc 
5231*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5232*0a6a1f1dSLionel Sambuc     //  A variable of class type (or array thereof) that appears in a
5233*0a6a1f1dSLionel Sambuc     //  copyin clause requires an accessible, unambiguous copy assignment
5234*0a6a1f1dSLionel Sambuc     //  operator for the class type.
5235*0a6a1f1dSLionel Sambuc     Type = Context.getBaseElementType(Type);
5236*0a6a1f1dSLionel Sambuc     CXXRecordDecl *RD =
5237*0a6a1f1dSLionel Sambuc         getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5238*0a6a1f1dSLionel Sambuc     // FIXME This code must be replaced by actual assignment of the
5239*0a6a1f1dSLionel Sambuc     // threadprivate variable.
5240*0a6a1f1dSLionel Sambuc     if (RD) {
5241*0a6a1f1dSLionel Sambuc       CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5242*0a6a1f1dSLionel Sambuc       DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5243*0a6a1f1dSLionel Sambuc       if (MD) {
5244*0a6a1f1dSLionel Sambuc         if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5245*0a6a1f1dSLionel Sambuc             MD->isDeleted()) {
5246*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_required_method)
5247*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_copyin) << 2;
5248*0a6a1f1dSLionel Sambuc           bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5249*0a6a1f1dSLionel Sambuc                         VarDecl::DeclarationOnly;
5250*0a6a1f1dSLionel Sambuc           Diag(VD->getLocation(),
5251*0a6a1f1dSLionel Sambuc                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5252*0a6a1f1dSLionel Sambuc               << VD;
5253*0a6a1f1dSLionel Sambuc           Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5254*0a6a1f1dSLionel Sambuc           continue;
5255*0a6a1f1dSLionel Sambuc         }
5256*0a6a1f1dSLionel Sambuc         MarkFunctionReferenced(ELoc, MD);
5257*0a6a1f1dSLionel Sambuc         DiagnoseUseOfDecl(MD, ELoc);
5258*0a6a1f1dSLionel Sambuc       }
5259*0a6a1f1dSLionel Sambuc     }
5260*0a6a1f1dSLionel Sambuc 
5261*0a6a1f1dSLionel Sambuc     DSAStack->addDSA(VD, DE, OMPC_copyin);
5262*0a6a1f1dSLionel Sambuc     Vars.push_back(DE);
5263*0a6a1f1dSLionel Sambuc   }
5264*0a6a1f1dSLionel Sambuc 
5265*0a6a1f1dSLionel Sambuc   if (Vars.empty())
5266*0a6a1f1dSLionel Sambuc     return nullptr;
5267*0a6a1f1dSLionel Sambuc 
5268*0a6a1f1dSLionel Sambuc   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5269*0a6a1f1dSLionel Sambuc }
5270*0a6a1f1dSLionel Sambuc 
ActOnOpenMPCopyprivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)5271*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5272*0a6a1f1dSLionel Sambuc                                               SourceLocation StartLoc,
5273*0a6a1f1dSLionel Sambuc                                               SourceLocation LParenLoc,
5274*0a6a1f1dSLionel Sambuc                                               SourceLocation EndLoc) {
5275*0a6a1f1dSLionel Sambuc   SmallVector<Expr *, 8> Vars;
5276*0a6a1f1dSLionel Sambuc   for (auto &RefExpr : VarList) {
5277*0a6a1f1dSLionel Sambuc     assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5278*0a6a1f1dSLionel Sambuc     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5279*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
5280*0a6a1f1dSLionel Sambuc       Vars.push_back(RefExpr);
5281*0a6a1f1dSLionel Sambuc       continue;
5282*0a6a1f1dSLionel Sambuc     }
5283*0a6a1f1dSLionel Sambuc 
5284*0a6a1f1dSLionel Sambuc     SourceLocation ELoc = RefExpr->getExprLoc();
5285*0a6a1f1dSLionel Sambuc     // OpenMP [2.1, C/C++]
5286*0a6a1f1dSLionel Sambuc     //  A list item is a variable name.
5287*0a6a1f1dSLionel Sambuc     // OpenMP  [2.14.4.1, Restrictions, p.1]
5288*0a6a1f1dSLionel Sambuc     //  A list item that appears in a copyin clause must be threadprivate.
5289*0a6a1f1dSLionel Sambuc     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5290*0a6a1f1dSLionel Sambuc     if (!DE || !isa<VarDecl>(DE->getDecl())) {
5291*0a6a1f1dSLionel Sambuc       Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5292*0a6a1f1dSLionel Sambuc       continue;
5293*0a6a1f1dSLionel Sambuc     }
5294*0a6a1f1dSLionel Sambuc 
5295*0a6a1f1dSLionel Sambuc     Decl *D = DE->getDecl();
5296*0a6a1f1dSLionel Sambuc     VarDecl *VD = cast<VarDecl>(D);
5297*0a6a1f1dSLionel Sambuc 
5298*0a6a1f1dSLionel Sambuc     QualType Type = VD->getType();
5299*0a6a1f1dSLionel Sambuc     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5300*0a6a1f1dSLionel Sambuc       // It will be analyzed later.
5301*0a6a1f1dSLionel Sambuc       Vars.push_back(DE);
5302*0a6a1f1dSLionel Sambuc       continue;
5303*0a6a1f1dSLionel Sambuc     }
5304*0a6a1f1dSLionel Sambuc 
5305*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.4.2, Restrictions, p.2]
5306*0a6a1f1dSLionel Sambuc     //  A list item that appears in a copyprivate clause may not appear in a
5307*0a6a1f1dSLionel Sambuc     //  private or firstprivate clause on the single construct.
5308*0a6a1f1dSLionel Sambuc     if (!DSAStack->isThreadPrivate(VD)) {
5309*0a6a1f1dSLionel Sambuc       auto DVar = DSAStack->getTopDSA(VD, false);
5310*0a6a1f1dSLionel Sambuc       if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5311*0a6a1f1dSLionel Sambuc           !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5312*0a6a1f1dSLionel Sambuc         Diag(ELoc, diag::err_omp_wrong_dsa)
5313*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(DVar.CKind)
5314*0a6a1f1dSLionel Sambuc             << getOpenMPClauseName(OMPC_copyprivate);
5315*0a6a1f1dSLionel Sambuc         ReportOriginalDSA(*this, DSAStack, VD, DVar);
5316*0a6a1f1dSLionel Sambuc         continue;
5317*0a6a1f1dSLionel Sambuc       }
5318*0a6a1f1dSLionel Sambuc 
5319*0a6a1f1dSLionel Sambuc       // OpenMP [2.11.4.2, Restrictions, p.1]
5320*0a6a1f1dSLionel Sambuc       //  All list items that appear in a copyprivate clause must be either
5321*0a6a1f1dSLionel Sambuc       //  threadprivate or private in the enclosing context.
5322*0a6a1f1dSLionel Sambuc       if (DVar.CKind == OMPC_unknown) {
5323*0a6a1f1dSLionel Sambuc         DVar = DSAStack->getImplicitDSA(VD, false);
5324*0a6a1f1dSLionel Sambuc         if (DVar.CKind == OMPC_shared) {
5325*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_required_access)
5326*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_copyprivate)
5327*0a6a1f1dSLionel Sambuc               << "threadprivate or private in the enclosing context";
5328*0a6a1f1dSLionel Sambuc           ReportOriginalDSA(*this, DSAStack, VD, DVar);
5329*0a6a1f1dSLionel Sambuc           continue;
5330*0a6a1f1dSLionel Sambuc         }
5331*0a6a1f1dSLionel Sambuc       }
5332*0a6a1f1dSLionel Sambuc     }
5333*0a6a1f1dSLionel Sambuc 
5334*0a6a1f1dSLionel Sambuc     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5335*0a6a1f1dSLionel Sambuc     //  A variable of class type (or array thereof) that appears in a
5336*0a6a1f1dSLionel Sambuc     //  copyin clause requires an accessible, unambiguous copy assignment
5337*0a6a1f1dSLionel Sambuc     //  operator for the class type.
5338*0a6a1f1dSLionel Sambuc     Type = Context.getBaseElementType(Type);
5339*0a6a1f1dSLionel Sambuc     CXXRecordDecl *RD =
5340*0a6a1f1dSLionel Sambuc         getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5341*0a6a1f1dSLionel Sambuc     // FIXME This code must be replaced by actual assignment of the
5342*0a6a1f1dSLionel Sambuc     // threadprivate variable.
5343*0a6a1f1dSLionel Sambuc     if (RD) {
5344*0a6a1f1dSLionel Sambuc       CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5345*0a6a1f1dSLionel Sambuc       DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5346*0a6a1f1dSLionel Sambuc       if (MD) {
5347*0a6a1f1dSLionel Sambuc         if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5348*0a6a1f1dSLionel Sambuc             MD->isDeleted()) {
5349*0a6a1f1dSLionel Sambuc           Diag(ELoc, diag::err_omp_required_method)
5350*0a6a1f1dSLionel Sambuc               << getOpenMPClauseName(OMPC_copyprivate) << 2;
5351*0a6a1f1dSLionel Sambuc           bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5352*0a6a1f1dSLionel Sambuc                         VarDecl::DeclarationOnly;
5353*0a6a1f1dSLionel Sambuc           Diag(VD->getLocation(),
5354*0a6a1f1dSLionel Sambuc                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5355*0a6a1f1dSLionel Sambuc               << VD;
5356*0a6a1f1dSLionel Sambuc           Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5357*0a6a1f1dSLionel Sambuc           continue;
5358*0a6a1f1dSLionel Sambuc         }
5359*0a6a1f1dSLionel Sambuc         MarkFunctionReferenced(ELoc, MD);
5360*0a6a1f1dSLionel Sambuc         DiagnoseUseOfDecl(MD, ELoc);
5361*0a6a1f1dSLionel Sambuc       }
5362*0a6a1f1dSLionel Sambuc     }
5363*0a6a1f1dSLionel Sambuc 
5364*0a6a1f1dSLionel Sambuc     // No need to mark vars as copyprivate, they are already threadprivate or
5365*0a6a1f1dSLionel Sambuc     // implicitly private.
5366*0a6a1f1dSLionel Sambuc     Vars.push_back(DE);
5367*0a6a1f1dSLionel Sambuc   }
5368*0a6a1f1dSLionel Sambuc 
5369*0a6a1f1dSLionel Sambuc   if (Vars.empty())
5370*0a6a1f1dSLionel Sambuc     return nullptr;
5371*0a6a1f1dSLionel Sambuc 
5372*0a6a1f1dSLionel Sambuc   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5373*0a6a1f1dSLionel Sambuc }
5374*0a6a1f1dSLionel Sambuc 
ActOnOpenMPFlushClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)5375*0a6a1f1dSLionel Sambuc OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5376*0a6a1f1dSLionel Sambuc                                         SourceLocation StartLoc,
5377*0a6a1f1dSLionel Sambuc                                         SourceLocation LParenLoc,
5378*0a6a1f1dSLionel Sambuc                                         SourceLocation EndLoc) {
5379*0a6a1f1dSLionel Sambuc   if (VarList.empty())
5380*0a6a1f1dSLionel Sambuc     return nullptr;
5381*0a6a1f1dSLionel Sambuc 
5382*0a6a1f1dSLionel Sambuc   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5383*0a6a1f1dSLionel Sambuc }
5384*0a6a1f1dSLionel Sambuc 
5385