xref: /aosp_15_r20/external/clang/lib/Sema/SemaOpenMP.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li /// \file
10*67e74705SXin Li /// \brief This file implements semantic analysis for OpenMP directives and
11*67e74705SXin Li /// clauses.
12*67e74705SXin Li ///
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li 
15*67e74705SXin Li #include "TreeTransform.h"
16*67e74705SXin Li #include "clang/AST/ASTContext.h"
17*67e74705SXin Li #include "clang/AST/ASTMutationListener.h"
18*67e74705SXin Li #include "clang/AST/CXXInheritance.h"
19*67e74705SXin Li #include "clang/AST/Decl.h"
20*67e74705SXin Li #include "clang/AST/DeclCXX.h"
21*67e74705SXin Li #include "clang/AST/DeclOpenMP.h"
22*67e74705SXin Li #include "clang/AST/StmtCXX.h"
23*67e74705SXin Li #include "clang/AST/StmtOpenMP.h"
24*67e74705SXin Li #include "clang/AST/StmtVisitor.h"
25*67e74705SXin Li #include "clang/AST/TypeOrdering.h"
26*67e74705SXin Li #include "clang/Basic/OpenMPKinds.h"
27*67e74705SXin Li #include "clang/Basic/TargetInfo.h"
28*67e74705SXin Li #include "clang/Lex/Preprocessor.h"
29*67e74705SXin Li #include "clang/Sema/Initialization.h"
30*67e74705SXin Li #include "clang/Sema/Lookup.h"
31*67e74705SXin Li #include "clang/Sema/Scope.h"
32*67e74705SXin Li #include "clang/Sema/ScopeInfo.h"
33*67e74705SXin Li #include "clang/Sema/SemaInternal.h"
34*67e74705SXin Li using namespace clang;
35*67e74705SXin Li 
36*67e74705SXin Li //===----------------------------------------------------------------------===//
37*67e74705SXin Li // Stack of data-sharing attributes for variables
38*67e74705SXin Li //===----------------------------------------------------------------------===//
39*67e74705SXin Li 
40*67e74705SXin Li namespace {
41*67e74705SXin Li /// \brief Default data sharing attributes, which can be applied to directive.
42*67e74705SXin Li enum DefaultDataSharingAttributes {
43*67e74705SXin Li   DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
44*67e74705SXin Li   DSA_none = 1 << 0,   /// \brief Default data sharing attribute 'none'.
45*67e74705SXin Li   DSA_shared = 1 << 1  /// \brief Default data sharing attribute 'shared'.
46*67e74705SXin Li };
47*67e74705SXin Li 
48*67e74705SXin Li /// \brief Stack for tracking declarations used in OpenMP directives and
49*67e74705SXin Li /// clauses and their data-sharing attributes.
50*67e74705SXin Li class DSAStackTy final {
51*67e74705SXin Li public:
52*67e74705SXin Li   struct DSAVarData final {
53*67e74705SXin Li     OpenMPDirectiveKind DKind = OMPD_unknown;
54*67e74705SXin Li     OpenMPClauseKind CKind = OMPC_unknown;
55*67e74705SXin Li     Expr *RefExpr = nullptr;
56*67e74705SXin Li     DeclRefExpr *PrivateCopy = nullptr;
57*67e74705SXin Li     SourceLocation ImplicitDSALoc;
DSAVarData__anon0fd400dc0111::DSAStackTy::DSAVarData58*67e74705SXin Li     DSAVarData() {}
59*67e74705SXin Li   };
60*67e74705SXin Li   typedef llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>
61*67e74705SXin Li       OperatorOffsetTy;
62*67e74705SXin Li 
63*67e74705SXin Li private:
64*67e74705SXin Li   struct DSAInfo final {
65*67e74705SXin Li     OpenMPClauseKind Attributes = OMPC_unknown;
66*67e74705SXin Li     /// Pointer to a reference expression and a flag which shows that the
67*67e74705SXin Li     /// variable is marked as lastprivate(true) or not (false).
68*67e74705SXin Li     llvm::PointerIntPair<Expr *, 1, bool> RefExpr;
69*67e74705SXin Li     DeclRefExpr *PrivateCopy = nullptr;
70*67e74705SXin Li   };
71*67e74705SXin Li   typedef llvm::DenseMap<ValueDecl *, DSAInfo> DeclSAMapTy;
72*67e74705SXin Li   typedef llvm::DenseMap<ValueDecl *, Expr *> AlignedMapTy;
73*67e74705SXin Li   typedef std::pair<unsigned, VarDecl *> LCDeclInfo;
74*67e74705SXin Li   typedef llvm::DenseMap<ValueDecl *, LCDeclInfo> LoopControlVariablesMapTy;
75*67e74705SXin Li   typedef llvm::DenseMap<
76*67e74705SXin Li       ValueDecl *, OMPClauseMappableExprCommon::MappableExprComponentLists>
77*67e74705SXin Li       MappedExprComponentsTy;
78*67e74705SXin Li   typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
79*67e74705SXin Li       CriticalsWithHintsTy;
80*67e74705SXin Li   typedef llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>
81*67e74705SXin Li       DoacrossDependMapTy;
82*67e74705SXin Li 
83*67e74705SXin Li   struct SharingMapTy final {
84*67e74705SXin Li     DeclSAMapTy SharingMap;
85*67e74705SXin Li     AlignedMapTy AlignedMap;
86*67e74705SXin Li     MappedExprComponentsTy MappedExprComponents;
87*67e74705SXin Li     LoopControlVariablesMapTy LCVMap;
88*67e74705SXin Li     DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
89*67e74705SXin Li     SourceLocation DefaultAttrLoc;
90*67e74705SXin Li     OpenMPDirectiveKind Directive = OMPD_unknown;
91*67e74705SXin Li     DeclarationNameInfo DirectiveName;
92*67e74705SXin Li     Scope *CurScope = nullptr;
93*67e74705SXin Li     SourceLocation ConstructLoc;
94*67e74705SXin Li     /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
95*67e74705SXin Li     /// get the data (loop counters etc.) about enclosing loop-based construct.
96*67e74705SXin Li     /// This data is required during codegen.
97*67e74705SXin Li     DoacrossDependMapTy DoacrossDepends;
98*67e74705SXin Li     /// \brief first argument (Expr *) contains optional argument of the
99*67e74705SXin Li     /// 'ordered' clause, the second one is true if the regions has 'ordered'
100*67e74705SXin Li     /// clause, false otherwise.
101*67e74705SXin Li     llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
102*67e74705SXin Li     bool NowaitRegion = false;
103*67e74705SXin Li     bool CancelRegion = false;
104*67e74705SXin Li     unsigned AssociatedLoops = 1;
105*67e74705SXin Li     SourceLocation InnerTeamsRegionLoc;
SharingMapTy__anon0fd400dc0111::DSAStackTy::SharingMapTy106*67e74705SXin Li     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
107*67e74705SXin Li                  Scope *CurScope, SourceLocation Loc)
108*67e74705SXin Li         : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
109*67e74705SXin Li           ConstructLoc(Loc) {}
SharingMapTy__anon0fd400dc0111::DSAStackTy::SharingMapTy110*67e74705SXin Li     SharingMapTy() {}
111*67e74705SXin Li   };
112*67e74705SXin Li 
113*67e74705SXin Li   typedef SmallVector<SharingMapTy, 4> StackTy;
114*67e74705SXin Li 
115*67e74705SXin Li   /// \brief Stack of used declaration and their data-sharing attributes.
116*67e74705SXin Li   StackTy Stack;
117*67e74705SXin Li   /// \brief true, if check for DSA must be from parent directive, false, if
118*67e74705SXin Li   /// from current directive.
119*67e74705SXin Li   OpenMPClauseKind ClauseKindMode = OMPC_unknown;
120*67e74705SXin Li   Sema &SemaRef;
121*67e74705SXin Li   bool ForceCapturing = false;
122*67e74705SXin Li   CriticalsWithHintsTy Criticals;
123*67e74705SXin Li 
124*67e74705SXin Li   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
125*67e74705SXin Li 
126*67e74705SXin Li   DSAVarData getDSA(StackTy::reverse_iterator& Iter, ValueDecl *D);
127*67e74705SXin Li 
128*67e74705SXin Li   /// \brief Checks if the variable is a local for OpenMP region.
129*67e74705SXin Li   bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
130*67e74705SXin Li 
131*67e74705SXin Li public:
DSAStackTy(Sema & S)132*67e74705SXin Li   explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
133*67e74705SXin Li 
isClauseParsingMode() const134*67e74705SXin Li   bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
setClauseParsingMode(OpenMPClauseKind K)135*67e74705SXin Li   void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
136*67e74705SXin Li 
isForceVarCapturing() const137*67e74705SXin Li   bool isForceVarCapturing() const { return ForceCapturing; }
setForceVarCapturing(bool V)138*67e74705SXin Li   void setForceVarCapturing(bool V) { ForceCapturing = V; }
139*67e74705SXin Li 
push(OpenMPDirectiveKind DKind,const DeclarationNameInfo & DirName,Scope * CurScope,SourceLocation Loc)140*67e74705SXin Li   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
141*67e74705SXin Li             Scope *CurScope, SourceLocation Loc) {
142*67e74705SXin Li     Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
143*67e74705SXin Li     Stack.back().DefaultAttrLoc = Loc;
144*67e74705SXin Li   }
145*67e74705SXin Li 
pop()146*67e74705SXin Li   void pop() {
147*67e74705SXin Li     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
148*67e74705SXin Li     Stack.pop_back();
149*67e74705SXin Li   }
150*67e74705SXin Li 
addCriticalWithHint(OMPCriticalDirective * D,llvm::APSInt Hint)151*67e74705SXin Li   void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
152*67e74705SXin Li     Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
153*67e74705SXin Li   }
154*67e74705SXin Li   const std::pair<OMPCriticalDirective *, llvm::APSInt>
getCriticalWithHint(const DeclarationNameInfo & Name) const155*67e74705SXin Li   getCriticalWithHint(const DeclarationNameInfo &Name) const {
156*67e74705SXin Li     auto I = Criticals.find(Name.getAsString());
157*67e74705SXin Li     if (I != Criticals.end())
158*67e74705SXin Li       return I->second;
159*67e74705SXin Li     return std::make_pair(nullptr, llvm::APSInt());
160*67e74705SXin Li   }
161*67e74705SXin Li   /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
162*67e74705SXin Li   /// add it and return NULL; otherwise return previous occurrence's expression
163*67e74705SXin Li   /// for diagnostics.
164*67e74705SXin Li   Expr *addUniqueAligned(ValueDecl *D, Expr *NewDE);
165*67e74705SXin Li 
166*67e74705SXin Li   /// \brief Register specified variable as loop control variable.
167*67e74705SXin Li   void addLoopControlVariable(ValueDecl *D, VarDecl *Capture);
168*67e74705SXin Li   /// \brief Check if the specified variable is a loop control variable for
169*67e74705SXin Li   /// current region.
170*67e74705SXin Li   /// \return The index of the loop control variable in the list of associated
171*67e74705SXin Li   /// for-loops (from outer to inner).
172*67e74705SXin Li   LCDeclInfo isLoopControlVariable(ValueDecl *D);
173*67e74705SXin Li   /// \brief Check if the specified variable is a loop control variable for
174*67e74705SXin Li   /// parent region.
175*67e74705SXin Li   /// \return The index of the loop control variable in the list of associated
176*67e74705SXin Li   /// for-loops (from outer to inner).
177*67e74705SXin Li   LCDeclInfo isParentLoopControlVariable(ValueDecl *D);
178*67e74705SXin Li   /// \brief Get the loop control variable for the I-th loop (or nullptr) in
179*67e74705SXin Li   /// parent directive.
180*67e74705SXin Li   ValueDecl *getParentLoopControlVariable(unsigned I);
181*67e74705SXin Li 
182*67e74705SXin Li   /// \brief Adds explicit data sharing attribute to the specified declaration.
183*67e74705SXin Li   void addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
184*67e74705SXin Li               DeclRefExpr *PrivateCopy = nullptr);
185*67e74705SXin Li 
186*67e74705SXin Li   /// \brief Returns data sharing attributes from top of the stack for the
187*67e74705SXin Li   /// specified declaration.
188*67e74705SXin Li   DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
189*67e74705SXin Li   /// \brief Returns data-sharing attributes for the specified declaration.
190*67e74705SXin Li   DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent);
191*67e74705SXin Li   /// \brief Checks if the specified variables has data-sharing attributes which
192*67e74705SXin Li   /// match specified \a CPred predicate in any directive which matches \a DPred
193*67e74705SXin Li   /// predicate.
194*67e74705SXin Li   DSAVarData hasDSA(ValueDecl *D,
195*67e74705SXin Li                     const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
196*67e74705SXin Li                     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
197*67e74705SXin Li                     bool FromParent);
198*67e74705SXin Li   /// \brief Checks if the specified variables has data-sharing attributes which
199*67e74705SXin Li   /// match specified \a CPred predicate in any innermost directive which
200*67e74705SXin Li   /// matches \a DPred predicate.
201*67e74705SXin Li   DSAVarData
202*67e74705SXin Li   hasInnermostDSA(ValueDecl *D,
203*67e74705SXin Li                   const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
204*67e74705SXin Li                   const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
205*67e74705SXin Li                   bool FromParent);
206*67e74705SXin Li   /// \brief Checks if the specified variables has explicit data-sharing
207*67e74705SXin Li   /// attributes which match specified \a CPred predicate at the specified
208*67e74705SXin Li   /// OpenMP region.
209*67e74705SXin Li   bool hasExplicitDSA(ValueDecl *D,
210*67e74705SXin Li                       const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
211*67e74705SXin Li                       unsigned Level, bool NotLastprivate = false);
212*67e74705SXin Li 
213*67e74705SXin Li   /// \brief Returns true if the directive at level \Level matches in the
214*67e74705SXin Li   /// specified \a DPred predicate.
215*67e74705SXin Li   bool hasExplicitDirective(
216*67e74705SXin Li       const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
217*67e74705SXin Li       unsigned Level);
218*67e74705SXin Li 
219*67e74705SXin Li   /// \brief Finds a directive which matches specified \a DPred predicate.
220*67e74705SXin Li   bool hasDirective(const llvm::function_ref<bool(OpenMPDirectiveKind,
221*67e74705SXin Li                                                   const DeclarationNameInfo &,
222*67e74705SXin Li                                                   SourceLocation)> &DPred,
223*67e74705SXin Li                     bool FromParent);
224*67e74705SXin Li 
225*67e74705SXin Li   /// \brief Returns currently analyzed directive.
getCurrentDirective() const226*67e74705SXin Li   OpenMPDirectiveKind getCurrentDirective() const {
227*67e74705SXin Li     return Stack.back().Directive;
228*67e74705SXin Li   }
229*67e74705SXin Li   /// \brief Returns parent directive.
getParentDirective() const230*67e74705SXin Li   OpenMPDirectiveKind getParentDirective() const {
231*67e74705SXin Li     if (Stack.size() > 2)
232*67e74705SXin Li       return Stack[Stack.size() - 2].Directive;
233*67e74705SXin Li     return OMPD_unknown;
234*67e74705SXin Li   }
235*67e74705SXin Li 
236*67e74705SXin Li   /// \brief Set default data sharing attribute to none.
setDefaultDSANone(SourceLocation Loc)237*67e74705SXin Li   void setDefaultDSANone(SourceLocation Loc) {
238*67e74705SXin Li     Stack.back().DefaultAttr = DSA_none;
239*67e74705SXin Li     Stack.back().DefaultAttrLoc = Loc;
240*67e74705SXin Li   }
241*67e74705SXin Li   /// \brief Set default data sharing attribute to shared.
setDefaultDSAShared(SourceLocation Loc)242*67e74705SXin Li   void setDefaultDSAShared(SourceLocation Loc) {
243*67e74705SXin Li     Stack.back().DefaultAttr = DSA_shared;
244*67e74705SXin Li     Stack.back().DefaultAttrLoc = Loc;
245*67e74705SXin Li   }
246*67e74705SXin Li 
getDefaultDSA() const247*67e74705SXin Li   DefaultDataSharingAttributes getDefaultDSA() const {
248*67e74705SXin Li     return Stack.back().DefaultAttr;
249*67e74705SXin Li   }
getDefaultDSALocation() const250*67e74705SXin Li   SourceLocation getDefaultDSALocation() const {
251*67e74705SXin Li     return Stack.back().DefaultAttrLoc;
252*67e74705SXin Li   }
253*67e74705SXin Li 
254*67e74705SXin Li   /// \brief Checks if the specified variable is a threadprivate.
isThreadPrivate(VarDecl * D)255*67e74705SXin Li   bool isThreadPrivate(VarDecl *D) {
256*67e74705SXin Li     DSAVarData DVar = getTopDSA(D, false);
257*67e74705SXin Li     return isOpenMPThreadPrivate(DVar.CKind);
258*67e74705SXin Li   }
259*67e74705SXin Li 
260*67e74705SXin Li   /// \brief Marks current region as ordered (it has an 'ordered' clause).
setOrderedRegion(bool IsOrdered,Expr * Param)261*67e74705SXin Li   void setOrderedRegion(bool IsOrdered, Expr *Param) {
262*67e74705SXin Li     Stack.back().OrderedRegion.setInt(IsOrdered);
263*67e74705SXin Li     Stack.back().OrderedRegion.setPointer(Param);
264*67e74705SXin Li   }
265*67e74705SXin Li   /// \brief Returns true, if parent region is ordered (has associated
266*67e74705SXin Li   /// 'ordered' clause), false - otherwise.
isParentOrderedRegion() const267*67e74705SXin Li   bool isParentOrderedRegion() const {
268*67e74705SXin Li     if (Stack.size() > 2)
269*67e74705SXin Li       return Stack[Stack.size() - 2].OrderedRegion.getInt();
270*67e74705SXin Li     return false;
271*67e74705SXin Li   }
272*67e74705SXin Li   /// \brief Returns optional parameter for the ordered region.
getParentOrderedRegionParam() const273*67e74705SXin Li   Expr *getParentOrderedRegionParam() const {
274*67e74705SXin Li     if (Stack.size() > 2)
275*67e74705SXin Li       return Stack[Stack.size() - 2].OrderedRegion.getPointer();
276*67e74705SXin Li     return nullptr;
277*67e74705SXin Li   }
278*67e74705SXin Li   /// \brief Marks current region as nowait (it has a 'nowait' clause).
setNowaitRegion(bool IsNowait=true)279*67e74705SXin Li   void setNowaitRegion(bool IsNowait = true) {
280*67e74705SXin Li     Stack.back().NowaitRegion = IsNowait;
281*67e74705SXin Li   }
282*67e74705SXin Li   /// \brief Returns true, if parent region is nowait (has associated
283*67e74705SXin Li   /// 'nowait' clause), false - otherwise.
isParentNowaitRegion() const284*67e74705SXin Li   bool isParentNowaitRegion() const {
285*67e74705SXin Li     if (Stack.size() > 2)
286*67e74705SXin Li       return Stack[Stack.size() - 2].NowaitRegion;
287*67e74705SXin Li     return false;
288*67e74705SXin Li   }
289*67e74705SXin Li   /// \brief Marks parent region as cancel region.
setParentCancelRegion(bool Cancel=true)290*67e74705SXin Li   void setParentCancelRegion(bool Cancel = true) {
291*67e74705SXin Li     if (Stack.size() > 2)
292*67e74705SXin Li       Stack[Stack.size() - 2].CancelRegion =
293*67e74705SXin Li           Stack[Stack.size() - 2].CancelRegion || Cancel;
294*67e74705SXin Li   }
295*67e74705SXin Li   /// \brief Return true if current region has inner cancel construct.
isCancelRegion() const296*67e74705SXin Li   bool isCancelRegion() const {
297*67e74705SXin Li     return Stack.back().CancelRegion;
298*67e74705SXin Li   }
299*67e74705SXin Li 
300*67e74705SXin Li   /// \brief Set collapse value for the region.
setAssociatedLoops(unsigned Val)301*67e74705SXin Li   void setAssociatedLoops(unsigned Val) { Stack.back().AssociatedLoops = Val; }
302*67e74705SXin Li   /// \brief Return collapse value for region.
getAssociatedLoops() const303*67e74705SXin Li   unsigned getAssociatedLoops() const { return Stack.back().AssociatedLoops; }
304*67e74705SXin Li 
305*67e74705SXin Li   /// \brief Marks current target region as one with closely nested teams
306*67e74705SXin Li   /// region.
setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc)307*67e74705SXin Li   void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
308*67e74705SXin Li     if (Stack.size() > 2)
309*67e74705SXin Li       Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
310*67e74705SXin Li   }
311*67e74705SXin Li   /// \brief Returns true, if current region has closely nested teams region.
hasInnerTeamsRegion() const312*67e74705SXin Li   bool hasInnerTeamsRegion() const {
313*67e74705SXin Li     return getInnerTeamsRegionLoc().isValid();
314*67e74705SXin Li   }
315*67e74705SXin Li   /// \brief Returns location of the nested teams region (if any).
getInnerTeamsRegionLoc() const316*67e74705SXin Li   SourceLocation getInnerTeamsRegionLoc() const {
317*67e74705SXin Li     if (Stack.size() > 1)
318*67e74705SXin Li       return Stack.back().InnerTeamsRegionLoc;
319*67e74705SXin Li     return SourceLocation();
320*67e74705SXin Li   }
321*67e74705SXin Li 
getCurScope() const322*67e74705SXin Li   Scope *getCurScope() const { return Stack.back().CurScope; }
getCurScope()323*67e74705SXin Li   Scope *getCurScope() { return Stack.back().CurScope; }
getConstructLoc()324*67e74705SXin Li   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
325*67e74705SXin Li 
326*67e74705SXin Li   // Do the check specified in \a Check to all component lists and return true
327*67e74705SXin Li   // if any issue is found.
checkMappableExprComponentListsForDecl(ValueDecl * VD,bool CurrentRegionOnly,const llvm::function_ref<bool (OMPClauseMappableExprCommon::MappableExprComponentListRef)> & Check)328*67e74705SXin Li   bool checkMappableExprComponentListsForDecl(
329*67e74705SXin Li       ValueDecl *VD, bool CurrentRegionOnly,
330*67e74705SXin Li       const llvm::function_ref<bool(
331*67e74705SXin Li           OMPClauseMappableExprCommon::MappableExprComponentListRef)> &Check) {
332*67e74705SXin Li     auto SI = Stack.rbegin();
333*67e74705SXin Li     auto SE = Stack.rend();
334*67e74705SXin Li 
335*67e74705SXin Li     if (SI == SE)
336*67e74705SXin Li       return false;
337*67e74705SXin Li 
338*67e74705SXin Li     if (CurrentRegionOnly) {
339*67e74705SXin Li       SE = std::next(SI);
340*67e74705SXin Li     } else {
341*67e74705SXin Li       ++SI;
342*67e74705SXin Li     }
343*67e74705SXin Li 
344*67e74705SXin Li     for (; SI != SE; ++SI) {
345*67e74705SXin Li       auto MI = SI->MappedExprComponents.find(VD);
346*67e74705SXin Li       if (MI != SI->MappedExprComponents.end())
347*67e74705SXin Li         for (auto &L : MI->second)
348*67e74705SXin Li           if (Check(L))
349*67e74705SXin Li             return true;
350*67e74705SXin Li     }
351*67e74705SXin Li     return false;
352*67e74705SXin Li   }
353*67e74705SXin Li 
354*67e74705SXin Li   // Create a new mappable expression component list associated with a given
355*67e74705SXin Li   // declaration and initialize it with the provided list of components.
addMappableExpressionComponents(ValueDecl * VD,OMPClauseMappableExprCommon::MappableExprComponentListRef Components)356*67e74705SXin Li   void addMappableExpressionComponents(
357*67e74705SXin Li       ValueDecl *VD,
358*67e74705SXin Li       OMPClauseMappableExprCommon::MappableExprComponentListRef Components) {
359*67e74705SXin Li     assert(Stack.size() > 1 &&
360*67e74705SXin Li            "Not expecting to retrieve components from a empty stack!");
361*67e74705SXin Li     auto &MEC = Stack.back().MappedExprComponents[VD];
362*67e74705SXin Li     // Create new entry and append the new components there.
363*67e74705SXin Li     MEC.resize(MEC.size() + 1);
364*67e74705SXin Li     MEC.back().append(Components.begin(), Components.end());
365*67e74705SXin Li   }
366*67e74705SXin Li 
getNestingLevel() const367*67e74705SXin Li   unsigned getNestingLevel() const {
368*67e74705SXin Li     assert(Stack.size() > 1);
369*67e74705SXin Li     return Stack.size() - 2;
370*67e74705SXin Li   }
addDoacrossDependClause(OMPDependClause * C,OperatorOffsetTy & OpsOffs)371*67e74705SXin Li   void addDoacrossDependClause(OMPDependClause *C, OperatorOffsetTy &OpsOffs) {
372*67e74705SXin Li     assert(Stack.size() > 2);
373*67e74705SXin Li     assert(isOpenMPWorksharingDirective(Stack[Stack.size() - 2].Directive));
374*67e74705SXin Li     Stack[Stack.size() - 2].DoacrossDepends.insert({C, OpsOffs});
375*67e74705SXin Li   }
376*67e74705SXin Li   llvm::iterator_range<DoacrossDependMapTy::const_iterator>
getDoacrossDependClauses() const377*67e74705SXin Li   getDoacrossDependClauses() const {
378*67e74705SXin Li     assert(Stack.size() > 1);
379*67e74705SXin Li     if (isOpenMPWorksharingDirective(Stack[Stack.size() - 1].Directive)) {
380*67e74705SXin Li       auto &Ref = Stack[Stack.size() - 1].DoacrossDepends;
381*67e74705SXin Li       return llvm::make_range(Ref.begin(), Ref.end());
382*67e74705SXin Li     }
383*67e74705SXin Li     return llvm::make_range(Stack[0].DoacrossDepends.end(),
384*67e74705SXin Li                             Stack[0].DoacrossDepends.end());
385*67e74705SXin Li   }
386*67e74705SXin Li };
isParallelOrTaskRegion(OpenMPDirectiveKind DKind)387*67e74705SXin Li bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
388*67e74705SXin Li   return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
389*67e74705SXin Li          isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
390*67e74705SXin Li }
391*67e74705SXin Li } // namespace
392*67e74705SXin Li 
getCanonicalDecl(ValueDecl * D)393*67e74705SXin Li static ValueDecl *getCanonicalDecl(ValueDecl *D) {
394*67e74705SXin Li   auto *VD = dyn_cast<VarDecl>(D);
395*67e74705SXin Li   auto *FD = dyn_cast<FieldDecl>(D);
396*67e74705SXin Li   if (VD  != nullptr) {
397*67e74705SXin Li     VD = VD->getCanonicalDecl();
398*67e74705SXin Li     D = VD;
399*67e74705SXin Li   } else {
400*67e74705SXin Li     assert(FD);
401*67e74705SXin Li     FD = FD->getCanonicalDecl();
402*67e74705SXin Li     D = FD;
403*67e74705SXin Li   }
404*67e74705SXin Li   return D;
405*67e74705SXin Li }
406*67e74705SXin Li 
getDSA(StackTy::reverse_iterator & Iter,ValueDecl * D)407*67e74705SXin Li DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator& Iter,
408*67e74705SXin Li                                           ValueDecl *D) {
409*67e74705SXin Li   D = getCanonicalDecl(D);
410*67e74705SXin Li   auto *VD = dyn_cast<VarDecl>(D);
411*67e74705SXin Li   auto *FD = dyn_cast<FieldDecl>(D);
412*67e74705SXin Li   DSAVarData DVar;
413*67e74705SXin Li   if (Iter == std::prev(Stack.rend())) {
414*67e74705SXin Li     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
415*67e74705SXin Li     // in a region but not in construct]
416*67e74705SXin Li     //  File-scope or namespace-scope variables referenced in called routines
417*67e74705SXin Li     //  in the region are shared unless they appear in a threadprivate
418*67e74705SXin Li     //  directive.
419*67e74705SXin Li     if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
420*67e74705SXin Li       DVar.CKind = OMPC_shared;
421*67e74705SXin Li 
422*67e74705SXin Li     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
423*67e74705SXin Li     // in a region but not in construct]
424*67e74705SXin Li     //  Variables with static storage duration that are declared in called
425*67e74705SXin Li     //  routines in the region are shared.
426*67e74705SXin Li     if (VD && VD->hasGlobalStorage())
427*67e74705SXin Li       DVar.CKind = OMPC_shared;
428*67e74705SXin Li 
429*67e74705SXin Li     // Non-static data members are shared by default.
430*67e74705SXin Li     if (FD)
431*67e74705SXin Li       DVar.CKind = OMPC_shared;
432*67e74705SXin Li 
433*67e74705SXin Li     return DVar;
434*67e74705SXin Li   }
435*67e74705SXin Li 
436*67e74705SXin Li   DVar.DKind = Iter->Directive;
437*67e74705SXin Li   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
438*67e74705SXin Li   // in a Construct, C/C++, predetermined, p.1]
439*67e74705SXin Li   // Variables with automatic storage duration that are declared in a scope
440*67e74705SXin Li   // inside the construct are private.
441*67e74705SXin Li   if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
442*67e74705SXin Li       (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
443*67e74705SXin Li     DVar.CKind = OMPC_private;
444*67e74705SXin Li     return DVar;
445*67e74705SXin Li   }
446*67e74705SXin Li 
447*67e74705SXin Li   // Explicitly specified attributes and local variables with predetermined
448*67e74705SXin Li   // attributes.
449*67e74705SXin Li   if (Iter->SharingMap.count(D)) {
450*67e74705SXin Li     DVar.RefExpr = Iter->SharingMap[D].RefExpr.getPointer();
451*67e74705SXin Li     DVar.PrivateCopy = Iter->SharingMap[D].PrivateCopy;
452*67e74705SXin Li     DVar.CKind = Iter->SharingMap[D].Attributes;
453*67e74705SXin Li     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
454*67e74705SXin Li     return DVar;
455*67e74705SXin Li   }
456*67e74705SXin Li 
457*67e74705SXin Li   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
458*67e74705SXin Li   // in a Construct, C/C++, implicitly determined, p.1]
459*67e74705SXin Li   //  In a parallel or task construct, the data-sharing attributes of these
460*67e74705SXin Li   //  variables are determined by the default clause, if present.
461*67e74705SXin Li   switch (Iter->DefaultAttr) {
462*67e74705SXin Li   case DSA_shared:
463*67e74705SXin Li     DVar.CKind = OMPC_shared;
464*67e74705SXin Li     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
465*67e74705SXin Li     return DVar;
466*67e74705SXin Li   case DSA_none:
467*67e74705SXin Li     return DVar;
468*67e74705SXin Li   case DSA_unspecified:
469*67e74705SXin Li     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
470*67e74705SXin Li     // in a Construct, implicitly determined, p.2]
471*67e74705SXin Li     //  In a parallel construct, if no default clause is present, these
472*67e74705SXin Li     //  variables are shared.
473*67e74705SXin Li     DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
474*67e74705SXin Li     if (isOpenMPParallelDirective(DVar.DKind) ||
475*67e74705SXin Li         isOpenMPTeamsDirective(DVar.DKind)) {
476*67e74705SXin Li       DVar.CKind = OMPC_shared;
477*67e74705SXin Li       return DVar;
478*67e74705SXin Li     }
479*67e74705SXin Li 
480*67e74705SXin Li     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
481*67e74705SXin Li     // in a Construct, implicitly determined, p.4]
482*67e74705SXin Li     //  In a task construct, if no default clause is present, a variable that in
483*67e74705SXin Li     //  the enclosing context is determined to be shared by all implicit tasks
484*67e74705SXin Li     //  bound to the current team is shared.
485*67e74705SXin Li     if (isOpenMPTaskingDirective(DVar.DKind)) {
486*67e74705SXin Li       DSAVarData DVarTemp;
487*67e74705SXin Li       for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
488*67e74705SXin Li            I != EE; ++I) {
489*67e74705SXin Li         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
490*67e74705SXin Li         // Referenced in a Construct, implicitly determined, p.6]
491*67e74705SXin Li         //  In a task construct, if no default clause is present, a variable
492*67e74705SXin Li         //  whose data-sharing attribute is not determined by the rules above is
493*67e74705SXin Li         //  firstprivate.
494*67e74705SXin Li         DVarTemp = getDSA(I, D);
495*67e74705SXin Li         if (DVarTemp.CKind != OMPC_shared) {
496*67e74705SXin Li           DVar.RefExpr = nullptr;
497*67e74705SXin Li           DVar.CKind = OMPC_firstprivate;
498*67e74705SXin Li           return DVar;
499*67e74705SXin Li         }
500*67e74705SXin Li         if (isParallelOrTaskRegion(I->Directive))
501*67e74705SXin Li           break;
502*67e74705SXin Li       }
503*67e74705SXin Li       DVar.CKind =
504*67e74705SXin Li           (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
505*67e74705SXin Li       return DVar;
506*67e74705SXin Li     }
507*67e74705SXin Li   }
508*67e74705SXin Li   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509*67e74705SXin Li   // in a Construct, implicitly determined, p.3]
510*67e74705SXin Li   //  For constructs other than task, if no default clause is present, these
511*67e74705SXin Li   //  variables inherit their data-sharing attributes from the enclosing
512*67e74705SXin Li   //  context.
513*67e74705SXin Li   return getDSA(++Iter, D);
514*67e74705SXin Li }
515*67e74705SXin Li 
addUniqueAligned(ValueDecl * D,Expr * NewDE)516*67e74705SXin Li Expr *DSAStackTy::addUniqueAligned(ValueDecl *D, Expr *NewDE) {
517*67e74705SXin Li   assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
518*67e74705SXin Li   D = getCanonicalDecl(D);
519*67e74705SXin Li   auto It = Stack.back().AlignedMap.find(D);
520*67e74705SXin Li   if (It == Stack.back().AlignedMap.end()) {
521*67e74705SXin Li     assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
522*67e74705SXin Li     Stack.back().AlignedMap[D] = NewDE;
523*67e74705SXin Li     return nullptr;
524*67e74705SXin Li   } else {
525*67e74705SXin Li     assert(It->second && "Unexpected nullptr expr in the aligned map");
526*67e74705SXin Li     return It->second;
527*67e74705SXin Li   }
528*67e74705SXin Li   return nullptr;
529*67e74705SXin Li }
530*67e74705SXin Li 
addLoopControlVariable(ValueDecl * D,VarDecl * Capture)531*67e74705SXin Li void DSAStackTy::addLoopControlVariable(ValueDecl *D, VarDecl *Capture) {
532*67e74705SXin Li   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
533*67e74705SXin Li   D = getCanonicalDecl(D);
534*67e74705SXin Li   Stack.back().LCVMap.insert(
535*67e74705SXin Li       std::make_pair(D, LCDeclInfo(Stack.back().LCVMap.size() + 1, Capture)));
536*67e74705SXin Li }
537*67e74705SXin Li 
isLoopControlVariable(ValueDecl * D)538*67e74705SXin Li DSAStackTy::LCDeclInfo DSAStackTy::isLoopControlVariable(ValueDecl *D) {
539*67e74705SXin Li   assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
540*67e74705SXin Li   D = getCanonicalDecl(D);
541*67e74705SXin Li   return Stack.back().LCVMap.count(D) > 0 ? Stack.back().LCVMap[D]
542*67e74705SXin Li                                           : LCDeclInfo(0, nullptr);
543*67e74705SXin Li }
544*67e74705SXin Li 
isParentLoopControlVariable(ValueDecl * D)545*67e74705SXin Li DSAStackTy::LCDeclInfo DSAStackTy::isParentLoopControlVariable(ValueDecl *D) {
546*67e74705SXin Li   assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
547*67e74705SXin Li   D = getCanonicalDecl(D);
548*67e74705SXin Li   return Stack[Stack.size() - 2].LCVMap.count(D) > 0
549*67e74705SXin Li              ? Stack[Stack.size() - 2].LCVMap[D]
550*67e74705SXin Li              : LCDeclInfo(0, nullptr);
551*67e74705SXin Li }
552*67e74705SXin Li 
getParentLoopControlVariable(unsigned I)553*67e74705SXin Li ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) {
554*67e74705SXin Li   assert(Stack.size() > 2 && "Data-sharing attributes stack is empty");
555*67e74705SXin Li   if (Stack[Stack.size() - 2].LCVMap.size() < I)
556*67e74705SXin Li     return nullptr;
557*67e74705SXin Li   for (auto &Pair : Stack[Stack.size() - 2].LCVMap) {
558*67e74705SXin Li     if (Pair.second.first == I)
559*67e74705SXin Li       return Pair.first;
560*67e74705SXin Li   }
561*67e74705SXin Li   return nullptr;
562*67e74705SXin Li }
563*67e74705SXin Li 
addDSA(ValueDecl * D,Expr * E,OpenMPClauseKind A,DeclRefExpr * PrivateCopy)564*67e74705SXin Li void DSAStackTy::addDSA(ValueDecl *D, Expr *E, OpenMPClauseKind A,
565*67e74705SXin Li                         DeclRefExpr *PrivateCopy) {
566*67e74705SXin Li   D = getCanonicalDecl(D);
567*67e74705SXin Li   if (A == OMPC_threadprivate) {
568*67e74705SXin Li     auto &Data = Stack[0].SharingMap[D];
569*67e74705SXin Li     Data.Attributes = A;
570*67e74705SXin Li     Data.RefExpr.setPointer(E);
571*67e74705SXin Li     Data.PrivateCopy = nullptr;
572*67e74705SXin Li   } else {
573*67e74705SXin Li     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
574*67e74705SXin Li     auto &Data = Stack.back().SharingMap[D];
575*67e74705SXin Li     assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
576*67e74705SXin Li            (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
577*67e74705SXin Li            (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
578*67e74705SXin Li            (isLoopControlVariable(D).first && A == OMPC_private));
579*67e74705SXin Li     if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
580*67e74705SXin Li       Data.RefExpr.setInt(/*IntVal=*/true);
581*67e74705SXin Li       return;
582*67e74705SXin Li     }
583*67e74705SXin Li     const bool IsLastprivate =
584*67e74705SXin Li         A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
585*67e74705SXin Li     Data.Attributes = A;
586*67e74705SXin Li     Data.RefExpr.setPointerAndInt(E, IsLastprivate);
587*67e74705SXin Li     Data.PrivateCopy = PrivateCopy;
588*67e74705SXin Li     if (PrivateCopy) {
589*67e74705SXin Li       auto &Data = Stack.back().SharingMap[PrivateCopy->getDecl()];
590*67e74705SXin Li       Data.Attributes = A;
591*67e74705SXin Li       Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
592*67e74705SXin Li       Data.PrivateCopy = nullptr;
593*67e74705SXin Li     }
594*67e74705SXin Li   }
595*67e74705SXin Li }
596*67e74705SXin Li 
isOpenMPLocal(VarDecl * D,StackTy::reverse_iterator Iter)597*67e74705SXin Li bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
598*67e74705SXin Li   D = D->getCanonicalDecl();
599*67e74705SXin Li   if (Stack.size() > 2) {
600*67e74705SXin Li     reverse_iterator I = Iter, E = std::prev(Stack.rend());
601*67e74705SXin Li     Scope *TopScope = nullptr;
602*67e74705SXin Li     while (I != E && !isParallelOrTaskRegion(I->Directive)) {
603*67e74705SXin Li       ++I;
604*67e74705SXin Li     }
605*67e74705SXin Li     if (I == E)
606*67e74705SXin Li       return false;
607*67e74705SXin Li     TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
608*67e74705SXin Li     Scope *CurScope = getCurScope();
609*67e74705SXin Li     while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
610*67e74705SXin Li       CurScope = CurScope->getParent();
611*67e74705SXin Li     }
612*67e74705SXin Li     return CurScope != TopScope;
613*67e74705SXin Li   }
614*67e74705SXin Li   return false;
615*67e74705SXin Li }
616*67e74705SXin Li 
617*67e74705SXin Li /// \brief Build a variable declaration for OpenMP loop iteration variable.
buildVarDecl(Sema & SemaRef,SourceLocation Loc,QualType Type,StringRef Name,const AttrVec * Attrs=nullptr)618*67e74705SXin Li static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
619*67e74705SXin Li                              StringRef Name, const AttrVec *Attrs = nullptr) {
620*67e74705SXin Li   DeclContext *DC = SemaRef.CurContext;
621*67e74705SXin Li   IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
622*67e74705SXin Li   TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
623*67e74705SXin Li   VarDecl *Decl =
624*67e74705SXin Li       VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
625*67e74705SXin Li   if (Attrs) {
626*67e74705SXin Li     for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
627*67e74705SXin Li          I != E; ++I)
628*67e74705SXin Li       Decl->addAttr(*I);
629*67e74705SXin Li   }
630*67e74705SXin Li   Decl->setImplicit();
631*67e74705SXin Li   return Decl;
632*67e74705SXin Li }
633*67e74705SXin Li 
buildDeclRefExpr(Sema & S,VarDecl * D,QualType Ty,SourceLocation Loc,bool RefersToCapture=false)634*67e74705SXin Li static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
635*67e74705SXin Li                                      SourceLocation Loc,
636*67e74705SXin Li                                      bool RefersToCapture = false) {
637*67e74705SXin Li   D->setReferenced();
638*67e74705SXin Li   D->markUsed(S.Context);
639*67e74705SXin Li   return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
640*67e74705SXin Li                              SourceLocation(), D, RefersToCapture, Loc, Ty,
641*67e74705SXin Li                              VK_LValue);
642*67e74705SXin Li }
643*67e74705SXin Li 
getTopDSA(ValueDecl * D,bool FromParent)644*67e74705SXin Li DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D, bool FromParent) {
645*67e74705SXin Li   D = getCanonicalDecl(D);
646*67e74705SXin Li   DSAVarData DVar;
647*67e74705SXin Li 
648*67e74705SXin Li   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
649*67e74705SXin Li   // in a Construct, C/C++, predetermined, p.1]
650*67e74705SXin Li   //  Variables appearing in threadprivate directives are threadprivate.
651*67e74705SXin Li   auto *VD = dyn_cast<VarDecl>(D);
652*67e74705SXin Li   if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
653*67e74705SXin Li        !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
654*67e74705SXin Li          SemaRef.getLangOpts().OpenMPUseTLS &&
655*67e74705SXin Li          SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
656*67e74705SXin Li       (VD && VD->getStorageClass() == SC_Register &&
657*67e74705SXin Li        VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
658*67e74705SXin Li     addDSA(D, buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
659*67e74705SXin Li                                D->getLocation()),
660*67e74705SXin Li            OMPC_threadprivate);
661*67e74705SXin Li   }
662*67e74705SXin Li   if (Stack[0].SharingMap.count(D)) {
663*67e74705SXin Li     DVar.RefExpr = Stack[0].SharingMap[D].RefExpr.getPointer();
664*67e74705SXin Li     DVar.CKind = OMPC_threadprivate;
665*67e74705SXin Li     return DVar;
666*67e74705SXin Li   }
667*67e74705SXin Li 
668*67e74705SXin Li   if (Stack.size() == 1) {
669*67e74705SXin Li     // Not in OpenMP execution region and top scope was already checked.
670*67e74705SXin Li     return DVar;
671*67e74705SXin Li   }
672*67e74705SXin Li 
673*67e74705SXin Li   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
674*67e74705SXin Li   // in a Construct, C/C++, predetermined, p.4]
675*67e74705SXin Li   //  Static data members are shared.
676*67e74705SXin Li   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
677*67e74705SXin Li   // in a Construct, C/C++, predetermined, p.7]
678*67e74705SXin Li   //  Variables with static storage duration that are declared in a scope
679*67e74705SXin Li   //  inside the construct are shared.
680*67e74705SXin Li   auto &&MatchesAlways = [](OpenMPDirectiveKind) -> bool { return true; };
681*67e74705SXin Li   if (VD && VD->isStaticDataMember()) {
682*67e74705SXin Li     DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
683*67e74705SXin Li     if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
684*67e74705SXin Li       return DVar;
685*67e74705SXin Li 
686*67e74705SXin Li     DVar.CKind = OMPC_shared;
687*67e74705SXin Li     return DVar;
688*67e74705SXin Li   }
689*67e74705SXin Li 
690*67e74705SXin Li   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
691*67e74705SXin Li   bool IsConstant = Type.isConstant(SemaRef.getASTContext());
692*67e74705SXin Li   Type = SemaRef.getASTContext().getBaseElementType(Type);
693*67e74705SXin Li   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
694*67e74705SXin Li   // in a Construct, C/C++, predetermined, p.6]
695*67e74705SXin Li   //  Variables with const qualified type having no mutable member are
696*67e74705SXin Li   //  shared.
697*67e74705SXin Li   CXXRecordDecl *RD =
698*67e74705SXin Li       SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
699*67e74705SXin Li   if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
700*67e74705SXin Li     if (auto *CTD = CTSD->getSpecializedTemplate())
701*67e74705SXin Li       RD = CTD->getTemplatedDecl();
702*67e74705SXin Li   if (IsConstant &&
703*67e74705SXin Li       !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
704*67e74705SXin Li         RD->hasMutableFields())) {
705*67e74705SXin Li     // Variables with const-qualified type having no mutable member may be
706*67e74705SXin Li     // listed in a firstprivate clause, even if they are static data members.
707*67e74705SXin Li     DSAVarData DVarTemp = hasDSA(
708*67e74705SXin Li         D, [](OpenMPClauseKind C) -> bool { return C == OMPC_firstprivate; },
709*67e74705SXin Li         MatchesAlways, FromParent);
710*67e74705SXin Li     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
711*67e74705SXin Li       return DVar;
712*67e74705SXin Li 
713*67e74705SXin Li     DVar.CKind = OMPC_shared;
714*67e74705SXin Li     return DVar;
715*67e74705SXin Li   }
716*67e74705SXin Li 
717*67e74705SXin Li   // Explicitly specified attributes and local variables with predetermined
718*67e74705SXin Li   // attributes.
719*67e74705SXin Li   auto StartI = std::next(Stack.rbegin());
720*67e74705SXin Li   auto EndI = std::prev(Stack.rend());
721*67e74705SXin Li   if (FromParent && StartI != EndI) {
722*67e74705SXin Li     StartI = std::next(StartI);
723*67e74705SXin Li   }
724*67e74705SXin Li   auto I = std::prev(StartI);
725*67e74705SXin Li   if (I->SharingMap.count(D)) {
726*67e74705SXin Li     DVar.RefExpr = I->SharingMap[D].RefExpr.getPointer();
727*67e74705SXin Li     DVar.PrivateCopy = I->SharingMap[D].PrivateCopy;
728*67e74705SXin Li     DVar.CKind = I->SharingMap[D].Attributes;
729*67e74705SXin Li     DVar.ImplicitDSALoc = I->DefaultAttrLoc;
730*67e74705SXin Li   }
731*67e74705SXin Li 
732*67e74705SXin Li   return DVar;
733*67e74705SXin Li }
734*67e74705SXin Li 
getImplicitDSA(ValueDecl * D,bool FromParent)735*67e74705SXin Li DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
736*67e74705SXin Li                                                   bool FromParent) {
737*67e74705SXin Li   D = getCanonicalDecl(D);
738*67e74705SXin Li   auto StartI = Stack.rbegin();
739*67e74705SXin Li   auto EndI = std::prev(Stack.rend());
740*67e74705SXin Li   if (FromParent && StartI != EndI) {
741*67e74705SXin Li     StartI = std::next(StartI);
742*67e74705SXin Li   }
743*67e74705SXin Li   return getDSA(StartI, D);
744*67e74705SXin Li }
745*67e74705SXin Li 
746*67e74705SXin Li DSAStackTy::DSAVarData
hasDSA(ValueDecl * D,const llvm::function_ref<bool (OpenMPClauseKind)> & CPred,const llvm::function_ref<bool (OpenMPDirectiveKind)> & DPred,bool FromParent)747*67e74705SXin Li DSAStackTy::hasDSA(ValueDecl *D,
748*67e74705SXin Li                    const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
749*67e74705SXin Li                    const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
750*67e74705SXin Li                    bool FromParent) {
751*67e74705SXin Li   D = getCanonicalDecl(D);
752*67e74705SXin Li   auto StartI = std::next(Stack.rbegin());
753*67e74705SXin Li   auto EndI = Stack.rend();
754*67e74705SXin Li   if (FromParent && StartI != EndI) {
755*67e74705SXin Li     StartI = std::next(StartI);
756*67e74705SXin Li   }
757*67e74705SXin Li   for (auto I = StartI, EE = EndI; I != EE; ++I) {
758*67e74705SXin Li     if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
759*67e74705SXin Li       continue;
760*67e74705SXin Li     DSAVarData DVar = getDSA(I, D);
761*67e74705SXin Li     if (CPred(DVar.CKind))
762*67e74705SXin Li       return DVar;
763*67e74705SXin Li   }
764*67e74705SXin Li   return DSAVarData();
765*67e74705SXin Li }
766*67e74705SXin Li 
hasInnermostDSA(ValueDecl * D,const llvm::function_ref<bool (OpenMPClauseKind)> & CPred,const llvm::function_ref<bool (OpenMPDirectiveKind)> & DPred,bool FromParent)767*67e74705SXin Li DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
768*67e74705SXin Li     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
769*67e74705SXin Li     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
770*67e74705SXin Li     bool FromParent) {
771*67e74705SXin Li   D = getCanonicalDecl(D);
772*67e74705SXin Li   auto StartI = std::next(Stack.rbegin());
773*67e74705SXin Li   auto EndI = Stack.rend();
774*67e74705SXin Li   if (FromParent && StartI != EndI) {
775*67e74705SXin Li     StartI = std::next(StartI);
776*67e74705SXin Li   }
777*67e74705SXin Li   for (auto I = StartI, EE = EndI; I != EE; ++I) {
778*67e74705SXin Li     if (!DPred(I->Directive))
779*67e74705SXin Li       break;
780*67e74705SXin Li     DSAVarData DVar = getDSA(I, D);
781*67e74705SXin Li     if (CPred(DVar.CKind))
782*67e74705SXin Li       return DVar;
783*67e74705SXin Li     return DSAVarData();
784*67e74705SXin Li   }
785*67e74705SXin Li   return DSAVarData();
786*67e74705SXin Li }
787*67e74705SXin Li 
hasExplicitDSA(ValueDecl * D,const llvm::function_ref<bool (OpenMPClauseKind)> & CPred,unsigned Level,bool NotLastprivate)788*67e74705SXin Li bool DSAStackTy::hasExplicitDSA(
789*67e74705SXin Li     ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
790*67e74705SXin Li     unsigned Level, bool NotLastprivate) {
791*67e74705SXin Li   if (CPred(ClauseKindMode))
792*67e74705SXin Li     return true;
793*67e74705SXin Li   D = getCanonicalDecl(D);
794*67e74705SXin Li   auto StartI = std::next(Stack.begin());
795*67e74705SXin Li   auto EndI = Stack.end();
796*67e74705SXin Li   if (std::distance(StartI, EndI) <= (int)Level)
797*67e74705SXin Li     return false;
798*67e74705SXin Li   std::advance(StartI, Level);
799*67e74705SXin Li   return (StartI->SharingMap.count(D) > 0) &&
800*67e74705SXin Li          StartI->SharingMap[D].RefExpr.getPointer() &&
801*67e74705SXin Li          CPred(StartI->SharingMap[D].Attributes) &&
802*67e74705SXin Li          (!NotLastprivate || !StartI->SharingMap[D].RefExpr.getInt());
803*67e74705SXin Li }
804*67e74705SXin Li 
hasExplicitDirective(const llvm::function_ref<bool (OpenMPDirectiveKind)> & DPred,unsigned Level)805*67e74705SXin Li bool DSAStackTy::hasExplicitDirective(
806*67e74705SXin Li     const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
807*67e74705SXin Li     unsigned Level) {
808*67e74705SXin Li   auto StartI = std::next(Stack.begin());
809*67e74705SXin Li   auto EndI = Stack.end();
810*67e74705SXin Li   if (std::distance(StartI, EndI) <= (int)Level)
811*67e74705SXin Li     return false;
812*67e74705SXin Li   std::advance(StartI, Level);
813*67e74705SXin Li   return DPred(StartI->Directive);
814*67e74705SXin Li }
815*67e74705SXin Li 
hasDirective(const llvm::function_ref<bool (OpenMPDirectiveKind,const DeclarationNameInfo &,SourceLocation)> & DPred,bool FromParent)816*67e74705SXin Li bool DSAStackTy::hasDirective(
817*67e74705SXin Li     const llvm::function_ref<bool(OpenMPDirectiveKind,
818*67e74705SXin Li                                   const DeclarationNameInfo &, SourceLocation)>
819*67e74705SXin Li         &DPred,
820*67e74705SXin Li     bool FromParent) {
821*67e74705SXin Li   // We look only in the enclosing region.
822*67e74705SXin Li   if (Stack.size() < 2)
823*67e74705SXin Li     return false;
824*67e74705SXin Li   auto StartI = std::next(Stack.rbegin());
825*67e74705SXin Li   auto EndI = std::prev(Stack.rend());
826*67e74705SXin Li   if (FromParent && StartI != EndI) {
827*67e74705SXin Li     StartI = std::next(StartI);
828*67e74705SXin Li   }
829*67e74705SXin Li   for (auto I = StartI, EE = EndI; I != EE; ++I) {
830*67e74705SXin Li     if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
831*67e74705SXin Li       return true;
832*67e74705SXin Li   }
833*67e74705SXin Li   return false;
834*67e74705SXin Li }
835*67e74705SXin Li 
InitDataSharingAttributesStack()836*67e74705SXin Li void Sema::InitDataSharingAttributesStack() {
837*67e74705SXin Li   VarDataSharingAttributesStack = new DSAStackTy(*this);
838*67e74705SXin Li }
839*67e74705SXin Li 
840*67e74705SXin Li #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
841*67e74705SXin Li 
IsOpenMPCapturedByRef(ValueDecl * D,unsigned Level)842*67e74705SXin Li bool Sema::IsOpenMPCapturedByRef(ValueDecl *D, unsigned Level) {
843*67e74705SXin Li   assert(LangOpts.OpenMP && "OpenMP is not allowed");
844*67e74705SXin Li 
845*67e74705SXin Li   auto &Ctx = getASTContext();
846*67e74705SXin Li   bool IsByRef = true;
847*67e74705SXin Li 
848*67e74705SXin Li   // Find the directive that is associated with the provided scope.
849*67e74705SXin Li   auto Ty = D->getType();
850*67e74705SXin Li 
851*67e74705SXin Li   if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
852*67e74705SXin Li     // This table summarizes how a given variable should be passed to the device
853*67e74705SXin Li     // given its type and the clauses where it appears. This table is based on
854*67e74705SXin Li     // the description in OpenMP 4.5 [2.10.4, target Construct] and
855*67e74705SXin Li     // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
856*67e74705SXin Li     //
857*67e74705SXin Li     // =========================================================================
858*67e74705SXin Li     // | type |  defaultmap   | pvt | first | is_device_ptr |    map   | res.  |
859*67e74705SXin Li     // |      |(tofrom:scalar)|     |  pvt  |               |          |       |
860*67e74705SXin Li     // =========================================================================
861*67e74705SXin Li     // | scl  |               |     |       |       -       |          | bycopy|
862*67e74705SXin Li     // | scl  |               |  -  |   x   |       -       |     -    | bycopy|
863*67e74705SXin Li     // | scl  |               |  x  |   -   |       -       |     -    | null  |
864*67e74705SXin Li     // | scl  |       x       |     |       |       -       |          | byref |
865*67e74705SXin Li     // | scl  |       x       |  -  |   x   |       -       |     -    | bycopy|
866*67e74705SXin Li     // | scl  |       x       |  x  |   -   |       -       |     -    | null  |
867*67e74705SXin Li     // | scl  |               |  -  |   -   |       -       |     x    | byref |
868*67e74705SXin Li     // | scl  |       x       |  -  |   -   |       -       |     x    | byref |
869*67e74705SXin Li     //
870*67e74705SXin Li     // | agg  |      n.a.     |     |       |       -       |          | byref |
871*67e74705SXin Li     // | agg  |      n.a.     |  -  |   x   |       -       |     -    | byref |
872*67e74705SXin Li     // | agg  |      n.a.     |  x  |   -   |       -       |     -    | null  |
873*67e74705SXin Li     // | agg  |      n.a.     |  -  |   -   |       -       |     x    | byref |
874*67e74705SXin Li     // | agg  |      n.a.     |  -  |   -   |       -       |    x[]   | byref |
875*67e74705SXin Li     //
876*67e74705SXin Li     // | ptr  |      n.a.     |     |       |       -       |          | bycopy|
877*67e74705SXin Li     // | ptr  |      n.a.     |  -  |   x   |       -       |     -    | bycopy|
878*67e74705SXin Li     // | ptr  |      n.a.     |  x  |   -   |       -       |     -    | null  |
879*67e74705SXin Li     // | ptr  |      n.a.     |  -  |   -   |       -       |     x    | byref |
880*67e74705SXin Li     // | ptr  |      n.a.     |  -  |   -   |       -       |    x[]   | bycopy|
881*67e74705SXin Li     // | ptr  |      n.a.     |  -  |   -   |       x       |          | bycopy|
882*67e74705SXin Li     // | ptr  |      n.a.     |  -  |   -   |       x       |     x    | bycopy|
883*67e74705SXin Li     // | ptr  |      n.a.     |  -  |   -   |       x       |    x[]   | bycopy|
884*67e74705SXin Li     // =========================================================================
885*67e74705SXin Li     // Legend:
886*67e74705SXin Li     //  scl - scalar
887*67e74705SXin Li     //  ptr - pointer
888*67e74705SXin Li     //  agg - aggregate
889*67e74705SXin Li     //  x - applies
890*67e74705SXin Li     //  - - invalid in this combination
891*67e74705SXin Li     //  [] - mapped with an array section
892*67e74705SXin Li     //  byref - should be mapped by reference
893*67e74705SXin Li     //  byval - should be mapped by value
894*67e74705SXin Li     //  null - initialize a local variable to null on the device
895*67e74705SXin Li     //
896*67e74705SXin Li     // Observations:
897*67e74705SXin Li     //  - All scalar declarations that show up in a map clause have to be passed
898*67e74705SXin Li     //    by reference, because they may have been mapped in the enclosing data
899*67e74705SXin Li     //    environment.
900*67e74705SXin Li     //  - If the scalar value does not fit the size of uintptr, it has to be
901*67e74705SXin Li     //    passed by reference, regardless the result in the table above.
902*67e74705SXin Li     //  - For pointers mapped by value that have either an implicit map or an
903*67e74705SXin Li     //    array section, the runtime library may pass the NULL value to the
904*67e74705SXin Li     //    device instead of the value passed to it by the compiler.
905*67e74705SXin Li 
906*67e74705SXin Li 
907*67e74705SXin Li     if (Ty->isReferenceType())
908*67e74705SXin Li       Ty = Ty->castAs<ReferenceType>()->getPointeeType();
909*67e74705SXin Li 
910*67e74705SXin Li     // Locate map clauses and see if the variable being captured is referred to
911*67e74705SXin Li     // in any of those clauses. Here we only care about variables, not fields,
912*67e74705SXin Li     // because fields are part of aggregates.
913*67e74705SXin Li     bool IsVariableUsedInMapClause = false;
914*67e74705SXin Li     bool IsVariableAssociatedWithSection = false;
915*67e74705SXin Li 
916*67e74705SXin Li     DSAStack->checkMappableExprComponentListsForDecl(
917*67e74705SXin Li         D, /*CurrentRegionOnly=*/true,
918*67e74705SXin Li         [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
919*67e74705SXin Li                 MapExprComponents) {
920*67e74705SXin Li 
921*67e74705SXin Li           auto EI = MapExprComponents.rbegin();
922*67e74705SXin Li           auto EE = MapExprComponents.rend();
923*67e74705SXin Li 
924*67e74705SXin Li           assert(EI != EE && "Invalid map expression!");
925*67e74705SXin Li 
926*67e74705SXin Li           if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
927*67e74705SXin Li             IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
928*67e74705SXin Li 
929*67e74705SXin Li           ++EI;
930*67e74705SXin Li           if (EI == EE)
931*67e74705SXin Li             return false;
932*67e74705SXin Li 
933*67e74705SXin Li           if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
934*67e74705SXin Li               isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
935*67e74705SXin Li               isa<MemberExpr>(EI->getAssociatedExpression())) {
936*67e74705SXin Li             IsVariableAssociatedWithSection = true;
937*67e74705SXin Li             // There is nothing more we need to know about this variable.
938*67e74705SXin Li             return true;
939*67e74705SXin Li           }
940*67e74705SXin Li 
941*67e74705SXin Li           // Keep looking for more map info.
942*67e74705SXin Li           return false;
943*67e74705SXin Li         });
944*67e74705SXin Li 
945*67e74705SXin Li     if (IsVariableUsedInMapClause) {
946*67e74705SXin Li       // If variable is identified in a map clause it is always captured by
947*67e74705SXin Li       // reference except if it is a pointer that is dereferenced somehow.
948*67e74705SXin Li       IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
949*67e74705SXin Li     } else {
950*67e74705SXin Li       // By default, all the data that has a scalar type is mapped by copy.
951*67e74705SXin Li       IsByRef = !Ty->isScalarType();
952*67e74705SXin Li     }
953*67e74705SXin Li   }
954*67e74705SXin Li 
955*67e74705SXin Li   if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
956*67e74705SXin Li     IsByRef = !DSAStack->hasExplicitDSA(
957*67e74705SXin Li         D, [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
958*67e74705SXin Li         Level, /*NotLastprivate=*/true);
959*67e74705SXin Li   }
960*67e74705SXin Li 
961*67e74705SXin Li   // When passing data by copy, we need to make sure it fits the uintptr size
962*67e74705SXin Li   // and alignment, because the runtime library only deals with uintptr types.
963*67e74705SXin Li   // If it does not fit the uintptr size, we need to pass the data by reference
964*67e74705SXin Li   // instead.
965*67e74705SXin Li   if (!IsByRef &&
966*67e74705SXin Li       (Ctx.getTypeSizeInChars(Ty) >
967*67e74705SXin Li            Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
968*67e74705SXin Li        Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
969*67e74705SXin Li     IsByRef = true;
970*67e74705SXin Li   }
971*67e74705SXin Li 
972*67e74705SXin Li   return IsByRef;
973*67e74705SXin Li }
974*67e74705SXin Li 
getOpenMPNestingLevel() const975*67e74705SXin Li unsigned Sema::getOpenMPNestingLevel() const {
976*67e74705SXin Li   assert(getLangOpts().OpenMP);
977*67e74705SXin Li   return DSAStack->getNestingLevel();
978*67e74705SXin Li }
979*67e74705SXin Li 
IsOpenMPCapturedDecl(ValueDecl * D)980*67e74705SXin Li VarDecl *Sema::IsOpenMPCapturedDecl(ValueDecl *D) {
981*67e74705SXin Li   assert(LangOpts.OpenMP && "OpenMP is not allowed");
982*67e74705SXin Li   D = getCanonicalDecl(D);
983*67e74705SXin Li 
984*67e74705SXin Li   // If we are attempting to capture a global variable in a directive with
985*67e74705SXin Li   // 'target' we return true so that this global is also mapped to the device.
986*67e74705SXin Li   //
987*67e74705SXin Li   // FIXME: If the declaration is enclosed in a 'declare target' directive,
988*67e74705SXin Li   // then it should not be captured. Therefore, an extra check has to be
989*67e74705SXin Li   // inserted here once support for 'declare target' is added.
990*67e74705SXin Li   //
991*67e74705SXin Li   auto *VD = dyn_cast<VarDecl>(D);
992*67e74705SXin Li   if (VD && !VD->hasLocalStorage()) {
993*67e74705SXin Li     if (DSAStack->getCurrentDirective() == OMPD_target &&
994*67e74705SXin Li         !DSAStack->isClauseParsingMode())
995*67e74705SXin Li       return VD;
996*67e74705SXin Li     if (DSAStack->hasDirective(
997*67e74705SXin Li             [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
998*67e74705SXin Li                SourceLocation) -> bool {
999*67e74705SXin Li               return isOpenMPTargetExecutionDirective(K);
1000*67e74705SXin Li             },
1001*67e74705SXin Li             false))
1002*67e74705SXin Li       return VD;
1003*67e74705SXin Li   }
1004*67e74705SXin Li 
1005*67e74705SXin Li   if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1006*67e74705SXin Li       (!DSAStack->isClauseParsingMode() ||
1007*67e74705SXin Li        DSAStack->getParentDirective() != OMPD_unknown)) {
1008*67e74705SXin Li     auto &&Info = DSAStack->isLoopControlVariable(D);
1009*67e74705SXin Li     if (Info.first ||
1010*67e74705SXin Li         (VD && VD->hasLocalStorage() &&
1011*67e74705SXin Li          isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
1012*67e74705SXin Li         (VD && DSAStack->isForceVarCapturing()))
1013*67e74705SXin Li       return VD ? VD : Info.second;
1014*67e74705SXin Li     auto DVarPrivate = DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
1015*67e74705SXin Li     if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
1016*67e74705SXin Li       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1017*67e74705SXin Li     DVarPrivate = DSAStack->hasDSA(
1018*67e74705SXin Li         D, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
1019*67e74705SXin Li         DSAStack->isClauseParsingMode());
1020*67e74705SXin Li     if (DVarPrivate.CKind != OMPC_unknown)
1021*67e74705SXin Li       return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
1022*67e74705SXin Li   }
1023*67e74705SXin Li   return nullptr;
1024*67e74705SXin Li }
1025*67e74705SXin Li 
isOpenMPPrivateDecl(ValueDecl * D,unsigned Level)1026*67e74705SXin Li bool Sema::isOpenMPPrivateDecl(ValueDecl *D, unsigned Level) {
1027*67e74705SXin Li   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1028*67e74705SXin Li   return DSAStack->hasExplicitDSA(
1029*67e74705SXin Li       D, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
1030*67e74705SXin Li }
1031*67e74705SXin Li 
isOpenMPTargetCapturedDecl(ValueDecl * D,unsigned Level)1032*67e74705SXin Li bool Sema::isOpenMPTargetCapturedDecl(ValueDecl *D, unsigned Level) {
1033*67e74705SXin Li   assert(LangOpts.OpenMP && "OpenMP is not allowed");
1034*67e74705SXin Li   // Return true if the current level is no longer enclosed in a target region.
1035*67e74705SXin Li 
1036*67e74705SXin Li   auto *VD = dyn_cast<VarDecl>(D);
1037*67e74705SXin Li   return VD && !VD->hasLocalStorage() &&
1038*67e74705SXin Li          DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1039*67e74705SXin Li                                         Level);
1040*67e74705SXin Li }
1041*67e74705SXin Li 
DestroyDataSharingAttributesStack()1042*67e74705SXin Li void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
1043*67e74705SXin Li 
StartOpenMPDSABlock(OpenMPDirectiveKind DKind,const DeclarationNameInfo & DirName,Scope * CurScope,SourceLocation Loc)1044*67e74705SXin Li void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1045*67e74705SXin Li                                const DeclarationNameInfo &DirName,
1046*67e74705SXin Li                                Scope *CurScope, SourceLocation Loc) {
1047*67e74705SXin Li   DSAStack->push(DKind, DirName, CurScope, Loc);
1048*67e74705SXin Li   PushExpressionEvaluationContext(PotentiallyEvaluated);
1049*67e74705SXin Li }
1050*67e74705SXin Li 
StartOpenMPClause(OpenMPClauseKind K)1051*67e74705SXin Li void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1052*67e74705SXin Li   DSAStack->setClauseParsingMode(K);
1053*67e74705SXin Li }
1054*67e74705SXin Li 
EndOpenMPClause()1055*67e74705SXin Li void Sema::EndOpenMPClause() {
1056*67e74705SXin Li   DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
1057*67e74705SXin Li }
1058*67e74705SXin Li 
EndOpenMPDSABlock(Stmt * CurDirective)1059*67e74705SXin Li void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
1060*67e74705SXin Li   // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1061*67e74705SXin Li   //  A variable of class type (or array thereof) that appears in a lastprivate
1062*67e74705SXin Li   //  clause requires an accessible, unambiguous default constructor for the
1063*67e74705SXin Li   //  class type, unless the list item is also specified in a firstprivate
1064*67e74705SXin Li   //  clause.
1065*67e74705SXin Li   if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1066*67e74705SXin Li     for (auto *C : D->clauses()) {
1067*67e74705SXin Li       if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1068*67e74705SXin Li         SmallVector<Expr *, 8> PrivateCopies;
1069*67e74705SXin Li         for (auto *DE : Clause->varlists()) {
1070*67e74705SXin Li           if (DE->isValueDependent() || DE->isTypeDependent()) {
1071*67e74705SXin Li             PrivateCopies.push_back(nullptr);
1072*67e74705SXin Li             continue;
1073*67e74705SXin Li           }
1074*67e74705SXin Li           auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
1075*67e74705SXin Li           VarDecl *VD = cast<VarDecl>(DRE->getDecl());
1076*67e74705SXin Li           QualType Type = VD->getType().getNonReferenceType();
1077*67e74705SXin Li           auto DVar = DSAStack->getTopDSA(VD, false);
1078*67e74705SXin Li           if (DVar.CKind == OMPC_lastprivate) {
1079*67e74705SXin Li             // Generate helper private variable and initialize it with the
1080*67e74705SXin Li             // default value. The address of the original variable is replaced
1081*67e74705SXin Li             // by the address of the new private variable in CodeGen. This new
1082*67e74705SXin Li             // variable is not added to IdResolver, so the code in the OpenMP
1083*67e74705SXin Li             // region uses original variable for proper diagnostics.
1084*67e74705SXin Li             auto *VDPrivate = buildVarDecl(
1085*67e74705SXin Li                 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
1086*67e74705SXin Li                 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
1087*67e74705SXin Li             ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
1088*67e74705SXin Li             if (VDPrivate->isInvalidDecl())
1089*67e74705SXin Li               continue;
1090*67e74705SXin Li             PrivateCopies.push_back(buildDeclRefExpr(
1091*67e74705SXin Li                 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
1092*67e74705SXin Li           } else {
1093*67e74705SXin Li             // The variable is also a firstprivate, so initialization sequence
1094*67e74705SXin Li             // for private copy is generated already.
1095*67e74705SXin Li             PrivateCopies.push_back(nullptr);
1096*67e74705SXin Li           }
1097*67e74705SXin Li         }
1098*67e74705SXin Li         // Set initializers to private copies if no errors were found.
1099*67e74705SXin Li         if (PrivateCopies.size() == Clause->varlist_size())
1100*67e74705SXin Li           Clause->setPrivateCopies(PrivateCopies);
1101*67e74705SXin Li       }
1102*67e74705SXin Li     }
1103*67e74705SXin Li   }
1104*67e74705SXin Li 
1105*67e74705SXin Li   DSAStack->pop();
1106*67e74705SXin Li   DiscardCleanupsInEvaluationContext();
1107*67e74705SXin Li   PopExpressionEvaluationContext();
1108*67e74705SXin Li }
1109*67e74705SXin Li 
1110*67e74705SXin Li static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1111*67e74705SXin Li                                      Expr *NumIterations, Sema &SemaRef,
1112*67e74705SXin Li                                      Scope *S, DSAStackTy *Stack);
1113*67e74705SXin Li 
1114*67e74705SXin Li namespace {
1115*67e74705SXin Li 
1116*67e74705SXin Li class VarDeclFilterCCC : public CorrectionCandidateCallback {
1117*67e74705SXin Li private:
1118*67e74705SXin Li   Sema &SemaRef;
1119*67e74705SXin Li 
1120*67e74705SXin Li public:
VarDeclFilterCCC(Sema & S)1121*67e74705SXin Li   explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
ValidateCandidate(const TypoCorrection & Candidate)1122*67e74705SXin Li   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1123*67e74705SXin Li     NamedDecl *ND = Candidate.getCorrectionDecl();
1124*67e74705SXin Li     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
1125*67e74705SXin Li       return VD->hasGlobalStorage() &&
1126*67e74705SXin Li              SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1127*67e74705SXin Li                                    SemaRef.getCurScope());
1128*67e74705SXin Li     }
1129*67e74705SXin Li     return false;
1130*67e74705SXin Li   }
1131*67e74705SXin Li };
1132*67e74705SXin Li 
1133*67e74705SXin Li class VarOrFuncDeclFilterCCC : public CorrectionCandidateCallback {
1134*67e74705SXin Li private:
1135*67e74705SXin Li   Sema &SemaRef;
1136*67e74705SXin Li 
1137*67e74705SXin Li public:
VarOrFuncDeclFilterCCC(Sema & S)1138*67e74705SXin Li   explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
ValidateCandidate(const TypoCorrection & Candidate)1139*67e74705SXin Li   bool ValidateCandidate(const TypoCorrection &Candidate) override {
1140*67e74705SXin Li     NamedDecl *ND = Candidate.getCorrectionDecl();
1141*67e74705SXin Li     if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
1142*67e74705SXin Li       return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1143*67e74705SXin Li                                    SemaRef.getCurScope());
1144*67e74705SXin Li     }
1145*67e74705SXin Li     return false;
1146*67e74705SXin Li   }
1147*67e74705SXin Li };
1148*67e74705SXin Li 
1149*67e74705SXin Li } // namespace
1150*67e74705SXin Li 
ActOnOpenMPIdExpression(Scope * CurScope,CXXScopeSpec & ScopeSpec,const DeclarationNameInfo & Id)1151*67e74705SXin Li ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1152*67e74705SXin Li                                          CXXScopeSpec &ScopeSpec,
1153*67e74705SXin Li                                          const DeclarationNameInfo &Id) {
1154*67e74705SXin Li   LookupResult Lookup(*this, Id, LookupOrdinaryName);
1155*67e74705SXin Li   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1156*67e74705SXin Li 
1157*67e74705SXin Li   if (Lookup.isAmbiguous())
1158*67e74705SXin Li     return ExprError();
1159*67e74705SXin Li 
1160*67e74705SXin Li   VarDecl *VD;
1161*67e74705SXin Li   if (!Lookup.isSingleResult()) {
1162*67e74705SXin Li     if (TypoCorrection Corrected = CorrectTypo(
1163*67e74705SXin Li             Id, LookupOrdinaryName, CurScope, nullptr,
1164*67e74705SXin Li             llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
1165*67e74705SXin Li       diagnoseTypo(Corrected,
1166*67e74705SXin Li                    PDiag(Lookup.empty()
1167*67e74705SXin Li                              ? diag::err_undeclared_var_use_suggest
1168*67e74705SXin Li                              : diag::err_omp_expected_var_arg_suggest)
1169*67e74705SXin Li                        << Id.getName());
1170*67e74705SXin Li       VD = Corrected.getCorrectionDeclAs<VarDecl>();
1171*67e74705SXin Li     } else {
1172*67e74705SXin Li       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1173*67e74705SXin Li                                        : diag::err_omp_expected_var_arg)
1174*67e74705SXin Li           << Id.getName();
1175*67e74705SXin Li       return ExprError();
1176*67e74705SXin Li     }
1177*67e74705SXin Li   } else {
1178*67e74705SXin Li     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1179*67e74705SXin Li       Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1180*67e74705SXin Li       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1181*67e74705SXin Li       return ExprError();
1182*67e74705SXin Li     }
1183*67e74705SXin Li   }
1184*67e74705SXin Li   Lookup.suppressDiagnostics();
1185*67e74705SXin Li 
1186*67e74705SXin Li   // OpenMP [2.9.2, Syntax, C/C++]
1187*67e74705SXin Li   //   Variables must be file-scope, namespace-scope, or static block-scope.
1188*67e74705SXin Li   if (!VD->hasGlobalStorage()) {
1189*67e74705SXin Li     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
1190*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1191*67e74705SXin Li     bool IsDecl =
1192*67e74705SXin Li         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1193*67e74705SXin Li     Diag(VD->getLocation(),
1194*67e74705SXin Li          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1195*67e74705SXin Li         << VD;
1196*67e74705SXin Li     return ExprError();
1197*67e74705SXin Li   }
1198*67e74705SXin Li 
1199*67e74705SXin Li   VarDecl *CanonicalVD = VD->getCanonicalDecl();
1200*67e74705SXin Li   NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
1201*67e74705SXin Li   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1202*67e74705SXin Li   //   A threadprivate directive for file-scope variables must appear outside
1203*67e74705SXin Li   //   any definition or declaration.
1204*67e74705SXin Li   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1205*67e74705SXin Li       !getCurLexicalContext()->isTranslationUnit()) {
1206*67e74705SXin Li     Diag(Id.getLoc(), diag::err_omp_var_scope)
1207*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1208*67e74705SXin Li     bool IsDecl =
1209*67e74705SXin Li         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1210*67e74705SXin Li     Diag(VD->getLocation(),
1211*67e74705SXin Li          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1212*67e74705SXin Li         << VD;
1213*67e74705SXin Li     return ExprError();
1214*67e74705SXin Li   }
1215*67e74705SXin Li   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1216*67e74705SXin Li   //   A threadprivate directive for static class member variables must appear
1217*67e74705SXin Li   //   in the class definition, in the same scope in which the member
1218*67e74705SXin Li   //   variables are declared.
1219*67e74705SXin Li   if (CanonicalVD->isStaticDataMember() &&
1220*67e74705SXin Li       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1221*67e74705SXin Li     Diag(Id.getLoc(), diag::err_omp_var_scope)
1222*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1223*67e74705SXin Li     bool IsDecl =
1224*67e74705SXin Li         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1225*67e74705SXin Li     Diag(VD->getLocation(),
1226*67e74705SXin Li          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1227*67e74705SXin Li         << VD;
1228*67e74705SXin Li     return ExprError();
1229*67e74705SXin Li   }
1230*67e74705SXin Li   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1231*67e74705SXin Li   //   A threadprivate directive for namespace-scope variables must appear
1232*67e74705SXin Li   //   outside any definition or declaration other than the namespace
1233*67e74705SXin Li   //   definition itself.
1234*67e74705SXin Li   if (CanonicalVD->getDeclContext()->isNamespace() &&
1235*67e74705SXin Li       (!getCurLexicalContext()->isFileContext() ||
1236*67e74705SXin Li        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1237*67e74705SXin Li     Diag(Id.getLoc(), diag::err_omp_var_scope)
1238*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1239*67e74705SXin Li     bool IsDecl =
1240*67e74705SXin Li         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1241*67e74705SXin Li     Diag(VD->getLocation(),
1242*67e74705SXin Li          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1243*67e74705SXin Li         << VD;
1244*67e74705SXin Li     return ExprError();
1245*67e74705SXin Li   }
1246*67e74705SXin Li   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1247*67e74705SXin Li   //   A threadprivate directive for static block-scope variables must appear
1248*67e74705SXin Li   //   in the scope of the variable and not in a nested scope.
1249*67e74705SXin Li   if (CanonicalVD->isStaticLocal() && CurScope &&
1250*67e74705SXin Li       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
1251*67e74705SXin Li     Diag(Id.getLoc(), diag::err_omp_var_scope)
1252*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1253*67e74705SXin Li     bool IsDecl =
1254*67e74705SXin Li         VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1255*67e74705SXin Li     Diag(VD->getLocation(),
1256*67e74705SXin Li          IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1257*67e74705SXin Li         << VD;
1258*67e74705SXin Li     return ExprError();
1259*67e74705SXin Li   }
1260*67e74705SXin Li 
1261*67e74705SXin Li   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1262*67e74705SXin Li   //   A threadprivate directive must lexically precede all references to any
1263*67e74705SXin Li   //   of the variables in its list.
1264*67e74705SXin Li   if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
1265*67e74705SXin Li     Diag(Id.getLoc(), diag::err_omp_var_used)
1266*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1267*67e74705SXin Li     return ExprError();
1268*67e74705SXin Li   }
1269*67e74705SXin Li 
1270*67e74705SXin Li   QualType ExprType = VD->getType().getNonReferenceType();
1271*67e74705SXin Li   return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1272*67e74705SXin Li                              SourceLocation(), VD,
1273*67e74705SXin Li                              /*RefersToEnclosingVariableOrCapture=*/false,
1274*67e74705SXin Li                              Id.getLoc(), ExprType, VK_LValue);
1275*67e74705SXin Li }
1276*67e74705SXin Li 
1277*67e74705SXin Li Sema::DeclGroupPtrTy
ActOnOpenMPThreadprivateDirective(SourceLocation Loc,ArrayRef<Expr * > VarList)1278*67e74705SXin Li Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1279*67e74705SXin Li                                         ArrayRef<Expr *> VarList) {
1280*67e74705SXin Li   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
1281*67e74705SXin Li     CurContext->addDecl(D);
1282*67e74705SXin Li     return DeclGroupPtrTy::make(DeclGroupRef(D));
1283*67e74705SXin Li   }
1284*67e74705SXin Li   return nullptr;
1285*67e74705SXin Li }
1286*67e74705SXin Li 
1287*67e74705SXin Li namespace {
1288*67e74705SXin Li class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1289*67e74705SXin Li   Sema &SemaRef;
1290*67e74705SXin Li 
1291*67e74705SXin Li public:
VisitDeclRefExpr(const DeclRefExpr * E)1292*67e74705SXin Li   bool VisitDeclRefExpr(const DeclRefExpr *E) {
1293*67e74705SXin Li     if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1294*67e74705SXin Li       if (VD->hasLocalStorage()) {
1295*67e74705SXin Li         SemaRef.Diag(E->getLocStart(),
1296*67e74705SXin Li                      diag::err_omp_local_var_in_threadprivate_init)
1297*67e74705SXin Li             << E->getSourceRange();
1298*67e74705SXin Li         SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1299*67e74705SXin Li             << VD << VD->getSourceRange();
1300*67e74705SXin Li         return true;
1301*67e74705SXin Li       }
1302*67e74705SXin Li     }
1303*67e74705SXin Li     return false;
1304*67e74705SXin Li   }
VisitStmt(const Stmt * S)1305*67e74705SXin Li   bool VisitStmt(const Stmt *S) {
1306*67e74705SXin Li     for (auto Child : S->children()) {
1307*67e74705SXin Li       if (Child && Visit(Child))
1308*67e74705SXin Li         return true;
1309*67e74705SXin Li     }
1310*67e74705SXin Li     return false;
1311*67e74705SXin Li   }
LocalVarRefChecker(Sema & SemaRef)1312*67e74705SXin Li   explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
1313*67e74705SXin Li };
1314*67e74705SXin Li } // namespace
1315*67e74705SXin Li 
1316*67e74705SXin Li OMPThreadPrivateDecl *
CheckOMPThreadPrivateDecl(SourceLocation Loc,ArrayRef<Expr * > VarList)1317*67e74705SXin Li Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
1318*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
1319*67e74705SXin Li   for (auto &RefExpr : VarList) {
1320*67e74705SXin Li     DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
1321*67e74705SXin Li     VarDecl *VD = cast<VarDecl>(DE->getDecl());
1322*67e74705SXin Li     SourceLocation ILoc = DE->getExprLoc();
1323*67e74705SXin Li 
1324*67e74705SXin Li     // Mark variable as used.
1325*67e74705SXin Li     VD->setReferenced();
1326*67e74705SXin Li     VD->markUsed(Context);
1327*67e74705SXin Li 
1328*67e74705SXin Li     QualType QType = VD->getType();
1329*67e74705SXin Li     if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1330*67e74705SXin Li       // It will be analyzed later.
1331*67e74705SXin Li       Vars.push_back(DE);
1332*67e74705SXin Li       continue;
1333*67e74705SXin Li     }
1334*67e74705SXin Li 
1335*67e74705SXin Li     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1336*67e74705SXin Li     //   A threadprivate variable must not have an incomplete type.
1337*67e74705SXin Li     if (RequireCompleteType(ILoc, VD->getType(),
1338*67e74705SXin Li                             diag::err_omp_threadprivate_incomplete_type)) {
1339*67e74705SXin Li       continue;
1340*67e74705SXin Li     }
1341*67e74705SXin Li 
1342*67e74705SXin Li     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1343*67e74705SXin Li     //   A threadprivate variable must not have a reference type.
1344*67e74705SXin Li     if (VD->getType()->isReferenceType()) {
1345*67e74705SXin Li       Diag(ILoc, diag::err_omp_ref_type_arg)
1346*67e74705SXin Li           << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1347*67e74705SXin Li       bool IsDecl =
1348*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1349*67e74705SXin Li       Diag(VD->getLocation(),
1350*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1351*67e74705SXin Li           << VD;
1352*67e74705SXin Li       continue;
1353*67e74705SXin Li     }
1354*67e74705SXin Li 
1355*67e74705SXin Li     // Check if this is a TLS variable. If TLS is not being supported, produce
1356*67e74705SXin Li     // the corresponding diagnostic.
1357*67e74705SXin Li     if ((VD->getTLSKind() != VarDecl::TLS_None &&
1358*67e74705SXin Li          !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1359*67e74705SXin Li            getLangOpts().OpenMPUseTLS &&
1360*67e74705SXin Li            getASTContext().getTargetInfo().isTLSSupported())) ||
1361*67e74705SXin Li         (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1362*67e74705SXin Li          !VD->isLocalVarDecl())) {
1363*67e74705SXin Li       Diag(ILoc, diag::err_omp_var_thread_local)
1364*67e74705SXin Li           << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
1365*67e74705SXin Li       bool IsDecl =
1366*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1367*67e74705SXin Li       Diag(VD->getLocation(),
1368*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1369*67e74705SXin Li           << VD;
1370*67e74705SXin Li       continue;
1371*67e74705SXin Li     }
1372*67e74705SXin Li 
1373*67e74705SXin Li     // Check if initial value of threadprivate variable reference variable with
1374*67e74705SXin Li     // local storage (it is not supported by runtime).
1375*67e74705SXin Li     if (auto Init = VD->getAnyInitializer()) {
1376*67e74705SXin Li       LocalVarRefChecker Checker(*this);
1377*67e74705SXin Li       if (Checker.Visit(Init))
1378*67e74705SXin Li         continue;
1379*67e74705SXin Li     }
1380*67e74705SXin Li 
1381*67e74705SXin Li     Vars.push_back(RefExpr);
1382*67e74705SXin Li     DSAStack->addDSA(VD, DE, OMPC_threadprivate);
1383*67e74705SXin Li     VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1384*67e74705SXin Li         Context, SourceRange(Loc, Loc)));
1385*67e74705SXin Li     if (auto *ML = Context.getASTMutationListener())
1386*67e74705SXin Li       ML->DeclarationMarkedOpenMPThreadPrivate(VD);
1387*67e74705SXin Li   }
1388*67e74705SXin Li   OMPThreadPrivateDecl *D = nullptr;
1389*67e74705SXin Li   if (!Vars.empty()) {
1390*67e74705SXin Li     D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1391*67e74705SXin Li                                      Vars);
1392*67e74705SXin Li     D->setAccess(AS_public);
1393*67e74705SXin Li   }
1394*67e74705SXin Li   return D;
1395*67e74705SXin Li }
1396*67e74705SXin Li 
ReportOriginalDSA(Sema & SemaRef,DSAStackTy * Stack,const ValueDecl * D,DSAStackTy::DSAVarData DVar,bool IsLoopIterVar=false)1397*67e74705SXin Li static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1398*67e74705SXin Li                               const ValueDecl *D, DSAStackTy::DSAVarData DVar,
1399*67e74705SXin Li                               bool IsLoopIterVar = false) {
1400*67e74705SXin Li   if (DVar.RefExpr) {
1401*67e74705SXin Li     SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1402*67e74705SXin Li         << getOpenMPClauseName(DVar.CKind);
1403*67e74705SXin Li     return;
1404*67e74705SXin Li   }
1405*67e74705SXin Li   enum {
1406*67e74705SXin Li     PDSA_StaticMemberShared,
1407*67e74705SXin Li     PDSA_StaticLocalVarShared,
1408*67e74705SXin Li     PDSA_LoopIterVarPrivate,
1409*67e74705SXin Li     PDSA_LoopIterVarLinear,
1410*67e74705SXin Li     PDSA_LoopIterVarLastprivate,
1411*67e74705SXin Li     PDSA_ConstVarShared,
1412*67e74705SXin Li     PDSA_GlobalVarShared,
1413*67e74705SXin Li     PDSA_TaskVarFirstprivate,
1414*67e74705SXin Li     PDSA_LocalVarPrivate,
1415*67e74705SXin Li     PDSA_Implicit
1416*67e74705SXin Li   } Reason = PDSA_Implicit;
1417*67e74705SXin Li   bool ReportHint = false;
1418*67e74705SXin Li   auto ReportLoc = D->getLocation();
1419*67e74705SXin Li   auto *VD = dyn_cast<VarDecl>(D);
1420*67e74705SXin Li   if (IsLoopIterVar) {
1421*67e74705SXin Li     if (DVar.CKind == OMPC_private)
1422*67e74705SXin Li       Reason = PDSA_LoopIterVarPrivate;
1423*67e74705SXin Li     else if (DVar.CKind == OMPC_lastprivate)
1424*67e74705SXin Li       Reason = PDSA_LoopIterVarLastprivate;
1425*67e74705SXin Li     else
1426*67e74705SXin Li       Reason = PDSA_LoopIterVarLinear;
1427*67e74705SXin Li   } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1428*67e74705SXin Li              DVar.CKind == OMPC_firstprivate) {
1429*67e74705SXin Li     Reason = PDSA_TaskVarFirstprivate;
1430*67e74705SXin Li     ReportLoc = DVar.ImplicitDSALoc;
1431*67e74705SXin Li   } else if (VD && VD->isStaticLocal())
1432*67e74705SXin Li     Reason = PDSA_StaticLocalVarShared;
1433*67e74705SXin Li   else if (VD && VD->isStaticDataMember())
1434*67e74705SXin Li     Reason = PDSA_StaticMemberShared;
1435*67e74705SXin Li   else if (VD && VD->isFileVarDecl())
1436*67e74705SXin Li     Reason = PDSA_GlobalVarShared;
1437*67e74705SXin Li   else if (D->getType().isConstant(SemaRef.getASTContext()))
1438*67e74705SXin Li     Reason = PDSA_ConstVarShared;
1439*67e74705SXin Li   else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
1440*67e74705SXin Li     ReportHint = true;
1441*67e74705SXin Li     Reason = PDSA_LocalVarPrivate;
1442*67e74705SXin Li   }
1443*67e74705SXin Li   if (Reason != PDSA_Implicit) {
1444*67e74705SXin Li     SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
1445*67e74705SXin Li         << Reason << ReportHint
1446*67e74705SXin Li         << getOpenMPDirectiveName(Stack->getCurrentDirective());
1447*67e74705SXin Li   } else if (DVar.ImplicitDSALoc.isValid()) {
1448*67e74705SXin Li     SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1449*67e74705SXin Li         << getOpenMPClauseName(DVar.CKind);
1450*67e74705SXin Li   }
1451*67e74705SXin Li }
1452*67e74705SXin Li 
1453*67e74705SXin Li namespace {
1454*67e74705SXin Li class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1455*67e74705SXin Li   DSAStackTy *Stack;
1456*67e74705SXin Li   Sema &SemaRef;
1457*67e74705SXin Li   bool ErrorFound;
1458*67e74705SXin Li   CapturedStmt *CS;
1459*67e74705SXin Li   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
1460*67e74705SXin Li   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
1461*67e74705SXin Li 
1462*67e74705SXin Li public:
VisitDeclRefExpr(DeclRefExpr * E)1463*67e74705SXin Li   void VisitDeclRefExpr(DeclRefExpr *E) {
1464*67e74705SXin Li     if (E->isTypeDependent() || E->isValueDependent() ||
1465*67e74705SXin Li         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1466*67e74705SXin Li       return;
1467*67e74705SXin Li     if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
1468*67e74705SXin Li       // Skip internally declared variables.
1469*67e74705SXin Li       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1470*67e74705SXin Li         return;
1471*67e74705SXin Li 
1472*67e74705SXin Li       auto DVar = Stack->getTopDSA(VD, false);
1473*67e74705SXin Li       // Check if the variable has explicit DSA set and stop analysis if it so.
1474*67e74705SXin Li       if (DVar.RefExpr) return;
1475*67e74705SXin Li 
1476*67e74705SXin Li       auto ELoc = E->getExprLoc();
1477*67e74705SXin Li       auto DKind = Stack->getCurrentDirective();
1478*67e74705SXin Li       // The default(none) clause requires that each variable that is referenced
1479*67e74705SXin Li       // in the construct, and does not have a predetermined data-sharing
1480*67e74705SXin Li       // attribute, must have its data-sharing attribute explicitly determined
1481*67e74705SXin Li       // by being listed in a data-sharing attribute clause.
1482*67e74705SXin Li       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
1483*67e74705SXin Li           isParallelOrTaskRegion(DKind) &&
1484*67e74705SXin Li           VarsWithInheritedDSA.count(VD) == 0) {
1485*67e74705SXin Li         VarsWithInheritedDSA[VD] = E;
1486*67e74705SXin Li         return;
1487*67e74705SXin Li       }
1488*67e74705SXin Li 
1489*67e74705SXin Li       // OpenMP [2.9.3.6, Restrictions, p.2]
1490*67e74705SXin Li       //  A list item that appears in a reduction clause of the innermost
1491*67e74705SXin Li       //  enclosing worksharing or parallel construct may not be accessed in an
1492*67e74705SXin Li       //  explicit task.
1493*67e74705SXin Li       DVar = Stack->hasInnermostDSA(
1494*67e74705SXin Li           VD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1495*67e74705SXin Li           [](OpenMPDirectiveKind K) -> bool {
1496*67e74705SXin Li             return isOpenMPParallelDirective(K) ||
1497*67e74705SXin Li                    isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
1498*67e74705SXin Li           },
1499*67e74705SXin Li           false);
1500*67e74705SXin Li       if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1501*67e74705SXin Li         ErrorFound = true;
1502*67e74705SXin Li         SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1503*67e74705SXin Li         ReportOriginalDSA(SemaRef, Stack, VD, DVar);
1504*67e74705SXin Li         return;
1505*67e74705SXin Li       }
1506*67e74705SXin Li 
1507*67e74705SXin Li       // Define implicit data-sharing attributes for task.
1508*67e74705SXin Li       DVar = Stack->getImplicitDSA(VD, false);
1509*67e74705SXin Li       if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1510*67e74705SXin Li           !Stack->isLoopControlVariable(VD).first)
1511*67e74705SXin Li         ImplicitFirstprivate.push_back(E);
1512*67e74705SXin Li     }
1513*67e74705SXin Li   }
VisitMemberExpr(MemberExpr * E)1514*67e74705SXin Li   void VisitMemberExpr(MemberExpr *E) {
1515*67e74705SXin Li     if (E->isTypeDependent() || E->isValueDependent() ||
1516*67e74705SXin Li         E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
1517*67e74705SXin Li       return;
1518*67e74705SXin Li     if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
1519*67e74705SXin Li       if (auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
1520*67e74705SXin Li         auto DVar = Stack->getTopDSA(FD, false);
1521*67e74705SXin Li         // Check if the variable has explicit DSA set and stop analysis if it
1522*67e74705SXin Li         // so.
1523*67e74705SXin Li         if (DVar.RefExpr)
1524*67e74705SXin Li           return;
1525*67e74705SXin Li 
1526*67e74705SXin Li         auto ELoc = E->getExprLoc();
1527*67e74705SXin Li         auto DKind = Stack->getCurrentDirective();
1528*67e74705SXin Li         // OpenMP [2.9.3.6, Restrictions, p.2]
1529*67e74705SXin Li         //  A list item that appears in a reduction clause of the innermost
1530*67e74705SXin Li         //  enclosing worksharing or parallel construct may not be accessed in
1531*67e74705SXin Li         //  an  explicit task.
1532*67e74705SXin Li         DVar = Stack->hasInnermostDSA(
1533*67e74705SXin Li             FD, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
1534*67e74705SXin Li             [](OpenMPDirectiveKind K) -> bool {
1535*67e74705SXin Li               return isOpenMPParallelDirective(K) ||
1536*67e74705SXin Li                      isOpenMPWorksharingDirective(K) ||
1537*67e74705SXin Li                      isOpenMPTeamsDirective(K);
1538*67e74705SXin Li             },
1539*67e74705SXin Li             false);
1540*67e74705SXin Li         if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
1541*67e74705SXin Li           ErrorFound = true;
1542*67e74705SXin Li           SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1543*67e74705SXin Li           ReportOriginalDSA(SemaRef, Stack, FD, DVar);
1544*67e74705SXin Li           return;
1545*67e74705SXin Li         }
1546*67e74705SXin Li 
1547*67e74705SXin Li         // Define implicit data-sharing attributes for task.
1548*67e74705SXin Li         DVar = Stack->getImplicitDSA(FD, false);
1549*67e74705SXin Li         if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
1550*67e74705SXin Li             !Stack->isLoopControlVariable(FD).first)
1551*67e74705SXin Li           ImplicitFirstprivate.push_back(E);
1552*67e74705SXin Li       }
1553*67e74705SXin Li     }
1554*67e74705SXin Li   }
VisitOMPExecutableDirective(OMPExecutableDirective * S)1555*67e74705SXin Li   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
1556*67e74705SXin Li     for (auto *C : S->clauses()) {
1557*67e74705SXin Li       // Skip analysis of arguments of implicitly defined firstprivate clause
1558*67e74705SXin Li       // for task directives.
1559*67e74705SXin Li       if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1560*67e74705SXin Li         for (auto *CC : C->children()) {
1561*67e74705SXin Li           if (CC)
1562*67e74705SXin Li             Visit(CC);
1563*67e74705SXin Li         }
1564*67e74705SXin Li     }
1565*67e74705SXin Li   }
VisitStmt(Stmt * S)1566*67e74705SXin Li   void VisitStmt(Stmt *S) {
1567*67e74705SXin Li     for (auto *C : S->children()) {
1568*67e74705SXin Li       if (C && !isa<OMPExecutableDirective>(C))
1569*67e74705SXin Li         Visit(C);
1570*67e74705SXin Li     }
1571*67e74705SXin Li   }
1572*67e74705SXin Li 
isErrorFound()1573*67e74705SXin Li   bool isErrorFound() { return ErrorFound; }
getImplicitFirstprivate()1574*67e74705SXin Li   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
getVarsWithInheritedDSA()1575*67e74705SXin Li   llvm::DenseMap<ValueDecl *, Expr *> &getVarsWithInheritedDSA() {
1576*67e74705SXin Li     return VarsWithInheritedDSA;
1577*67e74705SXin Li   }
1578*67e74705SXin Li 
DSAAttrChecker(DSAStackTy * S,Sema & SemaRef,CapturedStmt * CS)1579*67e74705SXin Li   DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1580*67e74705SXin Li       : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
1581*67e74705SXin Li };
1582*67e74705SXin Li } // namespace
1583*67e74705SXin Li 
ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind,Scope * CurScope)1584*67e74705SXin Li void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
1585*67e74705SXin Li   switch (DKind) {
1586*67e74705SXin Li   case OMPD_parallel:
1587*67e74705SXin Li   case OMPD_parallel_for:
1588*67e74705SXin Li   case OMPD_parallel_for_simd:
1589*67e74705SXin Li   case OMPD_parallel_sections:
1590*67e74705SXin Li   case OMPD_teams: {
1591*67e74705SXin Li     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1592*67e74705SXin Li     QualType KmpInt32PtrTy =
1593*67e74705SXin Li         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1594*67e74705SXin Li     Sema::CapturedParamNameType Params[] = {
1595*67e74705SXin Li         std::make_pair(".global_tid.", KmpInt32PtrTy),
1596*67e74705SXin Li         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1597*67e74705SXin Li         std::make_pair(StringRef(), QualType()) // __context with shared vars
1598*67e74705SXin Li     };
1599*67e74705SXin Li     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1600*67e74705SXin Li                              Params);
1601*67e74705SXin Li     break;
1602*67e74705SXin Li   }
1603*67e74705SXin Li   case OMPD_simd:
1604*67e74705SXin Li   case OMPD_for:
1605*67e74705SXin Li   case OMPD_for_simd:
1606*67e74705SXin Li   case OMPD_sections:
1607*67e74705SXin Li   case OMPD_section:
1608*67e74705SXin Li   case OMPD_single:
1609*67e74705SXin Li   case OMPD_master:
1610*67e74705SXin Li   case OMPD_critical:
1611*67e74705SXin Li   case OMPD_taskgroup:
1612*67e74705SXin Li   case OMPD_distribute:
1613*67e74705SXin Li   case OMPD_ordered:
1614*67e74705SXin Li   case OMPD_atomic:
1615*67e74705SXin Li   case OMPD_target_data:
1616*67e74705SXin Li   case OMPD_target:
1617*67e74705SXin Li   case OMPD_target_parallel:
1618*67e74705SXin Li   case OMPD_target_parallel_for:
1619*67e74705SXin Li   case OMPD_target_parallel_for_simd: {
1620*67e74705SXin Li     Sema::CapturedParamNameType Params[] = {
1621*67e74705SXin Li         std::make_pair(StringRef(), QualType()) // __context with shared vars
1622*67e74705SXin Li     };
1623*67e74705SXin Li     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1624*67e74705SXin Li                              Params);
1625*67e74705SXin Li     break;
1626*67e74705SXin Li   }
1627*67e74705SXin Li   case OMPD_task: {
1628*67e74705SXin Li     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1629*67e74705SXin Li     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1630*67e74705SXin Li     FunctionProtoType::ExtProtoInfo EPI;
1631*67e74705SXin Li     EPI.Variadic = true;
1632*67e74705SXin Li     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1633*67e74705SXin Li     Sema::CapturedParamNameType Params[] = {
1634*67e74705SXin Li         std::make_pair(".global_tid.", KmpInt32Ty),
1635*67e74705SXin Li         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1636*67e74705SXin Li         std::make_pair(".privates.", Context.VoidPtrTy.withConst()),
1637*67e74705SXin Li         std::make_pair(".copy_fn.",
1638*67e74705SXin Li                        Context.getPointerType(CopyFnType).withConst()),
1639*67e74705SXin Li         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1640*67e74705SXin Li         std::make_pair(StringRef(), QualType()) // __context with shared vars
1641*67e74705SXin Li     };
1642*67e74705SXin Li     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1643*67e74705SXin Li                              Params);
1644*67e74705SXin Li     // Mark this captured region as inlined, because we don't use outlined
1645*67e74705SXin Li     // function directly.
1646*67e74705SXin Li     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1647*67e74705SXin Li         AlwaysInlineAttr::CreateImplicit(
1648*67e74705SXin Li             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1649*67e74705SXin Li     break;
1650*67e74705SXin Li   }
1651*67e74705SXin Li   case OMPD_taskloop:
1652*67e74705SXin Li   case OMPD_taskloop_simd: {
1653*67e74705SXin Li     QualType KmpInt32Ty =
1654*67e74705SXin Li         Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1);
1655*67e74705SXin Li     QualType KmpUInt64Ty =
1656*67e74705SXin Li         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
1657*67e74705SXin Li     QualType KmpInt64Ty =
1658*67e74705SXin Li         Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
1659*67e74705SXin Li     QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1660*67e74705SXin Li     FunctionProtoType::ExtProtoInfo EPI;
1661*67e74705SXin Li     EPI.Variadic = true;
1662*67e74705SXin Li     QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
1663*67e74705SXin Li     Sema::CapturedParamNameType Params[] = {
1664*67e74705SXin Li         std::make_pair(".global_tid.", KmpInt32Ty),
1665*67e74705SXin Li         std::make_pair(".part_id.", Context.getPointerType(KmpInt32Ty)),
1666*67e74705SXin Li         std::make_pair(".privates.",
1667*67e74705SXin Li                        Context.VoidPtrTy.withConst().withRestrict()),
1668*67e74705SXin Li         std::make_pair(
1669*67e74705SXin Li             ".copy_fn.",
1670*67e74705SXin Li             Context.getPointerType(CopyFnType).withConst().withRestrict()),
1671*67e74705SXin Li         std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
1672*67e74705SXin Li         std::make_pair(".lb.", KmpUInt64Ty),
1673*67e74705SXin Li         std::make_pair(".ub.", KmpUInt64Ty), std::make_pair(".st.", KmpInt64Ty),
1674*67e74705SXin Li         std::make_pair(".liter.", KmpInt32Ty),
1675*67e74705SXin Li         std::make_pair(StringRef(), QualType()) // __context with shared vars
1676*67e74705SXin Li     };
1677*67e74705SXin Li     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1678*67e74705SXin Li                              Params);
1679*67e74705SXin Li     // Mark this captured region as inlined, because we don't use outlined
1680*67e74705SXin Li     // function directly.
1681*67e74705SXin Li     getCurCapturedRegion()->TheCapturedDecl->addAttr(
1682*67e74705SXin Li         AlwaysInlineAttr::CreateImplicit(
1683*67e74705SXin Li             Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
1684*67e74705SXin Li     break;
1685*67e74705SXin Li   }
1686*67e74705SXin Li   case OMPD_distribute_parallel_for_simd:
1687*67e74705SXin Li   case OMPD_distribute_simd:
1688*67e74705SXin Li   case OMPD_distribute_parallel_for: {
1689*67e74705SXin Li     QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1690*67e74705SXin Li     QualType KmpInt32PtrTy =
1691*67e74705SXin Li         Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
1692*67e74705SXin Li     Sema::CapturedParamNameType Params[] = {
1693*67e74705SXin Li         std::make_pair(".global_tid.", KmpInt32PtrTy),
1694*67e74705SXin Li         std::make_pair(".bound_tid.", KmpInt32PtrTy),
1695*67e74705SXin Li         std::make_pair(".previous.lb.", Context.getSizeType()),
1696*67e74705SXin Li         std::make_pair(".previous.ub.", Context.getSizeType()),
1697*67e74705SXin Li         std::make_pair(StringRef(), QualType()) // __context with shared vars
1698*67e74705SXin Li     };
1699*67e74705SXin Li     ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1700*67e74705SXin Li                              Params);
1701*67e74705SXin Li     break;
1702*67e74705SXin Li   }
1703*67e74705SXin Li   case OMPD_threadprivate:
1704*67e74705SXin Li   case OMPD_taskyield:
1705*67e74705SXin Li   case OMPD_barrier:
1706*67e74705SXin Li   case OMPD_taskwait:
1707*67e74705SXin Li   case OMPD_cancellation_point:
1708*67e74705SXin Li   case OMPD_cancel:
1709*67e74705SXin Li   case OMPD_flush:
1710*67e74705SXin Li   case OMPD_target_enter_data:
1711*67e74705SXin Li   case OMPD_target_exit_data:
1712*67e74705SXin Li   case OMPD_declare_reduction:
1713*67e74705SXin Li   case OMPD_declare_simd:
1714*67e74705SXin Li   case OMPD_declare_target:
1715*67e74705SXin Li   case OMPD_end_declare_target:
1716*67e74705SXin Li   case OMPD_target_update:
1717*67e74705SXin Li     llvm_unreachable("OpenMP Directive is not allowed");
1718*67e74705SXin Li   case OMPD_unknown:
1719*67e74705SXin Li     llvm_unreachable("Unknown OpenMP directive");
1720*67e74705SXin Li   }
1721*67e74705SXin Li }
1722*67e74705SXin Li 
buildCaptureDecl(Sema & S,IdentifierInfo * Id,Expr * CaptureExpr,bool WithInit,bool AsExpression)1723*67e74705SXin Li static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
1724*67e74705SXin Li                                              Expr *CaptureExpr, bool WithInit,
1725*67e74705SXin Li                                              bool AsExpression) {
1726*67e74705SXin Li   assert(CaptureExpr);
1727*67e74705SXin Li   ASTContext &C = S.getASTContext();
1728*67e74705SXin Li   Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
1729*67e74705SXin Li   QualType Ty = Init->getType();
1730*67e74705SXin Li   if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
1731*67e74705SXin Li     if (S.getLangOpts().CPlusPlus)
1732*67e74705SXin Li       Ty = C.getLValueReferenceType(Ty);
1733*67e74705SXin Li     else {
1734*67e74705SXin Li       Ty = C.getPointerType(Ty);
1735*67e74705SXin Li       ExprResult Res =
1736*67e74705SXin Li           S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
1737*67e74705SXin Li       if (!Res.isUsable())
1738*67e74705SXin Li         return nullptr;
1739*67e74705SXin Li       Init = Res.get();
1740*67e74705SXin Li     }
1741*67e74705SXin Li     WithInit = true;
1742*67e74705SXin Li   }
1743*67e74705SXin Li   auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty);
1744*67e74705SXin Li   if (!WithInit)
1745*67e74705SXin Li     CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C, SourceRange()));
1746*67e74705SXin Li   S.CurContext->addHiddenDecl(CED);
1747*67e74705SXin Li   S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false,
1748*67e74705SXin Li                          /*TypeMayContainAuto=*/true);
1749*67e74705SXin Li   return CED;
1750*67e74705SXin Li }
1751*67e74705SXin Li 
buildCapture(Sema & S,ValueDecl * D,Expr * CaptureExpr,bool WithInit)1752*67e74705SXin Li static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
1753*67e74705SXin Li                                  bool WithInit) {
1754*67e74705SXin Li   OMPCapturedExprDecl *CD;
1755*67e74705SXin Li   if (auto *VD = S.IsOpenMPCapturedDecl(D))
1756*67e74705SXin Li     CD = cast<OMPCapturedExprDecl>(VD);
1757*67e74705SXin Li   else
1758*67e74705SXin Li     CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
1759*67e74705SXin Li                           /*AsExpression=*/false);
1760*67e74705SXin Li   return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1761*67e74705SXin Li                           CaptureExpr->getExprLoc());
1762*67e74705SXin Li }
1763*67e74705SXin Li 
buildCapture(Sema & S,Expr * CaptureExpr,DeclRefExpr * & Ref)1764*67e74705SXin Li static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
1765*67e74705SXin Li   if (!Ref) {
1766*67e74705SXin Li     auto *CD =
1767*67e74705SXin Li         buildCaptureDecl(S, &S.getASTContext().Idents.get(".capture_expr."),
1768*67e74705SXin Li                          CaptureExpr, /*WithInit=*/true, /*AsExpression=*/true);
1769*67e74705SXin Li     Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
1770*67e74705SXin Li                            CaptureExpr->getExprLoc());
1771*67e74705SXin Li   }
1772*67e74705SXin Li   ExprResult Res = Ref;
1773*67e74705SXin Li   if (!S.getLangOpts().CPlusPlus &&
1774*67e74705SXin Li       CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
1775*67e74705SXin Li       Ref->getType()->isPointerType())
1776*67e74705SXin Li     Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
1777*67e74705SXin Li   if (!Res.isUsable())
1778*67e74705SXin Li     return ExprError();
1779*67e74705SXin Li   return CaptureExpr->isGLValue() ? Res : S.DefaultLvalueConversion(Res.get());
1780*67e74705SXin Li }
1781*67e74705SXin Li 
ActOnOpenMPRegionEnd(StmtResult S,ArrayRef<OMPClause * > Clauses)1782*67e74705SXin Li StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1783*67e74705SXin Li                                       ArrayRef<OMPClause *> Clauses) {
1784*67e74705SXin Li   if (!S.isUsable()) {
1785*67e74705SXin Li     ActOnCapturedRegionError();
1786*67e74705SXin Li     return StmtError();
1787*67e74705SXin Li   }
1788*67e74705SXin Li 
1789*67e74705SXin Li   OMPOrderedClause *OC = nullptr;
1790*67e74705SXin Li   OMPScheduleClause *SC = nullptr;
1791*67e74705SXin Li   SmallVector<OMPLinearClause *, 4> LCs;
1792*67e74705SXin Li   // This is required for proper codegen.
1793*67e74705SXin Li   for (auto *Clause : Clauses) {
1794*67e74705SXin Li     if (isOpenMPPrivate(Clause->getClauseKind()) ||
1795*67e74705SXin Li         Clause->getClauseKind() == OMPC_copyprivate ||
1796*67e74705SXin Li         (getLangOpts().OpenMPUseTLS &&
1797*67e74705SXin Li          getASTContext().getTargetInfo().isTLSSupported() &&
1798*67e74705SXin Li          Clause->getClauseKind() == OMPC_copyin)) {
1799*67e74705SXin Li       DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
1800*67e74705SXin Li       // Mark all variables in private list clauses as used in inner region.
1801*67e74705SXin Li       for (auto *VarRef : Clause->children()) {
1802*67e74705SXin Li         if (auto *E = cast_or_null<Expr>(VarRef)) {
1803*67e74705SXin Li           MarkDeclarationsReferencedInExpr(E);
1804*67e74705SXin Li         }
1805*67e74705SXin Li       }
1806*67e74705SXin Li       DSAStack->setForceVarCapturing(/*V=*/false);
1807*67e74705SXin Li     } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
1808*67e74705SXin Li       // Mark all variables in private list clauses as used in inner region.
1809*67e74705SXin Li       // Required for proper codegen of combined directives.
1810*67e74705SXin Li       // TODO: add processing for other clauses.
1811*67e74705SXin Li       if (auto *C = OMPClauseWithPreInit::get(Clause)) {
1812*67e74705SXin Li         if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
1813*67e74705SXin Li           for (auto *D : DS->decls())
1814*67e74705SXin Li             MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
1815*67e74705SXin Li         }
1816*67e74705SXin Li       }
1817*67e74705SXin Li       if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
1818*67e74705SXin Li         if (auto *E = C->getPostUpdateExpr())
1819*67e74705SXin Li           MarkDeclarationsReferencedInExpr(E);
1820*67e74705SXin Li       }
1821*67e74705SXin Li     }
1822*67e74705SXin Li     if (Clause->getClauseKind() == OMPC_schedule)
1823*67e74705SXin Li       SC = cast<OMPScheduleClause>(Clause);
1824*67e74705SXin Li     else if (Clause->getClauseKind() == OMPC_ordered)
1825*67e74705SXin Li       OC = cast<OMPOrderedClause>(Clause);
1826*67e74705SXin Li     else if (Clause->getClauseKind() == OMPC_linear)
1827*67e74705SXin Li       LCs.push_back(cast<OMPLinearClause>(Clause));
1828*67e74705SXin Li   }
1829*67e74705SXin Li   bool ErrorFound = false;
1830*67e74705SXin Li   // OpenMP, 2.7.1 Loop Construct, Restrictions
1831*67e74705SXin Li   // The nonmonotonic modifier cannot be specified if an ordered clause is
1832*67e74705SXin Li   // specified.
1833*67e74705SXin Li   if (SC &&
1834*67e74705SXin Li       (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
1835*67e74705SXin Li        SC->getSecondScheduleModifier() ==
1836*67e74705SXin Li            OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
1837*67e74705SXin Li       OC) {
1838*67e74705SXin Li     Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
1839*67e74705SXin Li              ? SC->getFirstScheduleModifierLoc()
1840*67e74705SXin Li              : SC->getSecondScheduleModifierLoc(),
1841*67e74705SXin Li          diag::err_omp_schedule_nonmonotonic_ordered)
1842*67e74705SXin Li         << SourceRange(OC->getLocStart(), OC->getLocEnd());
1843*67e74705SXin Li     ErrorFound = true;
1844*67e74705SXin Li   }
1845*67e74705SXin Li   if (!LCs.empty() && OC && OC->getNumForLoops()) {
1846*67e74705SXin Li     for (auto *C : LCs) {
1847*67e74705SXin Li       Diag(C->getLocStart(), diag::err_omp_linear_ordered)
1848*67e74705SXin Li           << SourceRange(OC->getLocStart(), OC->getLocEnd());
1849*67e74705SXin Li     }
1850*67e74705SXin Li     ErrorFound = true;
1851*67e74705SXin Li   }
1852*67e74705SXin Li   if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
1853*67e74705SXin Li       isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
1854*67e74705SXin Li       OC->getNumForLoops()) {
1855*67e74705SXin Li     Diag(OC->getLocStart(), diag::err_omp_ordered_simd)
1856*67e74705SXin Li         << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
1857*67e74705SXin Li     ErrorFound = true;
1858*67e74705SXin Li   }
1859*67e74705SXin Li   if (ErrorFound) {
1860*67e74705SXin Li     ActOnCapturedRegionError();
1861*67e74705SXin Li     return StmtError();
1862*67e74705SXin Li   }
1863*67e74705SXin Li   return ActOnCapturedRegionEnd(S.get());
1864*67e74705SXin Li }
1865*67e74705SXin Li 
CheckNestingOfRegions(Sema & SemaRef,DSAStackTy * Stack,OpenMPDirectiveKind CurrentRegion,const DeclarationNameInfo & CurrentName,OpenMPDirectiveKind CancelRegion,SourceLocation StartLoc)1866*67e74705SXin Li static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1867*67e74705SXin Li                                   OpenMPDirectiveKind CurrentRegion,
1868*67e74705SXin Li                                   const DeclarationNameInfo &CurrentName,
1869*67e74705SXin Li                                   OpenMPDirectiveKind CancelRegion,
1870*67e74705SXin Li                                   SourceLocation StartLoc) {
1871*67e74705SXin Li   // Allowed nesting of constructs
1872*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
1873*67e74705SXin Li   // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1874*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
1875*67e74705SXin Li   // | parallel         | parallel        | *                                  |
1876*67e74705SXin Li   // | parallel         | for             | *                                  |
1877*67e74705SXin Li   // | parallel         | for simd        | *                                  |
1878*67e74705SXin Li   // | parallel         | master          | *                                  |
1879*67e74705SXin Li   // | parallel         | critical        | *                                  |
1880*67e74705SXin Li   // | parallel         | simd            | *                                  |
1881*67e74705SXin Li   // | parallel         | sections        | *                                  |
1882*67e74705SXin Li   // | parallel         | section         | +                                  |
1883*67e74705SXin Li   // | parallel         | single          | *                                  |
1884*67e74705SXin Li   // | parallel         | parallel for    | *                                  |
1885*67e74705SXin Li   // | parallel         |parallel for simd| *                                  |
1886*67e74705SXin Li   // | parallel         |parallel sections| *                                  |
1887*67e74705SXin Li   // | parallel         | task            | *                                  |
1888*67e74705SXin Li   // | parallel         | taskyield       | *                                  |
1889*67e74705SXin Li   // | parallel         | barrier         | *                                  |
1890*67e74705SXin Li   // | parallel         | taskwait        | *                                  |
1891*67e74705SXin Li   // | parallel         | taskgroup       | *                                  |
1892*67e74705SXin Li   // | parallel         | flush           | *                                  |
1893*67e74705SXin Li   // | parallel         | ordered         | +                                  |
1894*67e74705SXin Li   // | parallel         | atomic          | *                                  |
1895*67e74705SXin Li   // | parallel         | target          | *                                  |
1896*67e74705SXin Li   // | parallel         | target parallel | *                                  |
1897*67e74705SXin Li   // | parallel         | target parallel | *                                  |
1898*67e74705SXin Li   // |                  | for             |                                    |
1899*67e74705SXin Li   // | parallel         | target enter    | *                                  |
1900*67e74705SXin Li   // |                  | data            |                                    |
1901*67e74705SXin Li   // | parallel         | target exit     | *                                  |
1902*67e74705SXin Li   // |                  | data            |                                    |
1903*67e74705SXin Li   // | parallel         | teams           | +                                  |
1904*67e74705SXin Li   // | parallel         | cancellation    |                                    |
1905*67e74705SXin Li   // |                  | point           | !                                  |
1906*67e74705SXin Li   // | parallel         | cancel          | !                                  |
1907*67e74705SXin Li   // | parallel         | taskloop        | *                                  |
1908*67e74705SXin Li   // | parallel         | taskloop simd   | *                                  |
1909*67e74705SXin Li   // | parallel         | distribute      | +                                  |
1910*67e74705SXin Li   // | parallel         | distribute      | +                                  |
1911*67e74705SXin Li   // |                  | parallel for    |                                    |
1912*67e74705SXin Li   // | parallel         | distribute      | +                                  |
1913*67e74705SXin Li   // |                  |parallel for simd|                                    |
1914*67e74705SXin Li   // | parallel         | distribute simd | +                                  |
1915*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
1916*67e74705SXin Li   // | for              | parallel        | *                                  |
1917*67e74705SXin Li   // | for              | for             | +                                  |
1918*67e74705SXin Li   // | for              | for simd        | +                                  |
1919*67e74705SXin Li   // | for              | master          | +                                  |
1920*67e74705SXin Li   // | for              | critical        | *                                  |
1921*67e74705SXin Li   // | for              | simd            | *                                  |
1922*67e74705SXin Li   // | for              | sections        | +                                  |
1923*67e74705SXin Li   // | for              | section         | +                                  |
1924*67e74705SXin Li   // | for              | single          | +                                  |
1925*67e74705SXin Li   // | for              | parallel for    | *                                  |
1926*67e74705SXin Li   // | for              |parallel for simd| *                                  |
1927*67e74705SXin Li   // | for              |parallel sections| *                                  |
1928*67e74705SXin Li   // | for              | task            | *                                  |
1929*67e74705SXin Li   // | for              | taskyield       | *                                  |
1930*67e74705SXin Li   // | for              | barrier         | +                                  |
1931*67e74705SXin Li   // | for              | taskwait        | *                                  |
1932*67e74705SXin Li   // | for              | taskgroup       | *                                  |
1933*67e74705SXin Li   // | for              | flush           | *                                  |
1934*67e74705SXin Li   // | for              | ordered         | * (if construct is ordered)        |
1935*67e74705SXin Li   // | for              | atomic          | *                                  |
1936*67e74705SXin Li   // | for              | target          | *                                  |
1937*67e74705SXin Li   // | for              | target parallel | *                                  |
1938*67e74705SXin Li   // | for              | target parallel | *                                  |
1939*67e74705SXin Li   // |                  | for             |                                    |
1940*67e74705SXin Li   // | for              | target enter    | *                                  |
1941*67e74705SXin Li   // |                  | data            |                                    |
1942*67e74705SXin Li   // | for              | target exit     | *                                  |
1943*67e74705SXin Li   // |                  | data            |                                    |
1944*67e74705SXin Li   // | for              | teams           | +                                  |
1945*67e74705SXin Li   // | for              | cancellation    |                                    |
1946*67e74705SXin Li   // |                  | point           | !                                  |
1947*67e74705SXin Li   // | for              | cancel          | !                                  |
1948*67e74705SXin Li   // | for              | taskloop        | *                                  |
1949*67e74705SXin Li   // | for              | taskloop simd   | *                                  |
1950*67e74705SXin Li   // | for              | distribute      | +                                  |
1951*67e74705SXin Li   // | for              | distribute      | +                                  |
1952*67e74705SXin Li   // |                  | parallel for    |                                    |
1953*67e74705SXin Li   // | for              | distribute      | +                                  |
1954*67e74705SXin Li   // |                  |parallel for simd|                                    |
1955*67e74705SXin Li   // | for              | distribute simd | +                                  |
1956*67e74705SXin Li   // | for              | target parallel | +                                  |
1957*67e74705SXin Li   // |                  | for simd        |                                    |
1958*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
1959*67e74705SXin Li   // | master           | parallel        | *                                  |
1960*67e74705SXin Li   // | master           | for             | +                                  |
1961*67e74705SXin Li   // | master           | for simd        | +                                  |
1962*67e74705SXin Li   // | master           | master          | *                                  |
1963*67e74705SXin Li   // | master           | critical        | *                                  |
1964*67e74705SXin Li   // | master           | simd            | *                                  |
1965*67e74705SXin Li   // | master           | sections        | +                                  |
1966*67e74705SXin Li   // | master           | section         | +                                  |
1967*67e74705SXin Li   // | master           | single          | +                                  |
1968*67e74705SXin Li   // | master           | parallel for    | *                                  |
1969*67e74705SXin Li   // | master           |parallel for simd| *                                  |
1970*67e74705SXin Li   // | master           |parallel sections| *                                  |
1971*67e74705SXin Li   // | master           | task            | *                                  |
1972*67e74705SXin Li   // | master           | taskyield       | *                                  |
1973*67e74705SXin Li   // | master           | barrier         | +                                  |
1974*67e74705SXin Li   // | master           | taskwait        | *                                  |
1975*67e74705SXin Li   // | master           | taskgroup       | *                                  |
1976*67e74705SXin Li   // | master           | flush           | *                                  |
1977*67e74705SXin Li   // | master           | ordered         | +                                  |
1978*67e74705SXin Li   // | master           | atomic          | *                                  |
1979*67e74705SXin Li   // | master           | target          | *                                  |
1980*67e74705SXin Li   // | master           | target parallel | *                                  |
1981*67e74705SXin Li   // | master           | target parallel | *                                  |
1982*67e74705SXin Li   // |                  | for             |                                    |
1983*67e74705SXin Li   // | master           | target enter    | *                                  |
1984*67e74705SXin Li   // |                  | data            |                                    |
1985*67e74705SXin Li   // | master           | target exit     | *                                  |
1986*67e74705SXin Li   // |                  | data            |                                    |
1987*67e74705SXin Li   // | master           | teams           | +                                  |
1988*67e74705SXin Li   // | master           | cancellation    |                                    |
1989*67e74705SXin Li   // |                  | point           |                                    |
1990*67e74705SXin Li   // | master           | cancel          |                                    |
1991*67e74705SXin Li   // | master           | taskloop        | *                                  |
1992*67e74705SXin Li   // | master           | taskloop simd   | *                                  |
1993*67e74705SXin Li   // | master           | distribute      | +                                  |
1994*67e74705SXin Li   // | master           | distribute      | +                                  |
1995*67e74705SXin Li   // |                  | parallel for    |                                    |
1996*67e74705SXin Li   // | master           | distribute      | +                                  |
1997*67e74705SXin Li   // |                  |parallel for simd|                                    |
1998*67e74705SXin Li   // | master           | distribute simd | +                                  |
1999*67e74705SXin Li   // | master           | target parallel | +                                  |
2000*67e74705SXin Li   // |                  | for simd        |                                    |
2001*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2002*67e74705SXin Li   // | critical         | parallel        | *                                  |
2003*67e74705SXin Li   // | critical         | for             | +                                  |
2004*67e74705SXin Li   // | critical         | for simd        | +                                  |
2005*67e74705SXin Li   // | critical         | master          | *                                  |
2006*67e74705SXin Li   // | critical         | critical        | * (should have different names)    |
2007*67e74705SXin Li   // | critical         | simd            | *                                  |
2008*67e74705SXin Li   // | critical         | sections        | +                                  |
2009*67e74705SXin Li   // | critical         | section         | +                                  |
2010*67e74705SXin Li   // | critical         | single          | +                                  |
2011*67e74705SXin Li   // | critical         | parallel for    | *                                  |
2012*67e74705SXin Li   // | critical         |parallel for simd| *                                  |
2013*67e74705SXin Li   // | critical         |parallel sections| *                                  |
2014*67e74705SXin Li   // | critical         | task            | *                                  |
2015*67e74705SXin Li   // | critical         | taskyield       | *                                  |
2016*67e74705SXin Li   // | critical         | barrier         | +                                  |
2017*67e74705SXin Li   // | critical         | taskwait        | *                                  |
2018*67e74705SXin Li   // | critical         | taskgroup       | *                                  |
2019*67e74705SXin Li   // | critical         | ordered         | +                                  |
2020*67e74705SXin Li   // | critical         | atomic          | *                                  |
2021*67e74705SXin Li   // | critical         | target          | *                                  |
2022*67e74705SXin Li   // | critical         | target parallel | *                                  |
2023*67e74705SXin Li   // | critical         | target parallel | *                                  |
2024*67e74705SXin Li   // |                  | for             |                                    |
2025*67e74705SXin Li   // | critical         | target enter    | *                                  |
2026*67e74705SXin Li   // |                  | data            |                                    |
2027*67e74705SXin Li   // | critical         | target exit     | *                                  |
2028*67e74705SXin Li   // |                  | data            |                                    |
2029*67e74705SXin Li   // | critical         | teams           | +                                  |
2030*67e74705SXin Li   // | critical         | cancellation    |                                    |
2031*67e74705SXin Li   // |                  | point           |                                    |
2032*67e74705SXin Li   // | critical         | cancel          |                                    |
2033*67e74705SXin Li   // | critical         | taskloop        | *                                  |
2034*67e74705SXin Li   // | critical         | taskloop simd   | *                                  |
2035*67e74705SXin Li   // | critical         | distribute      | +                                  |
2036*67e74705SXin Li   // | critical         | distribute      | +                                  |
2037*67e74705SXin Li   // |                  | parallel for    |                                    |
2038*67e74705SXin Li   // | critical         | distribute      | +                                  |
2039*67e74705SXin Li   // |                  |parallel for simd|                                    |
2040*67e74705SXin Li   // | critical         | distribute simd | +                                  |
2041*67e74705SXin Li   // | critical         | target parallel | +                                  |
2042*67e74705SXin Li   // |                  | for simd        |                                    |
2043*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2044*67e74705SXin Li   // | simd             | parallel        |                                    |
2045*67e74705SXin Li   // | simd             | for             |                                    |
2046*67e74705SXin Li   // | simd             | for simd        |                                    |
2047*67e74705SXin Li   // | simd             | master          |                                    |
2048*67e74705SXin Li   // | simd             | critical        |                                    |
2049*67e74705SXin Li   // | simd             | simd            | *                                  |
2050*67e74705SXin Li   // | simd             | sections        |                                    |
2051*67e74705SXin Li   // | simd             | section         |                                    |
2052*67e74705SXin Li   // | simd             | single          |                                    |
2053*67e74705SXin Li   // | simd             | parallel for    |                                    |
2054*67e74705SXin Li   // | simd             |parallel for simd|                                    |
2055*67e74705SXin Li   // | simd             |parallel sections|                                    |
2056*67e74705SXin Li   // | simd             | task            |                                    |
2057*67e74705SXin Li   // | simd             | taskyield       |                                    |
2058*67e74705SXin Li   // | simd             | barrier         |                                    |
2059*67e74705SXin Li   // | simd             | taskwait        |                                    |
2060*67e74705SXin Li   // | simd             | taskgroup       |                                    |
2061*67e74705SXin Li   // | simd             | flush           |                                    |
2062*67e74705SXin Li   // | simd             | ordered         | + (with simd clause)               |
2063*67e74705SXin Li   // | simd             | atomic          |                                    |
2064*67e74705SXin Li   // | simd             | target          |                                    |
2065*67e74705SXin Li   // | simd             | target parallel |                                    |
2066*67e74705SXin Li   // | simd             | target parallel |                                    |
2067*67e74705SXin Li   // |                  | for             |                                    |
2068*67e74705SXin Li   // | simd             | target enter    |                                    |
2069*67e74705SXin Li   // |                  | data            |                                    |
2070*67e74705SXin Li   // | simd             | target exit     |                                    |
2071*67e74705SXin Li   // |                  | data            |                                    |
2072*67e74705SXin Li   // | simd             | teams           |                                    |
2073*67e74705SXin Li   // | simd             | cancellation    |                                    |
2074*67e74705SXin Li   // |                  | point           |                                    |
2075*67e74705SXin Li   // | simd             | cancel          |                                    |
2076*67e74705SXin Li   // | simd             | taskloop        |                                    |
2077*67e74705SXin Li   // | simd             | taskloop simd   |                                    |
2078*67e74705SXin Li   // | simd             | distribute      |                                    |
2079*67e74705SXin Li   // | simd             | distribute      |                                    |
2080*67e74705SXin Li   // |                  | parallel for    |                                    |
2081*67e74705SXin Li   // | simd             | distribute      |                                    |
2082*67e74705SXin Li   // |                  |parallel for simd|                                    |
2083*67e74705SXin Li   // | simd             | distribute simd |                                    |
2084*67e74705SXin Li   // | simd             | target parallel |                                    |
2085*67e74705SXin Li   // |                  | for simd        |                                    |
2086*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2087*67e74705SXin Li   // | for simd         | parallel        |                                    |
2088*67e74705SXin Li   // | for simd         | for             |                                    |
2089*67e74705SXin Li   // | for simd         | for simd        |                                    |
2090*67e74705SXin Li   // | for simd         | master          |                                    |
2091*67e74705SXin Li   // | for simd         | critical        |                                    |
2092*67e74705SXin Li   // | for simd         | simd            | *                                  |
2093*67e74705SXin Li   // | for simd         | sections        |                                    |
2094*67e74705SXin Li   // | for simd         | section         |                                    |
2095*67e74705SXin Li   // | for simd         | single          |                                    |
2096*67e74705SXin Li   // | for simd         | parallel for    |                                    |
2097*67e74705SXin Li   // | for simd         |parallel for simd|                                    |
2098*67e74705SXin Li   // | for simd         |parallel sections|                                    |
2099*67e74705SXin Li   // | for simd         | task            |                                    |
2100*67e74705SXin Li   // | for simd         | taskyield       |                                    |
2101*67e74705SXin Li   // | for simd         | barrier         |                                    |
2102*67e74705SXin Li   // | for simd         | taskwait        |                                    |
2103*67e74705SXin Li   // | for simd         | taskgroup       |                                    |
2104*67e74705SXin Li   // | for simd         | flush           |                                    |
2105*67e74705SXin Li   // | for simd         | ordered         | + (with simd clause)               |
2106*67e74705SXin Li   // | for simd         | atomic          |                                    |
2107*67e74705SXin Li   // | for simd         | target          |                                    |
2108*67e74705SXin Li   // | for simd         | target parallel |                                    |
2109*67e74705SXin Li   // | for simd         | target parallel |                                    |
2110*67e74705SXin Li   // |                  | for             |                                    |
2111*67e74705SXin Li   // | for simd         | target enter    |                                    |
2112*67e74705SXin Li   // |                  | data            |                                    |
2113*67e74705SXin Li   // | for simd         | target exit     |                                    |
2114*67e74705SXin Li   // |                  | data            |                                    |
2115*67e74705SXin Li   // | for simd         | teams           |                                    |
2116*67e74705SXin Li   // | for simd         | cancellation    |                                    |
2117*67e74705SXin Li   // |                  | point           |                                    |
2118*67e74705SXin Li   // | for simd         | cancel          |                                    |
2119*67e74705SXin Li   // | for simd         | taskloop        |                                    |
2120*67e74705SXin Li   // | for simd         | taskloop simd   |                                    |
2121*67e74705SXin Li   // | for simd         | distribute      |                                    |
2122*67e74705SXin Li   // | for simd         | distribute      |                                    |
2123*67e74705SXin Li   // |                  | parallel for    |                                    |
2124*67e74705SXin Li   // | for simd         | distribute      |                                    |
2125*67e74705SXin Li   // |                  |parallel for simd|                                    |
2126*67e74705SXin Li   // | for simd         | distribute simd |                                    |
2127*67e74705SXin Li   // | for simd         | target parallel |                                    |
2128*67e74705SXin Li   // |                  | for simd        |                                    |
2129*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2130*67e74705SXin Li   // | parallel for simd| parallel        |                                    |
2131*67e74705SXin Li   // | parallel for simd| for             |                                    |
2132*67e74705SXin Li   // | parallel for simd| for simd        |                                    |
2133*67e74705SXin Li   // | parallel for simd| master          |                                    |
2134*67e74705SXin Li   // | parallel for simd| critical        |                                    |
2135*67e74705SXin Li   // | parallel for simd| simd            | *                                  |
2136*67e74705SXin Li   // | parallel for simd| sections        |                                    |
2137*67e74705SXin Li   // | parallel for simd| section         |                                    |
2138*67e74705SXin Li   // | parallel for simd| single          |                                    |
2139*67e74705SXin Li   // | parallel for simd| parallel for    |                                    |
2140*67e74705SXin Li   // | parallel for simd|parallel for simd|                                    |
2141*67e74705SXin Li   // | parallel for simd|parallel sections|                                    |
2142*67e74705SXin Li   // | parallel for simd| task            |                                    |
2143*67e74705SXin Li   // | parallel for simd| taskyield       |                                    |
2144*67e74705SXin Li   // | parallel for simd| barrier         |                                    |
2145*67e74705SXin Li   // | parallel for simd| taskwait        |                                    |
2146*67e74705SXin Li   // | parallel for simd| taskgroup       |                                    |
2147*67e74705SXin Li   // | parallel for simd| flush           |                                    |
2148*67e74705SXin Li   // | parallel for simd| ordered         | + (with simd clause)               |
2149*67e74705SXin Li   // | parallel for simd| atomic          |                                    |
2150*67e74705SXin Li   // | parallel for simd| target          |                                    |
2151*67e74705SXin Li   // | parallel for simd| target parallel |                                    |
2152*67e74705SXin Li   // | parallel for simd| target parallel |                                    |
2153*67e74705SXin Li   // |                  | for             |                                    |
2154*67e74705SXin Li   // | parallel for simd| target enter    |                                    |
2155*67e74705SXin Li   // |                  | data            |                                    |
2156*67e74705SXin Li   // | parallel for simd| target exit     |                                    |
2157*67e74705SXin Li   // |                  | data            |                                    |
2158*67e74705SXin Li   // | parallel for simd| teams           |                                    |
2159*67e74705SXin Li   // | parallel for simd| cancellation    |                                    |
2160*67e74705SXin Li   // |                  | point           |                                    |
2161*67e74705SXin Li   // | parallel for simd| cancel          |                                    |
2162*67e74705SXin Li   // | parallel for simd| taskloop        |                                    |
2163*67e74705SXin Li   // | parallel for simd| taskloop simd   |                                    |
2164*67e74705SXin Li   // | parallel for simd| distribute      |                                    |
2165*67e74705SXin Li   // | parallel for simd| distribute      |                                    |
2166*67e74705SXin Li   // |                  | parallel for    |                                    |
2167*67e74705SXin Li   // | parallel for simd| distribute      |                                    |
2168*67e74705SXin Li   // |                  |parallel for simd|                                    |
2169*67e74705SXin Li   // | parallel for simd| distribute simd |                                    |
2170*67e74705SXin Li   // |                  | for simd        |                                    |
2171*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2172*67e74705SXin Li   // | sections         | parallel        | *                                  |
2173*67e74705SXin Li   // | sections         | for             | +                                  |
2174*67e74705SXin Li   // | sections         | for simd        | +                                  |
2175*67e74705SXin Li   // | sections         | master          | +                                  |
2176*67e74705SXin Li   // | sections         | critical        | *                                  |
2177*67e74705SXin Li   // | sections         | simd            | *                                  |
2178*67e74705SXin Li   // | sections         | sections        | +                                  |
2179*67e74705SXin Li   // | sections         | section         | *                                  |
2180*67e74705SXin Li   // | sections         | single          | +                                  |
2181*67e74705SXin Li   // | sections         | parallel for    | *                                  |
2182*67e74705SXin Li   // | sections         |parallel for simd| *                                  |
2183*67e74705SXin Li   // | sections         |parallel sections| *                                  |
2184*67e74705SXin Li   // | sections         | task            | *                                  |
2185*67e74705SXin Li   // | sections         | taskyield       | *                                  |
2186*67e74705SXin Li   // | sections         | barrier         | +                                  |
2187*67e74705SXin Li   // | sections         | taskwait        | *                                  |
2188*67e74705SXin Li   // | sections         | taskgroup       | *                                  |
2189*67e74705SXin Li   // | sections         | flush           | *                                  |
2190*67e74705SXin Li   // | sections         | ordered         | +                                  |
2191*67e74705SXin Li   // | sections         | atomic          | *                                  |
2192*67e74705SXin Li   // | sections         | target          | *                                  |
2193*67e74705SXin Li   // | sections         | target parallel | *                                  |
2194*67e74705SXin Li   // | sections         | target parallel | *                                  |
2195*67e74705SXin Li   // |                  | for             |                                    |
2196*67e74705SXin Li   // | sections         | target enter    | *                                  |
2197*67e74705SXin Li   // |                  | data            |                                    |
2198*67e74705SXin Li   // | sections         | target exit     | *                                  |
2199*67e74705SXin Li   // |                  | data            |                                    |
2200*67e74705SXin Li   // | sections         | teams           | +                                  |
2201*67e74705SXin Li   // | sections         | cancellation    |                                    |
2202*67e74705SXin Li   // |                  | point           | !                                  |
2203*67e74705SXin Li   // | sections         | cancel          | !                                  |
2204*67e74705SXin Li   // | sections         | taskloop        | *                                  |
2205*67e74705SXin Li   // | sections         | taskloop simd   | *                                  |
2206*67e74705SXin Li   // | sections         | distribute      | +                                  |
2207*67e74705SXin Li   // | sections         | distribute      | +                                  |
2208*67e74705SXin Li   // |                  | parallel for    |                                    |
2209*67e74705SXin Li   // | sections         | distribute      | +                                  |
2210*67e74705SXin Li   // |                  |parallel for simd|                                    |
2211*67e74705SXin Li   // | sections         | distribute simd | +                                  |
2212*67e74705SXin Li   // | sections         | target parallel | +                                  |
2213*67e74705SXin Li   // |                  | for simd        |                                    |
2214*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2215*67e74705SXin Li   // | section          | parallel        | *                                  |
2216*67e74705SXin Li   // | section          | for             | +                                  |
2217*67e74705SXin Li   // | section          | for simd        | +                                  |
2218*67e74705SXin Li   // | section          | master          | +                                  |
2219*67e74705SXin Li   // | section          | critical        | *                                  |
2220*67e74705SXin Li   // | section          | simd            | *                                  |
2221*67e74705SXin Li   // | section          | sections        | +                                  |
2222*67e74705SXin Li   // | section          | section         | +                                  |
2223*67e74705SXin Li   // | section          | single          | +                                  |
2224*67e74705SXin Li   // | section          | parallel for    | *                                  |
2225*67e74705SXin Li   // | section          |parallel for simd| *                                  |
2226*67e74705SXin Li   // | section          |parallel sections| *                                  |
2227*67e74705SXin Li   // | section          | task            | *                                  |
2228*67e74705SXin Li   // | section          | taskyield       | *                                  |
2229*67e74705SXin Li   // | section          | barrier         | +                                  |
2230*67e74705SXin Li   // | section          | taskwait        | *                                  |
2231*67e74705SXin Li   // | section          | taskgroup       | *                                  |
2232*67e74705SXin Li   // | section          | flush           | *                                  |
2233*67e74705SXin Li   // | section          | ordered         | +                                  |
2234*67e74705SXin Li   // | section          | atomic          | *                                  |
2235*67e74705SXin Li   // | section          | target          | *                                  |
2236*67e74705SXin Li   // | section          | target parallel | *                                  |
2237*67e74705SXin Li   // | section          | target parallel | *                                  |
2238*67e74705SXin Li   // |                  | for             |                                    |
2239*67e74705SXin Li   // | section          | target enter    | *                                  |
2240*67e74705SXin Li   // |                  | data            |                                    |
2241*67e74705SXin Li   // | section          | target exit     | *                                  |
2242*67e74705SXin Li   // |                  | data            |                                    |
2243*67e74705SXin Li   // | section          | teams           | +                                  |
2244*67e74705SXin Li   // | section          | cancellation    |                                    |
2245*67e74705SXin Li   // |                  | point           | !                                  |
2246*67e74705SXin Li   // | section          | cancel          | !                                  |
2247*67e74705SXin Li   // | section          | taskloop        | *                                  |
2248*67e74705SXin Li   // | section          | taskloop simd   | *                                  |
2249*67e74705SXin Li   // | section          | distribute      | +                                  |
2250*67e74705SXin Li   // | section          | distribute      | +                                  |
2251*67e74705SXin Li   // |                  | parallel for    |                                    |
2252*67e74705SXin Li   // | section          | distribute      | +                                  |
2253*67e74705SXin Li   // |                  |parallel for simd|                                    |
2254*67e74705SXin Li   // | section          | distribute simd | +                                  |
2255*67e74705SXin Li   // | section          | target parallel | +                                  |
2256*67e74705SXin Li   // |                  | for simd        |                                    |
2257*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2258*67e74705SXin Li   // | single           | parallel        | *                                  |
2259*67e74705SXin Li   // | single           | for             | +                                  |
2260*67e74705SXin Li   // | single           | for simd        | +                                  |
2261*67e74705SXin Li   // | single           | master          | +                                  |
2262*67e74705SXin Li   // | single           | critical        | *                                  |
2263*67e74705SXin Li   // | single           | simd            | *                                  |
2264*67e74705SXin Li   // | single           | sections        | +                                  |
2265*67e74705SXin Li   // | single           | section         | +                                  |
2266*67e74705SXin Li   // | single           | single          | +                                  |
2267*67e74705SXin Li   // | single           | parallel for    | *                                  |
2268*67e74705SXin Li   // | single           |parallel for simd| *                                  |
2269*67e74705SXin Li   // | single           |parallel sections| *                                  |
2270*67e74705SXin Li   // | single           | task            | *                                  |
2271*67e74705SXin Li   // | single           | taskyield       | *                                  |
2272*67e74705SXin Li   // | single           | barrier         | +                                  |
2273*67e74705SXin Li   // | single           | taskwait        | *                                  |
2274*67e74705SXin Li   // | single           | taskgroup       | *                                  |
2275*67e74705SXin Li   // | single           | flush           | *                                  |
2276*67e74705SXin Li   // | single           | ordered         | +                                  |
2277*67e74705SXin Li   // | single           | atomic          | *                                  |
2278*67e74705SXin Li   // | single           | target          | *                                  |
2279*67e74705SXin Li   // | single           | target parallel | *                                  |
2280*67e74705SXin Li   // | single           | target parallel | *                                  |
2281*67e74705SXin Li   // |                  | for             |                                    |
2282*67e74705SXin Li   // | single           | target enter    | *                                  |
2283*67e74705SXin Li   // |                  | data            |                                    |
2284*67e74705SXin Li   // | single           | target exit     | *                                  |
2285*67e74705SXin Li   // |                  | data            |                                    |
2286*67e74705SXin Li   // | single           | teams           | +                                  |
2287*67e74705SXin Li   // | single           | cancellation    |                                    |
2288*67e74705SXin Li   // |                  | point           |                                    |
2289*67e74705SXin Li   // | single           | cancel          |                                    |
2290*67e74705SXin Li   // | single           | taskloop        | *                                  |
2291*67e74705SXin Li   // | single           | taskloop simd   | *                                  |
2292*67e74705SXin Li   // | single           | distribute      | +                                  |
2293*67e74705SXin Li   // | single           | distribute      | +                                  |
2294*67e74705SXin Li   // |                  | parallel for    |                                    |
2295*67e74705SXin Li   // | single           | distribute      | +                                  |
2296*67e74705SXin Li   // |                  |parallel for simd|                                    |
2297*67e74705SXin Li   // | single           | distribute simd | +                                  |
2298*67e74705SXin Li   // | single           | target parallel | +                                  |
2299*67e74705SXin Li   // |                  | for simd        |                                    |
2300*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2301*67e74705SXin Li   // | parallel for     | parallel        | *                                  |
2302*67e74705SXin Li   // | parallel for     | for             | +                                  |
2303*67e74705SXin Li   // | parallel for     | for simd        | +                                  |
2304*67e74705SXin Li   // | parallel for     | master          | +                                  |
2305*67e74705SXin Li   // | parallel for     | critical        | *                                  |
2306*67e74705SXin Li   // | parallel for     | simd            | *                                  |
2307*67e74705SXin Li   // | parallel for     | sections        | +                                  |
2308*67e74705SXin Li   // | parallel for     | section         | +                                  |
2309*67e74705SXin Li   // | parallel for     | single          | +                                  |
2310*67e74705SXin Li   // | parallel for     | parallel for    | *                                  |
2311*67e74705SXin Li   // | parallel for     |parallel for simd| *                                  |
2312*67e74705SXin Li   // | parallel for     |parallel sections| *                                  |
2313*67e74705SXin Li   // | parallel for     | task            | *                                  |
2314*67e74705SXin Li   // | parallel for     | taskyield       | *                                  |
2315*67e74705SXin Li   // | parallel for     | barrier         | +                                  |
2316*67e74705SXin Li   // | parallel for     | taskwait        | *                                  |
2317*67e74705SXin Li   // | parallel for     | taskgroup       | *                                  |
2318*67e74705SXin Li   // | parallel for     | flush           | *                                  |
2319*67e74705SXin Li   // | parallel for     | ordered         | * (if construct is ordered)        |
2320*67e74705SXin Li   // | parallel for     | atomic          | *                                  |
2321*67e74705SXin Li   // | parallel for     | target          | *                                  |
2322*67e74705SXin Li   // | parallel for     | target parallel | *                                  |
2323*67e74705SXin Li   // | parallel for     | target parallel | *                                  |
2324*67e74705SXin Li   // |                  | for             |                                    |
2325*67e74705SXin Li   // | parallel for     | target enter    | *                                  |
2326*67e74705SXin Li   // |                  | data            |                                    |
2327*67e74705SXin Li   // | parallel for     | target exit     | *                                  |
2328*67e74705SXin Li   // |                  | data            |                                    |
2329*67e74705SXin Li   // | parallel for     | teams           | +                                  |
2330*67e74705SXin Li   // | parallel for     | cancellation    |                                    |
2331*67e74705SXin Li   // |                  | point           | !                                  |
2332*67e74705SXin Li   // | parallel for     | cancel          | !                                  |
2333*67e74705SXin Li   // | parallel for     | taskloop        | *                                  |
2334*67e74705SXin Li   // | parallel for     | taskloop simd   | *                                  |
2335*67e74705SXin Li   // | parallel for     | distribute      | +                                  |
2336*67e74705SXin Li   // | parallel for     | distribute      | +                                  |
2337*67e74705SXin Li   // |                  | parallel for    |                                    |
2338*67e74705SXin Li   // | parallel for     | distribute      | +                                  |
2339*67e74705SXin Li   // |                  |parallel for simd|                                    |
2340*67e74705SXin Li   // | parallel for     | distribute simd | +                                  |
2341*67e74705SXin Li   // | parallel for     | target parallel | +                                  |
2342*67e74705SXin Li   // |                  | for simd        |                                    |
2343*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2344*67e74705SXin Li   // | parallel sections| parallel        | *                                  |
2345*67e74705SXin Li   // | parallel sections| for             | +                                  |
2346*67e74705SXin Li   // | parallel sections| for simd        | +                                  |
2347*67e74705SXin Li   // | parallel sections| master          | +                                  |
2348*67e74705SXin Li   // | parallel sections| critical        | +                                  |
2349*67e74705SXin Li   // | parallel sections| simd            | *                                  |
2350*67e74705SXin Li   // | parallel sections| sections        | +                                  |
2351*67e74705SXin Li   // | parallel sections| section         | *                                  |
2352*67e74705SXin Li   // | parallel sections| single          | +                                  |
2353*67e74705SXin Li   // | parallel sections| parallel for    | *                                  |
2354*67e74705SXin Li   // | parallel sections|parallel for simd| *                                  |
2355*67e74705SXin Li   // | parallel sections|parallel sections| *                                  |
2356*67e74705SXin Li   // | parallel sections| task            | *                                  |
2357*67e74705SXin Li   // | parallel sections| taskyield       | *                                  |
2358*67e74705SXin Li   // | parallel sections| barrier         | +                                  |
2359*67e74705SXin Li   // | parallel sections| taskwait        | *                                  |
2360*67e74705SXin Li   // | parallel sections| taskgroup       | *                                  |
2361*67e74705SXin Li   // | parallel sections| flush           | *                                  |
2362*67e74705SXin Li   // | parallel sections| ordered         | +                                  |
2363*67e74705SXin Li   // | parallel sections| atomic          | *                                  |
2364*67e74705SXin Li   // | parallel sections| target          | *                                  |
2365*67e74705SXin Li   // | parallel sections| target parallel | *                                  |
2366*67e74705SXin Li   // | parallel sections| target parallel | *                                  |
2367*67e74705SXin Li   // |                  | for             |                                    |
2368*67e74705SXin Li   // | parallel sections| target enter    | *                                  |
2369*67e74705SXin Li   // |                  | data            |                                    |
2370*67e74705SXin Li   // | parallel sections| target exit     | *                                  |
2371*67e74705SXin Li   // |                  | data            |                                    |
2372*67e74705SXin Li   // | parallel sections| teams           | +                                  |
2373*67e74705SXin Li   // | parallel sections| cancellation    |                                    |
2374*67e74705SXin Li   // |                  | point           | !                                  |
2375*67e74705SXin Li   // | parallel sections| cancel          | !                                  |
2376*67e74705SXin Li   // | parallel sections| taskloop        | *                                  |
2377*67e74705SXin Li   // | parallel sections| taskloop simd   | *                                  |
2378*67e74705SXin Li   // | parallel sections| distribute      | +                                  |
2379*67e74705SXin Li   // | parallel sections| distribute      | +                                  |
2380*67e74705SXin Li   // |                  | parallel for    |                                    |
2381*67e74705SXin Li   // | parallel sections| distribute      | +                                  |
2382*67e74705SXin Li   // |                  |parallel for simd|                                    |
2383*67e74705SXin Li   // | parallel sections| distribute simd | +                                  |
2384*67e74705SXin Li   // | parallel sections| target parallel | +                                  |
2385*67e74705SXin Li   // |                  | for simd        |                                    |
2386*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2387*67e74705SXin Li   // | task             | parallel        | *                                  |
2388*67e74705SXin Li   // | task             | for             | +                                  |
2389*67e74705SXin Li   // | task             | for simd        | +                                  |
2390*67e74705SXin Li   // | task             | master          | +                                  |
2391*67e74705SXin Li   // | task             | critical        | *                                  |
2392*67e74705SXin Li   // | task             | simd            | *                                  |
2393*67e74705SXin Li   // | task             | sections        | +                                  |
2394*67e74705SXin Li   // | task             | section         | +                                  |
2395*67e74705SXin Li   // | task             | single          | +                                  |
2396*67e74705SXin Li   // | task             | parallel for    | *                                  |
2397*67e74705SXin Li   // | task             |parallel for simd| *                                  |
2398*67e74705SXin Li   // | task             |parallel sections| *                                  |
2399*67e74705SXin Li   // | task             | task            | *                                  |
2400*67e74705SXin Li   // | task             | taskyield       | *                                  |
2401*67e74705SXin Li   // | task             | barrier         | +                                  |
2402*67e74705SXin Li   // | task             | taskwait        | *                                  |
2403*67e74705SXin Li   // | task             | taskgroup       | *                                  |
2404*67e74705SXin Li   // | task             | flush           | *                                  |
2405*67e74705SXin Li   // | task             | ordered         | +                                  |
2406*67e74705SXin Li   // | task             | atomic          | *                                  |
2407*67e74705SXin Li   // | task             | target          | *                                  |
2408*67e74705SXin Li   // | task             | target parallel | *                                  |
2409*67e74705SXin Li   // | task             | target parallel | *                                  |
2410*67e74705SXin Li   // |                  | for             |                                    |
2411*67e74705SXin Li   // | task             | target enter    | *                                  |
2412*67e74705SXin Li   // |                  | data            |                                    |
2413*67e74705SXin Li   // | task             | target exit     | *                                  |
2414*67e74705SXin Li   // |                  | data            |                                    |
2415*67e74705SXin Li   // | task             | teams           | +                                  |
2416*67e74705SXin Li   // | task             | cancellation    |                                    |
2417*67e74705SXin Li   // |                  | point           | !                                  |
2418*67e74705SXin Li   // | task             | cancel          | !                                  |
2419*67e74705SXin Li   // | task             | taskloop        | *                                  |
2420*67e74705SXin Li   // | task             | taskloop simd   | *                                  |
2421*67e74705SXin Li   // | task             | distribute      | +                                  |
2422*67e74705SXin Li   // | task             | distribute      | +                                  |
2423*67e74705SXin Li   // |                  | parallel for    |                                    |
2424*67e74705SXin Li   // | task             | distribute      | +                                  |
2425*67e74705SXin Li   // |                  |parallel for simd|                                    |
2426*67e74705SXin Li   // | task             | distribute simd | +                                  |
2427*67e74705SXin Li   // | task             | target parallel | +                                  |
2428*67e74705SXin Li   // |                  | for simd        |                                    |
2429*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2430*67e74705SXin Li   // | ordered          | parallel        | *                                  |
2431*67e74705SXin Li   // | ordered          | for             | +                                  |
2432*67e74705SXin Li   // | ordered          | for simd        | +                                  |
2433*67e74705SXin Li   // | ordered          | master          | *                                  |
2434*67e74705SXin Li   // | ordered          | critical        | *                                  |
2435*67e74705SXin Li   // | ordered          | simd            | *                                  |
2436*67e74705SXin Li   // | ordered          | sections        | +                                  |
2437*67e74705SXin Li   // | ordered          | section         | +                                  |
2438*67e74705SXin Li   // | ordered          | single          | +                                  |
2439*67e74705SXin Li   // | ordered          | parallel for    | *                                  |
2440*67e74705SXin Li   // | ordered          |parallel for simd| *                                  |
2441*67e74705SXin Li   // | ordered          |parallel sections| *                                  |
2442*67e74705SXin Li   // | ordered          | task            | *                                  |
2443*67e74705SXin Li   // | ordered          | taskyield       | *                                  |
2444*67e74705SXin Li   // | ordered          | barrier         | +                                  |
2445*67e74705SXin Li   // | ordered          | taskwait        | *                                  |
2446*67e74705SXin Li   // | ordered          | taskgroup       | *                                  |
2447*67e74705SXin Li   // | ordered          | flush           | *                                  |
2448*67e74705SXin Li   // | ordered          | ordered         | +                                  |
2449*67e74705SXin Li   // | ordered          | atomic          | *                                  |
2450*67e74705SXin Li   // | ordered          | target          | *                                  |
2451*67e74705SXin Li   // | ordered          | target parallel | *                                  |
2452*67e74705SXin Li   // | ordered          | target parallel | *                                  |
2453*67e74705SXin Li   // |                  | for             |                                    |
2454*67e74705SXin Li   // | ordered          | target enter    | *                                  |
2455*67e74705SXin Li   // |                  | data            |                                    |
2456*67e74705SXin Li   // | ordered          | target exit     | *                                  |
2457*67e74705SXin Li   // |                  | data            |                                    |
2458*67e74705SXin Li   // | ordered          | teams           | +                                  |
2459*67e74705SXin Li   // | ordered          | cancellation    |                                    |
2460*67e74705SXin Li   // |                  | point           |                                    |
2461*67e74705SXin Li   // | ordered          | cancel          |                                    |
2462*67e74705SXin Li   // | ordered          | taskloop        | *                                  |
2463*67e74705SXin Li   // | ordered          | taskloop simd   | *                                  |
2464*67e74705SXin Li   // | ordered          | distribute      | +                                  |
2465*67e74705SXin Li   // | ordered          | distribute      | +                                  |
2466*67e74705SXin Li   // |                  | parallel for    |                                    |
2467*67e74705SXin Li   // | ordered          | distribute      | +                                  |
2468*67e74705SXin Li   // |                  |parallel for simd|                                    |
2469*67e74705SXin Li   // | ordered          | distribute simd | +                                  |
2470*67e74705SXin Li   // | ordered          | target parallel | +                                  |
2471*67e74705SXin Li   // |                  | for simd        |                                    |
2472*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2473*67e74705SXin Li   // | atomic           | parallel        |                                    |
2474*67e74705SXin Li   // | atomic           | for             |                                    |
2475*67e74705SXin Li   // | atomic           | for simd        |                                    |
2476*67e74705SXin Li   // | atomic           | master          |                                    |
2477*67e74705SXin Li   // | atomic           | critical        |                                    |
2478*67e74705SXin Li   // | atomic           | simd            |                                    |
2479*67e74705SXin Li   // | atomic           | sections        |                                    |
2480*67e74705SXin Li   // | atomic           | section         |                                    |
2481*67e74705SXin Li   // | atomic           | single          |                                    |
2482*67e74705SXin Li   // | atomic           | parallel for    |                                    |
2483*67e74705SXin Li   // | atomic           |parallel for simd|                                    |
2484*67e74705SXin Li   // | atomic           |parallel sections|                                    |
2485*67e74705SXin Li   // | atomic           | task            |                                    |
2486*67e74705SXin Li   // | atomic           | taskyield       |                                    |
2487*67e74705SXin Li   // | atomic           | barrier         |                                    |
2488*67e74705SXin Li   // | atomic           | taskwait        |                                    |
2489*67e74705SXin Li   // | atomic           | taskgroup       |                                    |
2490*67e74705SXin Li   // | atomic           | flush           |                                    |
2491*67e74705SXin Li   // | atomic           | ordered         |                                    |
2492*67e74705SXin Li   // | atomic           | atomic          |                                    |
2493*67e74705SXin Li   // | atomic           | target          |                                    |
2494*67e74705SXin Li   // | atomic           | target parallel |                                    |
2495*67e74705SXin Li   // | atomic           | target parallel |                                    |
2496*67e74705SXin Li   // |                  | for             |                                    |
2497*67e74705SXin Li   // | atomic           | target enter    |                                    |
2498*67e74705SXin Li   // |                  | data            |                                    |
2499*67e74705SXin Li   // | atomic           | target exit     |                                    |
2500*67e74705SXin Li   // |                  | data            |                                    |
2501*67e74705SXin Li   // | atomic           | teams           |                                    |
2502*67e74705SXin Li   // | atomic           | cancellation    |                                    |
2503*67e74705SXin Li   // |                  | point           |                                    |
2504*67e74705SXin Li   // | atomic           | cancel          |                                    |
2505*67e74705SXin Li   // | atomic           | taskloop        |                                    |
2506*67e74705SXin Li   // | atomic           | taskloop simd   |                                    |
2507*67e74705SXin Li   // | atomic           | distribute      |                                    |
2508*67e74705SXin Li   // | atomic           | distribute      |                                    |
2509*67e74705SXin Li   // |                  | parallel for    |                                    |
2510*67e74705SXin Li   // | atomic           | distribute      |                                    |
2511*67e74705SXin Li   // |                  |parallel for simd|                                    |
2512*67e74705SXin Li   // | atomic           | distribute simd |                                    |
2513*67e74705SXin Li   // | atomic           | target parallel |                                    |
2514*67e74705SXin Li   // |                  | for simd        |                                    |
2515*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2516*67e74705SXin Li   // | target           | parallel        | *                                  |
2517*67e74705SXin Li   // | target           | for             | *                                  |
2518*67e74705SXin Li   // | target           | for simd        | *                                  |
2519*67e74705SXin Li   // | target           | master          | *                                  |
2520*67e74705SXin Li   // | target           | critical        | *                                  |
2521*67e74705SXin Li   // | target           | simd            | *                                  |
2522*67e74705SXin Li   // | target           | sections        | *                                  |
2523*67e74705SXin Li   // | target           | section         | *                                  |
2524*67e74705SXin Li   // | target           | single          | *                                  |
2525*67e74705SXin Li   // | target           | parallel for    | *                                  |
2526*67e74705SXin Li   // | target           |parallel for simd| *                                  |
2527*67e74705SXin Li   // | target           |parallel sections| *                                  |
2528*67e74705SXin Li   // | target           | task            | *                                  |
2529*67e74705SXin Li   // | target           | taskyield       | *                                  |
2530*67e74705SXin Li   // | target           | barrier         | *                                  |
2531*67e74705SXin Li   // | target           | taskwait        | *                                  |
2532*67e74705SXin Li   // | target           | taskgroup       | *                                  |
2533*67e74705SXin Li   // | target           | flush           | *                                  |
2534*67e74705SXin Li   // | target           | ordered         | *                                  |
2535*67e74705SXin Li   // | target           | atomic          | *                                  |
2536*67e74705SXin Li   // | target           | target          |                                    |
2537*67e74705SXin Li   // | target           | target parallel |                                    |
2538*67e74705SXin Li   // | target           | target parallel |                                    |
2539*67e74705SXin Li   // |                  | for             |                                    |
2540*67e74705SXin Li   // | target           | target enter    |                                    |
2541*67e74705SXin Li   // |                  | data            |                                    |
2542*67e74705SXin Li   // | target           | target exit     |                                    |
2543*67e74705SXin Li   // |                  | data            |                                    |
2544*67e74705SXin Li   // | target           | teams           | *                                  |
2545*67e74705SXin Li   // | target           | cancellation    |                                    |
2546*67e74705SXin Li   // |                  | point           |                                    |
2547*67e74705SXin Li   // | target           | cancel          |                                    |
2548*67e74705SXin Li   // | target           | taskloop        | *                                  |
2549*67e74705SXin Li   // | target           | taskloop simd   | *                                  |
2550*67e74705SXin Li   // | target           | distribute      | +                                  |
2551*67e74705SXin Li   // | target           | distribute      | +                                  |
2552*67e74705SXin Li   // |                  | parallel for    |                                    |
2553*67e74705SXin Li   // | target           | distribute      | +                                  |
2554*67e74705SXin Li   // |                  |parallel for simd|                                    |
2555*67e74705SXin Li   // | target           | distribute simd | +                                  |
2556*67e74705SXin Li   // | target           | target parallel |                                    |
2557*67e74705SXin Li   // |                  | for simd        |                                    |
2558*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2559*67e74705SXin Li   // | target parallel  | parallel        | *                                  |
2560*67e74705SXin Li   // | target parallel  | for             | *                                  |
2561*67e74705SXin Li   // | target parallel  | for simd        | *                                  |
2562*67e74705SXin Li   // | target parallel  | master          | *                                  |
2563*67e74705SXin Li   // | target parallel  | critical        | *                                  |
2564*67e74705SXin Li   // | target parallel  | simd            | *                                  |
2565*67e74705SXin Li   // | target parallel  | sections        | *                                  |
2566*67e74705SXin Li   // | target parallel  | section         | *                                  |
2567*67e74705SXin Li   // | target parallel  | single          | *                                  |
2568*67e74705SXin Li   // | target parallel  | parallel for    | *                                  |
2569*67e74705SXin Li   // | target parallel  |parallel for simd| *                                  |
2570*67e74705SXin Li   // | target parallel  |parallel sections| *                                  |
2571*67e74705SXin Li   // | target parallel  | task            | *                                  |
2572*67e74705SXin Li   // | target parallel  | taskyield       | *                                  |
2573*67e74705SXin Li   // | target parallel  | barrier         | *                                  |
2574*67e74705SXin Li   // | target parallel  | taskwait        | *                                  |
2575*67e74705SXin Li   // | target parallel  | taskgroup       | *                                  |
2576*67e74705SXin Li   // | target parallel  | flush           | *                                  |
2577*67e74705SXin Li   // | target parallel  | ordered         | *                                  |
2578*67e74705SXin Li   // | target parallel  | atomic          | *                                  |
2579*67e74705SXin Li   // | target parallel  | target          |                                    |
2580*67e74705SXin Li   // | target parallel  | target parallel |                                    |
2581*67e74705SXin Li   // | target parallel  | target parallel |                                    |
2582*67e74705SXin Li   // |                  | for             |                                    |
2583*67e74705SXin Li   // | target parallel  | target enter    |                                    |
2584*67e74705SXin Li   // |                  | data            |                                    |
2585*67e74705SXin Li   // | target parallel  | target exit     |                                    |
2586*67e74705SXin Li   // |                  | data            |                                    |
2587*67e74705SXin Li   // | target parallel  | teams           |                                    |
2588*67e74705SXin Li   // | target parallel  | cancellation    |                                    |
2589*67e74705SXin Li   // |                  | point           | !                                  |
2590*67e74705SXin Li   // | target parallel  | cancel          | !                                  |
2591*67e74705SXin Li   // | target parallel  | taskloop        | *                                  |
2592*67e74705SXin Li   // | target parallel  | taskloop simd   | *                                  |
2593*67e74705SXin Li   // | target parallel  | distribute      |                                    |
2594*67e74705SXin Li   // | target parallel  | distribute      |                                    |
2595*67e74705SXin Li   // |                  | parallel for    |                                    |
2596*67e74705SXin Li   // | target parallel  | distribute      |                                    |
2597*67e74705SXin Li   // |                  |parallel for simd|                                    |
2598*67e74705SXin Li   // | target parallel  | distribute simd |                                    |
2599*67e74705SXin Li   // | target parallel  | target parallel |                                    |
2600*67e74705SXin Li   // |                  | for simd        |                                    |
2601*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2602*67e74705SXin Li   // | target parallel  | parallel        | *                                  |
2603*67e74705SXin Li   // | for              |                 |                                    |
2604*67e74705SXin Li   // | target parallel  | for             | *                                  |
2605*67e74705SXin Li   // | for              |                 |                                    |
2606*67e74705SXin Li   // | target parallel  | for simd        | *                                  |
2607*67e74705SXin Li   // | for              |                 |                                    |
2608*67e74705SXin Li   // | target parallel  | master          | *                                  |
2609*67e74705SXin Li   // | for              |                 |                                    |
2610*67e74705SXin Li   // | target parallel  | critical        | *                                  |
2611*67e74705SXin Li   // | for              |                 |                                    |
2612*67e74705SXin Li   // | target parallel  | simd            | *                                  |
2613*67e74705SXin Li   // | for              |                 |                                    |
2614*67e74705SXin Li   // | target parallel  | sections        | *                                  |
2615*67e74705SXin Li   // | for              |                 |                                    |
2616*67e74705SXin Li   // | target parallel  | section         | *                                  |
2617*67e74705SXin Li   // | for              |                 |                                    |
2618*67e74705SXin Li   // | target parallel  | single          | *                                  |
2619*67e74705SXin Li   // | for              |                 |                                    |
2620*67e74705SXin Li   // | target parallel  | parallel for    | *                                  |
2621*67e74705SXin Li   // | for              |                 |                                    |
2622*67e74705SXin Li   // | target parallel  |parallel for simd| *                                  |
2623*67e74705SXin Li   // | for              |                 |                                    |
2624*67e74705SXin Li   // | target parallel  |parallel sections| *                                  |
2625*67e74705SXin Li   // | for              |                 |                                    |
2626*67e74705SXin Li   // | target parallel  | task            | *                                  |
2627*67e74705SXin Li   // | for              |                 |                                    |
2628*67e74705SXin Li   // | target parallel  | taskyield       | *                                  |
2629*67e74705SXin Li   // | for              |                 |                                    |
2630*67e74705SXin Li   // | target parallel  | barrier         | *                                  |
2631*67e74705SXin Li   // | for              |                 |                                    |
2632*67e74705SXin Li   // | target parallel  | taskwait        | *                                  |
2633*67e74705SXin Li   // | for              |                 |                                    |
2634*67e74705SXin Li   // | target parallel  | taskgroup       | *                                  |
2635*67e74705SXin Li   // | for              |                 |                                    |
2636*67e74705SXin Li   // | target parallel  | flush           | *                                  |
2637*67e74705SXin Li   // | for              |                 |                                    |
2638*67e74705SXin Li   // | target parallel  | ordered         | *                                  |
2639*67e74705SXin Li   // | for              |                 |                                    |
2640*67e74705SXin Li   // | target parallel  | atomic          | *                                  |
2641*67e74705SXin Li   // | for              |                 |                                    |
2642*67e74705SXin Li   // | target parallel  | target          |                                    |
2643*67e74705SXin Li   // | for              |                 |                                    |
2644*67e74705SXin Li   // | target parallel  | target parallel |                                    |
2645*67e74705SXin Li   // | for              |                 |                                    |
2646*67e74705SXin Li   // | target parallel  | target parallel |                                    |
2647*67e74705SXin Li   // | for              | for             |                                    |
2648*67e74705SXin Li   // | target parallel  | target enter    |                                    |
2649*67e74705SXin Li   // | for              | data            |                                    |
2650*67e74705SXin Li   // | target parallel  | target exit     |                                    |
2651*67e74705SXin Li   // | for              | data            |                                    |
2652*67e74705SXin Li   // | target parallel  | teams           |                                    |
2653*67e74705SXin Li   // | for              |                 |                                    |
2654*67e74705SXin Li   // | target parallel  | cancellation    |                                    |
2655*67e74705SXin Li   // | for              | point           | !                                  |
2656*67e74705SXin Li   // | target parallel  | cancel          | !                                  |
2657*67e74705SXin Li   // | for              |                 |                                    |
2658*67e74705SXin Li   // | target parallel  | taskloop        | *                                  |
2659*67e74705SXin Li   // | for              |                 |                                    |
2660*67e74705SXin Li   // | target parallel  | taskloop simd   | *                                  |
2661*67e74705SXin Li   // | for              |                 |                                    |
2662*67e74705SXin Li   // | target parallel  | distribute      |                                    |
2663*67e74705SXin Li   // | for              |                 |                                    |
2664*67e74705SXin Li   // | target parallel  | distribute      |                                    |
2665*67e74705SXin Li   // | for              | parallel for    |                                    |
2666*67e74705SXin Li   // | target parallel  | distribute      |                                    |
2667*67e74705SXin Li   // | for              |parallel for simd|                                    |
2668*67e74705SXin Li   // | target parallel  | distribute simd |                                    |
2669*67e74705SXin Li   // | for              |                 |                                    |
2670*67e74705SXin Li   // | target parallel  | target parallel |                                    |
2671*67e74705SXin Li   // | for              | for simd        |                                    |
2672*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2673*67e74705SXin Li   // | teams            | parallel        | *                                  |
2674*67e74705SXin Li   // | teams            | for             | +                                  |
2675*67e74705SXin Li   // | teams            | for simd        | +                                  |
2676*67e74705SXin Li   // | teams            | master          | +                                  |
2677*67e74705SXin Li   // | teams            | critical        | +                                  |
2678*67e74705SXin Li   // | teams            | simd            | +                                  |
2679*67e74705SXin Li   // | teams            | sections        | +                                  |
2680*67e74705SXin Li   // | teams            | section         | +                                  |
2681*67e74705SXin Li   // | teams            | single          | +                                  |
2682*67e74705SXin Li   // | teams            | parallel for    | *                                  |
2683*67e74705SXin Li   // | teams            |parallel for simd| *                                  |
2684*67e74705SXin Li   // | teams            |parallel sections| *                                  |
2685*67e74705SXin Li   // | teams            | task            | +                                  |
2686*67e74705SXin Li   // | teams            | taskyield       | +                                  |
2687*67e74705SXin Li   // | teams            | barrier         | +                                  |
2688*67e74705SXin Li   // | teams            | taskwait        | +                                  |
2689*67e74705SXin Li   // | teams            | taskgroup       | +                                  |
2690*67e74705SXin Li   // | teams            | flush           | +                                  |
2691*67e74705SXin Li   // | teams            | ordered         | +                                  |
2692*67e74705SXin Li   // | teams            | atomic          | +                                  |
2693*67e74705SXin Li   // | teams            | target          | +                                  |
2694*67e74705SXin Li   // | teams            | target parallel | +                                  |
2695*67e74705SXin Li   // | teams            | target parallel | +                                  |
2696*67e74705SXin Li   // |                  | for             |                                    |
2697*67e74705SXin Li   // | teams            | target enter    | +                                  |
2698*67e74705SXin Li   // |                  | data            |                                    |
2699*67e74705SXin Li   // | teams            | target exit     | +                                  |
2700*67e74705SXin Li   // |                  | data            |                                    |
2701*67e74705SXin Li   // | teams            | teams           | +                                  |
2702*67e74705SXin Li   // | teams            | cancellation    |                                    |
2703*67e74705SXin Li   // |                  | point           |                                    |
2704*67e74705SXin Li   // | teams            | cancel          |                                    |
2705*67e74705SXin Li   // | teams            | taskloop        | +                                  |
2706*67e74705SXin Li   // | teams            | taskloop simd   | +                                  |
2707*67e74705SXin Li   // | teams            | distribute      | !                                  |
2708*67e74705SXin Li   // | teams            | distribute      | !                                  |
2709*67e74705SXin Li   // |                  | parallel for    |                                    |
2710*67e74705SXin Li   // | teams            | distribute      | !                                  |
2711*67e74705SXin Li   // |                  |parallel for simd|                                    |
2712*67e74705SXin Li   // | teams            | distribute simd | !                                  |
2713*67e74705SXin Li   // | teams            | target parallel | +                                  |
2714*67e74705SXin Li   // |                  | for simd        |                                    |
2715*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2716*67e74705SXin Li   // | taskloop         | parallel        | *                                  |
2717*67e74705SXin Li   // | taskloop         | for             | +                                  |
2718*67e74705SXin Li   // | taskloop         | for simd        | +                                  |
2719*67e74705SXin Li   // | taskloop         | master          | +                                  |
2720*67e74705SXin Li   // | taskloop         | critical        | *                                  |
2721*67e74705SXin Li   // | taskloop         | simd            | *                                  |
2722*67e74705SXin Li   // | taskloop         | sections        | +                                  |
2723*67e74705SXin Li   // | taskloop         | section         | +                                  |
2724*67e74705SXin Li   // | taskloop         | single          | +                                  |
2725*67e74705SXin Li   // | taskloop         | parallel for    | *                                  |
2726*67e74705SXin Li   // | taskloop         |parallel for simd| *                                  |
2727*67e74705SXin Li   // | taskloop         |parallel sections| *                                  |
2728*67e74705SXin Li   // | taskloop         | task            | *                                  |
2729*67e74705SXin Li   // | taskloop         | taskyield       | *                                  |
2730*67e74705SXin Li   // | taskloop         | barrier         | +                                  |
2731*67e74705SXin Li   // | taskloop         | taskwait        | *                                  |
2732*67e74705SXin Li   // | taskloop         | taskgroup       | *                                  |
2733*67e74705SXin Li   // | taskloop         | flush           | *                                  |
2734*67e74705SXin Li   // | taskloop         | ordered         | +                                  |
2735*67e74705SXin Li   // | taskloop         | atomic          | *                                  |
2736*67e74705SXin Li   // | taskloop         | target          | *                                  |
2737*67e74705SXin Li   // | taskloop         | target parallel | *                                  |
2738*67e74705SXin Li   // | taskloop         | target parallel | *                                  |
2739*67e74705SXin Li   // |                  | for             |                                    |
2740*67e74705SXin Li   // | taskloop         | target enter    | *                                  |
2741*67e74705SXin Li   // |                  | data            |                                    |
2742*67e74705SXin Li   // | taskloop         | target exit     | *                                  |
2743*67e74705SXin Li   // |                  | data            |                                    |
2744*67e74705SXin Li   // | taskloop         | teams           | +                                  |
2745*67e74705SXin Li   // | taskloop         | cancellation    |                                    |
2746*67e74705SXin Li   // |                  | point           |                                    |
2747*67e74705SXin Li   // | taskloop         | cancel          |                                    |
2748*67e74705SXin Li   // | taskloop         | taskloop        | *                                  |
2749*67e74705SXin Li   // | taskloop         | distribute      | +                                  |
2750*67e74705SXin Li   // | taskloop         | distribute      | +                                  |
2751*67e74705SXin Li   // |                  | parallel for    |                                    |
2752*67e74705SXin Li   // | taskloop         | distribute      | +                                  |
2753*67e74705SXin Li   // |                  |parallel for simd|                                    |
2754*67e74705SXin Li   // | taskloop         | distribute simd | +                                  |
2755*67e74705SXin Li   // | taskloop         | target parallel | *                                  |
2756*67e74705SXin Li   // |                  | for simd        |                                    |
2757*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2758*67e74705SXin Li   // | taskloop simd    | parallel        |                                    |
2759*67e74705SXin Li   // | taskloop simd    | for             |                                    |
2760*67e74705SXin Li   // | taskloop simd    | for simd        |                                    |
2761*67e74705SXin Li   // | taskloop simd    | master          |                                    |
2762*67e74705SXin Li   // | taskloop simd    | critical        |                                    |
2763*67e74705SXin Li   // | taskloop simd    | simd            | *                                  |
2764*67e74705SXin Li   // | taskloop simd    | sections        |                                    |
2765*67e74705SXin Li   // | taskloop simd    | section         |                                    |
2766*67e74705SXin Li   // | taskloop simd    | single          |                                    |
2767*67e74705SXin Li   // | taskloop simd    | parallel for    |                                    |
2768*67e74705SXin Li   // | taskloop simd    |parallel for simd|                                    |
2769*67e74705SXin Li   // | taskloop simd    |parallel sections|                                    |
2770*67e74705SXin Li   // | taskloop simd    | task            |                                    |
2771*67e74705SXin Li   // | taskloop simd    | taskyield       |                                    |
2772*67e74705SXin Li   // | taskloop simd    | barrier         |                                    |
2773*67e74705SXin Li   // | taskloop simd    | taskwait        |                                    |
2774*67e74705SXin Li   // | taskloop simd    | taskgroup       |                                    |
2775*67e74705SXin Li   // | taskloop simd    | flush           |                                    |
2776*67e74705SXin Li   // | taskloop simd    | ordered         | + (with simd clause)               |
2777*67e74705SXin Li   // | taskloop simd    | atomic          |                                    |
2778*67e74705SXin Li   // | taskloop simd    | target          |                                    |
2779*67e74705SXin Li   // | taskloop simd    | target parallel |                                    |
2780*67e74705SXin Li   // | taskloop simd    | target parallel |                                    |
2781*67e74705SXin Li   // |                  | for             |                                    |
2782*67e74705SXin Li   // | taskloop simd    | target enter    |                                    |
2783*67e74705SXin Li   // |                  | data            |                                    |
2784*67e74705SXin Li   // | taskloop simd    | target exit     |                                    |
2785*67e74705SXin Li   // |                  | data            |                                    |
2786*67e74705SXin Li   // | taskloop simd    | teams           |                                    |
2787*67e74705SXin Li   // | taskloop simd    | cancellation    |                                    |
2788*67e74705SXin Li   // |                  | point           |                                    |
2789*67e74705SXin Li   // | taskloop simd    | cancel          |                                    |
2790*67e74705SXin Li   // | taskloop simd    | taskloop        |                                    |
2791*67e74705SXin Li   // | taskloop simd    | taskloop simd   |                                    |
2792*67e74705SXin Li   // | taskloop simd    | distribute      |                                    |
2793*67e74705SXin Li   // | taskloop simd    | distribute      |                                    |
2794*67e74705SXin Li   // |                  | parallel for    |                                    |
2795*67e74705SXin Li   // | taskloop simd    | distribute      |                                    |
2796*67e74705SXin Li   // |                  |parallel for simd|                                    |
2797*67e74705SXin Li   // | taskloop simd    | distribute simd |                                    |
2798*67e74705SXin Li   // | taskloop simd    | target parallel |                                    |
2799*67e74705SXin Li   // |                  | for simd        |                                    |
2800*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2801*67e74705SXin Li   // | distribute       | parallel        | *                                  |
2802*67e74705SXin Li   // | distribute       | for             | *                                  |
2803*67e74705SXin Li   // | distribute       | for simd        | *                                  |
2804*67e74705SXin Li   // | distribute       | master          | *                                  |
2805*67e74705SXin Li   // | distribute       | critical        | *                                  |
2806*67e74705SXin Li   // | distribute       | simd            | *                                  |
2807*67e74705SXin Li   // | distribute       | sections        | *                                  |
2808*67e74705SXin Li   // | distribute       | section         | *                                  |
2809*67e74705SXin Li   // | distribute       | single          | *                                  |
2810*67e74705SXin Li   // | distribute       | parallel for    | *                                  |
2811*67e74705SXin Li   // | distribute       |parallel for simd| *                                  |
2812*67e74705SXin Li   // | distribute       |parallel sections| *                                  |
2813*67e74705SXin Li   // | distribute       | task            | *                                  |
2814*67e74705SXin Li   // | distribute       | taskyield       | *                                  |
2815*67e74705SXin Li   // | distribute       | barrier         | *                                  |
2816*67e74705SXin Li   // | distribute       | taskwait        | *                                  |
2817*67e74705SXin Li   // | distribute       | taskgroup       | *                                  |
2818*67e74705SXin Li   // | distribute       | flush           | *                                  |
2819*67e74705SXin Li   // | distribute       | ordered         | +                                  |
2820*67e74705SXin Li   // | distribute       | atomic          | *                                  |
2821*67e74705SXin Li   // | distribute       | target          |                                    |
2822*67e74705SXin Li   // | distribute       | target parallel |                                    |
2823*67e74705SXin Li   // | distribute       | target parallel |                                    |
2824*67e74705SXin Li   // |                  | for             |                                    |
2825*67e74705SXin Li   // | distribute       | target enter    |                                    |
2826*67e74705SXin Li   // |                  | data            |                                    |
2827*67e74705SXin Li   // | distribute       | target exit     |                                    |
2828*67e74705SXin Li   // |                  | data            |                                    |
2829*67e74705SXin Li   // | distribute       | teams           |                                    |
2830*67e74705SXin Li   // | distribute       | cancellation    | +                                  |
2831*67e74705SXin Li   // |                  | point           |                                    |
2832*67e74705SXin Li   // | distribute       | cancel          | +                                  |
2833*67e74705SXin Li   // | distribute       | taskloop        | *                                  |
2834*67e74705SXin Li   // | distribute       | taskloop simd   | *                                  |
2835*67e74705SXin Li   // | distribute       | distribute      |                                    |
2836*67e74705SXin Li   // | distribute       | distribute      |                                    |
2837*67e74705SXin Li   // |                  | parallel for    |                                    |
2838*67e74705SXin Li   // | distribute       | distribute      |                                    |
2839*67e74705SXin Li   // |                  |parallel for simd|                                    |
2840*67e74705SXin Li   // | distribute       | distribute simd |                                    |
2841*67e74705SXin Li   // | distribute       | target parallel |                                    |
2842*67e74705SXin Li   // |                  | for simd        |                                    |
2843*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2844*67e74705SXin Li   // | distribute       | parallel        | *                                  |
2845*67e74705SXin Li   // | parallel for     |                 |                                    |
2846*67e74705SXin Li   // | distribute       | for             | *                                  |
2847*67e74705SXin Li   // | parallel for     |                 |                                    |
2848*67e74705SXin Li   // | distribute       | for simd        | *                                  |
2849*67e74705SXin Li   // | parallel for     |                 |                                    |
2850*67e74705SXin Li   // | distribute       | master          | *                                  |
2851*67e74705SXin Li   // | parallel for     |                 |                                    |
2852*67e74705SXin Li   // | distribute       | critical        | *                                  |
2853*67e74705SXin Li   // | parallel for     |                 |                                    |
2854*67e74705SXin Li   // | distribute       | simd            | *                                  |
2855*67e74705SXin Li   // | parallel for     |                 |                                    |
2856*67e74705SXin Li   // | distribute       | sections        | *                                  |
2857*67e74705SXin Li   // | parallel for     |                 |                                    |
2858*67e74705SXin Li   // | distribute       | section         | *                                  |
2859*67e74705SXin Li   // | parallel for     |                 |                                    |
2860*67e74705SXin Li   // | distribute       | single          | *                                  |
2861*67e74705SXin Li   // | parallel for     |                 |                                    |
2862*67e74705SXin Li   // | distribute       | parallel for    | *                                  |
2863*67e74705SXin Li   // | parallel for     |                 |                                    |
2864*67e74705SXin Li   // | distribute       |parallel for simd| *                                  |
2865*67e74705SXin Li   // | parallel for     |                 |                                    |
2866*67e74705SXin Li   // | distribute       |parallel sections| *                                  |
2867*67e74705SXin Li   // | parallel for     |                 |                                    |
2868*67e74705SXin Li   // | distribute       | task            | *                                  |
2869*67e74705SXin Li   // | parallel for     |                 |                                    |
2870*67e74705SXin Li   // | parallel for     |                 |                                    |
2871*67e74705SXin Li   // | distribute       | taskyield       | *                                  |
2872*67e74705SXin Li   // | parallel for     |                 |                                    |
2873*67e74705SXin Li   // | distribute       | barrier         | *                                  |
2874*67e74705SXin Li   // | parallel for     |                 |                                    |
2875*67e74705SXin Li   // | distribute       | taskwait        | *                                  |
2876*67e74705SXin Li   // | parallel for     |                 |                                    |
2877*67e74705SXin Li   // | distribute       | taskgroup       | *                                  |
2878*67e74705SXin Li   // | parallel for     |                 |                                    |
2879*67e74705SXin Li   // | distribute       | flush           | *                                  |
2880*67e74705SXin Li   // | parallel for     |                 |                                    |
2881*67e74705SXin Li   // | distribute       | ordered         | +                                  |
2882*67e74705SXin Li   // | parallel for     |                 |                                    |
2883*67e74705SXin Li   // | distribute       | atomic          | *                                  |
2884*67e74705SXin Li   // | parallel for     |                 |                                    |
2885*67e74705SXin Li   // | distribute       | target          |                                    |
2886*67e74705SXin Li   // | parallel for     |                 |                                    |
2887*67e74705SXin Li   // | distribute       | target parallel |                                    |
2888*67e74705SXin Li   // | parallel for     |                 |                                    |
2889*67e74705SXin Li   // | distribute       | target parallel |                                    |
2890*67e74705SXin Li   // | parallel for     | for             |                                    |
2891*67e74705SXin Li   // | distribute       | target enter    |                                    |
2892*67e74705SXin Li   // | parallel for     | data            |                                    |
2893*67e74705SXin Li   // | distribute       | target exit     |                                    |
2894*67e74705SXin Li   // | parallel for     | data            |                                    |
2895*67e74705SXin Li   // | distribute       | teams           |                                    |
2896*67e74705SXin Li   // | parallel for     |                 |                                    |
2897*67e74705SXin Li   // | distribute       | cancellation    | +                                  |
2898*67e74705SXin Li   // | parallel for     | point           |                                    |
2899*67e74705SXin Li   // | distribute       | cancel          | +                                  |
2900*67e74705SXin Li   // | parallel for     |                 |                                    |
2901*67e74705SXin Li   // | distribute       | taskloop        | *                                  |
2902*67e74705SXin Li   // | parallel for     |                 |                                    |
2903*67e74705SXin Li   // | distribute       | taskloop simd   | *                                  |
2904*67e74705SXin Li   // | parallel for     |                 |                                    |
2905*67e74705SXin Li   // | distribute       | distribute      |                                    |
2906*67e74705SXin Li   // | parallel for     |                 |                                    |
2907*67e74705SXin Li   // | distribute       | distribute      |                                    |
2908*67e74705SXin Li   // | parallel for     | parallel for    |                                    |
2909*67e74705SXin Li   // | distribute       | distribute      |                                    |
2910*67e74705SXin Li   // | parallel for     |parallel for simd|                                    |
2911*67e74705SXin Li   // | distribute       | distribute simd |                                    |
2912*67e74705SXin Li   // | parallel for     |                 |                                    |
2913*67e74705SXin Li   // | distribute       | target parallel |                                    |
2914*67e74705SXin Li   // | parallel for     | for simd        |                                    |
2915*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2916*67e74705SXin Li   // | distribute       | parallel        | *                                  |
2917*67e74705SXin Li   // | parallel for simd|                 |                                    |
2918*67e74705SXin Li   // | distribute       | for             | *                                  |
2919*67e74705SXin Li   // | parallel for simd|                 |                                    |
2920*67e74705SXin Li   // | distribute       | for simd        | *                                  |
2921*67e74705SXin Li   // | parallel for simd|                 |                                    |
2922*67e74705SXin Li   // | distribute       | master          | *                                  |
2923*67e74705SXin Li   // | parallel for simd|                 |                                    |
2924*67e74705SXin Li   // | distribute       | critical        | *                                  |
2925*67e74705SXin Li   // | parallel for simd|                 |                                    |
2926*67e74705SXin Li   // | distribute       | simd            | *                                  |
2927*67e74705SXin Li   // | parallel for simd|                 |                                    |
2928*67e74705SXin Li   // | distribute       | sections        | *                                  |
2929*67e74705SXin Li   // | parallel for simd|                 |                                    |
2930*67e74705SXin Li   // | distribute       | section         | *                                  |
2931*67e74705SXin Li   // | parallel for simd|                 |                                    |
2932*67e74705SXin Li   // | distribute       | single          | *                                  |
2933*67e74705SXin Li   // | parallel for simd|                 |                                    |
2934*67e74705SXin Li   // | distribute       | parallel for    | *                                  |
2935*67e74705SXin Li   // | parallel for simd|                 |                                    |
2936*67e74705SXin Li   // | distribute       |parallel for simd| *                                  |
2937*67e74705SXin Li   // | parallel for simd|                 |                                    |
2938*67e74705SXin Li   // | distribute       |parallel sections| *                                  |
2939*67e74705SXin Li   // | parallel for simd|                 |                                    |
2940*67e74705SXin Li   // | distribute       | task            | *                                  |
2941*67e74705SXin Li   // | parallel for simd|                 |                                    |
2942*67e74705SXin Li   // | distribute       | taskyield       | *                                  |
2943*67e74705SXin Li   // | parallel for simd|                 |                                    |
2944*67e74705SXin Li   // | distribute       | barrier         | *                                  |
2945*67e74705SXin Li   // | parallel for simd|                 |                                    |
2946*67e74705SXin Li   // | distribute       | taskwait        | *                                  |
2947*67e74705SXin Li   // | parallel for simd|                 |                                    |
2948*67e74705SXin Li   // | distribute       | taskgroup       | *                                  |
2949*67e74705SXin Li   // | parallel for simd|                 |                                    |
2950*67e74705SXin Li   // | distribute       | flush           | *                                  |
2951*67e74705SXin Li   // | parallel for simd|                 |                                    |
2952*67e74705SXin Li   // | distribute       | ordered         | +                                  |
2953*67e74705SXin Li   // | parallel for simd|                 |                                    |
2954*67e74705SXin Li   // | distribute       | atomic          | *                                  |
2955*67e74705SXin Li   // | parallel for simd|                 |                                    |
2956*67e74705SXin Li   // | distribute       | target          |                                    |
2957*67e74705SXin Li   // | parallel for simd|                 |                                    |
2958*67e74705SXin Li   // | distribute       | target parallel |                                    |
2959*67e74705SXin Li   // | parallel for simd|                 |                                    |
2960*67e74705SXin Li   // | distribute       | target parallel |                                    |
2961*67e74705SXin Li   // | parallel for simd| for             |                                    |
2962*67e74705SXin Li   // | distribute       | target enter    |                                    |
2963*67e74705SXin Li   // | parallel for simd| data            |                                    |
2964*67e74705SXin Li   // | distribute       | target exit     |                                    |
2965*67e74705SXin Li   // | parallel for simd| data            |                                    |
2966*67e74705SXin Li   // | distribute       | teams           |                                    |
2967*67e74705SXin Li   // | parallel for simd|                 |                                    |
2968*67e74705SXin Li   // | distribute       | cancellation    | +                                  |
2969*67e74705SXin Li   // | parallel for simd| point           |                                    |
2970*67e74705SXin Li   // | distribute       | cancel          | +                                  |
2971*67e74705SXin Li   // | parallel for simd|                 |                                    |
2972*67e74705SXin Li   // | distribute       | taskloop        | *                                  |
2973*67e74705SXin Li   // | parallel for simd|                 |                                    |
2974*67e74705SXin Li   // | distribute       | taskloop simd   | *                                  |
2975*67e74705SXin Li   // | parallel for simd|                 |                                    |
2976*67e74705SXin Li   // | distribute       | distribute      |                                    |
2977*67e74705SXin Li   // | parallel for simd|                 |                                    |
2978*67e74705SXin Li   // | distribute       | distribute      | *                                  |
2979*67e74705SXin Li   // | parallel for simd| parallel for    |                                    |
2980*67e74705SXin Li   // | distribute       | distribute      | *                                  |
2981*67e74705SXin Li   // | parallel for simd|parallel for simd|                                    |
2982*67e74705SXin Li   // | distribute       | distribute simd | *                                  |
2983*67e74705SXin Li   // | parallel for simd|                 |                                    |
2984*67e74705SXin Li   // | distribute       | target parallel |                                    |
2985*67e74705SXin Li   // | parallel for simd| for simd        |                                    |
2986*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
2987*67e74705SXin Li   // | distribute simd  | parallel        | *                                  |
2988*67e74705SXin Li   // | distribute simd  | for             | *                                  |
2989*67e74705SXin Li   // | distribute simd  | for simd        | *                                  |
2990*67e74705SXin Li   // | distribute simd  | master          | *                                  |
2991*67e74705SXin Li   // | distribute simd  | critical        | *                                  |
2992*67e74705SXin Li   // | distribute simd  | simd            | *                                  |
2993*67e74705SXin Li   // | distribute simd  | sections        | *                                  |
2994*67e74705SXin Li   // | distribute simd  | section         | *                                  |
2995*67e74705SXin Li   // | distribute simd  | single          | *                                  |
2996*67e74705SXin Li   // | distribute simd  | parallel for    | *                                  |
2997*67e74705SXin Li   // | distribute simd  |parallel for simd| *                                  |
2998*67e74705SXin Li   // | distribute simd  |parallel sections| *                                  |
2999*67e74705SXin Li   // | distribute simd  | task            | *                                  |
3000*67e74705SXin Li   // | distribute simd  | taskyield       | *                                  |
3001*67e74705SXin Li   // | distribute simd  | barrier         | *                                  |
3002*67e74705SXin Li   // | distribute simd  | taskwait        | *                                  |
3003*67e74705SXin Li   // | distribute simd  | taskgroup       | *                                  |
3004*67e74705SXin Li   // | distribute simd  | flush           | *                                  |
3005*67e74705SXin Li   // | distribute simd  | ordered         | +                                  |
3006*67e74705SXin Li   // | distribute simd  | atomic          | *                                  |
3007*67e74705SXin Li   // | distribute simd  | target          | *                                  |
3008*67e74705SXin Li   // | distribute simd  | target parallel | *                                  |
3009*67e74705SXin Li   // | distribute simd  | target parallel | *                                  |
3010*67e74705SXin Li   // |                  | for             |                                    |
3011*67e74705SXin Li   // | distribute simd  | target enter    | *                                  |
3012*67e74705SXin Li   // |                  | data            |                                    |
3013*67e74705SXin Li   // | distribute simd  | target exit     | *                                  |
3014*67e74705SXin Li   // |                  | data            |                                    |
3015*67e74705SXin Li   // | distribute simd  | teams           | *                                  |
3016*67e74705SXin Li   // | distribute simd  | cancellation    | +                                  |
3017*67e74705SXin Li   // |                  | point           |                                    |
3018*67e74705SXin Li   // | distribute simd  | cancel          | +                                  |
3019*67e74705SXin Li   // | distribute simd  | taskloop        | *                                  |
3020*67e74705SXin Li   // | distribute simd  | taskloop simd   | *                                  |
3021*67e74705SXin Li   // | distribute simd  | distribute      |                                    |
3022*67e74705SXin Li   // | distribute simd  | distribute      | *                                  |
3023*67e74705SXin Li   // |                  | parallel for    |                                    |
3024*67e74705SXin Li   // | distribute simd  | distribute      | *                                  |
3025*67e74705SXin Li   // |                  |parallel for simd|                                    |
3026*67e74705SXin Li   // | distribute simd  | distribute simd | *                                  |
3027*67e74705SXin Li   // | distribute simd  | target parallel | *                                  |
3028*67e74705SXin Li   // |                  | for simd        |                                    |
3029*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
3030*67e74705SXin Li   // | target parallel  | parallel        | *                                  |
3031*67e74705SXin Li   // | for simd         |                 |                                    |
3032*67e74705SXin Li   // | target parallel  | for             | *                                  |
3033*67e74705SXin Li   // | for simd         |                 |                                    |
3034*67e74705SXin Li   // | target parallel  | for simd        | *                                  |
3035*67e74705SXin Li   // | for simd         |                 |                                    |
3036*67e74705SXin Li   // | target parallel  | master          | *                                  |
3037*67e74705SXin Li   // | for simd         |                 |                                    |
3038*67e74705SXin Li   // | target parallel  | critical        | *                                  |
3039*67e74705SXin Li   // | for simd         |                 |                                    |
3040*67e74705SXin Li   // | target parallel  | simd            | !                                  |
3041*67e74705SXin Li   // | for simd         |                 |                                    |
3042*67e74705SXin Li   // | target parallel  | sections        | *                                  |
3043*67e74705SXin Li   // | for simd         |                 |                                    |
3044*67e74705SXin Li   // | target parallel  | section         | *                                  |
3045*67e74705SXin Li   // | for simd         |                 |                                    |
3046*67e74705SXin Li   // | target parallel  | single          | *                                  |
3047*67e74705SXin Li   // | for simd         |                 |                                    |
3048*67e74705SXin Li   // | target parallel  | parallel for    | *                                  |
3049*67e74705SXin Li   // | for simd         |                 |                                    |
3050*67e74705SXin Li   // | target parallel  |parallel for simd| *                                  |
3051*67e74705SXin Li   // | for simd         |                 |                                    |
3052*67e74705SXin Li   // | target parallel  |parallel sections| *                                  |
3053*67e74705SXin Li   // | for simd         |                 |                                    |
3054*67e74705SXin Li   // | target parallel  | task            | *                                  |
3055*67e74705SXin Li   // | for simd         |                 |                                    |
3056*67e74705SXin Li   // | target parallel  | taskyield       | *                                  |
3057*67e74705SXin Li   // | for simd         |                 |                                    |
3058*67e74705SXin Li   // | target parallel  | barrier         | *                                  |
3059*67e74705SXin Li   // | for simd         |                 |                                    |
3060*67e74705SXin Li   // | target parallel  | taskwait        | *                                  |
3061*67e74705SXin Li   // | for simd         |                 |                                    |
3062*67e74705SXin Li   // | target parallel  | taskgroup       | *                                  |
3063*67e74705SXin Li   // | for simd         |                 |                                    |
3064*67e74705SXin Li   // | target parallel  | flush           | *                                  |
3065*67e74705SXin Li   // | for simd         |                 |                                    |
3066*67e74705SXin Li   // | target parallel  | ordered         | + (with simd clause)               |
3067*67e74705SXin Li   // | for simd         |                 |                                    |
3068*67e74705SXin Li   // | target parallel  | atomic          | *                                  |
3069*67e74705SXin Li   // | for simd         |                 |                                    |
3070*67e74705SXin Li   // | target parallel  | target          | *                                  |
3071*67e74705SXin Li   // | for simd         |                 |                                    |
3072*67e74705SXin Li   // | target parallel  | target parallel | *                                  |
3073*67e74705SXin Li   // | for simd         |                 |                                    |
3074*67e74705SXin Li   // | target parallel  | target parallel | *                                  |
3075*67e74705SXin Li   // | for simd         | for             |                                    |
3076*67e74705SXin Li   // | target parallel  | target enter    | *                                  |
3077*67e74705SXin Li   // | for simd         | data            |                                    |
3078*67e74705SXin Li   // | target parallel  | target exit     | *                                  |
3079*67e74705SXin Li   // | for simd         | data            |                                    |
3080*67e74705SXin Li   // | target parallel  | teams           | *                                  |
3081*67e74705SXin Li   // | for simd         |                 |                                    |
3082*67e74705SXin Li   // | target parallel  | cancellation    | *                                  |
3083*67e74705SXin Li   // | for simd         | point           |                                    |
3084*67e74705SXin Li   // | target parallel  | cancel          | *                                  |
3085*67e74705SXin Li   // | for simd         |                 |                                    |
3086*67e74705SXin Li   // | target parallel  | taskloop        | *                                  |
3087*67e74705SXin Li   // | for simd         |                 |                                    |
3088*67e74705SXin Li   // | target parallel  | taskloop simd   | *                                  |
3089*67e74705SXin Li   // | for simd         |                 |                                    |
3090*67e74705SXin Li   // | target parallel  | distribute      | *                                  |
3091*67e74705SXin Li   // | for simd         |                 |                                    |
3092*67e74705SXin Li   // | target parallel  | distribute      | *                                  |
3093*67e74705SXin Li   // | for simd         | parallel for    |                                    |
3094*67e74705SXin Li   // | target parallel  | distribute      | *                                  |
3095*67e74705SXin Li   // | for simd         |parallel for simd|                                    |
3096*67e74705SXin Li   // | target parallel  | distribute simd | *                                  |
3097*67e74705SXin Li   // | for simd         |                 |                                    |
3098*67e74705SXin Li   // | target parallel  | target parallel | *                                  |
3099*67e74705SXin Li   // | for simd         | for simd        |                                    |
3100*67e74705SXin Li   // +------------------+-----------------+------------------------------------+
3101*67e74705SXin Li   if (Stack->getCurScope()) {
3102*67e74705SXin Li     auto ParentRegion = Stack->getParentDirective();
3103*67e74705SXin Li     auto OffendingRegion = ParentRegion;
3104*67e74705SXin Li     bool NestingProhibited = false;
3105*67e74705SXin Li     bool CloseNesting = true;
3106*67e74705SXin Li     enum {
3107*67e74705SXin Li       NoRecommend,
3108*67e74705SXin Li       ShouldBeInParallelRegion,
3109*67e74705SXin Li       ShouldBeInOrderedRegion,
3110*67e74705SXin Li       ShouldBeInTargetRegion,
3111*67e74705SXin Li       ShouldBeInTeamsRegion
3112*67e74705SXin Li     } Recommend = NoRecommend;
3113*67e74705SXin Li     if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
3114*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3115*67e74705SXin Li       // OpenMP constructs may not be nested inside a simd region.
3116*67e74705SXin Li       // OpenMP [2.8.1,simd Construct, Restrictions]
3117*67e74705SXin Li       // An ordered construct with the simd clause is the only OpenMP
3118*67e74705SXin Li       // construct that can appear in the simd region.
3119*67e74705SXin Li       // Allowing a SIMD consruct nested in another SIMD construct is an
3120*67e74705SXin Li       // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3121*67e74705SXin Li       // message.
3122*67e74705SXin Li       SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3123*67e74705SXin Li                                  ? diag::err_omp_prohibited_region_simd
3124*67e74705SXin Li                                  : diag::warn_omp_nesting_simd);
3125*67e74705SXin Li       return CurrentRegion != OMPD_simd;
3126*67e74705SXin Li     }
3127*67e74705SXin Li     if (ParentRegion == OMPD_atomic) {
3128*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3129*67e74705SXin Li       // OpenMP constructs may not be nested inside an atomic region.
3130*67e74705SXin Li       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3131*67e74705SXin Li       return true;
3132*67e74705SXin Li     }
3133*67e74705SXin Li     if (CurrentRegion == OMPD_section) {
3134*67e74705SXin Li       // OpenMP [2.7.2, sections Construct, Restrictions]
3135*67e74705SXin Li       // Orphaned section directives are prohibited. That is, the section
3136*67e74705SXin Li       // directives must appear within the sections construct and must not be
3137*67e74705SXin Li       // encountered elsewhere in the sections region.
3138*67e74705SXin Li       if (ParentRegion != OMPD_sections &&
3139*67e74705SXin Li           ParentRegion != OMPD_parallel_sections) {
3140*67e74705SXin Li         SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3141*67e74705SXin Li             << (ParentRegion != OMPD_unknown)
3142*67e74705SXin Li             << getOpenMPDirectiveName(ParentRegion);
3143*67e74705SXin Li         return true;
3144*67e74705SXin Li       }
3145*67e74705SXin Li       return false;
3146*67e74705SXin Li     }
3147*67e74705SXin Li     // Allow some constructs to be orphaned (they could be used in functions,
3148*67e74705SXin Li     // called from OpenMP regions with the required preconditions).
3149*67e74705SXin Li     if (ParentRegion == OMPD_unknown)
3150*67e74705SXin Li       return false;
3151*67e74705SXin Li     if (CurrentRegion == OMPD_cancellation_point ||
3152*67e74705SXin Li         CurrentRegion == OMPD_cancel) {
3153*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3154*67e74705SXin Li       // A cancellation point construct for which construct-type-clause is
3155*67e74705SXin Li       // taskgroup must be nested inside a task construct. A cancellation
3156*67e74705SXin Li       // point construct for which construct-type-clause is not taskgroup must
3157*67e74705SXin Li       // be closely nested inside an OpenMP construct that matches the type
3158*67e74705SXin Li       // specified in construct-type-clause.
3159*67e74705SXin Li       // A cancel construct for which construct-type-clause is taskgroup must be
3160*67e74705SXin Li       // nested inside a task construct. A cancel construct for which
3161*67e74705SXin Li       // construct-type-clause is not taskgroup must be closely nested inside an
3162*67e74705SXin Li       // OpenMP construct that matches the type specified in
3163*67e74705SXin Li       // construct-type-clause.
3164*67e74705SXin Li       NestingProhibited =
3165*67e74705SXin Li           !((CancelRegion == OMPD_parallel &&
3166*67e74705SXin Li              (ParentRegion == OMPD_parallel ||
3167*67e74705SXin Li               ParentRegion == OMPD_target_parallel)) ||
3168*67e74705SXin Li             (CancelRegion == OMPD_for &&
3169*67e74705SXin Li              (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
3170*67e74705SXin Li               ParentRegion == OMPD_target_parallel_for)) ||
3171*67e74705SXin Li             (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3172*67e74705SXin Li             (CancelRegion == OMPD_sections &&
3173*67e74705SXin Li              (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3174*67e74705SXin Li               ParentRegion == OMPD_parallel_sections)));
3175*67e74705SXin Li     } else if (CurrentRegion == OMPD_master) {
3176*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3177*67e74705SXin Li       // A master region may not be closely nested inside a worksharing,
3178*67e74705SXin Li       // atomic, or explicit task region.
3179*67e74705SXin Li       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3180*67e74705SXin Li                           isOpenMPTaskingDirective(ParentRegion);
3181*67e74705SXin Li     } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3182*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3183*67e74705SXin Li       // A critical region may not be nested (closely or otherwise) inside a
3184*67e74705SXin Li       // critical region with the same name. Note that this restriction is not
3185*67e74705SXin Li       // sufficient to prevent deadlock.
3186*67e74705SXin Li       SourceLocation PreviousCriticalLoc;
3187*67e74705SXin Li       bool DeadLock =
3188*67e74705SXin Li           Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
3189*67e74705SXin Li                                   OpenMPDirectiveKind K,
3190*67e74705SXin Li                                   const DeclarationNameInfo &DNI,
3191*67e74705SXin Li                                   SourceLocation Loc)
3192*67e74705SXin Li                                   ->bool {
3193*67e74705SXin Li                                 if (K == OMPD_critical &&
3194*67e74705SXin Li                                     DNI.getName() == CurrentName.getName()) {
3195*67e74705SXin Li                                   PreviousCriticalLoc = Loc;
3196*67e74705SXin Li                                   return true;
3197*67e74705SXin Li                                 } else
3198*67e74705SXin Li                                   return false;
3199*67e74705SXin Li                               },
3200*67e74705SXin Li                               false /* skip top directive */);
3201*67e74705SXin Li       if (DeadLock) {
3202*67e74705SXin Li         SemaRef.Diag(StartLoc,
3203*67e74705SXin Li                      diag::err_omp_prohibited_region_critical_same_name)
3204*67e74705SXin Li             << CurrentName.getName();
3205*67e74705SXin Li         if (PreviousCriticalLoc.isValid())
3206*67e74705SXin Li           SemaRef.Diag(PreviousCriticalLoc,
3207*67e74705SXin Li                        diag::note_omp_previous_critical_region);
3208*67e74705SXin Li         return true;
3209*67e74705SXin Li       }
3210*67e74705SXin Li     } else if (CurrentRegion == OMPD_barrier) {
3211*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3212*67e74705SXin Li       // A barrier region may not be closely nested inside a worksharing,
3213*67e74705SXin Li       // explicit task, critical, ordered, atomic, or master region.
3214*67e74705SXin Li       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3215*67e74705SXin Li                           isOpenMPTaskingDirective(ParentRegion) ||
3216*67e74705SXin Li                           ParentRegion == OMPD_master ||
3217*67e74705SXin Li                           ParentRegion == OMPD_critical ||
3218*67e74705SXin Li                           ParentRegion == OMPD_ordered;
3219*67e74705SXin Li     } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
3220*67e74705SXin Li                !isOpenMPParallelDirective(CurrentRegion)) {
3221*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3222*67e74705SXin Li       // A worksharing region may not be closely nested inside a worksharing,
3223*67e74705SXin Li       // explicit task, critical, ordered, atomic, or master region.
3224*67e74705SXin Li       NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3225*67e74705SXin Li                           isOpenMPTaskingDirective(ParentRegion) ||
3226*67e74705SXin Li                           ParentRegion == OMPD_master ||
3227*67e74705SXin Li                           ParentRegion == OMPD_critical ||
3228*67e74705SXin Li                           ParentRegion == OMPD_ordered;
3229*67e74705SXin Li       Recommend = ShouldBeInParallelRegion;
3230*67e74705SXin Li     } else if (CurrentRegion == OMPD_ordered) {
3231*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3232*67e74705SXin Li       // An ordered region may not be closely nested inside a critical,
3233*67e74705SXin Li       // atomic, or explicit task region.
3234*67e74705SXin Li       // An ordered region must be closely nested inside a loop region (or
3235*67e74705SXin Li       // parallel loop region) with an ordered clause.
3236*67e74705SXin Li       // OpenMP [2.8.1,simd Construct, Restrictions]
3237*67e74705SXin Li       // An ordered construct with the simd clause is the only OpenMP construct
3238*67e74705SXin Li       // that can appear in the simd region.
3239*67e74705SXin Li       NestingProhibited = ParentRegion == OMPD_critical ||
3240*67e74705SXin Li                           isOpenMPTaskingDirective(ParentRegion) ||
3241*67e74705SXin Li                           !(isOpenMPSimdDirective(ParentRegion) ||
3242*67e74705SXin Li                             Stack->isParentOrderedRegion());
3243*67e74705SXin Li       Recommend = ShouldBeInOrderedRegion;
3244*67e74705SXin Li     } else if (isOpenMPTeamsDirective(CurrentRegion)) {
3245*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3246*67e74705SXin Li       // If specified, a teams construct must be contained within a target
3247*67e74705SXin Li       // construct.
3248*67e74705SXin Li       NestingProhibited = ParentRegion != OMPD_target;
3249*67e74705SXin Li       Recommend = ShouldBeInTargetRegion;
3250*67e74705SXin Li       Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
3251*67e74705SXin Li     }
3252*67e74705SXin Li     if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
3253*67e74705SXin Li       // OpenMP [2.16, Nesting of Regions]
3254*67e74705SXin Li       // distribute, parallel, parallel sections, parallel workshare, and the
3255*67e74705SXin Li       // parallel loop and parallel loop SIMD constructs are the only OpenMP
3256*67e74705SXin Li       // constructs that can be closely nested in the teams region.
3257*67e74705SXin Li       NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3258*67e74705SXin Li                           !isOpenMPDistributeDirective(CurrentRegion);
3259*67e74705SXin Li       Recommend = ShouldBeInParallelRegion;
3260*67e74705SXin Li     }
3261*67e74705SXin Li     if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
3262*67e74705SXin Li       // OpenMP 4.5 [2.17 Nesting of Regions]
3263*67e74705SXin Li       // The region associated with the distribute construct must be strictly
3264*67e74705SXin Li       // nested inside a teams region
3265*67e74705SXin Li       NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
3266*67e74705SXin Li       Recommend = ShouldBeInTeamsRegion;
3267*67e74705SXin Li     }
3268*67e74705SXin Li     if (!NestingProhibited &&
3269*67e74705SXin Li         (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3270*67e74705SXin Li          isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3271*67e74705SXin Li       // OpenMP 4.5 [2.17 Nesting of Regions]
3272*67e74705SXin Li       // If a target, target update, target data, target enter data, or
3273*67e74705SXin Li       // target exit data construct is encountered during execution of a
3274*67e74705SXin Li       // target region, the behavior is unspecified.
3275*67e74705SXin Li       NestingProhibited = Stack->hasDirective(
3276*67e74705SXin Li           [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
3277*67e74705SXin Li                              SourceLocation) -> bool {
3278*67e74705SXin Li             if (isOpenMPTargetExecutionDirective(K)) {
3279*67e74705SXin Li               OffendingRegion = K;
3280*67e74705SXin Li               return true;
3281*67e74705SXin Li             } else
3282*67e74705SXin Li               return false;
3283*67e74705SXin Li           },
3284*67e74705SXin Li           false /* don't skip top directive */);
3285*67e74705SXin Li       CloseNesting = false;
3286*67e74705SXin Li     }
3287*67e74705SXin Li     if (NestingProhibited) {
3288*67e74705SXin Li       SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3289*67e74705SXin Li           << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3290*67e74705SXin Li           << Recommend << getOpenMPDirectiveName(CurrentRegion);
3291*67e74705SXin Li       return true;
3292*67e74705SXin Li     }
3293*67e74705SXin Li   }
3294*67e74705SXin Li   return false;
3295*67e74705SXin Li }
3296*67e74705SXin Li 
checkIfClauses(Sema & S,OpenMPDirectiveKind Kind,ArrayRef<OMPClause * > Clauses,ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers)3297*67e74705SXin Li static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3298*67e74705SXin Li                            ArrayRef<OMPClause *> Clauses,
3299*67e74705SXin Li                            ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3300*67e74705SXin Li   bool ErrorFound = false;
3301*67e74705SXin Li   unsigned NamedModifiersNumber = 0;
3302*67e74705SXin Li   SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3303*67e74705SXin Li       OMPD_unknown + 1);
3304*67e74705SXin Li   SmallVector<SourceLocation, 4> NameModifierLoc;
3305*67e74705SXin Li   for (const auto *C : Clauses) {
3306*67e74705SXin Li     if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3307*67e74705SXin Li       // At most one if clause without a directive-name-modifier can appear on
3308*67e74705SXin Li       // the directive.
3309*67e74705SXin Li       OpenMPDirectiveKind CurNM = IC->getNameModifier();
3310*67e74705SXin Li       if (FoundNameModifiers[CurNM]) {
3311*67e74705SXin Li         S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
3312*67e74705SXin Li             << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3313*67e74705SXin Li             << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3314*67e74705SXin Li         ErrorFound = true;
3315*67e74705SXin Li       } else if (CurNM != OMPD_unknown) {
3316*67e74705SXin Li         NameModifierLoc.push_back(IC->getNameModifierLoc());
3317*67e74705SXin Li         ++NamedModifiersNumber;
3318*67e74705SXin Li       }
3319*67e74705SXin Li       FoundNameModifiers[CurNM] = IC;
3320*67e74705SXin Li       if (CurNM == OMPD_unknown)
3321*67e74705SXin Li         continue;
3322*67e74705SXin Li       // Check if the specified name modifier is allowed for the current
3323*67e74705SXin Li       // directive.
3324*67e74705SXin Li       // At most one if clause with the particular directive-name-modifier can
3325*67e74705SXin Li       // appear on the directive.
3326*67e74705SXin Li       bool MatchFound = false;
3327*67e74705SXin Li       for (auto NM : AllowedNameModifiers) {
3328*67e74705SXin Li         if (CurNM == NM) {
3329*67e74705SXin Li           MatchFound = true;
3330*67e74705SXin Li           break;
3331*67e74705SXin Li         }
3332*67e74705SXin Li       }
3333*67e74705SXin Li       if (!MatchFound) {
3334*67e74705SXin Li         S.Diag(IC->getNameModifierLoc(),
3335*67e74705SXin Li                diag::err_omp_wrong_if_directive_name_modifier)
3336*67e74705SXin Li             << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3337*67e74705SXin Li         ErrorFound = true;
3338*67e74705SXin Li       }
3339*67e74705SXin Li     }
3340*67e74705SXin Li   }
3341*67e74705SXin Li   // If any if clause on the directive includes a directive-name-modifier then
3342*67e74705SXin Li   // all if clauses on the directive must include a directive-name-modifier.
3343*67e74705SXin Li   if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3344*67e74705SXin Li     if (NamedModifiersNumber == AllowedNameModifiers.size()) {
3345*67e74705SXin Li       S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
3346*67e74705SXin Li              diag::err_omp_no_more_if_clause);
3347*67e74705SXin Li     } else {
3348*67e74705SXin Li       std::string Values;
3349*67e74705SXin Li       std::string Sep(", ");
3350*67e74705SXin Li       unsigned AllowedCnt = 0;
3351*67e74705SXin Li       unsigned TotalAllowedNum =
3352*67e74705SXin Li           AllowedNameModifiers.size() - NamedModifiersNumber;
3353*67e74705SXin Li       for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3354*67e74705SXin Li            ++Cnt) {
3355*67e74705SXin Li         OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3356*67e74705SXin Li         if (!FoundNameModifiers[NM]) {
3357*67e74705SXin Li           Values += "'";
3358*67e74705SXin Li           Values += getOpenMPDirectiveName(NM);
3359*67e74705SXin Li           Values += "'";
3360*67e74705SXin Li           if (AllowedCnt + 2 == TotalAllowedNum)
3361*67e74705SXin Li             Values += " or ";
3362*67e74705SXin Li           else if (AllowedCnt + 1 != TotalAllowedNum)
3363*67e74705SXin Li             Values += Sep;
3364*67e74705SXin Li           ++AllowedCnt;
3365*67e74705SXin Li         }
3366*67e74705SXin Li       }
3367*67e74705SXin Li       S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
3368*67e74705SXin Li              diag::err_omp_unnamed_if_clause)
3369*67e74705SXin Li           << (TotalAllowedNum > 1) << Values;
3370*67e74705SXin Li     }
3371*67e74705SXin Li     for (auto Loc : NameModifierLoc) {
3372*67e74705SXin Li       S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3373*67e74705SXin Li     }
3374*67e74705SXin Li     ErrorFound = true;
3375*67e74705SXin Li   }
3376*67e74705SXin Li   return ErrorFound;
3377*67e74705SXin Li }
3378*67e74705SXin Li 
ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,const DeclarationNameInfo & DirName,OpenMPDirectiveKind CancelRegion,ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3379*67e74705SXin Li StmtResult Sema::ActOnOpenMPExecutableDirective(
3380*67e74705SXin Li     OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3381*67e74705SXin Li     OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3382*67e74705SXin Li     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
3383*67e74705SXin Li   StmtResult Res = StmtError();
3384*67e74705SXin Li   if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
3385*67e74705SXin Li                             StartLoc))
3386*67e74705SXin Li     return StmtError();
3387*67e74705SXin Li 
3388*67e74705SXin Li   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
3389*67e74705SXin Li   llvm::DenseMap<ValueDecl *, Expr *> VarsWithInheritedDSA;
3390*67e74705SXin Li   bool ErrorFound = false;
3391*67e74705SXin Li   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
3392*67e74705SXin Li   if (AStmt) {
3393*67e74705SXin Li     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3394*67e74705SXin Li 
3395*67e74705SXin Li     // Check default data sharing attributes for referenced variables.
3396*67e74705SXin Li     DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
3397*67e74705SXin Li     DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
3398*67e74705SXin Li     if (DSAChecker.isErrorFound())
3399*67e74705SXin Li       return StmtError();
3400*67e74705SXin Li     // Generate list of implicitly defined firstprivate variables.
3401*67e74705SXin Li     VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
3402*67e74705SXin Li 
3403*67e74705SXin Li     if (!DSAChecker.getImplicitFirstprivate().empty()) {
3404*67e74705SXin Li       if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
3405*67e74705SXin Li               DSAChecker.getImplicitFirstprivate(), SourceLocation(),
3406*67e74705SXin Li               SourceLocation(), SourceLocation())) {
3407*67e74705SXin Li         ClausesWithImplicit.push_back(Implicit);
3408*67e74705SXin Li         ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
3409*67e74705SXin Li                      DSAChecker.getImplicitFirstprivate().size();
3410*67e74705SXin Li       } else
3411*67e74705SXin Li         ErrorFound = true;
3412*67e74705SXin Li     }
3413*67e74705SXin Li   }
3414*67e74705SXin Li 
3415*67e74705SXin Li   llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
3416*67e74705SXin Li   switch (Kind) {
3417*67e74705SXin Li   case OMPD_parallel:
3418*67e74705SXin Li     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3419*67e74705SXin Li                                        EndLoc);
3420*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3421*67e74705SXin Li     break;
3422*67e74705SXin Li   case OMPD_simd:
3423*67e74705SXin Li     Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3424*67e74705SXin Li                                    VarsWithInheritedDSA);
3425*67e74705SXin Li     break;
3426*67e74705SXin Li   case OMPD_for:
3427*67e74705SXin Li     Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3428*67e74705SXin Li                                   VarsWithInheritedDSA);
3429*67e74705SXin Li     break;
3430*67e74705SXin Li   case OMPD_for_simd:
3431*67e74705SXin Li     Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3432*67e74705SXin Li                                       EndLoc, VarsWithInheritedDSA);
3433*67e74705SXin Li     break;
3434*67e74705SXin Li   case OMPD_sections:
3435*67e74705SXin Li     Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3436*67e74705SXin Li                                        EndLoc);
3437*67e74705SXin Li     break;
3438*67e74705SXin Li   case OMPD_section:
3439*67e74705SXin Li     assert(ClausesWithImplicit.empty() &&
3440*67e74705SXin Li            "No clauses are allowed for 'omp section' directive");
3441*67e74705SXin Li     Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3442*67e74705SXin Li     break;
3443*67e74705SXin Li   case OMPD_single:
3444*67e74705SXin Li     Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3445*67e74705SXin Li                                      EndLoc);
3446*67e74705SXin Li     break;
3447*67e74705SXin Li   case OMPD_master:
3448*67e74705SXin Li     assert(ClausesWithImplicit.empty() &&
3449*67e74705SXin Li            "No clauses are allowed for 'omp master' directive");
3450*67e74705SXin Li     Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3451*67e74705SXin Li     break;
3452*67e74705SXin Li   case OMPD_critical:
3453*67e74705SXin Li     Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3454*67e74705SXin Li                                        StartLoc, EndLoc);
3455*67e74705SXin Li     break;
3456*67e74705SXin Li   case OMPD_parallel_for:
3457*67e74705SXin Li     Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3458*67e74705SXin Li                                           EndLoc, VarsWithInheritedDSA);
3459*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3460*67e74705SXin Li     break;
3461*67e74705SXin Li   case OMPD_parallel_for_simd:
3462*67e74705SXin Li     Res = ActOnOpenMPParallelForSimdDirective(
3463*67e74705SXin Li         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3464*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3465*67e74705SXin Li     break;
3466*67e74705SXin Li   case OMPD_parallel_sections:
3467*67e74705SXin Li     Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3468*67e74705SXin Li                                                StartLoc, EndLoc);
3469*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3470*67e74705SXin Li     break;
3471*67e74705SXin Li   case OMPD_task:
3472*67e74705SXin Li     Res =
3473*67e74705SXin Li         ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3474*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_task);
3475*67e74705SXin Li     break;
3476*67e74705SXin Li   case OMPD_taskyield:
3477*67e74705SXin Li     assert(ClausesWithImplicit.empty() &&
3478*67e74705SXin Li            "No clauses are allowed for 'omp taskyield' directive");
3479*67e74705SXin Li     assert(AStmt == nullptr &&
3480*67e74705SXin Li            "No associated statement allowed for 'omp taskyield' directive");
3481*67e74705SXin Li     Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3482*67e74705SXin Li     break;
3483*67e74705SXin Li   case OMPD_barrier:
3484*67e74705SXin Li     assert(ClausesWithImplicit.empty() &&
3485*67e74705SXin Li            "No clauses are allowed for 'omp barrier' directive");
3486*67e74705SXin Li     assert(AStmt == nullptr &&
3487*67e74705SXin Li            "No associated statement allowed for 'omp barrier' directive");
3488*67e74705SXin Li     Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3489*67e74705SXin Li     break;
3490*67e74705SXin Li   case OMPD_taskwait:
3491*67e74705SXin Li     assert(ClausesWithImplicit.empty() &&
3492*67e74705SXin Li            "No clauses are allowed for 'omp taskwait' directive");
3493*67e74705SXin Li     assert(AStmt == nullptr &&
3494*67e74705SXin Li            "No associated statement allowed for 'omp taskwait' directive");
3495*67e74705SXin Li     Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3496*67e74705SXin Li     break;
3497*67e74705SXin Li   case OMPD_taskgroup:
3498*67e74705SXin Li     assert(ClausesWithImplicit.empty() &&
3499*67e74705SXin Li            "No clauses are allowed for 'omp taskgroup' directive");
3500*67e74705SXin Li     Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
3501*67e74705SXin Li     break;
3502*67e74705SXin Li   case OMPD_flush:
3503*67e74705SXin Li     assert(AStmt == nullptr &&
3504*67e74705SXin Li            "No associated statement allowed for 'omp flush' directive");
3505*67e74705SXin Li     Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3506*67e74705SXin Li     break;
3507*67e74705SXin Li   case OMPD_ordered:
3508*67e74705SXin Li     Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3509*67e74705SXin Li                                       EndLoc);
3510*67e74705SXin Li     break;
3511*67e74705SXin Li   case OMPD_atomic:
3512*67e74705SXin Li     Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3513*67e74705SXin Li                                      EndLoc);
3514*67e74705SXin Li     break;
3515*67e74705SXin Li   case OMPD_teams:
3516*67e74705SXin Li     Res =
3517*67e74705SXin Li         ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3518*67e74705SXin Li     break;
3519*67e74705SXin Li   case OMPD_target:
3520*67e74705SXin Li     Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3521*67e74705SXin Li                                      EndLoc);
3522*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target);
3523*67e74705SXin Li     break;
3524*67e74705SXin Li   case OMPD_target_parallel:
3525*67e74705SXin Li     Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3526*67e74705SXin Li                                              StartLoc, EndLoc);
3527*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target);
3528*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3529*67e74705SXin Li     break;
3530*67e74705SXin Li   case OMPD_target_parallel_for:
3531*67e74705SXin Li     Res = ActOnOpenMPTargetParallelForDirective(
3532*67e74705SXin Li         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3533*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target);
3534*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3535*67e74705SXin Li     break;
3536*67e74705SXin Li   case OMPD_cancellation_point:
3537*67e74705SXin Li     assert(ClausesWithImplicit.empty() &&
3538*67e74705SXin Li            "No clauses are allowed for 'omp cancellation point' directive");
3539*67e74705SXin Li     assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3540*67e74705SXin Li                                "cancellation point' directive");
3541*67e74705SXin Li     Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3542*67e74705SXin Li     break;
3543*67e74705SXin Li   case OMPD_cancel:
3544*67e74705SXin Li     assert(AStmt == nullptr &&
3545*67e74705SXin Li            "No associated statement allowed for 'omp cancel' directive");
3546*67e74705SXin Li     Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3547*67e74705SXin Li                                      CancelRegion);
3548*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_cancel);
3549*67e74705SXin Li     break;
3550*67e74705SXin Li   case OMPD_target_data:
3551*67e74705SXin Li     Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3552*67e74705SXin Li                                          EndLoc);
3553*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target_data);
3554*67e74705SXin Li     break;
3555*67e74705SXin Li   case OMPD_target_enter_data:
3556*67e74705SXin Li     Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
3557*67e74705SXin Li                                               EndLoc);
3558*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target_enter_data);
3559*67e74705SXin Li     break;
3560*67e74705SXin Li   case OMPD_target_exit_data:
3561*67e74705SXin Li     Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
3562*67e74705SXin Li                                              EndLoc);
3563*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target_exit_data);
3564*67e74705SXin Li     break;
3565*67e74705SXin Li   case OMPD_taskloop:
3566*67e74705SXin Li     Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3567*67e74705SXin Li                                        EndLoc, VarsWithInheritedDSA);
3568*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_taskloop);
3569*67e74705SXin Li     break;
3570*67e74705SXin Li   case OMPD_taskloop_simd:
3571*67e74705SXin Li     Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3572*67e74705SXin Li                                            EndLoc, VarsWithInheritedDSA);
3573*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_taskloop);
3574*67e74705SXin Li     break;
3575*67e74705SXin Li   case OMPD_distribute:
3576*67e74705SXin Li     Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3577*67e74705SXin Li                                          EndLoc, VarsWithInheritedDSA);
3578*67e74705SXin Li     break;
3579*67e74705SXin Li   case OMPD_target_update:
3580*67e74705SXin Li     assert(!AStmt && "Statement is not allowed for target update");
3581*67e74705SXin Li     Res =
3582*67e74705SXin Li         ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc, EndLoc);
3583*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target_update);
3584*67e74705SXin Li     break;
3585*67e74705SXin Li   case OMPD_distribute_parallel_for:
3586*67e74705SXin Li     Res = ActOnOpenMPDistributeParallelForDirective(
3587*67e74705SXin Li         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3588*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3589*67e74705SXin Li     break;
3590*67e74705SXin Li   case OMPD_distribute_parallel_for_simd:
3591*67e74705SXin Li     Res = ActOnOpenMPDistributeParallelForSimdDirective(
3592*67e74705SXin Li         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3593*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3594*67e74705SXin Li     break;
3595*67e74705SXin Li   case OMPD_distribute_simd:
3596*67e74705SXin Li     Res = ActOnOpenMPDistributeSimdDirective(
3597*67e74705SXin Li         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3598*67e74705SXin Li     break;
3599*67e74705SXin Li   case OMPD_target_parallel_for_simd:
3600*67e74705SXin Li     Res = ActOnOpenMPTargetParallelForSimdDirective(
3601*67e74705SXin Li         ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3602*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_target);
3603*67e74705SXin Li     AllowedNameModifiers.push_back(OMPD_parallel);
3604*67e74705SXin Li     break;
3605*67e74705SXin Li   case OMPD_declare_target:
3606*67e74705SXin Li   case OMPD_end_declare_target:
3607*67e74705SXin Li   case OMPD_threadprivate:
3608*67e74705SXin Li   case OMPD_declare_reduction:
3609*67e74705SXin Li   case OMPD_declare_simd:
3610*67e74705SXin Li     llvm_unreachable("OpenMP Directive is not allowed");
3611*67e74705SXin Li   case OMPD_unknown:
3612*67e74705SXin Li     llvm_unreachable("Unknown OpenMP directive");
3613*67e74705SXin Li   }
3614*67e74705SXin Li 
3615*67e74705SXin Li   for (auto P : VarsWithInheritedDSA) {
3616*67e74705SXin Li     Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3617*67e74705SXin Li         << P.first << P.second->getSourceRange();
3618*67e74705SXin Li   }
3619*67e74705SXin Li   ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3620*67e74705SXin Li 
3621*67e74705SXin Li   if (!AllowedNameModifiers.empty())
3622*67e74705SXin Li     ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3623*67e74705SXin Li                  ErrorFound;
3624*67e74705SXin Li 
3625*67e74705SXin Li   if (ErrorFound)
3626*67e74705SXin Li     return StmtError();
3627*67e74705SXin Li   return Res;
3628*67e74705SXin Li }
3629*67e74705SXin Li 
ActOnOpenMPDeclareSimdDirective(DeclGroupPtrTy DG,OMPDeclareSimdDeclAttr::BranchStateTy BS,Expr * Simdlen,ArrayRef<Expr * > Uniforms,ArrayRef<Expr * > Aligneds,ArrayRef<Expr * > Alignments,ArrayRef<Expr * > Linears,ArrayRef<unsigned> LinModifiers,ArrayRef<Expr * > Steps,SourceRange SR)3630*67e74705SXin Li Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3631*67e74705SXin Li     DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
3632*67e74705SXin Li     ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
3633*67e74705SXin Li     ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3634*67e74705SXin Li     ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
3635*67e74705SXin Li   assert(Aligneds.size() == Alignments.size());
3636*67e74705SXin Li   assert(Linears.size() == LinModifiers.size());
3637*67e74705SXin Li   assert(Linears.size() == Steps.size());
3638*67e74705SXin Li   if (!DG || DG.get().isNull())
3639*67e74705SXin Li     return DeclGroupPtrTy();
3640*67e74705SXin Li 
3641*67e74705SXin Li   if (!DG.get().isSingleDecl()) {
3642*67e74705SXin Li     Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
3643*67e74705SXin Li     return DG;
3644*67e74705SXin Li   }
3645*67e74705SXin Li   auto *ADecl = DG.get().getSingleDecl();
3646*67e74705SXin Li   if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3647*67e74705SXin Li     ADecl = FTD->getTemplatedDecl();
3648*67e74705SXin Li 
3649*67e74705SXin Li   auto *FD = dyn_cast<FunctionDecl>(ADecl);
3650*67e74705SXin Li   if (!FD) {
3651*67e74705SXin Li     Diag(ADecl->getLocation(), diag::err_omp_function_expected);
3652*67e74705SXin Li     return DeclGroupPtrTy();
3653*67e74705SXin Li   }
3654*67e74705SXin Li 
3655*67e74705SXin Li   // OpenMP [2.8.2, declare simd construct, Description]
3656*67e74705SXin Li   // The parameter of the simdlen clause must be a constant positive integer
3657*67e74705SXin Li   // expression.
3658*67e74705SXin Li   ExprResult SL;
3659*67e74705SXin Li   if (Simdlen)
3660*67e74705SXin Li     SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
3661*67e74705SXin Li   // OpenMP [2.8.2, declare simd construct, Description]
3662*67e74705SXin Li   // The special this pointer can be used as if was one of the arguments to the
3663*67e74705SXin Li   // function in any of the linear, aligned, or uniform clauses.
3664*67e74705SXin Li   // The uniform clause declares one or more arguments to have an invariant
3665*67e74705SXin Li   // value for all concurrent invocations of the function in the execution of a
3666*67e74705SXin Li   // single SIMD loop.
3667*67e74705SXin Li   llvm::DenseMap<Decl *, Expr *> UniformedArgs;
3668*67e74705SXin Li   Expr *UniformedLinearThis = nullptr;
3669*67e74705SXin Li   for (auto *E : Uniforms) {
3670*67e74705SXin Li     E = E->IgnoreParenImpCasts();
3671*67e74705SXin Li     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3672*67e74705SXin Li       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
3673*67e74705SXin Li         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3674*67e74705SXin Li             FD->getParamDecl(PVD->getFunctionScopeIndex())
3675*67e74705SXin Li                     ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
3676*67e74705SXin Li           UniformedArgs.insert(std::make_pair(PVD->getCanonicalDecl(), E));
3677*67e74705SXin Li           continue;
3678*67e74705SXin Li         }
3679*67e74705SXin Li     if (isa<CXXThisExpr>(E)) {
3680*67e74705SXin Li       UniformedLinearThis = E;
3681*67e74705SXin Li       continue;
3682*67e74705SXin Li     }
3683*67e74705SXin Li     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3684*67e74705SXin Li         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3685*67e74705SXin Li   }
3686*67e74705SXin Li   // OpenMP [2.8.2, declare simd construct, Description]
3687*67e74705SXin Li   // The aligned clause declares that the object to which each list item points
3688*67e74705SXin Li   // is aligned to the number of bytes expressed in the optional parameter of
3689*67e74705SXin Li   // the aligned clause.
3690*67e74705SXin Li   // The special this pointer can be used as if was one of the arguments to the
3691*67e74705SXin Li   // function in any of the linear, aligned, or uniform clauses.
3692*67e74705SXin Li   // The type of list items appearing in the aligned clause must be array,
3693*67e74705SXin Li   // pointer, reference to array, or reference to pointer.
3694*67e74705SXin Li   llvm::DenseMap<Decl *, Expr *> AlignedArgs;
3695*67e74705SXin Li   Expr *AlignedThis = nullptr;
3696*67e74705SXin Li   for (auto *E : Aligneds) {
3697*67e74705SXin Li     E = E->IgnoreParenImpCasts();
3698*67e74705SXin Li     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3699*67e74705SXin Li       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3700*67e74705SXin Li         auto *CanonPVD = PVD->getCanonicalDecl();
3701*67e74705SXin Li         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3702*67e74705SXin Li             FD->getParamDecl(PVD->getFunctionScopeIndex())
3703*67e74705SXin Li                     ->getCanonicalDecl() == CanonPVD) {
3704*67e74705SXin Li           // OpenMP  [2.8.1, simd construct, Restrictions]
3705*67e74705SXin Li           // A list-item cannot appear in more than one aligned clause.
3706*67e74705SXin Li           if (AlignedArgs.count(CanonPVD) > 0) {
3707*67e74705SXin Li             Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3708*67e74705SXin Li                 << 1 << E->getSourceRange();
3709*67e74705SXin Li             Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3710*67e74705SXin Li                  diag::note_omp_explicit_dsa)
3711*67e74705SXin Li                 << getOpenMPClauseName(OMPC_aligned);
3712*67e74705SXin Li             continue;
3713*67e74705SXin Li           }
3714*67e74705SXin Li           AlignedArgs[CanonPVD] = E;
3715*67e74705SXin Li           QualType QTy = PVD->getType()
3716*67e74705SXin Li                              .getNonReferenceType()
3717*67e74705SXin Li                              .getUnqualifiedType()
3718*67e74705SXin Li                              .getCanonicalType();
3719*67e74705SXin Li           const Type *Ty = QTy.getTypePtrOrNull();
3720*67e74705SXin Li           if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3721*67e74705SXin Li             Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3722*67e74705SXin Li                 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3723*67e74705SXin Li             Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3724*67e74705SXin Li           }
3725*67e74705SXin Li           continue;
3726*67e74705SXin Li         }
3727*67e74705SXin Li       }
3728*67e74705SXin Li     if (isa<CXXThisExpr>(E)) {
3729*67e74705SXin Li       if (AlignedThis) {
3730*67e74705SXin Li         Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3731*67e74705SXin Li             << 2 << E->getSourceRange();
3732*67e74705SXin Li         Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3733*67e74705SXin Li             << getOpenMPClauseName(OMPC_aligned);
3734*67e74705SXin Li       }
3735*67e74705SXin Li       AlignedThis = E;
3736*67e74705SXin Li       continue;
3737*67e74705SXin Li     }
3738*67e74705SXin Li     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3739*67e74705SXin Li         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3740*67e74705SXin Li   }
3741*67e74705SXin Li   // The optional parameter of the aligned clause, alignment, must be a constant
3742*67e74705SXin Li   // positive integer expression. If no optional parameter is specified,
3743*67e74705SXin Li   // implementation-defined default alignments for SIMD instructions on the
3744*67e74705SXin Li   // target platforms are assumed.
3745*67e74705SXin Li   SmallVector<Expr *, 4> NewAligns;
3746*67e74705SXin Li   for (auto *E : Alignments) {
3747*67e74705SXin Li     ExprResult Align;
3748*67e74705SXin Li     if (E)
3749*67e74705SXin Li       Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3750*67e74705SXin Li     NewAligns.push_back(Align.get());
3751*67e74705SXin Li   }
3752*67e74705SXin Li   // OpenMP [2.8.2, declare simd construct, Description]
3753*67e74705SXin Li   // The linear clause declares one or more list items to be private to a SIMD
3754*67e74705SXin Li   // lane and to have a linear relationship with respect to the iteration space
3755*67e74705SXin Li   // of a loop.
3756*67e74705SXin Li   // The special this pointer can be used as if was one of the arguments to the
3757*67e74705SXin Li   // function in any of the linear, aligned, or uniform clauses.
3758*67e74705SXin Li   // When a linear-step expression is specified in a linear clause it must be
3759*67e74705SXin Li   // either a constant integer expression or an integer-typed parameter that is
3760*67e74705SXin Li   // specified in a uniform clause on the directive.
3761*67e74705SXin Li   llvm::DenseMap<Decl *, Expr *> LinearArgs;
3762*67e74705SXin Li   const bool IsUniformedThis = UniformedLinearThis != nullptr;
3763*67e74705SXin Li   auto MI = LinModifiers.begin();
3764*67e74705SXin Li   for (auto *E : Linears) {
3765*67e74705SXin Li     auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3766*67e74705SXin Li     ++MI;
3767*67e74705SXin Li     E = E->IgnoreParenImpCasts();
3768*67e74705SXin Li     if (auto *DRE = dyn_cast<DeclRefExpr>(E))
3769*67e74705SXin Li       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3770*67e74705SXin Li         auto *CanonPVD = PVD->getCanonicalDecl();
3771*67e74705SXin Li         if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3772*67e74705SXin Li             FD->getParamDecl(PVD->getFunctionScopeIndex())
3773*67e74705SXin Li                     ->getCanonicalDecl() == CanonPVD) {
3774*67e74705SXin Li           // OpenMP  [2.15.3.7, linear Clause, Restrictions]
3775*67e74705SXin Li           // A list-item cannot appear in more than one linear clause.
3776*67e74705SXin Li           if (LinearArgs.count(CanonPVD) > 0) {
3777*67e74705SXin Li             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3778*67e74705SXin Li                 << getOpenMPClauseName(OMPC_linear)
3779*67e74705SXin Li                 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3780*67e74705SXin Li             Diag(LinearArgs[CanonPVD]->getExprLoc(),
3781*67e74705SXin Li                  diag::note_omp_explicit_dsa)
3782*67e74705SXin Li                 << getOpenMPClauseName(OMPC_linear);
3783*67e74705SXin Li             continue;
3784*67e74705SXin Li           }
3785*67e74705SXin Li           // Each argument can appear in at most one uniform or linear clause.
3786*67e74705SXin Li           if (UniformedArgs.count(CanonPVD) > 0) {
3787*67e74705SXin Li             Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3788*67e74705SXin Li                 << getOpenMPClauseName(OMPC_linear)
3789*67e74705SXin Li                 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3790*67e74705SXin Li             Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3791*67e74705SXin Li                  diag::note_omp_explicit_dsa)
3792*67e74705SXin Li                 << getOpenMPClauseName(OMPC_uniform);
3793*67e74705SXin Li             continue;
3794*67e74705SXin Li           }
3795*67e74705SXin Li           LinearArgs[CanonPVD] = E;
3796*67e74705SXin Li           if (E->isValueDependent() || E->isTypeDependent() ||
3797*67e74705SXin Li               E->isInstantiationDependent() ||
3798*67e74705SXin Li               E->containsUnexpandedParameterPack())
3799*67e74705SXin Li             continue;
3800*67e74705SXin Li           (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3801*67e74705SXin Li                                       PVD->getOriginalType());
3802*67e74705SXin Li           continue;
3803*67e74705SXin Li         }
3804*67e74705SXin Li       }
3805*67e74705SXin Li     if (isa<CXXThisExpr>(E)) {
3806*67e74705SXin Li       if (UniformedLinearThis) {
3807*67e74705SXin Li         Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3808*67e74705SXin Li             << getOpenMPClauseName(OMPC_linear)
3809*67e74705SXin Li             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3810*67e74705SXin Li             << E->getSourceRange();
3811*67e74705SXin Li         Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3812*67e74705SXin Li             << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3813*67e74705SXin Li                                                    : OMPC_linear);
3814*67e74705SXin Li         continue;
3815*67e74705SXin Li       }
3816*67e74705SXin Li       UniformedLinearThis = E;
3817*67e74705SXin Li       if (E->isValueDependent() || E->isTypeDependent() ||
3818*67e74705SXin Li           E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3819*67e74705SXin Li         continue;
3820*67e74705SXin Li       (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3821*67e74705SXin Li                                   E->getType());
3822*67e74705SXin Li       continue;
3823*67e74705SXin Li     }
3824*67e74705SXin Li     Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3825*67e74705SXin Li         << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3826*67e74705SXin Li   }
3827*67e74705SXin Li   Expr *Step = nullptr;
3828*67e74705SXin Li   Expr *NewStep = nullptr;
3829*67e74705SXin Li   SmallVector<Expr *, 4> NewSteps;
3830*67e74705SXin Li   for (auto *E : Steps) {
3831*67e74705SXin Li     // Skip the same step expression, it was checked already.
3832*67e74705SXin Li     if (Step == E || !E) {
3833*67e74705SXin Li       NewSteps.push_back(E ? NewStep : nullptr);
3834*67e74705SXin Li       continue;
3835*67e74705SXin Li     }
3836*67e74705SXin Li     Step = E;
3837*67e74705SXin Li     if (auto *DRE = dyn_cast<DeclRefExpr>(Step))
3838*67e74705SXin Li       if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3839*67e74705SXin Li         auto *CanonPVD = PVD->getCanonicalDecl();
3840*67e74705SXin Li         if (UniformedArgs.count(CanonPVD) == 0) {
3841*67e74705SXin Li           Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3842*67e74705SXin Li               << Step->getSourceRange();
3843*67e74705SXin Li         } else if (E->isValueDependent() || E->isTypeDependent() ||
3844*67e74705SXin Li                    E->isInstantiationDependent() ||
3845*67e74705SXin Li                    E->containsUnexpandedParameterPack() ||
3846*67e74705SXin Li                    CanonPVD->getType()->hasIntegerRepresentation())
3847*67e74705SXin Li           NewSteps.push_back(Step);
3848*67e74705SXin Li         else {
3849*67e74705SXin Li           Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3850*67e74705SXin Li               << Step->getSourceRange();
3851*67e74705SXin Li         }
3852*67e74705SXin Li         continue;
3853*67e74705SXin Li       }
3854*67e74705SXin Li     NewStep = Step;
3855*67e74705SXin Li     if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3856*67e74705SXin Li         !Step->isInstantiationDependent() &&
3857*67e74705SXin Li         !Step->containsUnexpandedParameterPack()) {
3858*67e74705SXin Li       NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3859*67e74705SXin Li                     .get();
3860*67e74705SXin Li       if (NewStep)
3861*67e74705SXin Li         NewStep = VerifyIntegerConstantExpression(NewStep).get();
3862*67e74705SXin Li     }
3863*67e74705SXin Li     NewSteps.push_back(NewStep);
3864*67e74705SXin Li   }
3865*67e74705SXin Li   auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3866*67e74705SXin Li       Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
3867*67e74705SXin Li       Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
3868*67e74705SXin Li       const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3869*67e74705SXin Li       const_cast<Expr **>(Linears.data()), Linears.size(),
3870*67e74705SXin Li       const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3871*67e74705SXin Li       NewSteps.data(), NewSteps.size(), SR);
3872*67e74705SXin Li   ADecl->addAttr(NewAttr);
3873*67e74705SXin Li   return ConvertDeclToDeclGroup(ADecl);
3874*67e74705SXin Li }
3875*67e74705SXin Li 
ActOnOpenMPParallelDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)3876*67e74705SXin Li StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3877*67e74705SXin Li                                               Stmt *AStmt,
3878*67e74705SXin Li                                               SourceLocation StartLoc,
3879*67e74705SXin Li                                               SourceLocation EndLoc) {
3880*67e74705SXin Li   if (!AStmt)
3881*67e74705SXin Li     return StmtError();
3882*67e74705SXin Li 
3883*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3884*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
3885*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
3886*67e74705SXin Li   // top and a single exit at the bottom.
3887*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
3888*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
3889*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
3890*67e74705SXin Li 
3891*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
3892*67e74705SXin Li 
3893*67e74705SXin Li   return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3894*67e74705SXin Li                                       DSAStack->isCancelRegion());
3895*67e74705SXin Li }
3896*67e74705SXin Li 
3897*67e74705SXin Li namespace {
3898*67e74705SXin Li /// \brief Helper class for checking canonical form of the OpenMP loops and
3899*67e74705SXin Li /// extracting iteration space of each loop in the loop nest, that will be used
3900*67e74705SXin Li /// for IR generation.
3901*67e74705SXin Li class OpenMPIterationSpaceChecker {
3902*67e74705SXin Li   /// \brief Reference to Sema.
3903*67e74705SXin Li   Sema &SemaRef;
3904*67e74705SXin Li   /// \brief A location for diagnostics (when there is no some better location).
3905*67e74705SXin Li   SourceLocation DefaultLoc;
3906*67e74705SXin Li   /// \brief A location for diagnostics (when increment is not compatible).
3907*67e74705SXin Li   SourceLocation ConditionLoc;
3908*67e74705SXin Li   /// \brief A source location for referring to loop init later.
3909*67e74705SXin Li   SourceRange InitSrcRange;
3910*67e74705SXin Li   /// \brief A source location for referring to condition later.
3911*67e74705SXin Li   SourceRange ConditionSrcRange;
3912*67e74705SXin Li   /// \brief A source location for referring to increment later.
3913*67e74705SXin Li   SourceRange IncrementSrcRange;
3914*67e74705SXin Li   /// \brief Loop variable.
3915*67e74705SXin Li   ValueDecl *LCDecl = nullptr;
3916*67e74705SXin Li   /// \brief Reference to loop variable.
3917*67e74705SXin Li   Expr *LCRef = nullptr;
3918*67e74705SXin Li   /// \brief Lower bound (initializer for the var).
3919*67e74705SXin Li   Expr *LB = nullptr;
3920*67e74705SXin Li   /// \brief Upper bound.
3921*67e74705SXin Li   Expr *UB = nullptr;
3922*67e74705SXin Li   /// \brief Loop step (increment).
3923*67e74705SXin Li   Expr *Step = nullptr;
3924*67e74705SXin Li   /// \brief This flag is true when condition is one of:
3925*67e74705SXin Li   ///   Var <  UB
3926*67e74705SXin Li   ///   Var <= UB
3927*67e74705SXin Li   ///   UB  >  Var
3928*67e74705SXin Li   ///   UB  >= Var
3929*67e74705SXin Li   bool TestIsLessOp = false;
3930*67e74705SXin Li   /// \brief This flag is true when condition is strict ( < or > ).
3931*67e74705SXin Li   bool TestIsStrictOp = false;
3932*67e74705SXin Li   /// \brief This flag is true when step is subtracted on each iteration.
3933*67e74705SXin Li   bool SubtractStep = false;
3934*67e74705SXin Li 
3935*67e74705SXin Li public:
OpenMPIterationSpaceChecker(Sema & SemaRef,SourceLocation DefaultLoc)3936*67e74705SXin Li   OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
3937*67e74705SXin Li       : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
3938*67e74705SXin Li   /// \brief Check init-expr for canonical loop form and save loop counter
3939*67e74705SXin Li   /// variable - #Var and its initialization value - #LB.
3940*67e74705SXin Li   bool CheckInit(Stmt *S, bool EmitDiags = true);
3941*67e74705SXin Li   /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
3942*67e74705SXin Li   /// for less/greater and for strict/non-strict comparison.
3943*67e74705SXin Li   bool CheckCond(Expr *S);
3944*67e74705SXin Li   /// \brief Check incr-expr for canonical loop form and return true if it
3945*67e74705SXin Li   /// does not conform, otherwise save loop step (#Step).
3946*67e74705SXin Li   bool CheckInc(Expr *S);
3947*67e74705SXin Li   /// \brief Return the loop counter variable.
GetLoopDecl() const3948*67e74705SXin Li   ValueDecl *GetLoopDecl() const { return LCDecl; }
3949*67e74705SXin Li   /// \brief Return the reference expression to loop counter variable.
GetLoopDeclRefExpr() const3950*67e74705SXin Li   Expr *GetLoopDeclRefExpr() const { return LCRef; }
3951*67e74705SXin Li   /// \brief Source range of the loop init.
GetInitSrcRange() const3952*67e74705SXin Li   SourceRange GetInitSrcRange() const { return InitSrcRange; }
3953*67e74705SXin Li   /// \brief Source range of the loop condition.
GetConditionSrcRange() const3954*67e74705SXin Li   SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
3955*67e74705SXin Li   /// \brief Source range of the loop increment.
GetIncrementSrcRange() const3956*67e74705SXin Li   SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
3957*67e74705SXin Li   /// \brief True if the step should be subtracted.
ShouldSubtractStep() const3958*67e74705SXin Li   bool ShouldSubtractStep() const { return SubtractStep; }
3959*67e74705SXin Li   /// \brief Build the expression to calculate the number of iterations.
3960*67e74705SXin Li   Expr *
3961*67e74705SXin Li   BuildNumIterations(Scope *S, const bool LimitedType,
3962*67e74705SXin Li                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3963*67e74705SXin Li   /// \brief Build the precondition expression for the loops.
3964*67e74705SXin Li   Expr *BuildPreCond(Scope *S, Expr *Cond,
3965*67e74705SXin Li                      llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const;
3966*67e74705SXin Li   /// \brief Build reference expression to the counter be used for codegen.
3967*67e74705SXin Li   DeclRefExpr *BuildCounterVar(llvm::MapVector<Expr *, DeclRefExpr *> &Captures,
3968*67e74705SXin Li                                DSAStackTy &DSA) const;
3969*67e74705SXin Li   /// \brief Build reference expression to the private counter be used for
3970*67e74705SXin Li   /// codegen.
3971*67e74705SXin Li   Expr *BuildPrivateCounterVar() const;
3972*67e74705SXin Li   /// \brief Build initization of the counter be used for codegen.
3973*67e74705SXin Li   Expr *BuildCounterInit() const;
3974*67e74705SXin Li   /// \brief Build step of the counter be used for codegen.
3975*67e74705SXin Li   Expr *BuildCounterStep() const;
3976*67e74705SXin Li   /// \brief Return true if any expression is dependent.
3977*67e74705SXin Li   bool Dependent() const;
3978*67e74705SXin Li 
3979*67e74705SXin Li private:
3980*67e74705SXin Li   /// \brief Check the right-hand side of an assignment in the increment
3981*67e74705SXin Li   /// expression.
3982*67e74705SXin Li   bool CheckIncRHS(Expr *RHS);
3983*67e74705SXin Li   /// \brief Helper to set loop counter variable and its initializer.
3984*67e74705SXin Li   bool SetLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
3985*67e74705SXin Li   /// \brief Helper to set upper bound.
3986*67e74705SXin Li   bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
3987*67e74705SXin Li              SourceLocation SL);
3988*67e74705SXin Li   /// \brief Helper to set loop increment.
3989*67e74705SXin Li   bool SetStep(Expr *NewStep, bool Subtract);
3990*67e74705SXin Li };
3991*67e74705SXin Li 
Dependent() const3992*67e74705SXin Li bool OpenMPIterationSpaceChecker::Dependent() const {
3993*67e74705SXin Li   if (!LCDecl) {
3994*67e74705SXin Li     assert(!LB && !UB && !Step);
3995*67e74705SXin Li     return false;
3996*67e74705SXin Li   }
3997*67e74705SXin Li   return LCDecl->getType()->isDependentType() ||
3998*67e74705SXin Li          (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3999*67e74705SXin Li          (Step && Step->isValueDependent());
4000*67e74705SXin Li }
4001*67e74705SXin Li 
getExprAsWritten(Expr * E)4002*67e74705SXin Li static Expr *getExprAsWritten(Expr *E) {
4003*67e74705SXin Li   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
4004*67e74705SXin Li     E = ExprTemp->getSubExpr();
4005*67e74705SXin Li 
4006*67e74705SXin Li   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
4007*67e74705SXin Li     E = MTE->GetTemporaryExpr();
4008*67e74705SXin Li 
4009*67e74705SXin Li   while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
4010*67e74705SXin Li     E = Binder->getSubExpr();
4011*67e74705SXin Li 
4012*67e74705SXin Li   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
4013*67e74705SXin Li     E = ICE->getSubExprAsWritten();
4014*67e74705SXin Li   return E->IgnoreParens();
4015*67e74705SXin Li }
4016*67e74705SXin Li 
SetLCDeclAndLB(ValueDecl * NewLCDecl,Expr * NewLCRefExpr,Expr * NewLB)4017*67e74705SXin Li bool OpenMPIterationSpaceChecker::SetLCDeclAndLB(ValueDecl *NewLCDecl,
4018*67e74705SXin Li                                                  Expr *NewLCRefExpr,
4019*67e74705SXin Li                                                  Expr *NewLB) {
4020*67e74705SXin Li   // State consistency checking to ensure correct usage.
4021*67e74705SXin Li   assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
4022*67e74705SXin Li          UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4023*67e74705SXin Li   if (!NewLCDecl || !NewLB)
4024*67e74705SXin Li     return true;
4025*67e74705SXin Li   LCDecl = getCanonicalDecl(NewLCDecl);
4026*67e74705SXin Li   LCRef = NewLCRefExpr;
4027*67e74705SXin Li   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4028*67e74705SXin Li     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4029*67e74705SXin Li       if ((Ctor->isCopyOrMoveConstructor() ||
4030*67e74705SXin Li            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4031*67e74705SXin Li           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4032*67e74705SXin Li         NewLB = CE->getArg(0)->IgnoreParenImpCasts();
4033*67e74705SXin Li   LB = NewLB;
4034*67e74705SXin Li   return false;
4035*67e74705SXin Li }
4036*67e74705SXin Li 
SetUB(Expr * NewUB,bool LessOp,bool StrictOp,SourceRange SR,SourceLocation SL)4037*67e74705SXin Li bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
4038*67e74705SXin Li                                         SourceRange SR, SourceLocation SL) {
4039*67e74705SXin Li   // State consistency checking to ensure correct usage.
4040*67e74705SXin Li   assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4041*67e74705SXin Li          Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
4042*67e74705SXin Li   if (!NewUB)
4043*67e74705SXin Li     return true;
4044*67e74705SXin Li   UB = NewUB;
4045*67e74705SXin Li   TestIsLessOp = LessOp;
4046*67e74705SXin Li   TestIsStrictOp = StrictOp;
4047*67e74705SXin Li   ConditionSrcRange = SR;
4048*67e74705SXin Li   ConditionLoc = SL;
4049*67e74705SXin Li   return false;
4050*67e74705SXin Li }
4051*67e74705SXin Li 
SetStep(Expr * NewStep,bool Subtract)4052*67e74705SXin Li bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
4053*67e74705SXin Li   // State consistency checking to ensure correct usage.
4054*67e74705SXin Li   assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
4055*67e74705SXin Li   if (!NewStep)
4056*67e74705SXin Li     return true;
4057*67e74705SXin Li   if (!NewStep->isValueDependent()) {
4058*67e74705SXin Li     // Check that the step is integer expression.
4059*67e74705SXin Li     SourceLocation StepLoc = NewStep->getLocStart();
4060*67e74705SXin Li     ExprResult Val =
4061*67e74705SXin Li         SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
4062*67e74705SXin Li     if (Val.isInvalid())
4063*67e74705SXin Li       return true;
4064*67e74705SXin Li     NewStep = Val.get();
4065*67e74705SXin Li 
4066*67e74705SXin Li     // OpenMP [2.6, Canonical Loop Form, Restrictions]
4067*67e74705SXin Li     //  If test-expr is of form var relational-op b and relational-op is < or
4068*67e74705SXin Li     //  <= then incr-expr must cause var to increase on each iteration of the
4069*67e74705SXin Li     //  loop. If test-expr is of form var relational-op b and relational-op is
4070*67e74705SXin Li     //  > or >= then incr-expr must cause var to decrease on each iteration of
4071*67e74705SXin Li     //  the loop.
4072*67e74705SXin Li     //  If test-expr is of form b relational-op var and relational-op is < or
4073*67e74705SXin Li     //  <= then incr-expr must cause var to decrease on each iteration of the
4074*67e74705SXin Li     //  loop. If test-expr is of form b relational-op var and relational-op is
4075*67e74705SXin Li     //  > or >= then incr-expr must cause var to increase on each iteration of
4076*67e74705SXin Li     //  the loop.
4077*67e74705SXin Li     llvm::APSInt Result;
4078*67e74705SXin Li     bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4079*67e74705SXin Li     bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4080*67e74705SXin Li     bool IsConstNeg =
4081*67e74705SXin Li         IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
4082*67e74705SXin Li     bool IsConstPos =
4083*67e74705SXin Li         IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
4084*67e74705SXin Li     bool IsConstZero = IsConstant && !Result.getBoolValue();
4085*67e74705SXin Li     if (UB && (IsConstZero ||
4086*67e74705SXin Li                (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
4087*67e74705SXin Li                              : (IsConstPos || (IsUnsigned && !Subtract))))) {
4088*67e74705SXin Li       SemaRef.Diag(NewStep->getExprLoc(),
4089*67e74705SXin Li                    diag::err_omp_loop_incr_not_compatible)
4090*67e74705SXin Li           << LCDecl << TestIsLessOp << NewStep->getSourceRange();
4091*67e74705SXin Li       SemaRef.Diag(ConditionLoc,
4092*67e74705SXin Li                    diag::note_omp_loop_cond_requres_compatible_incr)
4093*67e74705SXin Li           << TestIsLessOp << ConditionSrcRange;
4094*67e74705SXin Li       return true;
4095*67e74705SXin Li     }
4096*67e74705SXin Li     if (TestIsLessOp == Subtract) {
4097*67e74705SXin Li       NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
4098*67e74705SXin Li                                              NewStep).get();
4099*67e74705SXin Li       Subtract = !Subtract;
4100*67e74705SXin Li     }
4101*67e74705SXin Li   }
4102*67e74705SXin Li 
4103*67e74705SXin Li   Step = NewStep;
4104*67e74705SXin Li   SubtractStep = Subtract;
4105*67e74705SXin Li   return false;
4106*67e74705SXin Li }
4107*67e74705SXin Li 
CheckInit(Stmt * S,bool EmitDiags)4108*67e74705SXin Li bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
4109*67e74705SXin Li   // Check init-expr for canonical loop form and save loop counter
4110*67e74705SXin Li   // variable - #Var and its initialization value - #LB.
4111*67e74705SXin Li   // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4112*67e74705SXin Li   //   var = lb
4113*67e74705SXin Li   //   integer-type var = lb
4114*67e74705SXin Li   //   random-access-iterator-type var = lb
4115*67e74705SXin Li   //   pointer-type var = lb
4116*67e74705SXin Li   //
4117*67e74705SXin Li   if (!S) {
4118*67e74705SXin Li     if (EmitDiags) {
4119*67e74705SXin Li       SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4120*67e74705SXin Li     }
4121*67e74705SXin Li     return true;
4122*67e74705SXin Li   }
4123*67e74705SXin Li   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4124*67e74705SXin Li     if (!ExprTemp->cleanupsHaveSideEffects())
4125*67e74705SXin Li       S = ExprTemp->getSubExpr();
4126*67e74705SXin Li 
4127*67e74705SXin Li   InitSrcRange = S->getSourceRange();
4128*67e74705SXin Li   if (Expr *E = dyn_cast<Expr>(S))
4129*67e74705SXin Li     S = E->IgnoreParens();
4130*67e74705SXin Li   if (auto BO = dyn_cast<BinaryOperator>(S)) {
4131*67e74705SXin Li     if (BO->getOpcode() == BO_Assign) {
4132*67e74705SXin Li       auto *LHS = BO->getLHS()->IgnoreParens();
4133*67e74705SXin Li       if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4134*67e74705SXin Li         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4135*67e74705SXin Li           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4136*67e74705SXin Li             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4137*67e74705SXin Li         return SetLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
4138*67e74705SXin Li       }
4139*67e74705SXin Li       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4140*67e74705SXin Li         if (ME->isArrow() &&
4141*67e74705SXin Li             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4142*67e74705SXin Li           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4143*67e74705SXin Li       }
4144*67e74705SXin Li     }
4145*67e74705SXin Li   } else if (auto DS = dyn_cast<DeclStmt>(S)) {
4146*67e74705SXin Li     if (DS->isSingleDecl()) {
4147*67e74705SXin Li       if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
4148*67e74705SXin Li         if (Var->hasInit() && !Var->getType()->isReferenceType()) {
4149*67e74705SXin Li           // Accept non-canonical init form here but emit ext. warning.
4150*67e74705SXin Li           if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
4151*67e74705SXin Li             SemaRef.Diag(S->getLocStart(),
4152*67e74705SXin Li                          diag::ext_omp_loop_not_canonical_init)
4153*67e74705SXin Li                 << S->getSourceRange();
4154*67e74705SXin Li           return SetLCDeclAndLB(Var, nullptr, Var->getInit());
4155*67e74705SXin Li         }
4156*67e74705SXin Li       }
4157*67e74705SXin Li     }
4158*67e74705SXin Li   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4159*67e74705SXin Li     if (CE->getOperator() == OO_Equal) {
4160*67e74705SXin Li       auto *LHS = CE->getArg(0);
4161*67e74705SXin Li       if (auto DRE = dyn_cast<DeclRefExpr>(LHS)) {
4162*67e74705SXin Li         if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4163*67e74705SXin Li           if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4164*67e74705SXin Li             return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4165*67e74705SXin Li         return SetLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
4166*67e74705SXin Li       }
4167*67e74705SXin Li       if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4168*67e74705SXin Li         if (ME->isArrow() &&
4169*67e74705SXin Li             isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4170*67e74705SXin Li           return SetLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4171*67e74705SXin Li       }
4172*67e74705SXin Li     }
4173*67e74705SXin Li   }
4174*67e74705SXin Li 
4175*67e74705SXin Li   if (Dependent() || SemaRef.CurContext->isDependentContext())
4176*67e74705SXin Li     return false;
4177*67e74705SXin Li   if (EmitDiags) {
4178*67e74705SXin Li     SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
4179*67e74705SXin Li         << S->getSourceRange();
4180*67e74705SXin Li   }
4181*67e74705SXin Li   return true;
4182*67e74705SXin Li }
4183*67e74705SXin Li 
4184*67e74705SXin Li /// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
4185*67e74705SXin Li /// variable (which may be the loop variable) if possible.
GetInitLCDecl(Expr * E)4186*67e74705SXin Li static const ValueDecl *GetInitLCDecl(Expr *E) {
4187*67e74705SXin Li   if (!E)
4188*67e74705SXin Li     return nullptr;
4189*67e74705SXin Li   E = getExprAsWritten(E);
4190*67e74705SXin Li   if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
4191*67e74705SXin Li     if (const CXXConstructorDecl *Ctor = CE->getConstructor())
4192*67e74705SXin Li       if ((Ctor->isCopyOrMoveConstructor() ||
4193*67e74705SXin Li            Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4194*67e74705SXin Li           CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
4195*67e74705SXin Li         E = CE->getArg(0)->IgnoreParenImpCasts();
4196*67e74705SXin Li   if (auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4197*67e74705SXin Li     if (auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
4198*67e74705SXin Li       if (auto *CED = dyn_cast<OMPCapturedExprDecl>(VD))
4199*67e74705SXin Li         if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
4200*67e74705SXin Li           return getCanonicalDecl(ME->getMemberDecl());
4201*67e74705SXin Li       return getCanonicalDecl(VD);
4202*67e74705SXin Li     }
4203*67e74705SXin Li   }
4204*67e74705SXin Li   if (auto *ME = dyn_cast_or_null<MemberExpr>(E))
4205*67e74705SXin Li     if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4206*67e74705SXin Li       return getCanonicalDecl(ME->getMemberDecl());
4207*67e74705SXin Li   return nullptr;
4208*67e74705SXin Li }
4209*67e74705SXin Li 
CheckCond(Expr * S)4210*67e74705SXin Li bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
4211*67e74705SXin Li   // Check test-expr for canonical form, save upper-bound UB, flags for
4212*67e74705SXin Li   // less/greater and for strict/non-strict comparison.
4213*67e74705SXin Li   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4214*67e74705SXin Li   //   var relational-op b
4215*67e74705SXin Li   //   b relational-op var
4216*67e74705SXin Li   //
4217*67e74705SXin Li   if (!S) {
4218*67e74705SXin Li     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
4219*67e74705SXin Li     return true;
4220*67e74705SXin Li   }
4221*67e74705SXin Li   S = getExprAsWritten(S);
4222*67e74705SXin Li   SourceLocation CondLoc = S->getLocStart();
4223*67e74705SXin Li   if (auto BO = dyn_cast<BinaryOperator>(S)) {
4224*67e74705SXin Li     if (BO->isRelationalOp()) {
4225*67e74705SXin Li       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4226*67e74705SXin Li         return SetUB(BO->getRHS(),
4227*67e74705SXin Li                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4228*67e74705SXin Li                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4229*67e74705SXin Li                      BO->getSourceRange(), BO->getOperatorLoc());
4230*67e74705SXin Li       if (GetInitLCDecl(BO->getRHS()) == LCDecl)
4231*67e74705SXin Li         return SetUB(BO->getLHS(),
4232*67e74705SXin Li                      (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4233*67e74705SXin Li                      (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4234*67e74705SXin Li                      BO->getSourceRange(), BO->getOperatorLoc());
4235*67e74705SXin Li     }
4236*67e74705SXin Li   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4237*67e74705SXin Li     if (CE->getNumArgs() == 2) {
4238*67e74705SXin Li       auto Op = CE->getOperator();
4239*67e74705SXin Li       switch (Op) {
4240*67e74705SXin Li       case OO_Greater:
4241*67e74705SXin Li       case OO_GreaterEqual:
4242*67e74705SXin Li       case OO_Less:
4243*67e74705SXin Li       case OO_LessEqual:
4244*67e74705SXin Li         if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4245*67e74705SXin Li           return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
4246*67e74705SXin Li                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4247*67e74705SXin Li                        CE->getOperatorLoc());
4248*67e74705SXin Li         if (GetInitLCDecl(CE->getArg(1)) == LCDecl)
4249*67e74705SXin Li           return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
4250*67e74705SXin Li                        Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4251*67e74705SXin Li                        CE->getOperatorLoc());
4252*67e74705SXin Li         break;
4253*67e74705SXin Li       default:
4254*67e74705SXin Li         break;
4255*67e74705SXin Li       }
4256*67e74705SXin Li     }
4257*67e74705SXin Li   }
4258*67e74705SXin Li   if (Dependent() || SemaRef.CurContext->isDependentContext())
4259*67e74705SXin Li     return false;
4260*67e74705SXin Li   SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
4261*67e74705SXin Li       << S->getSourceRange() << LCDecl;
4262*67e74705SXin Li   return true;
4263*67e74705SXin Li }
4264*67e74705SXin Li 
CheckIncRHS(Expr * RHS)4265*67e74705SXin Li bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
4266*67e74705SXin Li   // RHS of canonical loop form increment can be:
4267*67e74705SXin Li   //   var + incr
4268*67e74705SXin Li   //   incr + var
4269*67e74705SXin Li   //   var - incr
4270*67e74705SXin Li   //
4271*67e74705SXin Li   RHS = RHS->IgnoreParenImpCasts();
4272*67e74705SXin Li   if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
4273*67e74705SXin Li     if (BO->isAdditiveOp()) {
4274*67e74705SXin Li       bool IsAdd = BO->getOpcode() == BO_Add;
4275*67e74705SXin Li       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4276*67e74705SXin Li         return SetStep(BO->getRHS(), !IsAdd);
4277*67e74705SXin Li       if (IsAdd && GetInitLCDecl(BO->getRHS()) == LCDecl)
4278*67e74705SXin Li         return SetStep(BO->getLHS(), false);
4279*67e74705SXin Li     }
4280*67e74705SXin Li   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
4281*67e74705SXin Li     bool IsAdd = CE->getOperator() == OO_Plus;
4282*67e74705SXin Li     if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
4283*67e74705SXin Li       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4284*67e74705SXin Li         return SetStep(CE->getArg(1), !IsAdd);
4285*67e74705SXin Li       if (IsAdd && GetInitLCDecl(CE->getArg(1)) == LCDecl)
4286*67e74705SXin Li         return SetStep(CE->getArg(0), false);
4287*67e74705SXin Li     }
4288*67e74705SXin Li   }
4289*67e74705SXin Li   if (Dependent() || SemaRef.CurContext->isDependentContext())
4290*67e74705SXin Li     return false;
4291*67e74705SXin Li   SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
4292*67e74705SXin Li       << RHS->getSourceRange() << LCDecl;
4293*67e74705SXin Li   return true;
4294*67e74705SXin Li }
4295*67e74705SXin Li 
CheckInc(Expr * S)4296*67e74705SXin Li bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
4297*67e74705SXin Li   // Check incr-expr for canonical loop form and return true if it
4298*67e74705SXin Li   // does not conform.
4299*67e74705SXin Li   // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4300*67e74705SXin Li   //   ++var
4301*67e74705SXin Li   //   var++
4302*67e74705SXin Li   //   --var
4303*67e74705SXin Li   //   var--
4304*67e74705SXin Li   //   var += incr
4305*67e74705SXin Li   //   var -= incr
4306*67e74705SXin Li   //   var = var + incr
4307*67e74705SXin Li   //   var = incr + var
4308*67e74705SXin Li   //   var = var - incr
4309*67e74705SXin Li   //
4310*67e74705SXin Li   if (!S) {
4311*67e74705SXin Li     SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
4312*67e74705SXin Li     return true;
4313*67e74705SXin Li   }
4314*67e74705SXin Li   if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4315*67e74705SXin Li     if (!ExprTemp->cleanupsHaveSideEffects())
4316*67e74705SXin Li       S = ExprTemp->getSubExpr();
4317*67e74705SXin Li 
4318*67e74705SXin Li   IncrementSrcRange = S->getSourceRange();
4319*67e74705SXin Li   S = S->IgnoreParens();
4320*67e74705SXin Li   if (auto UO = dyn_cast<UnaryOperator>(S)) {
4321*67e74705SXin Li     if (UO->isIncrementDecrementOp() &&
4322*67e74705SXin Li         GetInitLCDecl(UO->getSubExpr()) == LCDecl)
4323*67e74705SXin Li       return SetStep(
4324*67e74705SXin Li           SemaRef.ActOnIntegerConstant(UO->getLocStart(),
4325*67e74705SXin Li                                        (UO->isDecrementOp() ? -1 : 1)).get(),
4326*67e74705SXin Li           false);
4327*67e74705SXin Li   } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
4328*67e74705SXin Li     switch (BO->getOpcode()) {
4329*67e74705SXin Li     case BO_AddAssign:
4330*67e74705SXin Li     case BO_SubAssign:
4331*67e74705SXin Li       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4332*67e74705SXin Li         return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
4333*67e74705SXin Li       break;
4334*67e74705SXin Li     case BO_Assign:
4335*67e74705SXin Li       if (GetInitLCDecl(BO->getLHS()) == LCDecl)
4336*67e74705SXin Li         return CheckIncRHS(BO->getRHS());
4337*67e74705SXin Li       break;
4338*67e74705SXin Li     default:
4339*67e74705SXin Li       break;
4340*67e74705SXin Li     }
4341*67e74705SXin Li   } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
4342*67e74705SXin Li     switch (CE->getOperator()) {
4343*67e74705SXin Li     case OO_PlusPlus:
4344*67e74705SXin Li     case OO_MinusMinus:
4345*67e74705SXin Li       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4346*67e74705SXin Li         return SetStep(
4347*67e74705SXin Li             SemaRef.ActOnIntegerConstant(
4348*67e74705SXin Li                         CE->getLocStart(),
4349*67e74705SXin Li                         ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
4350*67e74705SXin Li             false);
4351*67e74705SXin Li       break;
4352*67e74705SXin Li     case OO_PlusEqual:
4353*67e74705SXin Li     case OO_MinusEqual:
4354*67e74705SXin Li       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4355*67e74705SXin Li         return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
4356*67e74705SXin Li       break;
4357*67e74705SXin Li     case OO_Equal:
4358*67e74705SXin Li       if (GetInitLCDecl(CE->getArg(0)) == LCDecl)
4359*67e74705SXin Li         return CheckIncRHS(CE->getArg(1));
4360*67e74705SXin Li       break;
4361*67e74705SXin Li     default:
4362*67e74705SXin Li       break;
4363*67e74705SXin Li     }
4364*67e74705SXin Li   }
4365*67e74705SXin Li   if (Dependent() || SemaRef.CurContext->isDependentContext())
4366*67e74705SXin Li     return false;
4367*67e74705SXin Li   SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
4368*67e74705SXin Li       << S->getSourceRange() << LCDecl;
4369*67e74705SXin Li   return true;
4370*67e74705SXin Li }
4371*67e74705SXin Li 
4372*67e74705SXin Li static ExprResult
tryBuildCapture(Sema & SemaRef,Expr * Capture,llvm::MapVector<Expr *,DeclRefExpr * > & Captures)4373*67e74705SXin Li tryBuildCapture(Sema &SemaRef, Expr *Capture,
4374*67e74705SXin Li                 llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4375*67e74705SXin Li   if (SemaRef.CurContext->isDependentContext())
4376*67e74705SXin Li     return ExprResult(Capture);
4377*67e74705SXin Li   if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4378*67e74705SXin Li     return SemaRef.PerformImplicitConversion(
4379*67e74705SXin Li         Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4380*67e74705SXin Li         /*AllowExplicit=*/true);
4381*67e74705SXin Li   auto I = Captures.find(Capture);
4382*67e74705SXin Li   if (I != Captures.end())
4383*67e74705SXin Li     return buildCapture(SemaRef, Capture, I->second);
4384*67e74705SXin Li   DeclRefExpr *Ref = nullptr;
4385*67e74705SXin Li   ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4386*67e74705SXin Li   Captures[Capture] = Ref;
4387*67e74705SXin Li   return Res;
4388*67e74705SXin Li }
4389*67e74705SXin Li 
4390*67e74705SXin Li /// \brief Build the expression to calculate the number of iterations.
BuildNumIterations(Scope * S,const bool LimitedType,llvm::MapVector<Expr *,DeclRefExpr * > & Captures) const4391*67e74705SXin Li Expr *OpenMPIterationSpaceChecker::BuildNumIterations(
4392*67e74705SXin Li     Scope *S, const bool LimitedType,
4393*67e74705SXin Li     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4394*67e74705SXin Li   ExprResult Diff;
4395*67e74705SXin Li   auto VarType = LCDecl->getType().getNonReferenceType();
4396*67e74705SXin Li   if (VarType->isIntegerType() || VarType->isPointerType() ||
4397*67e74705SXin Li       SemaRef.getLangOpts().CPlusPlus) {
4398*67e74705SXin Li     // Upper - Lower
4399*67e74705SXin Li     auto *UBExpr = TestIsLessOp ? UB : LB;
4400*67e74705SXin Li     auto *LBExpr = TestIsLessOp ? LB : UB;
4401*67e74705SXin Li     Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4402*67e74705SXin Li     Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
4403*67e74705SXin Li     if (!Upper || !Lower)
4404*67e74705SXin Li       return nullptr;
4405*67e74705SXin Li 
4406*67e74705SXin Li     Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4407*67e74705SXin Li 
4408*67e74705SXin Li     if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4409*67e74705SXin Li       // BuildBinOp already emitted error, this one is to point user to upper
4410*67e74705SXin Li       // and lower bound, and to tell what is passed to 'operator-'.
4411*67e74705SXin Li       SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
4412*67e74705SXin Li           << Upper->getSourceRange() << Lower->getSourceRange();
4413*67e74705SXin Li       return nullptr;
4414*67e74705SXin Li     }
4415*67e74705SXin Li   }
4416*67e74705SXin Li 
4417*67e74705SXin Li   if (!Diff.isUsable())
4418*67e74705SXin Li     return nullptr;
4419*67e74705SXin Li 
4420*67e74705SXin Li   // Upper - Lower [- 1]
4421*67e74705SXin Li   if (TestIsStrictOp)
4422*67e74705SXin Li     Diff = SemaRef.BuildBinOp(
4423*67e74705SXin Li         S, DefaultLoc, BO_Sub, Diff.get(),
4424*67e74705SXin Li         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4425*67e74705SXin Li   if (!Diff.isUsable())
4426*67e74705SXin Li     return nullptr;
4427*67e74705SXin Li 
4428*67e74705SXin Li   // Upper - Lower [- 1] + Step
4429*67e74705SXin Li   auto NewStep = tryBuildCapture(SemaRef, Step, Captures);
4430*67e74705SXin Li   if (!NewStep.isUsable())
4431*67e74705SXin Li     return nullptr;
4432*67e74705SXin Li   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
4433*67e74705SXin Li   if (!Diff.isUsable())
4434*67e74705SXin Li     return nullptr;
4435*67e74705SXin Li 
4436*67e74705SXin Li   // Parentheses (for dumping/debugging purposes only).
4437*67e74705SXin Li   Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4438*67e74705SXin Li   if (!Diff.isUsable())
4439*67e74705SXin Li     return nullptr;
4440*67e74705SXin Li 
4441*67e74705SXin Li   // (Upper - Lower [- 1] + Step) / Step
4442*67e74705SXin Li   Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4443*67e74705SXin Li   if (!Diff.isUsable())
4444*67e74705SXin Li     return nullptr;
4445*67e74705SXin Li 
4446*67e74705SXin Li   // OpenMP runtime requires 32-bit or 64-bit loop variables.
4447*67e74705SXin Li   QualType Type = Diff.get()->getType();
4448*67e74705SXin Li   auto &C = SemaRef.Context;
4449*67e74705SXin Li   bool UseVarType = VarType->hasIntegerRepresentation() &&
4450*67e74705SXin Li                     C.getTypeSize(Type) > C.getTypeSize(VarType);
4451*67e74705SXin Li   if (!Type->isIntegerType() || UseVarType) {
4452*67e74705SXin Li     unsigned NewSize =
4453*67e74705SXin Li         UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4454*67e74705SXin Li     bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4455*67e74705SXin Li                                : Type->hasSignedIntegerRepresentation();
4456*67e74705SXin Li     Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
4457*67e74705SXin Li     if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4458*67e74705SXin Li       Diff = SemaRef.PerformImplicitConversion(
4459*67e74705SXin Li           Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4460*67e74705SXin Li       if (!Diff.isUsable())
4461*67e74705SXin Li         return nullptr;
4462*67e74705SXin Li     }
4463*67e74705SXin Li   }
4464*67e74705SXin Li   if (LimitedType) {
4465*67e74705SXin Li     unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4466*67e74705SXin Li     if (NewSize != C.getTypeSize(Type)) {
4467*67e74705SXin Li       if (NewSize < C.getTypeSize(Type)) {
4468*67e74705SXin Li         assert(NewSize == 64 && "incorrect loop var size");
4469*67e74705SXin Li         SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4470*67e74705SXin Li             << InitSrcRange << ConditionSrcRange;
4471*67e74705SXin Li       }
4472*67e74705SXin Li       QualType NewType = C.getIntTypeForBitwidth(
4473*67e74705SXin Li           NewSize, Type->hasSignedIntegerRepresentation() ||
4474*67e74705SXin Li                        C.getTypeSize(Type) < NewSize);
4475*67e74705SXin Li       if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4476*67e74705SXin Li         Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4477*67e74705SXin Li                                                  Sema::AA_Converting, true);
4478*67e74705SXin Li         if (!Diff.isUsable())
4479*67e74705SXin Li           return nullptr;
4480*67e74705SXin Li       }
4481*67e74705SXin Li     }
4482*67e74705SXin Li   }
4483*67e74705SXin Li 
4484*67e74705SXin Li   return Diff.get();
4485*67e74705SXin Li }
4486*67e74705SXin Li 
BuildPreCond(Scope * S,Expr * Cond,llvm::MapVector<Expr *,DeclRefExpr * > & Captures) const4487*67e74705SXin Li Expr *OpenMPIterationSpaceChecker::BuildPreCond(
4488*67e74705SXin Li     Scope *S, Expr *Cond,
4489*67e74705SXin Li     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) const {
4490*67e74705SXin Li   // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4491*67e74705SXin Li   bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4492*67e74705SXin Li   SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4493*67e74705SXin Li 
4494*67e74705SXin Li   auto NewLB = tryBuildCapture(SemaRef, LB, Captures);
4495*67e74705SXin Li   auto NewUB = tryBuildCapture(SemaRef, UB, Captures);
4496*67e74705SXin Li   if (!NewLB.isUsable() || !NewUB.isUsable())
4497*67e74705SXin Li     return nullptr;
4498*67e74705SXin Li 
4499*67e74705SXin Li   auto CondExpr = SemaRef.BuildBinOp(
4500*67e74705SXin Li       S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4501*67e74705SXin Li                                   : (TestIsStrictOp ? BO_GT : BO_GE),
4502*67e74705SXin Li       NewLB.get(), NewUB.get());
4503*67e74705SXin Li   if (CondExpr.isUsable()) {
4504*67e74705SXin Li     if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4505*67e74705SXin Li                                                 SemaRef.Context.BoolTy))
4506*67e74705SXin Li       CondExpr = SemaRef.PerformImplicitConversion(
4507*67e74705SXin Li           CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4508*67e74705SXin Li           /*AllowExplicit=*/true);
4509*67e74705SXin Li   }
4510*67e74705SXin Li   SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4511*67e74705SXin Li   // Otherwise use original loop conditon and evaluate it in runtime.
4512*67e74705SXin Li   return CondExpr.isUsable() ? CondExpr.get() : Cond;
4513*67e74705SXin Li }
4514*67e74705SXin Li 
4515*67e74705SXin Li /// \brief Build reference expression to the counter be used for codegen.
BuildCounterVar(llvm::MapVector<Expr *,DeclRefExpr * > & Captures,DSAStackTy & DSA) const4516*67e74705SXin Li DeclRefExpr *OpenMPIterationSpaceChecker::BuildCounterVar(
4517*67e74705SXin Li     llvm::MapVector<Expr *, DeclRefExpr *> &Captures, DSAStackTy &DSA) const {
4518*67e74705SXin Li   auto *VD = dyn_cast<VarDecl>(LCDecl);
4519*67e74705SXin Li   if (!VD) {
4520*67e74705SXin Li     VD = SemaRef.IsOpenMPCapturedDecl(LCDecl);
4521*67e74705SXin Li     auto *Ref = buildDeclRefExpr(
4522*67e74705SXin Li         SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
4523*67e74705SXin Li     DSAStackTy::DSAVarData Data = DSA.getTopDSA(LCDecl, /*FromParent=*/false);
4524*67e74705SXin Li     // If the loop control decl is explicitly marked as private, do not mark it
4525*67e74705SXin Li     // as captured again.
4526*67e74705SXin Li     if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4527*67e74705SXin Li       Captures.insert(std::make_pair(LCRef, Ref));
4528*67e74705SXin Li     return Ref;
4529*67e74705SXin Li   }
4530*67e74705SXin Li   return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4531*67e74705SXin Li                           DefaultLoc);
4532*67e74705SXin Li }
4533*67e74705SXin Li 
BuildPrivateCounterVar() const4534*67e74705SXin Li Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
4535*67e74705SXin Li   if (LCDecl && !LCDecl->isInvalidDecl()) {
4536*67e74705SXin Li     auto Type = LCDecl->getType().getNonReferenceType();
4537*67e74705SXin Li     auto *PrivateVar =
4538*67e74705SXin Li         buildVarDecl(SemaRef, DefaultLoc, Type, LCDecl->getName(),
4539*67e74705SXin Li                      LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr);
4540*67e74705SXin Li     if (PrivateVar->isInvalidDecl())
4541*67e74705SXin Li       return nullptr;
4542*67e74705SXin Li     return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4543*67e74705SXin Li   }
4544*67e74705SXin Li   return nullptr;
4545*67e74705SXin Li }
4546*67e74705SXin Li 
4547*67e74705SXin Li /// \brief Build initization of the counter be used for codegen.
BuildCounterInit() const4548*67e74705SXin Li Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
4549*67e74705SXin Li 
4550*67e74705SXin Li /// \brief Build step of the counter be used for codegen.
BuildCounterStep() const4551*67e74705SXin Li Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
4552*67e74705SXin Li 
4553*67e74705SXin Li /// \brief Iteration space of a single for loop.
4554*67e74705SXin Li struct LoopIterationSpace final {
4555*67e74705SXin Li   /// \brief Condition of the loop.
4556*67e74705SXin Li   Expr *PreCond = nullptr;
4557*67e74705SXin Li   /// \brief This expression calculates the number of iterations in the loop.
4558*67e74705SXin Li   /// It is always possible to calculate it before starting the loop.
4559*67e74705SXin Li   Expr *NumIterations = nullptr;
4560*67e74705SXin Li   /// \brief The loop counter variable.
4561*67e74705SXin Li   Expr *CounterVar = nullptr;
4562*67e74705SXin Li   /// \brief Private loop counter variable.
4563*67e74705SXin Li   Expr *PrivateCounterVar = nullptr;
4564*67e74705SXin Li   /// \brief This is initializer for the initial value of #CounterVar.
4565*67e74705SXin Li   Expr *CounterInit = nullptr;
4566*67e74705SXin Li   /// \brief This is step for the #CounterVar used to generate its update:
4567*67e74705SXin Li   /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
4568*67e74705SXin Li   Expr *CounterStep = nullptr;
4569*67e74705SXin Li   /// \brief Should step be subtracted?
4570*67e74705SXin Li   bool Subtract = false;
4571*67e74705SXin Li   /// \brief Source range of the loop init.
4572*67e74705SXin Li   SourceRange InitSrcRange;
4573*67e74705SXin Li   /// \brief Source range of the loop condition.
4574*67e74705SXin Li   SourceRange CondSrcRange;
4575*67e74705SXin Li   /// \brief Source range of the loop increment.
4576*67e74705SXin Li   SourceRange IncSrcRange;
4577*67e74705SXin Li };
4578*67e74705SXin Li 
4579*67e74705SXin Li } // namespace
4580*67e74705SXin Li 
ActOnOpenMPLoopInitialization(SourceLocation ForLoc,Stmt * Init)4581*67e74705SXin Li void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4582*67e74705SXin Li   assert(getLangOpts().OpenMP && "OpenMP is not active.");
4583*67e74705SXin Li   assert(Init && "Expected loop in canonical form.");
4584*67e74705SXin Li   unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4585*67e74705SXin Li   if (AssociatedLoops > 0 &&
4586*67e74705SXin Li       isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4587*67e74705SXin Li     OpenMPIterationSpaceChecker ISC(*this, ForLoc);
4588*67e74705SXin Li     if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
4589*67e74705SXin Li       if (auto *D = ISC.GetLoopDecl()) {
4590*67e74705SXin Li         auto *VD = dyn_cast<VarDecl>(D);
4591*67e74705SXin Li         if (!VD) {
4592*67e74705SXin Li           if (auto *Private = IsOpenMPCapturedDecl(D))
4593*67e74705SXin Li             VD = Private;
4594*67e74705SXin Li           else {
4595*67e74705SXin Li             auto *Ref = buildCapture(*this, D, ISC.GetLoopDeclRefExpr(),
4596*67e74705SXin Li                                      /*WithInit=*/false);
4597*67e74705SXin Li             VD = cast<VarDecl>(Ref->getDecl());
4598*67e74705SXin Li           }
4599*67e74705SXin Li         }
4600*67e74705SXin Li         DSAStack->addLoopControlVariable(D, VD);
4601*67e74705SXin Li       }
4602*67e74705SXin Li     }
4603*67e74705SXin Li     DSAStack->setAssociatedLoops(AssociatedLoops - 1);
4604*67e74705SXin Li   }
4605*67e74705SXin Li }
4606*67e74705SXin Li 
4607*67e74705SXin Li /// \brief Called on a for stmt to check and extract its iteration space
4608*67e74705SXin Li /// for further processing (such as collapsing).
CheckOpenMPIterationSpace(OpenMPDirectiveKind DKind,Stmt * S,Sema & SemaRef,DSAStackTy & DSA,unsigned CurrentNestedLoopCount,unsigned NestedLoopCount,Expr * CollapseLoopCountExpr,Expr * OrderedLoopCountExpr,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA,LoopIterationSpace & ResultIterSpace,llvm::MapVector<Expr *,DeclRefExpr * > & Captures)4609*67e74705SXin Li static bool CheckOpenMPIterationSpace(
4610*67e74705SXin Li     OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4611*67e74705SXin Li     unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
4612*67e74705SXin Li     Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
4613*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4614*67e74705SXin Li     LoopIterationSpace &ResultIterSpace,
4615*67e74705SXin Li     llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4616*67e74705SXin Li   // OpenMP [2.6, Canonical Loop Form]
4617*67e74705SXin Li   //   for (init-expr; test-expr; incr-expr) structured-block
4618*67e74705SXin Li   auto For = dyn_cast_or_null<ForStmt>(S);
4619*67e74705SXin Li   if (!For) {
4620*67e74705SXin Li     SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
4621*67e74705SXin Li         << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
4622*67e74705SXin Li         << getOpenMPDirectiveName(DKind) << NestedLoopCount
4623*67e74705SXin Li         << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
4624*67e74705SXin Li     if (NestedLoopCount > 1) {
4625*67e74705SXin Li       if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4626*67e74705SXin Li         SemaRef.Diag(DSA.getConstructLoc(),
4627*67e74705SXin Li                      diag::note_omp_collapse_ordered_expr)
4628*67e74705SXin Li             << 2 << CollapseLoopCountExpr->getSourceRange()
4629*67e74705SXin Li             << OrderedLoopCountExpr->getSourceRange();
4630*67e74705SXin Li       else if (CollapseLoopCountExpr)
4631*67e74705SXin Li         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4632*67e74705SXin Li                      diag::note_omp_collapse_ordered_expr)
4633*67e74705SXin Li             << 0 << CollapseLoopCountExpr->getSourceRange();
4634*67e74705SXin Li       else
4635*67e74705SXin Li         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4636*67e74705SXin Li                      diag::note_omp_collapse_ordered_expr)
4637*67e74705SXin Li             << 1 << OrderedLoopCountExpr->getSourceRange();
4638*67e74705SXin Li     }
4639*67e74705SXin Li     return true;
4640*67e74705SXin Li   }
4641*67e74705SXin Li   assert(For->getBody());
4642*67e74705SXin Li 
4643*67e74705SXin Li   OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4644*67e74705SXin Li 
4645*67e74705SXin Li   // Check init.
4646*67e74705SXin Li   auto Init = For->getInit();
4647*67e74705SXin Li   if (ISC.CheckInit(Init))
4648*67e74705SXin Li     return true;
4649*67e74705SXin Li 
4650*67e74705SXin Li   bool HasErrors = false;
4651*67e74705SXin Li 
4652*67e74705SXin Li   // Check loop variable's type.
4653*67e74705SXin Li   if (auto *LCDecl = ISC.GetLoopDecl()) {
4654*67e74705SXin Li     auto *LoopDeclRefExpr = ISC.GetLoopDeclRefExpr();
4655*67e74705SXin Li 
4656*67e74705SXin Li     // OpenMP [2.6, Canonical Loop Form]
4657*67e74705SXin Li     // Var is one of the following:
4658*67e74705SXin Li     //   A variable of signed or unsigned integer type.
4659*67e74705SXin Li     //   For C++, a variable of a random access iterator type.
4660*67e74705SXin Li     //   For C, a variable of a pointer type.
4661*67e74705SXin Li     auto VarType = LCDecl->getType().getNonReferenceType();
4662*67e74705SXin Li     if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4663*67e74705SXin Li         !VarType->isPointerType() &&
4664*67e74705SXin Li         !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
4665*67e74705SXin Li       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
4666*67e74705SXin Li           << SemaRef.getLangOpts().CPlusPlus;
4667*67e74705SXin Li       HasErrors = true;
4668*67e74705SXin Li     }
4669*67e74705SXin Li 
4670*67e74705SXin Li     // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4671*67e74705SXin Li     // a Construct
4672*67e74705SXin Li     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4673*67e74705SXin Li     // parallel for construct is (are) private.
4674*67e74705SXin Li     // The loop iteration variable in the associated for-loop of a simd
4675*67e74705SXin Li     // construct with just one associated for-loop is linear with a
4676*67e74705SXin Li     // constant-linear-step that is the increment of the associated for-loop.
4677*67e74705SXin Li     // Exclude loop var from the list of variables with implicitly defined data
4678*67e74705SXin Li     // sharing attributes.
4679*67e74705SXin Li     VarsWithImplicitDSA.erase(LCDecl);
4680*67e74705SXin Li 
4681*67e74705SXin Li     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4682*67e74705SXin Li     // in a Construct, C/C++].
4683*67e74705SXin Li     // The loop iteration variable in the associated for-loop of a simd
4684*67e74705SXin Li     // construct with just one associated for-loop may be listed in a linear
4685*67e74705SXin Li     // clause with a constant-linear-step that is the increment of the
4686*67e74705SXin Li     // associated for-loop.
4687*67e74705SXin Li     // The loop iteration variable(s) in the associated for-loop(s) of a for or
4688*67e74705SXin Li     // parallel for construct may be listed in a private or lastprivate clause.
4689*67e74705SXin Li     DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4690*67e74705SXin Li     // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4691*67e74705SXin Li     // declared in the loop and it is predetermined as a private.
4692*67e74705SXin Li     auto PredeterminedCKind =
4693*67e74705SXin Li         isOpenMPSimdDirective(DKind)
4694*67e74705SXin Li             ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4695*67e74705SXin Li             : OMPC_private;
4696*67e74705SXin Li     if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4697*67e74705SXin Li           DVar.CKind != PredeterminedCKind) ||
4698*67e74705SXin Li          ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4699*67e74705SXin Li            isOpenMPDistributeDirective(DKind)) &&
4700*67e74705SXin Li           !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4701*67e74705SXin Li           DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4702*67e74705SXin Li         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4703*67e74705SXin Li       SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
4704*67e74705SXin Li           << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4705*67e74705SXin Li           << getOpenMPClauseName(PredeterminedCKind);
4706*67e74705SXin Li       if (DVar.RefExpr == nullptr)
4707*67e74705SXin Li         DVar.CKind = PredeterminedCKind;
4708*67e74705SXin Li       ReportOriginalDSA(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
4709*67e74705SXin Li       HasErrors = true;
4710*67e74705SXin Li     } else if (LoopDeclRefExpr != nullptr) {
4711*67e74705SXin Li       // Make the loop iteration variable private (for worksharing constructs),
4712*67e74705SXin Li       // linear (for simd directives with the only one associated loop) or
4713*67e74705SXin Li       // lastprivate (for simd directives with several collapsed or ordered
4714*67e74705SXin Li       // loops).
4715*67e74705SXin Li       if (DVar.CKind == OMPC_unknown)
4716*67e74705SXin Li         DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4717*67e74705SXin Li                           [](OpenMPDirectiveKind) -> bool { return true; },
4718*67e74705SXin Li                           /*FromParent=*/false);
4719*67e74705SXin Li       DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4720*67e74705SXin Li     }
4721*67e74705SXin Li 
4722*67e74705SXin Li     assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4723*67e74705SXin Li 
4724*67e74705SXin Li     // Check test-expr.
4725*67e74705SXin Li     HasErrors |= ISC.CheckCond(For->getCond());
4726*67e74705SXin Li 
4727*67e74705SXin Li     // Check incr-expr.
4728*67e74705SXin Li     HasErrors |= ISC.CheckInc(For->getInc());
4729*67e74705SXin Li   }
4730*67e74705SXin Li 
4731*67e74705SXin Li   if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
4732*67e74705SXin Li     return HasErrors;
4733*67e74705SXin Li 
4734*67e74705SXin Li   // Build the loop's iteration space representation.
4735*67e74705SXin Li   ResultIterSpace.PreCond =
4736*67e74705SXin Li       ISC.BuildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4737*67e74705SXin Li   ResultIterSpace.NumIterations = ISC.BuildNumIterations(
4738*67e74705SXin Li       DSA.getCurScope(),
4739*67e74705SXin Li       (isOpenMPWorksharingDirective(DKind) ||
4740*67e74705SXin Li        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4741*67e74705SXin Li       Captures);
4742*67e74705SXin Li   ResultIterSpace.CounterVar = ISC.BuildCounterVar(Captures, DSA);
4743*67e74705SXin Li   ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
4744*67e74705SXin Li   ResultIterSpace.CounterInit = ISC.BuildCounterInit();
4745*67e74705SXin Li   ResultIterSpace.CounterStep = ISC.BuildCounterStep();
4746*67e74705SXin Li   ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
4747*67e74705SXin Li   ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
4748*67e74705SXin Li   ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
4749*67e74705SXin Li   ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
4750*67e74705SXin Li 
4751*67e74705SXin Li   HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4752*67e74705SXin Li                 ResultIterSpace.NumIterations == nullptr ||
4753*67e74705SXin Li                 ResultIterSpace.CounterVar == nullptr ||
4754*67e74705SXin Li                 ResultIterSpace.PrivateCounterVar == nullptr ||
4755*67e74705SXin Li                 ResultIterSpace.CounterInit == nullptr ||
4756*67e74705SXin Li                 ResultIterSpace.CounterStep == nullptr);
4757*67e74705SXin Li 
4758*67e74705SXin Li   return HasErrors;
4759*67e74705SXin Li }
4760*67e74705SXin Li 
4761*67e74705SXin Li /// \brief Build 'VarRef = Start.
4762*67e74705SXin Li static ExprResult
BuildCounterInit(Sema & SemaRef,Scope * S,SourceLocation Loc,ExprResult VarRef,ExprResult Start,llvm::MapVector<Expr *,DeclRefExpr * > & Captures)4763*67e74705SXin Li BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4764*67e74705SXin Li                  ExprResult Start,
4765*67e74705SXin Li                  llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4766*67e74705SXin Li   // Build 'VarRef = Start.
4767*67e74705SXin Li   auto NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
4768*67e74705SXin Li   if (!NewStart.isUsable())
4769*67e74705SXin Li     return ExprError();
4770*67e74705SXin Li   if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
4771*67e74705SXin Li                                    VarRef.get()->getType())) {
4772*67e74705SXin Li     NewStart = SemaRef.PerformImplicitConversion(
4773*67e74705SXin Li         NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4774*67e74705SXin Li         /*AllowExplicit=*/true);
4775*67e74705SXin Li     if (!NewStart.isUsable())
4776*67e74705SXin Li       return ExprError();
4777*67e74705SXin Li   }
4778*67e74705SXin Li 
4779*67e74705SXin Li   auto Init =
4780*67e74705SXin Li       SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4781*67e74705SXin Li   return Init;
4782*67e74705SXin Li }
4783*67e74705SXin Li 
4784*67e74705SXin Li /// \brief Build 'VarRef = Start + Iter * Step'.
4785*67e74705SXin Li static ExprResult
BuildCounterUpdate(Sema & SemaRef,Scope * S,SourceLocation Loc,ExprResult VarRef,ExprResult Start,ExprResult Iter,ExprResult Step,bool Subtract,llvm::MapVector<Expr *,DeclRefExpr * > * Captures=nullptr)4786*67e74705SXin Li BuildCounterUpdate(Sema &SemaRef, Scope *S, SourceLocation Loc,
4787*67e74705SXin Li                    ExprResult VarRef, ExprResult Start, ExprResult Iter,
4788*67e74705SXin Li                    ExprResult Step, bool Subtract,
4789*67e74705SXin Li                    llvm::MapVector<Expr *, DeclRefExpr *> *Captures = nullptr) {
4790*67e74705SXin Li   // Add parentheses (for debugging purposes only).
4791*67e74705SXin Li   Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4792*67e74705SXin Li   if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4793*67e74705SXin Li       !Step.isUsable())
4794*67e74705SXin Li     return ExprError();
4795*67e74705SXin Li 
4796*67e74705SXin Li   ExprResult NewStep = Step;
4797*67e74705SXin Li   if (Captures)
4798*67e74705SXin Li     NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
4799*67e74705SXin Li   if (NewStep.isInvalid())
4800*67e74705SXin Li     return ExprError();
4801*67e74705SXin Li   ExprResult Update =
4802*67e74705SXin Li       SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
4803*67e74705SXin Li   if (!Update.isUsable())
4804*67e74705SXin Li     return ExprError();
4805*67e74705SXin Li 
4806*67e74705SXin Li   // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4807*67e74705SXin Li   // 'VarRef = Start (+|-) Iter * Step'.
4808*67e74705SXin Li   ExprResult NewStart = Start;
4809*67e74705SXin Li   if (Captures)
4810*67e74705SXin Li     NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
4811*67e74705SXin Li   if (NewStart.isInvalid())
4812*67e74705SXin Li     return ExprError();
4813*67e74705SXin Li 
4814*67e74705SXin Li   // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4815*67e74705SXin Li   ExprResult SavedUpdate = Update;
4816*67e74705SXin Li   ExprResult UpdateVal;
4817*67e74705SXin Li   if (VarRef.get()->getType()->isOverloadableType() ||
4818*67e74705SXin Li       NewStart.get()->getType()->isOverloadableType() ||
4819*67e74705SXin Li       Update.get()->getType()->isOverloadableType()) {
4820*67e74705SXin Li     bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4821*67e74705SXin Li     SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4822*67e74705SXin Li     Update =
4823*67e74705SXin Li         SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4824*67e74705SXin Li     if (Update.isUsable()) {
4825*67e74705SXin Li       UpdateVal =
4826*67e74705SXin Li           SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4827*67e74705SXin Li                              VarRef.get(), SavedUpdate.get());
4828*67e74705SXin Li       if (UpdateVal.isUsable()) {
4829*67e74705SXin Li         Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4830*67e74705SXin Li                                             UpdateVal.get());
4831*67e74705SXin Li       }
4832*67e74705SXin Li     }
4833*67e74705SXin Li     SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4834*67e74705SXin Li   }
4835*67e74705SXin Li 
4836*67e74705SXin Li   // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4837*67e74705SXin Li   if (!Update.isUsable() || !UpdateVal.isUsable()) {
4838*67e74705SXin Li     Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4839*67e74705SXin Li                                 NewStart.get(), SavedUpdate.get());
4840*67e74705SXin Li     if (!Update.isUsable())
4841*67e74705SXin Li       return ExprError();
4842*67e74705SXin Li 
4843*67e74705SXin Li     if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4844*67e74705SXin Li                                      VarRef.get()->getType())) {
4845*67e74705SXin Li       Update = SemaRef.PerformImplicitConversion(
4846*67e74705SXin Li           Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4847*67e74705SXin Li       if (!Update.isUsable())
4848*67e74705SXin Li         return ExprError();
4849*67e74705SXin Li     }
4850*67e74705SXin Li 
4851*67e74705SXin Li     Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4852*67e74705SXin Li   }
4853*67e74705SXin Li   return Update;
4854*67e74705SXin Li }
4855*67e74705SXin Li 
4856*67e74705SXin Li /// \brief Convert integer expression \a E to make it have at least \a Bits
4857*67e74705SXin Li /// bits.
WidenIterationCount(unsigned Bits,Expr * E,Sema & SemaRef)4858*67e74705SXin Li static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
4859*67e74705SXin Li                                       Sema &SemaRef) {
4860*67e74705SXin Li   if (E == nullptr)
4861*67e74705SXin Li     return ExprError();
4862*67e74705SXin Li   auto &C = SemaRef.Context;
4863*67e74705SXin Li   QualType OldType = E->getType();
4864*67e74705SXin Li   unsigned HasBits = C.getTypeSize(OldType);
4865*67e74705SXin Li   if (HasBits >= Bits)
4866*67e74705SXin Li     return ExprResult(E);
4867*67e74705SXin Li   // OK to convert to signed, because new type has more bits than old.
4868*67e74705SXin Li   QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4869*67e74705SXin Li   return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4870*67e74705SXin Li                                            true);
4871*67e74705SXin Li }
4872*67e74705SXin Li 
4873*67e74705SXin Li /// \brief Check if the given expression \a E is a constant integer that fits
4874*67e74705SXin Li /// into \a Bits bits.
FitsInto(unsigned Bits,bool Signed,Expr * E,Sema & SemaRef)4875*67e74705SXin Li static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
4876*67e74705SXin Li   if (E == nullptr)
4877*67e74705SXin Li     return false;
4878*67e74705SXin Li   llvm::APSInt Result;
4879*67e74705SXin Li   if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4880*67e74705SXin Li     return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4881*67e74705SXin Li   return false;
4882*67e74705SXin Li }
4883*67e74705SXin Li 
4884*67e74705SXin Li /// Build preinits statement for the given declarations.
buildPreInits(ASTContext & Context,SmallVectorImpl<Decl * > & PreInits)4885*67e74705SXin Li static Stmt *buildPreInits(ASTContext &Context,
4886*67e74705SXin Li                            SmallVectorImpl<Decl *> &PreInits) {
4887*67e74705SXin Li   if (!PreInits.empty()) {
4888*67e74705SXin Li     return new (Context) DeclStmt(
4889*67e74705SXin Li         DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4890*67e74705SXin Li         SourceLocation(), SourceLocation());
4891*67e74705SXin Li   }
4892*67e74705SXin Li   return nullptr;
4893*67e74705SXin Li }
4894*67e74705SXin Li 
4895*67e74705SXin Li /// Build preinits statement for the given declarations.
buildPreInits(ASTContext & Context,llvm::MapVector<Expr *,DeclRefExpr * > & Captures)4896*67e74705SXin Li static Stmt *buildPreInits(ASTContext &Context,
4897*67e74705SXin Li                            llvm::MapVector<Expr *, DeclRefExpr *> &Captures) {
4898*67e74705SXin Li   if (!Captures.empty()) {
4899*67e74705SXin Li     SmallVector<Decl *, 16> PreInits;
4900*67e74705SXin Li     for (auto &Pair : Captures)
4901*67e74705SXin Li       PreInits.push_back(Pair.second->getDecl());
4902*67e74705SXin Li     return buildPreInits(Context, PreInits);
4903*67e74705SXin Li   }
4904*67e74705SXin Li   return nullptr;
4905*67e74705SXin Li }
4906*67e74705SXin Li 
4907*67e74705SXin Li /// Build postupdate expression for the given list of postupdates expressions.
buildPostUpdate(Sema & S,ArrayRef<Expr * > PostUpdates)4908*67e74705SXin Li static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4909*67e74705SXin Li   Expr *PostUpdate = nullptr;
4910*67e74705SXin Li   if (!PostUpdates.empty()) {
4911*67e74705SXin Li     for (auto *E : PostUpdates) {
4912*67e74705SXin Li       Expr *ConvE = S.BuildCStyleCastExpr(
4913*67e74705SXin Li                          E->getExprLoc(),
4914*67e74705SXin Li                          S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4915*67e74705SXin Li                          E->getExprLoc(), E)
4916*67e74705SXin Li                         .get();
4917*67e74705SXin Li       PostUpdate = PostUpdate
4918*67e74705SXin Li                        ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4919*67e74705SXin Li                                               PostUpdate, ConvE)
4920*67e74705SXin Li                              .get()
4921*67e74705SXin Li                        : ConvE;
4922*67e74705SXin Li     }
4923*67e74705SXin Li   }
4924*67e74705SXin Li   return PostUpdate;
4925*67e74705SXin Li }
4926*67e74705SXin Li 
4927*67e74705SXin Li /// \brief Called on a for stmt to check itself and nested loops (if any).
4928*67e74705SXin Li /// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4929*67e74705SXin Li /// number of collapsed loops otherwise.
4930*67e74705SXin Li static unsigned
CheckOpenMPLoop(OpenMPDirectiveKind DKind,Expr * CollapseLoopCountExpr,Expr * OrderedLoopCountExpr,Stmt * AStmt,Sema & SemaRef,DSAStackTy & DSA,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA,OMPLoopDirective::HelperExprs & Built)4931*67e74705SXin Li CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
4932*67e74705SXin Li                 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4933*67e74705SXin Li                 DSAStackTy &DSA,
4934*67e74705SXin Li                 llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA,
4935*67e74705SXin Li                 OMPLoopDirective::HelperExprs &Built) {
4936*67e74705SXin Li   unsigned NestedLoopCount = 1;
4937*67e74705SXin Li   if (CollapseLoopCountExpr) {
4938*67e74705SXin Li     // Found 'collapse' clause - calculate collapse number.
4939*67e74705SXin Li     llvm::APSInt Result;
4940*67e74705SXin Li     if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
4941*67e74705SXin Li       NestedLoopCount = Result.getLimitedValue();
4942*67e74705SXin Li   }
4943*67e74705SXin Li   if (OrderedLoopCountExpr) {
4944*67e74705SXin Li     // Found 'ordered' clause - calculate collapse number.
4945*67e74705SXin Li     llvm::APSInt Result;
4946*67e74705SXin Li     if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4947*67e74705SXin Li       if (Result.getLimitedValue() < NestedLoopCount) {
4948*67e74705SXin Li         SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4949*67e74705SXin Li                      diag::err_omp_wrong_ordered_loop_count)
4950*67e74705SXin Li             << OrderedLoopCountExpr->getSourceRange();
4951*67e74705SXin Li         SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4952*67e74705SXin Li                      diag::note_collapse_loop_count)
4953*67e74705SXin Li             << CollapseLoopCountExpr->getSourceRange();
4954*67e74705SXin Li       }
4955*67e74705SXin Li       NestedLoopCount = Result.getLimitedValue();
4956*67e74705SXin Li     }
4957*67e74705SXin Li   }
4958*67e74705SXin Li   // This is helper routine for loop directives (e.g., 'for', 'simd',
4959*67e74705SXin Li   // 'for simd', etc.).
4960*67e74705SXin Li   llvm::MapVector<Expr *, DeclRefExpr *> Captures;
4961*67e74705SXin Li   SmallVector<LoopIterationSpace, 4> IterSpaces;
4962*67e74705SXin Li   IterSpaces.resize(NestedLoopCount);
4963*67e74705SXin Li   Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
4964*67e74705SXin Li   for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
4965*67e74705SXin Li     if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
4966*67e74705SXin Li                                   NestedLoopCount, CollapseLoopCountExpr,
4967*67e74705SXin Li                                   OrderedLoopCountExpr, VarsWithImplicitDSA,
4968*67e74705SXin Li                                   IterSpaces[Cnt], Captures))
4969*67e74705SXin Li       return 0;
4970*67e74705SXin Li     // Move on to the next nested for loop, or to the loop body.
4971*67e74705SXin Li     // OpenMP [2.8.1, simd construct, Restrictions]
4972*67e74705SXin Li     // All loops associated with the construct must be perfectly nested; that
4973*67e74705SXin Li     // is, there must be no intervening code nor any OpenMP directive between
4974*67e74705SXin Li     // any two loops.
4975*67e74705SXin Li     CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4976*67e74705SXin Li   }
4977*67e74705SXin Li 
4978*67e74705SXin Li   Built.clear(/* size */ NestedLoopCount);
4979*67e74705SXin Li 
4980*67e74705SXin Li   if (SemaRef.CurContext->isDependentContext())
4981*67e74705SXin Li     return NestedLoopCount;
4982*67e74705SXin Li 
4983*67e74705SXin Li   // An example of what is generated for the following code:
4984*67e74705SXin Li   //
4985*67e74705SXin Li   //   #pragma omp simd collapse(2) ordered(2)
4986*67e74705SXin Li   //   for (i = 0; i < NI; ++i)
4987*67e74705SXin Li   //     for (k = 0; k < NK; ++k)
4988*67e74705SXin Li   //       for (j = J0; j < NJ; j+=2) {
4989*67e74705SXin Li   //         <loop body>
4990*67e74705SXin Li   //       }
4991*67e74705SXin Li   //
4992*67e74705SXin Li   // We generate the code below.
4993*67e74705SXin Li   // Note: the loop body may be outlined in CodeGen.
4994*67e74705SXin Li   // Note: some counters may be C++ classes, operator- is used to find number of
4995*67e74705SXin Li   // iterations and operator+= to calculate counter value.
4996*67e74705SXin Li   // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4997*67e74705SXin Li   // or i64 is currently supported).
4998*67e74705SXin Li   //
4999*67e74705SXin Li   //   #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5000*67e74705SXin Li   //   for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5001*67e74705SXin Li   //     .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5002*67e74705SXin Li   //     .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5003*67e74705SXin Li   //     // similar updates for vars in clauses (e.g. 'linear')
5004*67e74705SXin Li   //     <loop body (using local i and j)>
5005*67e74705SXin Li   //   }
5006*67e74705SXin Li   //   i = NI; // assign final values of counters
5007*67e74705SXin Li   //   j = NJ;
5008*67e74705SXin Li   //
5009*67e74705SXin Li 
5010*67e74705SXin Li   // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5011*67e74705SXin Li   // the iteration counts of the collapsed for loops.
5012*67e74705SXin Li   // Precondition tests if there is at least one iteration (all conditions are
5013*67e74705SXin Li   // true).
5014*67e74705SXin Li   auto PreCond = ExprResult(IterSpaces[0].PreCond);
5015*67e74705SXin Li   auto N0 = IterSpaces[0].NumIterations;
5016*67e74705SXin Li   ExprResult LastIteration32 = WidenIterationCount(
5017*67e74705SXin Li       32 /* Bits */, SemaRef.PerformImplicitConversion(
5018*67e74705SXin Li                                 N0->IgnoreImpCasts(), N0->getType(),
5019*67e74705SXin Li                                 Sema::AA_Converting, /*AllowExplicit=*/true)
5020*67e74705SXin Li                          .get(),
5021*67e74705SXin Li       SemaRef);
5022*67e74705SXin Li   ExprResult LastIteration64 = WidenIterationCount(
5023*67e74705SXin Li       64 /* Bits */, SemaRef.PerformImplicitConversion(
5024*67e74705SXin Li                                 N0->IgnoreImpCasts(), N0->getType(),
5025*67e74705SXin Li                                 Sema::AA_Converting, /*AllowExplicit=*/true)
5026*67e74705SXin Li                          .get(),
5027*67e74705SXin Li       SemaRef);
5028*67e74705SXin Li 
5029*67e74705SXin Li   if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5030*67e74705SXin Li     return NestedLoopCount;
5031*67e74705SXin Li 
5032*67e74705SXin Li   auto &C = SemaRef.Context;
5033*67e74705SXin Li   bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5034*67e74705SXin Li 
5035*67e74705SXin Li   Scope *CurScope = DSA.getCurScope();
5036*67e74705SXin Li   for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
5037*67e74705SXin Li     if (PreCond.isUsable()) {
5038*67e74705SXin Li       PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
5039*67e74705SXin Li                                    PreCond.get(), IterSpaces[Cnt].PreCond);
5040*67e74705SXin Li     }
5041*67e74705SXin Li     auto N = IterSpaces[Cnt].NumIterations;
5042*67e74705SXin Li     AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5043*67e74705SXin Li     if (LastIteration32.isUsable())
5044*67e74705SXin Li       LastIteration32 = SemaRef.BuildBinOp(
5045*67e74705SXin Li           CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
5046*67e74705SXin Li           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5047*67e74705SXin Li                                             Sema::AA_Converting,
5048*67e74705SXin Li                                             /*AllowExplicit=*/true)
5049*67e74705SXin Li               .get());
5050*67e74705SXin Li     if (LastIteration64.isUsable())
5051*67e74705SXin Li       LastIteration64 = SemaRef.BuildBinOp(
5052*67e74705SXin Li           CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
5053*67e74705SXin Li           SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5054*67e74705SXin Li                                             Sema::AA_Converting,
5055*67e74705SXin Li                                             /*AllowExplicit=*/true)
5056*67e74705SXin Li               .get());
5057*67e74705SXin Li   }
5058*67e74705SXin Li 
5059*67e74705SXin Li   // Choose either the 32-bit or 64-bit version.
5060*67e74705SXin Li   ExprResult LastIteration = LastIteration64;
5061*67e74705SXin Li   if (LastIteration32.isUsable() &&
5062*67e74705SXin Li       C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5063*67e74705SXin Li       (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5064*67e74705SXin Li        FitsInto(
5065*67e74705SXin Li            32 /* Bits */,
5066*67e74705SXin Li            LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5067*67e74705SXin Li            LastIteration64.get(), SemaRef)))
5068*67e74705SXin Li     LastIteration = LastIteration32;
5069*67e74705SXin Li   QualType VType = LastIteration.get()->getType();
5070*67e74705SXin Li   QualType RealVType = VType;
5071*67e74705SXin Li   QualType StrideVType = VType;
5072*67e74705SXin Li   if (isOpenMPTaskLoopDirective(DKind)) {
5073*67e74705SXin Li     VType =
5074*67e74705SXin Li         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5075*67e74705SXin Li     StrideVType =
5076*67e74705SXin Li         SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5077*67e74705SXin Li   }
5078*67e74705SXin Li 
5079*67e74705SXin Li   if (!LastIteration.isUsable())
5080*67e74705SXin Li     return 0;
5081*67e74705SXin Li 
5082*67e74705SXin Li   // Save the number of iterations.
5083*67e74705SXin Li   ExprResult NumIterations = LastIteration;
5084*67e74705SXin Li   {
5085*67e74705SXin Li     LastIteration = SemaRef.BuildBinOp(
5086*67e74705SXin Li         CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
5087*67e74705SXin Li         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5088*67e74705SXin Li     if (!LastIteration.isUsable())
5089*67e74705SXin Li       return 0;
5090*67e74705SXin Li   }
5091*67e74705SXin Li 
5092*67e74705SXin Li   // Calculate the last iteration number beforehand instead of doing this on
5093*67e74705SXin Li   // each iteration. Do not do this if the number of iterations may be kfold-ed.
5094*67e74705SXin Li   llvm::APSInt Result;
5095*67e74705SXin Li   bool IsConstant =
5096*67e74705SXin Li       LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5097*67e74705SXin Li   ExprResult CalcLastIteration;
5098*67e74705SXin Li   if (!IsConstant) {
5099*67e74705SXin Li     ExprResult SaveRef =
5100*67e74705SXin Li         tryBuildCapture(SemaRef, LastIteration.get(), Captures);
5101*67e74705SXin Li     LastIteration = SaveRef;
5102*67e74705SXin Li 
5103*67e74705SXin Li     // Prepare SaveRef + 1.
5104*67e74705SXin Li     NumIterations = SemaRef.BuildBinOp(
5105*67e74705SXin Li         CurScope, SourceLocation(), BO_Add, SaveRef.get(),
5106*67e74705SXin Li         SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5107*67e74705SXin Li     if (!NumIterations.isUsable())
5108*67e74705SXin Li       return 0;
5109*67e74705SXin Li   }
5110*67e74705SXin Li 
5111*67e74705SXin Li   SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5112*67e74705SXin Li 
5113*67e74705SXin Li   // Build variables passed into runtime, nesessary for worksharing directives.
5114*67e74705SXin Li   ExprResult LB, UB, IL, ST, EUB, PrevLB, PrevUB;
5115*67e74705SXin Li   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5116*67e74705SXin Li       isOpenMPDistributeDirective(DKind)) {
5117*67e74705SXin Li     // Lower bound variable, initialized with zero.
5118*67e74705SXin Li     VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5119*67e74705SXin Li     LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
5120*67e74705SXin Li     SemaRef.AddInitializerToDecl(
5121*67e74705SXin Li         LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5122*67e74705SXin Li         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5123*67e74705SXin Li 
5124*67e74705SXin Li     // Upper bound variable, initialized with last iteration number.
5125*67e74705SXin Li     VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5126*67e74705SXin Li     UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
5127*67e74705SXin Li     SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
5128*67e74705SXin Li                                  /*DirectInit*/ false,
5129*67e74705SXin Li                                  /*TypeMayContainAuto*/ false);
5130*67e74705SXin Li 
5131*67e74705SXin Li     // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5132*67e74705SXin Li     // This will be used to implement clause 'lastprivate'.
5133*67e74705SXin Li     QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
5134*67e74705SXin Li     VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5135*67e74705SXin Li     IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
5136*67e74705SXin Li     SemaRef.AddInitializerToDecl(
5137*67e74705SXin Li         ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5138*67e74705SXin Li         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5139*67e74705SXin Li 
5140*67e74705SXin Li     // Stride variable returned by runtime (we initialize it to 1 by default).
5141*67e74705SXin Li     VarDecl *STDecl =
5142*67e74705SXin Li         buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5143*67e74705SXin Li     ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
5144*67e74705SXin Li     SemaRef.AddInitializerToDecl(
5145*67e74705SXin Li         STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5146*67e74705SXin Li         /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
5147*67e74705SXin Li 
5148*67e74705SXin Li     // Build expression: UB = min(UB, LastIteration)
5149*67e74705SXin Li     // It is nesessary for CodeGen of directives with static scheduling.
5150*67e74705SXin Li     ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5151*67e74705SXin Li                                                 UB.get(), LastIteration.get());
5152*67e74705SXin Li     ExprResult CondOp = SemaRef.ActOnConditionalOp(
5153*67e74705SXin Li         InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
5154*67e74705SXin Li     EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5155*67e74705SXin Li                              CondOp.get());
5156*67e74705SXin Li     EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
5157*67e74705SXin Li 
5158*67e74705SXin Li     // If we have a combined directive that combines 'distribute', 'for' or
5159*67e74705SXin Li     // 'simd' we need to be able to access the bounds of the schedule of the
5160*67e74705SXin Li     // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5161*67e74705SXin Li     // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5162*67e74705SXin Li     if (isOpenMPLoopBoundSharingDirective(DKind)) {
5163*67e74705SXin Li       auto *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
5164*67e74705SXin Li 
5165*67e74705SXin Li       // We expect to have at least 2 more parameters than the 'parallel'
5166*67e74705SXin Li       // directive does - the lower and upper bounds of the previous schedule.
5167*67e74705SXin Li       assert(CD->getNumParams() >= 4 &&
5168*67e74705SXin Li              "Unexpected number of parameters in loop combined directive");
5169*67e74705SXin Li 
5170*67e74705SXin Li       // Set the proper type for the bounds given what we learned from the
5171*67e74705SXin Li       // enclosed loops.
5172*67e74705SXin Li       auto *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5173*67e74705SXin Li       auto *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
5174*67e74705SXin Li 
5175*67e74705SXin Li       // Previous lower and upper bounds are obtained from the region
5176*67e74705SXin Li       // parameters.
5177*67e74705SXin Li       PrevLB =
5178*67e74705SXin Li           buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5179*67e74705SXin Li       PrevUB =
5180*67e74705SXin Li           buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5181*67e74705SXin Li     }
5182*67e74705SXin Li   }
5183*67e74705SXin Li 
5184*67e74705SXin Li   // Build the iteration variable and its initialization before loop.
5185*67e74705SXin Li   ExprResult IV;
5186*67e74705SXin Li   ExprResult Init;
5187*67e74705SXin Li   {
5188*67e74705SXin Li     VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5189*67e74705SXin Li     IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
5190*67e74705SXin Li     Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
5191*67e74705SXin Li                  isOpenMPTaskLoopDirective(DKind) ||
5192*67e74705SXin Li                  isOpenMPDistributeDirective(DKind))
5193*67e74705SXin Li                     ? LB.get()
5194*67e74705SXin Li                     : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5195*67e74705SXin Li     Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5196*67e74705SXin Li     Init = SemaRef.ActOnFinishFullExpr(Init.get());
5197*67e74705SXin Li   }
5198*67e74705SXin Li 
5199*67e74705SXin Li   // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
5200*67e74705SXin Li   SourceLocation CondLoc;
5201*67e74705SXin Li   ExprResult Cond =
5202*67e74705SXin Li       (isOpenMPWorksharingDirective(DKind) ||
5203*67e74705SXin Li        isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5204*67e74705SXin Li           ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5205*67e74705SXin Li           : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5206*67e74705SXin Li                                NumIterations.get());
5207*67e74705SXin Li 
5208*67e74705SXin Li   // Loop increment (IV = IV + 1)
5209*67e74705SXin Li   SourceLocation IncLoc;
5210*67e74705SXin Li   ExprResult Inc =
5211*67e74705SXin Li       SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5212*67e74705SXin Li                          SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5213*67e74705SXin Li   if (!Inc.isUsable())
5214*67e74705SXin Li     return 0;
5215*67e74705SXin Li   Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
5216*67e74705SXin Li   Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5217*67e74705SXin Li   if (!Inc.isUsable())
5218*67e74705SXin Li     return 0;
5219*67e74705SXin Li 
5220*67e74705SXin Li   // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5221*67e74705SXin Li   // Used for directives with static scheduling.
5222*67e74705SXin Li   ExprResult NextLB, NextUB;
5223*67e74705SXin Li   if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5224*67e74705SXin Li       isOpenMPDistributeDirective(DKind)) {
5225*67e74705SXin Li     // LB + ST
5226*67e74705SXin Li     NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5227*67e74705SXin Li     if (!NextLB.isUsable())
5228*67e74705SXin Li       return 0;
5229*67e74705SXin Li     // LB = LB + ST
5230*67e74705SXin Li     NextLB =
5231*67e74705SXin Li         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5232*67e74705SXin Li     NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5233*67e74705SXin Li     if (!NextLB.isUsable())
5234*67e74705SXin Li       return 0;
5235*67e74705SXin Li     // UB + ST
5236*67e74705SXin Li     NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5237*67e74705SXin Li     if (!NextUB.isUsable())
5238*67e74705SXin Li       return 0;
5239*67e74705SXin Li     // UB = UB + ST
5240*67e74705SXin Li     NextUB =
5241*67e74705SXin Li         SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5242*67e74705SXin Li     NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5243*67e74705SXin Li     if (!NextUB.isUsable())
5244*67e74705SXin Li       return 0;
5245*67e74705SXin Li   }
5246*67e74705SXin Li 
5247*67e74705SXin Li   // Build updates and final values of the loop counters.
5248*67e74705SXin Li   bool HasErrors = false;
5249*67e74705SXin Li   Built.Counters.resize(NestedLoopCount);
5250*67e74705SXin Li   Built.Inits.resize(NestedLoopCount);
5251*67e74705SXin Li   Built.Updates.resize(NestedLoopCount);
5252*67e74705SXin Li   Built.Finals.resize(NestedLoopCount);
5253*67e74705SXin Li   SmallVector<Expr *, 4> LoopMultipliers;
5254*67e74705SXin Li   {
5255*67e74705SXin Li     ExprResult Div;
5256*67e74705SXin Li     // Go from inner nested loop to outer.
5257*67e74705SXin Li     for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5258*67e74705SXin Li       LoopIterationSpace &IS = IterSpaces[Cnt];
5259*67e74705SXin Li       SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5260*67e74705SXin Li       // Build: Iter = (IV / Div) % IS.NumIters
5261*67e74705SXin Li       // where Div is product of previous iterations' IS.NumIters.
5262*67e74705SXin Li       ExprResult Iter;
5263*67e74705SXin Li       if (Div.isUsable()) {
5264*67e74705SXin Li         Iter =
5265*67e74705SXin Li             SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5266*67e74705SXin Li       } else {
5267*67e74705SXin Li         Iter = IV;
5268*67e74705SXin Li         assert((Cnt == (int)NestedLoopCount - 1) &&
5269*67e74705SXin Li                "unusable div expected on first iteration only");
5270*67e74705SXin Li       }
5271*67e74705SXin Li 
5272*67e74705SXin Li       if (Cnt != 0 && Iter.isUsable())
5273*67e74705SXin Li         Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5274*67e74705SXin Li                                   IS.NumIterations);
5275*67e74705SXin Li       if (!Iter.isUsable()) {
5276*67e74705SXin Li         HasErrors = true;
5277*67e74705SXin Li         break;
5278*67e74705SXin Li       }
5279*67e74705SXin Li 
5280*67e74705SXin Li       // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
5281*67e74705SXin Li       auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
5282*67e74705SXin Li       auto *CounterVar = buildDeclRefExpr(SemaRef, VD, IS.CounterVar->getType(),
5283*67e74705SXin Li                                           IS.CounterVar->getExprLoc(),
5284*67e74705SXin Li                                           /*RefersToCapture=*/true);
5285*67e74705SXin Li       ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
5286*67e74705SXin Li                                          IS.CounterInit, Captures);
5287*67e74705SXin Li       if (!Init.isUsable()) {
5288*67e74705SXin Li         HasErrors = true;
5289*67e74705SXin Li         break;
5290*67e74705SXin Li       }
5291*67e74705SXin Li       ExprResult Update = BuildCounterUpdate(
5292*67e74705SXin Li           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5293*67e74705SXin Li           IS.CounterStep, IS.Subtract, &Captures);
5294*67e74705SXin Li       if (!Update.isUsable()) {
5295*67e74705SXin Li         HasErrors = true;
5296*67e74705SXin Li         break;
5297*67e74705SXin Li       }
5298*67e74705SXin Li 
5299*67e74705SXin Li       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
5300*67e74705SXin Li       ExprResult Final = BuildCounterUpdate(
5301*67e74705SXin Li           SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
5302*67e74705SXin Li           IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
5303*67e74705SXin Li       if (!Final.isUsable()) {
5304*67e74705SXin Li         HasErrors = true;
5305*67e74705SXin Li         break;
5306*67e74705SXin Li       }
5307*67e74705SXin Li 
5308*67e74705SXin Li       // Build Div for the next iteration: Div <- Div * IS.NumIters
5309*67e74705SXin Li       if (Cnt != 0) {
5310*67e74705SXin Li         if (Div.isUnset())
5311*67e74705SXin Li           Div = IS.NumIterations;
5312*67e74705SXin Li         else
5313*67e74705SXin Li           Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5314*67e74705SXin Li                                    IS.NumIterations);
5315*67e74705SXin Li 
5316*67e74705SXin Li         // Add parentheses (for debugging purposes only).
5317*67e74705SXin Li         if (Div.isUsable())
5318*67e74705SXin Li           Div = tryBuildCapture(SemaRef, Div.get(), Captures);
5319*67e74705SXin Li         if (!Div.isUsable()) {
5320*67e74705SXin Li           HasErrors = true;
5321*67e74705SXin Li           break;
5322*67e74705SXin Li         }
5323*67e74705SXin Li         LoopMultipliers.push_back(Div.get());
5324*67e74705SXin Li       }
5325*67e74705SXin Li       if (!Update.isUsable() || !Final.isUsable()) {
5326*67e74705SXin Li         HasErrors = true;
5327*67e74705SXin Li         break;
5328*67e74705SXin Li       }
5329*67e74705SXin Li       // Save results
5330*67e74705SXin Li       Built.Counters[Cnt] = IS.CounterVar;
5331*67e74705SXin Li       Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
5332*67e74705SXin Li       Built.Inits[Cnt] = Init.get();
5333*67e74705SXin Li       Built.Updates[Cnt] = Update.get();
5334*67e74705SXin Li       Built.Finals[Cnt] = Final.get();
5335*67e74705SXin Li     }
5336*67e74705SXin Li   }
5337*67e74705SXin Li 
5338*67e74705SXin Li   if (HasErrors)
5339*67e74705SXin Li     return 0;
5340*67e74705SXin Li 
5341*67e74705SXin Li   // Save results
5342*67e74705SXin Li   Built.IterationVarRef = IV.get();
5343*67e74705SXin Li   Built.LastIteration = LastIteration.get();
5344*67e74705SXin Li   Built.NumIterations = NumIterations.get();
5345*67e74705SXin Li   Built.CalcLastIteration =
5346*67e74705SXin Li       SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
5347*67e74705SXin Li   Built.PreCond = PreCond.get();
5348*67e74705SXin Li   Built.PreInits = buildPreInits(C, Captures);
5349*67e74705SXin Li   Built.Cond = Cond.get();
5350*67e74705SXin Li   Built.Init = Init.get();
5351*67e74705SXin Li   Built.Inc = Inc.get();
5352*67e74705SXin Li   Built.LB = LB.get();
5353*67e74705SXin Li   Built.UB = UB.get();
5354*67e74705SXin Li   Built.IL = IL.get();
5355*67e74705SXin Li   Built.ST = ST.get();
5356*67e74705SXin Li   Built.EUB = EUB.get();
5357*67e74705SXin Li   Built.NLB = NextLB.get();
5358*67e74705SXin Li   Built.NUB = NextUB.get();
5359*67e74705SXin Li   Built.PrevLB = PrevLB.get();
5360*67e74705SXin Li   Built.PrevUB = PrevUB.get();
5361*67e74705SXin Li 
5362*67e74705SXin Li   Expr *CounterVal = SemaRef.DefaultLvalueConversion(IV.get()).get();
5363*67e74705SXin Li   // Fill data for doacross depend clauses.
5364*67e74705SXin Li   for (auto Pair : DSA.getDoacrossDependClauses()) {
5365*67e74705SXin Li     if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5366*67e74705SXin Li       Pair.first->setCounterValue(CounterVal);
5367*67e74705SXin Li     else {
5368*67e74705SXin Li       if (NestedLoopCount != Pair.second.size() ||
5369*67e74705SXin Li           NestedLoopCount != LoopMultipliers.size() + 1) {
5370*67e74705SXin Li         // Erroneous case - clause has some problems.
5371*67e74705SXin Li         Pair.first->setCounterValue(CounterVal);
5372*67e74705SXin Li         continue;
5373*67e74705SXin Li       }
5374*67e74705SXin Li       assert(Pair.first->getDependencyKind() == OMPC_DEPEND_sink);
5375*67e74705SXin Li       auto I = Pair.second.rbegin();
5376*67e74705SXin Li       auto IS = IterSpaces.rbegin();
5377*67e74705SXin Li       auto ILM = LoopMultipliers.rbegin();
5378*67e74705SXin Li       Expr *UpCounterVal = CounterVal;
5379*67e74705SXin Li       Expr *Multiplier = nullptr;
5380*67e74705SXin Li       for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5381*67e74705SXin Li         if (I->first) {
5382*67e74705SXin Li           assert(IS->CounterStep);
5383*67e74705SXin Li           Expr *NormalizedOffset =
5384*67e74705SXin Li               SemaRef
5385*67e74705SXin Li                   .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Div,
5386*67e74705SXin Li                               I->first, IS->CounterStep)
5387*67e74705SXin Li                   .get();
5388*67e74705SXin Li           if (Multiplier) {
5389*67e74705SXin Li             NormalizedOffset =
5390*67e74705SXin Li                 SemaRef
5391*67e74705SXin Li                     .BuildBinOp(CurScope, I->first->getExprLoc(), BO_Mul,
5392*67e74705SXin Li                                 NormalizedOffset, Multiplier)
5393*67e74705SXin Li                     .get();
5394*67e74705SXin Li           }
5395*67e74705SXin Li           assert(I->second == OO_Plus || I->second == OO_Minus);
5396*67e74705SXin Li           BinaryOperatorKind BOK = (I->second == OO_Plus) ? BO_Add : BO_Sub;
5397*67e74705SXin Li           UpCounterVal =
5398*67e74705SXin Li               SemaRef.BuildBinOp(CurScope, I->first->getExprLoc(), BOK,
5399*67e74705SXin Li                                  UpCounterVal, NormalizedOffset).get();
5400*67e74705SXin Li         }
5401*67e74705SXin Li         Multiplier = *ILM;
5402*67e74705SXin Li         ++I;
5403*67e74705SXin Li         ++IS;
5404*67e74705SXin Li         ++ILM;
5405*67e74705SXin Li       }
5406*67e74705SXin Li       Pair.first->setCounterValue(UpCounterVal);
5407*67e74705SXin Li     }
5408*67e74705SXin Li   }
5409*67e74705SXin Li 
5410*67e74705SXin Li   return NestedLoopCount;
5411*67e74705SXin Li }
5412*67e74705SXin Li 
getCollapseNumberExpr(ArrayRef<OMPClause * > Clauses)5413*67e74705SXin Li static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
5414*67e74705SXin Li   auto CollapseClauses =
5415*67e74705SXin Li       OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5416*67e74705SXin Li   if (CollapseClauses.begin() != CollapseClauses.end())
5417*67e74705SXin Li     return (*CollapseClauses.begin())->getNumForLoops();
5418*67e74705SXin Li   return nullptr;
5419*67e74705SXin Li }
5420*67e74705SXin Li 
getOrderedNumberExpr(ArrayRef<OMPClause * > Clauses)5421*67e74705SXin Li static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
5422*67e74705SXin Li   auto OrderedClauses =
5423*67e74705SXin Li       OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5424*67e74705SXin Li   if (OrderedClauses.begin() != OrderedClauses.end())
5425*67e74705SXin Li     return (*OrderedClauses.begin())->getNumForLoops();
5426*67e74705SXin Li   return nullptr;
5427*67e74705SXin Li }
5428*67e74705SXin Li 
checkSimdlenSafelenValues(Sema & S,const Expr * Simdlen,const Expr * Safelen)5429*67e74705SXin Li static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
5430*67e74705SXin Li                                       const Expr *Safelen) {
5431*67e74705SXin Li   llvm::APSInt SimdlenRes, SafelenRes;
5432*67e74705SXin Li   if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
5433*67e74705SXin Li       Simdlen->isInstantiationDependent() ||
5434*67e74705SXin Li       Simdlen->containsUnexpandedParameterPack())
5435*67e74705SXin Li     return false;
5436*67e74705SXin Li   if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
5437*67e74705SXin Li       Safelen->isInstantiationDependent() ||
5438*67e74705SXin Li       Safelen->containsUnexpandedParameterPack())
5439*67e74705SXin Li     return false;
5440*67e74705SXin Li   Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
5441*67e74705SXin Li   Safelen->EvaluateAsInt(SafelenRes, S.Context);
5442*67e74705SXin Li   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5443*67e74705SXin Li   // If both simdlen and safelen clauses are specified, the value of the simdlen
5444*67e74705SXin Li   // parameter must be less than or equal to the value of the safelen parameter.
5445*67e74705SXin Li   if (SimdlenRes > SafelenRes) {
5446*67e74705SXin Li     S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
5447*67e74705SXin Li         << Simdlen->getSourceRange() << Safelen->getSourceRange();
5448*67e74705SXin Li     return true;
5449*67e74705SXin Li   }
5450*67e74705SXin Li   return false;
5451*67e74705SXin Li }
5452*67e74705SXin Li 
ActOnOpenMPSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)5453*67e74705SXin Li StmtResult Sema::ActOnOpenMPSimdDirective(
5454*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5455*67e74705SXin Li     SourceLocation EndLoc,
5456*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5457*67e74705SXin Li   if (!AStmt)
5458*67e74705SXin Li     return StmtError();
5459*67e74705SXin Li 
5460*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5461*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
5462*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5463*67e74705SXin Li   // define the nested loops number.
5464*67e74705SXin Li   unsigned NestedLoopCount = CheckOpenMPLoop(
5465*67e74705SXin Li       OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5466*67e74705SXin Li       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5467*67e74705SXin Li   if (NestedLoopCount == 0)
5468*67e74705SXin Li     return StmtError();
5469*67e74705SXin Li 
5470*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
5471*67e74705SXin Li          "omp simd loop exprs were not built");
5472*67e74705SXin Li 
5473*67e74705SXin Li   if (!CurContext->isDependentContext()) {
5474*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
5475*67e74705SXin Li     for (auto C : Clauses) {
5476*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
5477*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5478*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
5479*67e74705SXin Li                                      DSAStack))
5480*67e74705SXin Li           return StmtError();
5481*67e74705SXin Li     }
5482*67e74705SXin Li   }
5483*67e74705SXin Li 
5484*67e74705SXin Li   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5485*67e74705SXin Li   // If both simdlen and safelen clauses are specified, the value of the simdlen
5486*67e74705SXin Li   // parameter must be less than or equal to the value of the safelen parameter.
5487*67e74705SXin Li   OMPSafelenClause *Safelen = nullptr;
5488*67e74705SXin Li   OMPSimdlenClause *Simdlen = nullptr;
5489*67e74705SXin Li   for (auto *Clause : Clauses) {
5490*67e74705SXin Li     if (Clause->getClauseKind() == OMPC_safelen)
5491*67e74705SXin Li       Safelen = cast<OMPSafelenClause>(Clause);
5492*67e74705SXin Li     else if (Clause->getClauseKind() == OMPC_simdlen)
5493*67e74705SXin Li       Simdlen = cast<OMPSimdlenClause>(Clause);
5494*67e74705SXin Li     if (Safelen && Simdlen)
5495*67e74705SXin Li       break;
5496*67e74705SXin Li   }
5497*67e74705SXin Li   if (Simdlen && Safelen &&
5498*67e74705SXin Li       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5499*67e74705SXin Li                                 Safelen->getSafelen()))
5500*67e74705SXin Li     return StmtError();
5501*67e74705SXin Li 
5502*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5503*67e74705SXin Li   return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5504*67e74705SXin Li                                   Clauses, AStmt, B);
5505*67e74705SXin Li }
5506*67e74705SXin Li 
ActOnOpenMPForDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)5507*67e74705SXin Li StmtResult Sema::ActOnOpenMPForDirective(
5508*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5509*67e74705SXin Li     SourceLocation EndLoc,
5510*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5511*67e74705SXin Li   if (!AStmt)
5512*67e74705SXin Li     return StmtError();
5513*67e74705SXin Li 
5514*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5515*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
5516*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5517*67e74705SXin Li   // define the nested loops number.
5518*67e74705SXin Li   unsigned NestedLoopCount = CheckOpenMPLoop(
5519*67e74705SXin Li       OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5520*67e74705SXin Li       AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
5521*67e74705SXin Li   if (NestedLoopCount == 0)
5522*67e74705SXin Li     return StmtError();
5523*67e74705SXin Li 
5524*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
5525*67e74705SXin Li          "omp for loop exprs were not built");
5526*67e74705SXin Li 
5527*67e74705SXin Li   if (!CurContext->isDependentContext()) {
5528*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
5529*67e74705SXin Li     for (auto C : Clauses) {
5530*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
5531*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5532*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
5533*67e74705SXin Li                                      DSAStack))
5534*67e74705SXin Li           return StmtError();
5535*67e74705SXin Li     }
5536*67e74705SXin Li   }
5537*67e74705SXin Li 
5538*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5539*67e74705SXin Li   return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5540*67e74705SXin Li                                  Clauses, AStmt, B, DSAStack->isCancelRegion());
5541*67e74705SXin Li }
5542*67e74705SXin Li 
ActOnOpenMPForSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)5543*67e74705SXin Li StmtResult Sema::ActOnOpenMPForSimdDirective(
5544*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5545*67e74705SXin Li     SourceLocation EndLoc,
5546*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5547*67e74705SXin Li   if (!AStmt)
5548*67e74705SXin Li     return StmtError();
5549*67e74705SXin Li 
5550*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5551*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
5552*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5553*67e74705SXin Li   // define the nested loops number.
5554*67e74705SXin Li   unsigned NestedLoopCount =
5555*67e74705SXin Li       CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
5556*67e74705SXin Li                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5557*67e74705SXin Li                       VarsWithImplicitDSA, B);
5558*67e74705SXin Li   if (NestedLoopCount == 0)
5559*67e74705SXin Li     return StmtError();
5560*67e74705SXin Li 
5561*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
5562*67e74705SXin Li          "omp for simd loop exprs were not built");
5563*67e74705SXin Li 
5564*67e74705SXin Li   if (!CurContext->isDependentContext()) {
5565*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
5566*67e74705SXin Li     for (auto C : Clauses) {
5567*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
5568*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5569*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
5570*67e74705SXin Li                                      DSAStack))
5571*67e74705SXin Li           return StmtError();
5572*67e74705SXin Li     }
5573*67e74705SXin Li   }
5574*67e74705SXin Li 
5575*67e74705SXin Li   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5576*67e74705SXin Li   // If both simdlen and safelen clauses are specified, the value of the simdlen
5577*67e74705SXin Li   // parameter must be less than or equal to the value of the safelen parameter.
5578*67e74705SXin Li   OMPSafelenClause *Safelen = nullptr;
5579*67e74705SXin Li   OMPSimdlenClause *Simdlen = nullptr;
5580*67e74705SXin Li   for (auto *Clause : Clauses) {
5581*67e74705SXin Li     if (Clause->getClauseKind() == OMPC_safelen)
5582*67e74705SXin Li       Safelen = cast<OMPSafelenClause>(Clause);
5583*67e74705SXin Li     else if (Clause->getClauseKind() == OMPC_simdlen)
5584*67e74705SXin Li       Simdlen = cast<OMPSimdlenClause>(Clause);
5585*67e74705SXin Li     if (Safelen && Simdlen)
5586*67e74705SXin Li       break;
5587*67e74705SXin Li   }
5588*67e74705SXin Li   if (Simdlen && Safelen &&
5589*67e74705SXin Li       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5590*67e74705SXin Li                                 Safelen->getSafelen()))
5591*67e74705SXin Li     return StmtError();
5592*67e74705SXin Li 
5593*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5594*67e74705SXin Li   return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5595*67e74705SXin Li                                      Clauses, AStmt, B);
5596*67e74705SXin Li }
5597*67e74705SXin Li 
ActOnOpenMPSectionsDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5598*67e74705SXin Li StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5599*67e74705SXin Li                                               Stmt *AStmt,
5600*67e74705SXin Li                                               SourceLocation StartLoc,
5601*67e74705SXin Li                                               SourceLocation EndLoc) {
5602*67e74705SXin Li   if (!AStmt)
5603*67e74705SXin Li     return StmtError();
5604*67e74705SXin Li 
5605*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5606*67e74705SXin Li   auto BaseStmt = AStmt;
5607*67e74705SXin Li   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5608*67e74705SXin Li     BaseStmt = CS->getCapturedStmt();
5609*67e74705SXin Li   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5610*67e74705SXin Li     auto S = C->children();
5611*67e74705SXin Li     if (S.begin() == S.end())
5612*67e74705SXin Li       return StmtError();
5613*67e74705SXin Li     // All associated statements must be '#pragma omp section' except for
5614*67e74705SXin Li     // the first one.
5615*67e74705SXin Li     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5616*67e74705SXin Li       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5617*67e74705SXin Li         if (SectionStmt)
5618*67e74705SXin Li           Diag(SectionStmt->getLocStart(),
5619*67e74705SXin Li                diag::err_omp_sections_substmt_not_section);
5620*67e74705SXin Li         return StmtError();
5621*67e74705SXin Li       }
5622*67e74705SXin Li       cast<OMPSectionDirective>(SectionStmt)
5623*67e74705SXin Li           ->setHasCancel(DSAStack->isCancelRegion());
5624*67e74705SXin Li     }
5625*67e74705SXin Li   } else {
5626*67e74705SXin Li     Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
5627*67e74705SXin Li     return StmtError();
5628*67e74705SXin Li   }
5629*67e74705SXin Li 
5630*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5631*67e74705SXin Li 
5632*67e74705SXin Li   return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5633*67e74705SXin Li                                       DSAStack->isCancelRegion());
5634*67e74705SXin Li }
5635*67e74705SXin Li 
ActOnOpenMPSectionDirective(Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5636*67e74705SXin Li StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5637*67e74705SXin Li                                              SourceLocation StartLoc,
5638*67e74705SXin Li                                              SourceLocation EndLoc) {
5639*67e74705SXin Li   if (!AStmt)
5640*67e74705SXin Li     return StmtError();
5641*67e74705SXin Li 
5642*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5643*67e74705SXin Li 
5644*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5645*67e74705SXin Li   DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
5646*67e74705SXin Li 
5647*67e74705SXin Li   return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5648*67e74705SXin Li                                      DSAStack->isCancelRegion());
5649*67e74705SXin Li }
5650*67e74705SXin Li 
ActOnOpenMPSingleDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5651*67e74705SXin Li StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5652*67e74705SXin Li                                             Stmt *AStmt,
5653*67e74705SXin Li                                             SourceLocation StartLoc,
5654*67e74705SXin Li                                             SourceLocation EndLoc) {
5655*67e74705SXin Li   if (!AStmt)
5656*67e74705SXin Li     return StmtError();
5657*67e74705SXin Li 
5658*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5659*67e74705SXin Li 
5660*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5661*67e74705SXin Li 
5662*67e74705SXin Li   // OpenMP [2.7.3, single Construct, Restrictions]
5663*67e74705SXin Li   // The copyprivate clause must not be used with the nowait clause.
5664*67e74705SXin Li   OMPClause *Nowait = nullptr;
5665*67e74705SXin Li   OMPClause *Copyprivate = nullptr;
5666*67e74705SXin Li   for (auto *Clause : Clauses) {
5667*67e74705SXin Li     if (Clause->getClauseKind() == OMPC_nowait)
5668*67e74705SXin Li       Nowait = Clause;
5669*67e74705SXin Li     else if (Clause->getClauseKind() == OMPC_copyprivate)
5670*67e74705SXin Li       Copyprivate = Clause;
5671*67e74705SXin Li     if (Copyprivate && Nowait) {
5672*67e74705SXin Li       Diag(Copyprivate->getLocStart(),
5673*67e74705SXin Li            diag::err_omp_single_copyprivate_with_nowait);
5674*67e74705SXin Li       Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
5675*67e74705SXin Li       return StmtError();
5676*67e74705SXin Li     }
5677*67e74705SXin Li   }
5678*67e74705SXin Li 
5679*67e74705SXin Li   return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5680*67e74705SXin Li }
5681*67e74705SXin Li 
ActOnOpenMPMasterDirective(Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5682*67e74705SXin Li StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5683*67e74705SXin Li                                             SourceLocation StartLoc,
5684*67e74705SXin Li                                             SourceLocation EndLoc) {
5685*67e74705SXin Li   if (!AStmt)
5686*67e74705SXin Li     return StmtError();
5687*67e74705SXin Li 
5688*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5689*67e74705SXin Li 
5690*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5691*67e74705SXin Li 
5692*67e74705SXin Li   return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5693*67e74705SXin Li }
5694*67e74705SXin Li 
ActOnOpenMPCriticalDirective(const DeclarationNameInfo & DirName,ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5695*67e74705SXin Li StmtResult Sema::ActOnOpenMPCriticalDirective(
5696*67e74705SXin Li     const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5697*67e74705SXin Li     Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
5698*67e74705SXin Li   if (!AStmt)
5699*67e74705SXin Li     return StmtError();
5700*67e74705SXin Li 
5701*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5702*67e74705SXin Li 
5703*67e74705SXin Li   bool ErrorFound = false;
5704*67e74705SXin Li   llvm::APSInt Hint;
5705*67e74705SXin Li   SourceLocation HintLoc;
5706*67e74705SXin Li   bool DependentHint = false;
5707*67e74705SXin Li   for (auto *C : Clauses) {
5708*67e74705SXin Li     if (C->getClauseKind() == OMPC_hint) {
5709*67e74705SXin Li       if (!DirName.getName()) {
5710*67e74705SXin Li         Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
5711*67e74705SXin Li         ErrorFound = true;
5712*67e74705SXin Li       }
5713*67e74705SXin Li       Expr *E = cast<OMPHintClause>(C)->getHint();
5714*67e74705SXin Li       if (E->isTypeDependent() || E->isValueDependent() ||
5715*67e74705SXin Li           E->isInstantiationDependent())
5716*67e74705SXin Li         DependentHint = true;
5717*67e74705SXin Li       else {
5718*67e74705SXin Li         Hint = E->EvaluateKnownConstInt(Context);
5719*67e74705SXin Li         HintLoc = C->getLocStart();
5720*67e74705SXin Li       }
5721*67e74705SXin Li     }
5722*67e74705SXin Li   }
5723*67e74705SXin Li   if (ErrorFound)
5724*67e74705SXin Li     return StmtError();
5725*67e74705SXin Li   auto Pair = DSAStack->getCriticalWithHint(DirName);
5726*67e74705SXin Li   if (Pair.first && DirName.getName() && !DependentHint) {
5727*67e74705SXin Li     if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5728*67e74705SXin Li       Diag(StartLoc, diag::err_omp_critical_with_hint);
5729*67e74705SXin Li       if (HintLoc.isValid()) {
5730*67e74705SXin Li         Diag(HintLoc, diag::note_omp_critical_hint_here)
5731*67e74705SXin Li             << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
5732*67e74705SXin Li       } else
5733*67e74705SXin Li         Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
5734*67e74705SXin Li       if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
5735*67e74705SXin Li         Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
5736*67e74705SXin Li             << 1
5737*67e74705SXin Li             << C->getHint()->EvaluateKnownConstInt(Context).toString(
5738*67e74705SXin Li                    /*Radix=*/10, /*Signed=*/false);
5739*67e74705SXin Li       } else
5740*67e74705SXin Li         Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
5741*67e74705SXin Li     }
5742*67e74705SXin Li   }
5743*67e74705SXin Li 
5744*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5745*67e74705SXin Li 
5746*67e74705SXin Li   auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5747*67e74705SXin Li                                            Clauses, AStmt);
5748*67e74705SXin Li   if (!Pair.first && DirName.getName() && !DependentHint)
5749*67e74705SXin Li     DSAStack->addCriticalWithHint(Dir, Hint);
5750*67e74705SXin Li   return Dir;
5751*67e74705SXin Li }
5752*67e74705SXin Li 
ActOnOpenMPParallelForDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)5753*67e74705SXin Li StmtResult Sema::ActOnOpenMPParallelForDirective(
5754*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5755*67e74705SXin Li     SourceLocation EndLoc,
5756*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5757*67e74705SXin Li   if (!AStmt)
5758*67e74705SXin Li     return StmtError();
5759*67e74705SXin Li 
5760*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5761*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
5762*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
5763*67e74705SXin Li   // top and a single exit at the bottom.
5764*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
5765*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
5766*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
5767*67e74705SXin Li 
5768*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
5769*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5770*67e74705SXin Li   // define the nested loops number.
5771*67e74705SXin Li   unsigned NestedLoopCount =
5772*67e74705SXin Li       CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
5773*67e74705SXin Li                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5774*67e74705SXin Li                       VarsWithImplicitDSA, B);
5775*67e74705SXin Li   if (NestedLoopCount == 0)
5776*67e74705SXin Li     return StmtError();
5777*67e74705SXin Li 
5778*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
5779*67e74705SXin Li          "omp parallel for loop exprs were not built");
5780*67e74705SXin Li 
5781*67e74705SXin Li   if (!CurContext->isDependentContext()) {
5782*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
5783*67e74705SXin Li     for (auto C : Clauses) {
5784*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
5785*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5786*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
5787*67e74705SXin Li                                      DSAStack))
5788*67e74705SXin Li           return StmtError();
5789*67e74705SXin Li     }
5790*67e74705SXin Li   }
5791*67e74705SXin Li 
5792*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5793*67e74705SXin Li   return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
5794*67e74705SXin Li                                          NestedLoopCount, Clauses, AStmt, B,
5795*67e74705SXin Li                                          DSAStack->isCancelRegion());
5796*67e74705SXin Li }
5797*67e74705SXin Li 
ActOnOpenMPParallelForSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)5798*67e74705SXin Li StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5799*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5800*67e74705SXin Li     SourceLocation EndLoc,
5801*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
5802*67e74705SXin Li   if (!AStmt)
5803*67e74705SXin Li     return StmtError();
5804*67e74705SXin Li 
5805*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5806*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
5807*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
5808*67e74705SXin Li   // top and a single exit at the bottom.
5809*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
5810*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
5811*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
5812*67e74705SXin Li 
5813*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
5814*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5815*67e74705SXin Li   // define the nested loops number.
5816*67e74705SXin Li   unsigned NestedLoopCount =
5817*67e74705SXin Li       CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
5818*67e74705SXin Li                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5819*67e74705SXin Li                       VarsWithImplicitDSA, B);
5820*67e74705SXin Li   if (NestedLoopCount == 0)
5821*67e74705SXin Li     return StmtError();
5822*67e74705SXin Li 
5823*67e74705SXin Li   if (!CurContext->isDependentContext()) {
5824*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
5825*67e74705SXin Li     for (auto C : Clauses) {
5826*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
5827*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
5828*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
5829*67e74705SXin Li                                      DSAStack))
5830*67e74705SXin Li           return StmtError();
5831*67e74705SXin Li     }
5832*67e74705SXin Li   }
5833*67e74705SXin Li 
5834*67e74705SXin Li   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
5835*67e74705SXin Li   // If both simdlen and safelen clauses are specified, the value of the simdlen
5836*67e74705SXin Li   // parameter must be less than or equal to the value of the safelen parameter.
5837*67e74705SXin Li   OMPSafelenClause *Safelen = nullptr;
5838*67e74705SXin Li   OMPSimdlenClause *Simdlen = nullptr;
5839*67e74705SXin Li   for (auto *Clause : Clauses) {
5840*67e74705SXin Li     if (Clause->getClauseKind() == OMPC_safelen)
5841*67e74705SXin Li       Safelen = cast<OMPSafelenClause>(Clause);
5842*67e74705SXin Li     else if (Clause->getClauseKind() == OMPC_simdlen)
5843*67e74705SXin Li       Simdlen = cast<OMPSimdlenClause>(Clause);
5844*67e74705SXin Li     if (Safelen && Simdlen)
5845*67e74705SXin Li       break;
5846*67e74705SXin Li   }
5847*67e74705SXin Li   if (Simdlen && Safelen &&
5848*67e74705SXin Li       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
5849*67e74705SXin Li                                 Safelen->getSafelen()))
5850*67e74705SXin Li     return StmtError();
5851*67e74705SXin Li 
5852*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5853*67e74705SXin Li   return OMPParallelForSimdDirective::Create(
5854*67e74705SXin Li       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
5855*67e74705SXin Li }
5856*67e74705SXin Li 
5857*67e74705SXin Li StmtResult
ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5858*67e74705SXin Li Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5859*67e74705SXin Li                                            Stmt *AStmt, SourceLocation StartLoc,
5860*67e74705SXin Li                                            SourceLocation EndLoc) {
5861*67e74705SXin Li   if (!AStmt)
5862*67e74705SXin Li     return StmtError();
5863*67e74705SXin Li 
5864*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5865*67e74705SXin Li   auto BaseStmt = AStmt;
5866*67e74705SXin Li   while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
5867*67e74705SXin Li     BaseStmt = CS->getCapturedStmt();
5868*67e74705SXin Li   if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
5869*67e74705SXin Li     auto S = C->children();
5870*67e74705SXin Li     if (S.begin() == S.end())
5871*67e74705SXin Li       return StmtError();
5872*67e74705SXin Li     // All associated statements must be '#pragma omp section' except for
5873*67e74705SXin Li     // the first one.
5874*67e74705SXin Li     for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
5875*67e74705SXin Li       if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5876*67e74705SXin Li         if (SectionStmt)
5877*67e74705SXin Li           Diag(SectionStmt->getLocStart(),
5878*67e74705SXin Li                diag::err_omp_parallel_sections_substmt_not_section);
5879*67e74705SXin Li         return StmtError();
5880*67e74705SXin Li       }
5881*67e74705SXin Li       cast<OMPSectionDirective>(SectionStmt)
5882*67e74705SXin Li           ->setHasCancel(DSAStack->isCancelRegion());
5883*67e74705SXin Li     }
5884*67e74705SXin Li   } else {
5885*67e74705SXin Li     Diag(AStmt->getLocStart(),
5886*67e74705SXin Li          diag::err_omp_parallel_sections_not_compound_stmt);
5887*67e74705SXin Li     return StmtError();
5888*67e74705SXin Li   }
5889*67e74705SXin Li 
5890*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5891*67e74705SXin Li 
5892*67e74705SXin Li   return OMPParallelSectionsDirective::Create(
5893*67e74705SXin Li       Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
5894*67e74705SXin Li }
5895*67e74705SXin Li 
ActOnOpenMPTaskDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5896*67e74705SXin Li StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5897*67e74705SXin Li                                           Stmt *AStmt, SourceLocation StartLoc,
5898*67e74705SXin Li                                           SourceLocation EndLoc) {
5899*67e74705SXin Li   if (!AStmt)
5900*67e74705SXin Li     return StmtError();
5901*67e74705SXin Li 
5902*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5903*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
5904*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
5905*67e74705SXin Li   // top and a single exit at the bottom.
5906*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
5907*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
5908*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
5909*67e74705SXin Li 
5910*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5911*67e74705SXin Li 
5912*67e74705SXin Li   return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5913*67e74705SXin Li                                   DSAStack->isCancelRegion());
5914*67e74705SXin Li }
5915*67e74705SXin Li 
ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,SourceLocation EndLoc)5916*67e74705SXin Li StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5917*67e74705SXin Li                                                SourceLocation EndLoc) {
5918*67e74705SXin Li   return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5919*67e74705SXin Li }
5920*67e74705SXin Li 
ActOnOpenMPBarrierDirective(SourceLocation StartLoc,SourceLocation EndLoc)5921*67e74705SXin Li StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5922*67e74705SXin Li                                              SourceLocation EndLoc) {
5923*67e74705SXin Li   return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5924*67e74705SXin Li }
5925*67e74705SXin Li 
ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,SourceLocation EndLoc)5926*67e74705SXin Li StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5927*67e74705SXin Li                                               SourceLocation EndLoc) {
5928*67e74705SXin Li   return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5929*67e74705SXin Li }
5930*67e74705SXin Li 
ActOnOpenMPTaskgroupDirective(Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5931*67e74705SXin Li StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
5932*67e74705SXin Li                                                SourceLocation StartLoc,
5933*67e74705SXin Li                                                SourceLocation EndLoc) {
5934*67e74705SXin Li   if (!AStmt)
5935*67e74705SXin Li     return StmtError();
5936*67e74705SXin Li 
5937*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5938*67e74705SXin Li 
5939*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
5940*67e74705SXin Li 
5941*67e74705SXin Li   return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
5942*67e74705SXin Li }
5943*67e74705SXin Li 
ActOnOpenMPFlushDirective(ArrayRef<OMPClause * > Clauses,SourceLocation StartLoc,SourceLocation EndLoc)5944*67e74705SXin Li StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5945*67e74705SXin Li                                            SourceLocation StartLoc,
5946*67e74705SXin Li                                            SourceLocation EndLoc) {
5947*67e74705SXin Li   assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5948*67e74705SXin Li   return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5949*67e74705SXin Li }
5950*67e74705SXin Li 
ActOnOpenMPOrderedDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)5951*67e74705SXin Li StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5952*67e74705SXin Li                                              Stmt *AStmt,
5953*67e74705SXin Li                                              SourceLocation StartLoc,
5954*67e74705SXin Li                                              SourceLocation EndLoc) {
5955*67e74705SXin Li   OMPClause *DependFound = nullptr;
5956*67e74705SXin Li   OMPClause *DependSourceClause = nullptr;
5957*67e74705SXin Li   OMPClause *DependSinkClause = nullptr;
5958*67e74705SXin Li   bool ErrorFound = false;
5959*67e74705SXin Li   OMPThreadsClause *TC = nullptr;
5960*67e74705SXin Li   OMPSIMDClause *SC = nullptr;
5961*67e74705SXin Li   for (auto *C : Clauses) {
5962*67e74705SXin Li     if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5963*67e74705SXin Li       DependFound = C;
5964*67e74705SXin Li       if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5965*67e74705SXin Li         if (DependSourceClause) {
5966*67e74705SXin Li           Diag(C->getLocStart(), diag::err_omp_more_one_clause)
5967*67e74705SXin Li               << getOpenMPDirectiveName(OMPD_ordered)
5968*67e74705SXin Li               << getOpenMPClauseName(OMPC_depend) << 2;
5969*67e74705SXin Li           ErrorFound = true;
5970*67e74705SXin Li         } else
5971*67e74705SXin Li           DependSourceClause = C;
5972*67e74705SXin Li         if (DependSinkClause) {
5973*67e74705SXin Li           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5974*67e74705SXin Li               << 0;
5975*67e74705SXin Li           ErrorFound = true;
5976*67e74705SXin Li         }
5977*67e74705SXin Li       } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5978*67e74705SXin Li         if (DependSourceClause) {
5979*67e74705SXin Li           Diag(C->getLocStart(), diag::err_omp_depend_sink_source_not_allowed)
5980*67e74705SXin Li               << 1;
5981*67e74705SXin Li           ErrorFound = true;
5982*67e74705SXin Li         }
5983*67e74705SXin Li         DependSinkClause = C;
5984*67e74705SXin Li       }
5985*67e74705SXin Li     } else if (C->getClauseKind() == OMPC_threads)
5986*67e74705SXin Li       TC = cast<OMPThreadsClause>(C);
5987*67e74705SXin Li     else if (C->getClauseKind() == OMPC_simd)
5988*67e74705SXin Li       SC = cast<OMPSIMDClause>(C);
5989*67e74705SXin Li   }
5990*67e74705SXin Li   if (!ErrorFound && !SC &&
5991*67e74705SXin Li       isOpenMPSimdDirective(DSAStack->getParentDirective())) {
5992*67e74705SXin Li     // OpenMP [2.8.1,simd Construct, Restrictions]
5993*67e74705SXin Li     // An ordered construct with the simd clause is the only OpenMP construct
5994*67e74705SXin Li     // that can appear in the simd region.
5995*67e74705SXin Li     Diag(StartLoc, diag::err_omp_prohibited_region_simd);
5996*67e74705SXin Li     ErrorFound = true;
5997*67e74705SXin Li   } else if (DependFound && (TC || SC)) {
5998*67e74705SXin Li     Diag(DependFound->getLocStart(), diag::err_omp_depend_clause_thread_simd)
5999*67e74705SXin Li         << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6000*67e74705SXin Li     ErrorFound = true;
6001*67e74705SXin Li   } else if (DependFound && !DSAStack->getParentOrderedRegionParam()) {
6002*67e74705SXin Li     Diag(DependFound->getLocStart(),
6003*67e74705SXin Li          diag::err_omp_ordered_directive_without_param);
6004*67e74705SXin Li     ErrorFound = true;
6005*67e74705SXin Li   } else if (TC || Clauses.empty()) {
6006*67e74705SXin Li     if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
6007*67e74705SXin Li       SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
6008*67e74705SXin Li       Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6009*67e74705SXin Li           << (TC != nullptr);
6010*67e74705SXin Li       Diag(Param->getLocStart(), diag::note_omp_ordered_param);
6011*67e74705SXin Li       ErrorFound = true;
6012*67e74705SXin Li     }
6013*67e74705SXin Li   }
6014*67e74705SXin Li   if ((!AStmt && !DependFound) || ErrorFound)
6015*67e74705SXin Li     return StmtError();
6016*67e74705SXin Li 
6017*67e74705SXin Li   if (AStmt) {
6018*67e74705SXin Li     assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6019*67e74705SXin Li 
6020*67e74705SXin Li     getCurFunction()->setHasBranchProtectedScope();
6021*67e74705SXin Li   }
6022*67e74705SXin Li 
6023*67e74705SXin Li   return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6024*67e74705SXin Li }
6025*67e74705SXin Li 
6026*67e74705SXin Li namespace {
6027*67e74705SXin Li /// \brief Helper class for checking expression in 'omp atomic [update]'
6028*67e74705SXin Li /// construct.
6029*67e74705SXin Li class OpenMPAtomicUpdateChecker {
6030*67e74705SXin Li   /// \brief Error results for atomic update expressions.
6031*67e74705SXin Li   enum ExprAnalysisErrorCode {
6032*67e74705SXin Li     /// \brief A statement is not an expression statement.
6033*67e74705SXin Li     NotAnExpression,
6034*67e74705SXin Li     /// \brief Expression is not builtin binary or unary operation.
6035*67e74705SXin Li     NotABinaryOrUnaryExpression,
6036*67e74705SXin Li     /// \brief Unary operation is not post-/pre- increment/decrement operation.
6037*67e74705SXin Li     NotAnUnaryIncDecExpression,
6038*67e74705SXin Li     /// \brief An expression is not of scalar type.
6039*67e74705SXin Li     NotAScalarType,
6040*67e74705SXin Li     /// \brief A binary operation is not an assignment operation.
6041*67e74705SXin Li     NotAnAssignmentOp,
6042*67e74705SXin Li     /// \brief RHS part of the binary operation is not a binary expression.
6043*67e74705SXin Li     NotABinaryExpression,
6044*67e74705SXin Li     /// \brief RHS part is not additive/multiplicative/shift/biwise binary
6045*67e74705SXin Li     /// expression.
6046*67e74705SXin Li     NotABinaryOperator,
6047*67e74705SXin Li     /// \brief RHS binary operation does not have reference to the updated LHS
6048*67e74705SXin Li     /// part.
6049*67e74705SXin Li     NotAnUpdateExpression,
6050*67e74705SXin Li     /// \brief No errors is found.
6051*67e74705SXin Li     NoError
6052*67e74705SXin Li   };
6053*67e74705SXin Li   /// \brief Reference to Sema.
6054*67e74705SXin Li   Sema &SemaRef;
6055*67e74705SXin Li   /// \brief A location for note diagnostics (when error is found).
6056*67e74705SXin Li   SourceLocation NoteLoc;
6057*67e74705SXin Li   /// \brief 'x' lvalue part of the source atomic expression.
6058*67e74705SXin Li   Expr *X;
6059*67e74705SXin Li   /// \brief 'expr' rvalue part of the source atomic expression.
6060*67e74705SXin Li   Expr *E;
6061*67e74705SXin Li   /// \brief Helper expression of the form
6062*67e74705SXin Li   /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6063*67e74705SXin Li   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6064*67e74705SXin Li   Expr *UpdateExpr;
6065*67e74705SXin Li   /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
6066*67e74705SXin Li   /// important for non-associative operations.
6067*67e74705SXin Li   bool IsXLHSInRHSPart;
6068*67e74705SXin Li   BinaryOperatorKind Op;
6069*67e74705SXin Li   SourceLocation OpLoc;
6070*67e74705SXin Li   /// \brief true if the source expression is a postfix unary operation, false
6071*67e74705SXin Li   /// if it is a prefix unary operation.
6072*67e74705SXin Li   bool IsPostfixUpdate;
6073*67e74705SXin Li 
6074*67e74705SXin Li public:
OpenMPAtomicUpdateChecker(Sema & SemaRef)6075*67e74705SXin Li   OpenMPAtomicUpdateChecker(Sema &SemaRef)
6076*67e74705SXin Li       : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
6077*67e74705SXin Li         IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
6078*67e74705SXin Li   /// \brief Check specified statement that it is suitable for 'atomic update'
6079*67e74705SXin Li   /// constructs and extract 'x', 'expr' and Operation from the original
6080*67e74705SXin Li   /// expression. If DiagId and NoteId == 0, then only check is performed
6081*67e74705SXin Li   /// without error notification.
6082*67e74705SXin Li   /// \param DiagId Diagnostic which should be emitted if error is found.
6083*67e74705SXin Li   /// \param NoteId Diagnostic note for the main error message.
6084*67e74705SXin Li   /// \return true if statement is not an update expression, false otherwise.
6085*67e74705SXin Li   bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
6086*67e74705SXin Li   /// \brief Return the 'x' lvalue part of the source atomic expression.
getX() const6087*67e74705SXin Li   Expr *getX() const { return X; }
6088*67e74705SXin Li   /// \brief Return the 'expr' rvalue part of the source atomic expression.
getExpr() const6089*67e74705SXin Li   Expr *getExpr() const { return E; }
6090*67e74705SXin Li   /// \brief Return the update expression used in calculation of the updated
6091*67e74705SXin Li   /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6092*67e74705SXin Li   /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
getUpdateExpr() const6093*67e74705SXin Li   Expr *getUpdateExpr() const { return UpdateExpr; }
6094*67e74705SXin Li   /// \brief Return true if 'x' is LHS in RHS part of full update expression,
6095*67e74705SXin Li   /// false otherwise.
isXLHSInRHSPart() const6096*67e74705SXin Li   bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6097*67e74705SXin Li 
6098*67e74705SXin Li   /// \brief true if the source expression is a postfix unary operation, false
6099*67e74705SXin Li   /// if it is a prefix unary operation.
isPostfixUpdate() const6100*67e74705SXin Li   bool isPostfixUpdate() const { return IsPostfixUpdate; }
6101*67e74705SXin Li 
6102*67e74705SXin Li private:
6103*67e74705SXin Li   bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6104*67e74705SXin Li                             unsigned NoteId = 0);
6105*67e74705SXin Li };
6106*67e74705SXin Li } // namespace
6107*67e74705SXin Li 
checkBinaryOperation(BinaryOperator * AtomicBinOp,unsigned DiagId,unsigned NoteId)6108*67e74705SXin Li bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6109*67e74705SXin Li     BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6110*67e74705SXin Li   ExprAnalysisErrorCode ErrorFound = NoError;
6111*67e74705SXin Li   SourceLocation ErrorLoc, NoteLoc;
6112*67e74705SXin Li   SourceRange ErrorRange, NoteRange;
6113*67e74705SXin Li   // Allowed constructs are:
6114*67e74705SXin Li   //  x = x binop expr;
6115*67e74705SXin Li   //  x = expr binop x;
6116*67e74705SXin Li   if (AtomicBinOp->getOpcode() == BO_Assign) {
6117*67e74705SXin Li     X = AtomicBinOp->getLHS();
6118*67e74705SXin Li     if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
6119*67e74705SXin Li             AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6120*67e74705SXin Li       if (AtomicInnerBinOp->isMultiplicativeOp() ||
6121*67e74705SXin Li           AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6122*67e74705SXin Li           AtomicInnerBinOp->isBitwiseOp()) {
6123*67e74705SXin Li         Op = AtomicInnerBinOp->getOpcode();
6124*67e74705SXin Li         OpLoc = AtomicInnerBinOp->getOperatorLoc();
6125*67e74705SXin Li         auto *LHS = AtomicInnerBinOp->getLHS();
6126*67e74705SXin Li         auto *RHS = AtomicInnerBinOp->getRHS();
6127*67e74705SXin Li         llvm::FoldingSetNodeID XId, LHSId, RHSId;
6128*67e74705SXin Li         X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6129*67e74705SXin Li                                           /*Canonical=*/true);
6130*67e74705SXin Li         LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6131*67e74705SXin Li                                             /*Canonical=*/true);
6132*67e74705SXin Li         RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6133*67e74705SXin Li                                             /*Canonical=*/true);
6134*67e74705SXin Li         if (XId == LHSId) {
6135*67e74705SXin Li           E = RHS;
6136*67e74705SXin Li           IsXLHSInRHSPart = true;
6137*67e74705SXin Li         } else if (XId == RHSId) {
6138*67e74705SXin Li           E = LHS;
6139*67e74705SXin Li           IsXLHSInRHSPart = false;
6140*67e74705SXin Li         } else {
6141*67e74705SXin Li           ErrorLoc = AtomicInnerBinOp->getExprLoc();
6142*67e74705SXin Li           ErrorRange = AtomicInnerBinOp->getSourceRange();
6143*67e74705SXin Li           NoteLoc = X->getExprLoc();
6144*67e74705SXin Li           NoteRange = X->getSourceRange();
6145*67e74705SXin Li           ErrorFound = NotAnUpdateExpression;
6146*67e74705SXin Li         }
6147*67e74705SXin Li       } else {
6148*67e74705SXin Li         ErrorLoc = AtomicInnerBinOp->getExprLoc();
6149*67e74705SXin Li         ErrorRange = AtomicInnerBinOp->getSourceRange();
6150*67e74705SXin Li         NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6151*67e74705SXin Li         NoteRange = SourceRange(NoteLoc, NoteLoc);
6152*67e74705SXin Li         ErrorFound = NotABinaryOperator;
6153*67e74705SXin Li       }
6154*67e74705SXin Li     } else {
6155*67e74705SXin Li       NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6156*67e74705SXin Li       NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6157*67e74705SXin Li       ErrorFound = NotABinaryExpression;
6158*67e74705SXin Li     }
6159*67e74705SXin Li   } else {
6160*67e74705SXin Li     ErrorLoc = AtomicBinOp->getExprLoc();
6161*67e74705SXin Li     ErrorRange = AtomicBinOp->getSourceRange();
6162*67e74705SXin Li     NoteLoc = AtomicBinOp->getOperatorLoc();
6163*67e74705SXin Li     NoteRange = SourceRange(NoteLoc, NoteLoc);
6164*67e74705SXin Li     ErrorFound = NotAnAssignmentOp;
6165*67e74705SXin Li   }
6166*67e74705SXin Li   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6167*67e74705SXin Li     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6168*67e74705SXin Li     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6169*67e74705SXin Li     return true;
6170*67e74705SXin Li   } else if (SemaRef.CurContext->isDependentContext())
6171*67e74705SXin Li     E = X = UpdateExpr = nullptr;
6172*67e74705SXin Li   return ErrorFound != NoError;
6173*67e74705SXin Li }
6174*67e74705SXin Li 
checkStatement(Stmt * S,unsigned DiagId,unsigned NoteId)6175*67e74705SXin Li bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6176*67e74705SXin Li                                                unsigned NoteId) {
6177*67e74705SXin Li   ExprAnalysisErrorCode ErrorFound = NoError;
6178*67e74705SXin Li   SourceLocation ErrorLoc, NoteLoc;
6179*67e74705SXin Li   SourceRange ErrorRange, NoteRange;
6180*67e74705SXin Li   // Allowed constructs are:
6181*67e74705SXin Li   //  x++;
6182*67e74705SXin Li   //  x--;
6183*67e74705SXin Li   //  ++x;
6184*67e74705SXin Li   //  --x;
6185*67e74705SXin Li   //  x binop= expr;
6186*67e74705SXin Li   //  x = x binop expr;
6187*67e74705SXin Li   //  x = expr binop x;
6188*67e74705SXin Li   if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6189*67e74705SXin Li     AtomicBody = AtomicBody->IgnoreParenImpCasts();
6190*67e74705SXin Li     if (AtomicBody->getType()->isScalarType() ||
6191*67e74705SXin Li         AtomicBody->isInstantiationDependent()) {
6192*67e74705SXin Li       if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
6193*67e74705SXin Li               AtomicBody->IgnoreParenImpCasts())) {
6194*67e74705SXin Li         // Check for Compound Assignment Operation
6195*67e74705SXin Li         Op = BinaryOperator::getOpForCompoundAssignment(
6196*67e74705SXin Li             AtomicCompAssignOp->getOpcode());
6197*67e74705SXin Li         OpLoc = AtomicCompAssignOp->getOperatorLoc();
6198*67e74705SXin Li         E = AtomicCompAssignOp->getRHS();
6199*67e74705SXin Li         X = AtomicCompAssignOp->getLHS();
6200*67e74705SXin Li         IsXLHSInRHSPart = true;
6201*67e74705SXin Li       } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6202*67e74705SXin Li                      AtomicBody->IgnoreParenImpCasts())) {
6203*67e74705SXin Li         // Check for Binary Operation
6204*67e74705SXin Li         if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
6205*67e74705SXin Li           return true;
6206*67e74705SXin Li       } else if (auto *AtomicUnaryOp =
6207*67e74705SXin Li                  dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
6208*67e74705SXin Li         // Check for Unary Operation
6209*67e74705SXin Li         if (AtomicUnaryOp->isIncrementDecrementOp()) {
6210*67e74705SXin Li           IsPostfixUpdate = AtomicUnaryOp->isPostfix();
6211*67e74705SXin Li           Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6212*67e74705SXin Li           OpLoc = AtomicUnaryOp->getOperatorLoc();
6213*67e74705SXin Li           X = AtomicUnaryOp->getSubExpr();
6214*67e74705SXin Li           E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6215*67e74705SXin Li           IsXLHSInRHSPart = true;
6216*67e74705SXin Li         } else {
6217*67e74705SXin Li           ErrorFound = NotAnUnaryIncDecExpression;
6218*67e74705SXin Li           ErrorLoc = AtomicUnaryOp->getExprLoc();
6219*67e74705SXin Li           ErrorRange = AtomicUnaryOp->getSourceRange();
6220*67e74705SXin Li           NoteLoc = AtomicUnaryOp->getOperatorLoc();
6221*67e74705SXin Li           NoteRange = SourceRange(NoteLoc, NoteLoc);
6222*67e74705SXin Li         }
6223*67e74705SXin Li       } else if (!AtomicBody->isInstantiationDependent()) {
6224*67e74705SXin Li         ErrorFound = NotABinaryOrUnaryExpression;
6225*67e74705SXin Li         NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6226*67e74705SXin Li         NoteRange = ErrorRange = AtomicBody->getSourceRange();
6227*67e74705SXin Li       }
6228*67e74705SXin Li     } else {
6229*67e74705SXin Li       ErrorFound = NotAScalarType;
6230*67e74705SXin Li       NoteLoc = ErrorLoc = AtomicBody->getLocStart();
6231*67e74705SXin Li       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6232*67e74705SXin Li     }
6233*67e74705SXin Li   } else {
6234*67e74705SXin Li     ErrorFound = NotAnExpression;
6235*67e74705SXin Li     NoteLoc = ErrorLoc = S->getLocStart();
6236*67e74705SXin Li     NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6237*67e74705SXin Li   }
6238*67e74705SXin Li   if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
6239*67e74705SXin Li     SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6240*67e74705SXin Li     SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6241*67e74705SXin Li     return true;
6242*67e74705SXin Li   } else if (SemaRef.CurContext->isDependentContext())
6243*67e74705SXin Li     E = X = UpdateExpr = nullptr;
6244*67e74705SXin Li   if (ErrorFound == NoError && E && X) {
6245*67e74705SXin Li     // Build an update expression of form 'OpaqueValueExpr(x) binop
6246*67e74705SXin Li     // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6247*67e74705SXin Li     // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6248*67e74705SXin Li     auto *OVEX = new (SemaRef.getASTContext())
6249*67e74705SXin Li         OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6250*67e74705SXin Li     auto *OVEExpr = new (SemaRef.getASTContext())
6251*67e74705SXin Li         OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
6252*67e74705SXin Li     auto Update =
6253*67e74705SXin Li         SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6254*67e74705SXin Li                                    IsXLHSInRHSPart ? OVEExpr : OVEX);
6255*67e74705SXin Li     if (Update.isInvalid())
6256*67e74705SXin Li       return true;
6257*67e74705SXin Li     Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6258*67e74705SXin Li                                                Sema::AA_Casting);
6259*67e74705SXin Li     if (Update.isInvalid())
6260*67e74705SXin Li       return true;
6261*67e74705SXin Li     UpdateExpr = Update.get();
6262*67e74705SXin Li   }
6263*67e74705SXin Li   return ErrorFound != NoError;
6264*67e74705SXin Li }
6265*67e74705SXin Li 
ActOnOpenMPAtomicDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)6266*67e74705SXin Li StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6267*67e74705SXin Li                                             Stmt *AStmt,
6268*67e74705SXin Li                                             SourceLocation StartLoc,
6269*67e74705SXin Li                                             SourceLocation EndLoc) {
6270*67e74705SXin Li   if (!AStmt)
6271*67e74705SXin Li     return StmtError();
6272*67e74705SXin Li 
6273*67e74705SXin Li   auto CS = cast<CapturedStmt>(AStmt);
6274*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
6275*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
6276*67e74705SXin Li   // top and a single exit at the bottom.
6277*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
6278*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
6279*67e74705SXin Li   OpenMPClauseKind AtomicKind = OMPC_unknown;
6280*67e74705SXin Li   SourceLocation AtomicKindLoc;
6281*67e74705SXin Li   for (auto *C : Clauses) {
6282*67e74705SXin Li     if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
6283*67e74705SXin Li         C->getClauseKind() == OMPC_update ||
6284*67e74705SXin Li         C->getClauseKind() == OMPC_capture) {
6285*67e74705SXin Li       if (AtomicKind != OMPC_unknown) {
6286*67e74705SXin Li         Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
6287*67e74705SXin Li             << SourceRange(C->getLocStart(), C->getLocEnd());
6288*67e74705SXin Li         Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6289*67e74705SXin Li             << getOpenMPClauseName(AtomicKind);
6290*67e74705SXin Li       } else {
6291*67e74705SXin Li         AtomicKind = C->getClauseKind();
6292*67e74705SXin Li         AtomicKindLoc = C->getLocStart();
6293*67e74705SXin Li       }
6294*67e74705SXin Li     }
6295*67e74705SXin Li   }
6296*67e74705SXin Li 
6297*67e74705SXin Li   auto Body = CS->getCapturedStmt();
6298*67e74705SXin Li   if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6299*67e74705SXin Li     Body = EWC->getSubExpr();
6300*67e74705SXin Li 
6301*67e74705SXin Li   Expr *X = nullptr;
6302*67e74705SXin Li   Expr *V = nullptr;
6303*67e74705SXin Li   Expr *E = nullptr;
6304*67e74705SXin Li   Expr *UE = nullptr;
6305*67e74705SXin Li   bool IsXLHSInRHSPart = false;
6306*67e74705SXin Li   bool IsPostfixUpdate = false;
6307*67e74705SXin Li   // OpenMP [2.12.6, atomic Construct]
6308*67e74705SXin Li   // In the next expressions:
6309*67e74705SXin Li   // * x and v (as applicable) are both l-value expressions with scalar type.
6310*67e74705SXin Li   // * During the execution of an atomic region, multiple syntactic
6311*67e74705SXin Li   // occurrences of x must designate the same storage location.
6312*67e74705SXin Li   // * Neither of v and expr (as applicable) may access the storage location
6313*67e74705SXin Li   // designated by x.
6314*67e74705SXin Li   // * Neither of x and expr (as applicable) may access the storage location
6315*67e74705SXin Li   // designated by v.
6316*67e74705SXin Li   // * expr is an expression with scalar type.
6317*67e74705SXin Li   // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6318*67e74705SXin Li   // * binop, binop=, ++, and -- are not overloaded operators.
6319*67e74705SXin Li   // * The expression x binop expr must be numerically equivalent to x binop
6320*67e74705SXin Li   // (expr). This requirement is satisfied if the operators in expr have
6321*67e74705SXin Li   // precedence greater than binop, or by using parentheses around expr or
6322*67e74705SXin Li   // subexpressions of expr.
6323*67e74705SXin Li   // * The expression expr binop x must be numerically equivalent to (expr)
6324*67e74705SXin Li   // binop x. This requirement is satisfied if the operators in expr have
6325*67e74705SXin Li   // precedence equal to or greater than binop, or by using parentheses around
6326*67e74705SXin Li   // expr or subexpressions of expr.
6327*67e74705SXin Li   // * For forms that allow multiple occurrences of x, the number of times
6328*67e74705SXin Li   // that x is evaluated is unspecified.
6329*67e74705SXin Li   if (AtomicKind == OMPC_read) {
6330*67e74705SXin Li     enum {
6331*67e74705SXin Li       NotAnExpression,
6332*67e74705SXin Li       NotAnAssignmentOp,
6333*67e74705SXin Li       NotAScalarType,
6334*67e74705SXin Li       NotAnLValue,
6335*67e74705SXin Li       NoError
6336*67e74705SXin Li     } ErrorFound = NoError;
6337*67e74705SXin Li     SourceLocation ErrorLoc, NoteLoc;
6338*67e74705SXin Li     SourceRange ErrorRange, NoteRange;
6339*67e74705SXin Li     // If clause is read:
6340*67e74705SXin Li     //  v = x;
6341*67e74705SXin Li     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6342*67e74705SXin Li       auto AtomicBinOp =
6343*67e74705SXin Li           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6344*67e74705SXin Li       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6345*67e74705SXin Li         X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6346*67e74705SXin Li         V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6347*67e74705SXin Li         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6348*67e74705SXin Li             (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6349*67e74705SXin Li           if (!X->isLValue() || !V->isLValue()) {
6350*67e74705SXin Li             auto NotLValueExpr = X->isLValue() ? V : X;
6351*67e74705SXin Li             ErrorFound = NotAnLValue;
6352*67e74705SXin Li             ErrorLoc = AtomicBinOp->getExprLoc();
6353*67e74705SXin Li             ErrorRange = AtomicBinOp->getSourceRange();
6354*67e74705SXin Li             NoteLoc = NotLValueExpr->getExprLoc();
6355*67e74705SXin Li             NoteRange = NotLValueExpr->getSourceRange();
6356*67e74705SXin Li           }
6357*67e74705SXin Li         } else if (!X->isInstantiationDependent() ||
6358*67e74705SXin Li                    !V->isInstantiationDependent()) {
6359*67e74705SXin Li           auto NotScalarExpr =
6360*67e74705SXin Li               (X->isInstantiationDependent() || X->getType()->isScalarType())
6361*67e74705SXin Li                   ? V
6362*67e74705SXin Li                   : X;
6363*67e74705SXin Li           ErrorFound = NotAScalarType;
6364*67e74705SXin Li           ErrorLoc = AtomicBinOp->getExprLoc();
6365*67e74705SXin Li           ErrorRange = AtomicBinOp->getSourceRange();
6366*67e74705SXin Li           NoteLoc = NotScalarExpr->getExprLoc();
6367*67e74705SXin Li           NoteRange = NotScalarExpr->getSourceRange();
6368*67e74705SXin Li         }
6369*67e74705SXin Li       } else if (!AtomicBody->isInstantiationDependent()) {
6370*67e74705SXin Li         ErrorFound = NotAnAssignmentOp;
6371*67e74705SXin Li         ErrorLoc = AtomicBody->getExprLoc();
6372*67e74705SXin Li         ErrorRange = AtomicBody->getSourceRange();
6373*67e74705SXin Li         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6374*67e74705SXin Li                               : AtomicBody->getExprLoc();
6375*67e74705SXin Li         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6376*67e74705SXin Li                                 : AtomicBody->getSourceRange();
6377*67e74705SXin Li       }
6378*67e74705SXin Li     } else {
6379*67e74705SXin Li       ErrorFound = NotAnExpression;
6380*67e74705SXin Li       NoteLoc = ErrorLoc = Body->getLocStart();
6381*67e74705SXin Li       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6382*67e74705SXin Li     }
6383*67e74705SXin Li     if (ErrorFound != NoError) {
6384*67e74705SXin Li       Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6385*67e74705SXin Li           << ErrorRange;
6386*67e74705SXin Li       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6387*67e74705SXin Li                                                       << NoteRange;
6388*67e74705SXin Li       return StmtError();
6389*67e74705SXin Li     } else if (CurContext->isDependentContext())
6390*67e74705SXin Li       V = X = nullptr;
6391*67e74705SXin Li   } else if (AtomicKind == OMPC_write) {
6392*67e74705SXin Li     enum {
6393*67e74705SXin Li       NotAnExpression,
6394*67e74705SXin Li       NotAnAssignmentOp,
6395*67e74705SXin Li       NotAScalarType,
6396*67e74705SXin Li       NotAnLValue,
6397*67e74705SXin Li       NoError
6398*67e74705SXin Li     } ErrorFound = NoError;
6399*67e74705SXin Li     SourceLocation ErrorLoc, NoteLoc;
6400*67e74705SXin Li     SourceRange ErrorRange, NoteRange;
6401*67e74705SXin Li     // If clause is write:
6402*67e74705SXin Li     //  x = expr;
6403*67e74705SXin Li     if (auto AtomicBody = dyn_cast<Expr>(Body)) {
6404*67e74705SXin Li       auto AtomicBinOp =
6405*67e74705SXin Li           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6406*67e74705SXin Li       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6407*67e74705SXin Li         X = AtomicBinOp->getLHS();
6408*67e74705SXin Li         E = AtomicBinOp->getRHS();
6409*67e74705SXin Li         if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6410*67e74705SXin Li             (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6411*67e74705SXin Li           if (!X->isLValue()) {
6412*67e74705SXin Li             ErrorFound = NotAnLValue;
6413*67e74705SXin Li             ErrorLoc = AtomicBinOp->getExprLoc();
6414*67e74705SXin Li             ErrorRange = AtomicBinOp->getSourceRange();
6415*67e74705SXin Li             NoteLoc = X->getExprLoc();
6416*67e74705SXin Li             NoteRange = X->getSourceRange();
6417*67e74705SXin Li           }
6418*67e74705SXin Li         } else if (!X->isInstantiationDependent() ||
6419*67e74705SXin Li                    !E->isInstantiationDependent()) {
6420*67e74705SXin Li           auto NotScalarExpr =
6421*67e74705SXin Li               (X->isInstantiationDependent() || X->getType()->isScalarType())
6422*67e74705SXin Li                   ? E
6423*67e74705SXin Li                   : X;
6424*67e74705SXin Li           ErrorFound = NotAScalarType;
6425*67e74705SXin Li           ErrorLoc = AtomicBinOp->getExprLoc();
6426*67e74705SXin Li           ErrorRange = AtomicBinOp->getSourceRange();
6427*67e74705SXin Li           NoteLoc = NotScalarExpr->getExprLoc();
6428*67e74705SXin Li           NoteRange = NotScalarExpr->getSourceRange();
6429*67e74705SXin Li         }
6430*67e74705SXin Li       } else if (!AtomicBody->isInstantiationDependent()) {
6431*67e74705SXin Li         ErrorFound = NotAnAssignmentOp;
6432*67e74705SXin Li         ErrorLoc = AtomicBody->getExprLoc();
6433*67e74705SXin Li         ErrorRange = AtomicBody->getSourceRange();
6434*67e74705SXin Li         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6435*67e74705SXin Li                               : AtomicBody->getExprLoc();
6436*67e74705SXin Li         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6437*67e74705SXin Li                                 : AtomicBody->getSourceRange();
6438*67e74705SXin Li       }
6439*67e74705SXin Li     } else {
6440*67e74705SXin Li       ErrorFound = NotAnExpression;
6441*67e74705SXin Li       NoteLoc = ErrorLoc = Body->getLocStart();
6442*67e74705SXin Li       NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6443*67e74705SXin Li     }
6444*67e74705SXin Li     if (ErrorFound != NoError) {
6445*67e74705SXin Li       Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6446*67e74705SXin Li           << ErrorRange;
6447*67e74705SXin Li       Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6448*67e74705SXin Li                                                       << NoteRange;
6449*67e74705SXin Li       return StmtError();
6450*67e74705SXin Li     } else if (CurContext->isDependentContext())
6451*67e74705SXin Li       E = X = nullptr;
6452*67e74705SXin Li   } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
6453*67e74705SXin Li     // If clause is update:
6454*67e74705SXin Li     //  x++;
6455*67e74705SXin Li     //  x--;
6456*67e74705SXin Li     //  ++x;
6457*67e74705SXin Li     //  --x;
6458*67e74705SXin Li     //  x binop= expr;
6459*67e74705SXin Li     //  x = x binop expr;
6460*67e74705SXin Li     //  x = expr binop x;
6461*67e74705SXin Li     OpenMPAtomicUpdateChecker Checker(*this);
6462*67e74705SXin Li     if (Checker.checkStatement(
6463*67e74705SXin Li             Body, (AtomicKind == OMPC_update)
6464*67e74705SXin Li                       ? diag::err_omp_atomic_update_not_expression_statement
6465*67e74705SXin Li                       : diag::err_omp_atomic_not_expression_statement,
6466*67e74705SXin Li             diag::note_omp_atomic_update))
6467*67e74705SXin Li       return StmtError();
6468*67e74705SXin Li     if (!CurContext->isDependentContext()) {
6469*67e74705SXin Li       E = Checker.getExpr();
6470*67e74705SXin Li       X = Checker.getX();
6471*67e74705SXin Li       UE = Checker.getUpdateExpr();
6472*67e74705SXin Li       IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6473*67e74705SXin Li     }
6474*67e74705SXin Li   } else if (AtomicKind == OMPC_capture) {
6475*67e74705SXin Li     enum {
6476*67e74705SXin Li       NotAnAssignmentOp,
6477*67e74705SXin Li       NotACompoundStatement,
6478*67e74705SXin Li       NotTwoSubstatements,
6479*67e74705SXin Li       NotASpecificExpression,
6480*67e74705SXin Li       NoError
6481*67e74705SXin Li     } ErrorFound = NoError;
6482*67e74705SXin Li     SourceLocation ErrorLoc, NoteLoc;
6483*67e74705SXin Li     SourceRange ErrorRange, NoteRange;
6484*67e74705SXin Li     if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
6485*67e74705SXin Li       // If clause is a capture:
6486*67e74705SXin Li       //  v = x++;
6487*67e74705SXin Li       //  v = x--;
6488*67e74705SXin Li       //  v = ++x;
6489*67e74705SXin Li       //  v = --x;
6490*67e74705SXin Li       //  v = x binop= expr;
6491*67e74705SXin Li       //  v = x = x binop expr;
6492*67e74705SXin Li       //  v = x = expr binop x;
6493*67e74705SXin Li       auto *AtomicBinOp =
6494*67e74705SXin Li           dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6495*67e74705SXin Li       if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6496*67e74705SXin Li         V = AtomicBinOp->getLHS();
6497*67e74705SXin Li         Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6498*67e74705SXin Li         OpenMPAtomicUpdateChecker Checker(*this);
6499*67e74705SXin Li         if (Checker.checkStatement(
6500*67e74705SXin Li                 Body, diag::err_omp_atomic_capture_not_expression_statement,
6501*67e74705SXin Li                 diag::note_omp_atomic_update))
6502*67e74705SXin Li           return StmtError();
6503*67e74705SXin Li         E = Checker.getExpr();
6504*67e74705SXin Li         X = Checker.getX();
6505*67e74705SXin Li         UE = Checker.getUpdateExpr();
6506*67e74705SXin Li         IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6507*67e74705SXin Li         IsPostfixUpdate = Checker.isPostfixUpdate();
6508*67e74705SXin Li       } else if (!AtomicBody->isInstantiationDependent()) {
6509*67e74705SXin Li         ErrorLoc = AtomicBody->getExprLoc();
6510*67e74705SXin Li         ErrorRange = AtomicBody->getSourceRange();
6511*67e74705SXin Li         NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6512*67e74705SXin Li                               : AtomicBody->getExprLoc();
6513*67e74705SXin Li         NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6514*67e74705SXin Li                                 : AtomicBody->getSourceRange();
6515*67e74705SXin Li         ErrorFound = NotAnAssignmentOp;
6516*67e74705SXin Li       }
6517*67e74705SXin Li       if (ErrorFound != NoError) {
6518*67e74705SXin Li         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6519*67e74705SXin Li             << ErrorRange;
6520*67e74705SXin Li         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6521*67e74705SXin Li         return StmtError();
6522*67e74705SXin Li       } else if (CurContext->isDependentContext()) {
6523*67e74705SXin Li         UE = V = E = X = nullptr;
6524*67e74705SXin Li       }
6525*67e74705SXin Li     } else {
6526*67e74705SXin Li       // If clause is a capture:
6527*67e74705SXin Li       //  { v = x; x = expr; }
6528*67e74705SXin Li       //  { v = x; x++; }
6529*67e74705SXin Li       //  { v = x; x--; }
6530*67e74705SXin Li       //  { v = x; ++x; }
6531*67e74705SXin Li       //  { v = x; --x; }
6532*67e74705SXin Li       //  { v = x; x binop= expr; }
6533*67e74705SXin Li       //  { v = x; x = x binop expr; }
6534*67e74705SXin Li       //  { v = x; x = expr binop x; }
6535*67e74705SXin Li       //  { x++; v = x; }
6536*67e74705SXin Li       //  { x--; v = x; }
6537*67e74705SXin Li       //  { ++x; v = x; }
6538*67e74705SXin Li       //  { --x; v = x; }
6539*67e74705SXin Li       //  { x binop= expr; v = x; }
6540*67e74705SXin Li       //  { x = x binop expr; v = x; }
6541*67e74705SXin Li       //  { x = expr binop x; v = x; }
6542*67e74705SXin Li       if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6543*67e74705SXin Li         // Check that this is { expr1; expr2; }
6544*67e74705SXin Li         if (CS->size() == 2) {
6545*67e74705SXin Li           auto *First = CS->body_front();
6546*67e74705SXin Li           auto *Second = CS->body_back();
6547*67e74705SXin Li           if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6548*67e74705SXin Li             First = EWC->getSubExpr()->IgnoreParenImpCasts();
6549*67e74705SXin Li           if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6550*67e74705SXin Li             Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6551*67e74705SXin Li           // Need to find what subexpression is 'v' and what is 'x'.
6552*67e74705SXin Li           OpenMPAtomicUpdateChecker Checker(*this);
6553*67e74705SXin Li           bool IsUpdateExprFound = !Checker.checkStatement(Second);
6554*67e74705SXin Li           BinaryOperator *BinOp = nullptr;
6555*67e74705SXin Li           if (IsUpdateExprFound) {
6556*67e74705SXin Li             BinOp = dyn_cast<BinaryOperator>(First);
6557*67e74705SXin Li             IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6558*67e74705SXin Li           }
6559*67e74705SXin Li           if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6560*67e74705SXin Li             //  { v = x; x++; }
6561*67e74705SXin Li             //  { v = x; x--; }
6562*67e74705SXin Li             //  { v = x; ++x; }
6563*67e74705SXin Li             //  { v = x; --x; }
6564*67e74705SXin Li             //  { v = x; x binop= expr; }
6565*67e74705SXin Li             //  { v = x; x = x binop expr; }
6566*67e74705SXin Li             //  { v = x; x = expr binop x; }
6567*67e74705SXin Li             // Check that the first expression has form v = x.
6568*67e74705SXin Li             auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6569*67e74705SXin Li             llvm::FoldingSetNodeID XId, PossibleXId;
6570*67e74705SXin Li             Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6571*67e74705SXin Li             PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6572*67e74705SXin Li             IsUpdateExprFound = XId == PossibleXId;
6573*67e74705SXin Li             if (IsUpdateExprFound) {
6574*67e74705SXin Li               V = BinOp->getLHS();
6575*67e74705SXin Li               X = Checker.getX();
6576*67e74705SXin Li               E = Checker.getExpr();
6577*67e74705SXin Li               UE = Checker.getUpdateExpr();
6578*67e74705SXin Li               IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6579*67e74705SXin Li               IsPostfixUpdate = true;
6580*67e74705SXin Li             }
6581*67e74705SXin Li           }
6582*67e74705SXin Li           if (!IsUpdateExprFound) {
6583*67e74705SXin Li             IsUpdateExprFound = !Checker.checkStatement(First);
6584*67e74705SXin Li             BinOp = nullptr;
6585*67e74705SXin Li             if (IsUpdateExprFound) {
6586*67e74705SXin Li               BinOp = dyn_cast<BinaryOperator>(Second);
6587*67e74705SXin Li               IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6588*67e74705SXin Li             }
6589*67e74705SXin Li             if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6590*67e74705SXin Li               //  { x++; v = x; }
6591*67e74705SXin Li               //  { x--; v = x; }
6592*67e74705SXin Li               //  { ++x; v = x; }
6593*67e74705SXin Li               //  { --x; v = x; }
6594*67e74705SXin Li               //  { x binop= expr; v = x; }
6595*67e74705SXin Li               //  { x = x binop expr; v = x; }
6596*67e74705SXin Li               //  { x = expr binop x; v = x; }
6597*67e74705SXin Li               // Check that the second expression has form v = x.
6598*67e74705SXin Li               auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
6599*67e74705SXin Li               llvm::FoldingSetNodeID XId, PossibleXId;
6600*67e74705SXin Li               Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6601*67e74705SXin Li               PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6602*67e74705SXin Li               IsUpdateExprFound = XId == PossibleXId;
6603*67e74705SXin Li               if (IsUpdateExprFound) {
6604*67e74705SXin Li                 V = BinOp->getLHS();
6605*67e74705SXin Li                 X = Checker.getX();
6606*67e74705SXin Li                 E = Checker.getExpr();
6607*67e74705SXin Li                 UE = Checker.getUpdateExpr();
6608*67e74705SXin Li                 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6609*67e74705SXin Li                 IsPostfixUpdate = false;
6610*67e74705SXin Li               }
6611*67e74705SXin Li             }
6612*67e74705SXin Li           }
6613*67e74705SXin Li           if (!IsUpdateExprFound) {
6614*67e74705SXin Li             //  { v = x; x = expr; }
6615*67e74705SXin Li             auto *FirstExpr = dyn_cast<Expr>(First);
6616*67e74705SXin Li             auto *SecondExpr = dyn_cast<Expr>(Second);
6617*67e74705SXin Li             if (!FirstExpr || !SecondExpr ||
6618*67e74705SXin Li                 !(FirstExpr->isInstantiationDependent() ||
6619*67e74705SXin Li                   SecondExpr->isInstantiationDependent())) {
6620*67e74705SXin Li               auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6621*67e74705SXin Li               if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
6622*67e74705SXin Li                 ErrorFound = NotAnAssignmentOp;
6623*67e74705SXin Li                 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
6624*67e74705SXin Li                                                 : First->getLocStart();
6625*67e74705SXin Li                 NoteRange = ErrorRange = FirstBinOp
6626*67e74705SXin Li                                              ? FirstBinOp->getSourceRange()
6627*67e74705SXin Li                                              : SourceRange(ErrorLoc, ErrorLoc);
6628*67e74705SXin Li               } else {
6629*67e74705SXin Li                 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6630*67e74705SXin Li                 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6631*67e74705SXin Li                   ErrorFound = NotAnAssignmentOp;
6632*67e74705SXin Li                   NoteLoc = ErrorLoc = SecondBinOp
6633*67e74705SXin Li                                            ? SecondBinOp->getOperatorLoc()
6634*67e74705SXin Li                                            : Second->getLocStart();
6635*67e74705SXin Li                   NoteRange = ErrorRange =
6636*67e74705SXin Li                       SecondBinOp ? SecondBinOp->getSourceRange()
6637*67e74705SXin Li                                   : SourceRange(ErrorLoc, ErrorLoc);
6638*67e74705SXin Li                 } else {
6639*67e74705SXin Li                   auto *PossibleXRHSInFirst =
6640*67e74705SXin Li                       FirstBinOp->getRHS()->IgnoreParenImpCasts();
6641*67e74705SXin Li                   auto *PossibleXLHSInSecond =
6642*67e74705SXin Li                       SecondBinOp->getLHS()->IgnoreParenImpCasts();
6643*67e74705SXin Li                   llvm::FoldingSetNodeID X1Id, X2Id;
6644*67e74705SXin Li                   PossibleXRHSInFirst->Profile(X1Id, Context,
6645*67e74705SXin Li                                                /*Canonical=*/true);
6646*67e74705SXin Li                   PossibleXLHSInSecond->Profile(X2Id, Context,
6647*67e74705SXin Li                                                 /*Canonical=*/true);
6648*67e74705SXin Li                   IsUpdateExprFound = X1Id == X2Id;
6649*67e74705SXin Li                   if (IsUpdateExprFound) {
6650*67e74705SXin Li                     V = FirstBinOp->getLHS();
6651*67e74705SXin Li                     X = SecondBinOp->getLHS();
6652*67e74705SXin Li                     E = SecondBinOp->getRHS();
6653*67e74705SXin Li                     UE = nullptr;
6654*67e74705SXin Li                     IsXLHSInRHSPart = false;
6655*67e74705SXin Li                     IsPostfixUpdate = true;
6656*67e74705SXin Li                   } else {
6657*67e74705SXin Li                     ErrorFound = NotASpecificExpression;
6658*67e74705SXin Li                     ErrorLoc = FirstBinOp->getExprLoc();
6659*67e74705SXin Li                     ErrorRange = FirstBinOp->getSourceRange();
6660*67e74705SXin Li                     NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6661*67e74705SXin Li                     NoteRange = SecondBinOp->getRHS()->getSourceRange();
6662*67e74705SXin Li                   }
6663*67e74705SXin Li                 }
6664*67e74705SXin Li               }
6665*67e74705SXin Li             }
6666*67e74705SXin Li           }
6667*67e74705SXin Li         } else {
6668*67e74705SXin Li           NoteLoc = ErrorLoc = Body->getLocStart();
6669*67e74705SXin Li           NoteRange = ErrorRange =
6670*67e74705SXin Li               SourceRange(Body->getLocStart(), Body->getLocStart());
6671*67e74705SXin Li           ErrorFound = NotTwoSubstatements;
6672*67e74705SXin Li         }
6673*67e74705SXin Li       } else {
6674*67e74705SXin Li         NoteLoc = ErrorLoc = Body->getLocStart();
6675*67e74705SXin Li         NoteRange = ErrorRange =
6676*67e74705SXin Li             SourceRange(Body->getLocStart(), Body->getLocStart());
6677*67e74705SXin Li         ErrorFound = NotACompoundStatement;
6678*67e74705SXin Li       }
6679*67e74705SXin Li       if (ErrorFound != NoError) {
6680*67e74705SXin Li         Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6681*67e74705SXin Li             << ErrorRange;
6682*67e74705SXin Li         Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6683*67e74705SXin Li         return StmtError();
6684*67e74705SXin Li       } else if (CurContext->isDependentContext()) {
6685*67e74705SXin Li         UE = V = E = X = nullptr;
6686*67e74705SXin Li       }
6687*67e74705SXin Li     }
6688*67e74705SXin Li   }
6689*67e74705SXin Li 
6690*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
6691*67e74705SXin Li 
6692*67e74705SXin Li   return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6693*67e74705SXin Li                                     X, V, E, UE, IsXLHSInRHSPart,
6694*67e74705SXin Li                                     IsPostfixUpdate);
6695*67e74705SXin Li }
6696*67e74705SXin Li 
ActOnOpenMPTargetDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)6697*67e74705SXin Li StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6698*67e74705SXin Li                                             Stmt *AStmt,
6699*67e74705SXin Li                                             SourceLocation StartLoc,
6700*67e74705SXin Li                                             SourceLocation EndLoc) {
6701*67e74705SXin Li   if (!AStmt)
6702*67e74705SXin Li     return StmtError();
6703*67e74705SXin Li 
6704*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6705*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
6706*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
6707*67e74705SXin Li   // top and a single exit at the bottom.
6708*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
6709*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
6710*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
6711*67e74705SXin Li 
6712*67e74705SXin Li   // OpenMP [2.16, Nesting of Regions]
6713*67e74705SXin Li   // If specified, a teams construct must be contained within a target
6714*67e74705SXin Li   // construct. That target construct must contain no statements or directives
6715*67e74705SXin Li   // outside of the teams construct.
6716*67e74705SXin Li   if (DSAStack->hasInnerTeamsRegion()) {
6717*67e74705SXin Li     auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
6718*67e74705SXin Li     bool OMPTeamsFound = true;
6719*67e74705SXin Li     if (auto *CS = dyn_cast<CompoundStmt>(S)) {
6720*67e74705SXin Li       auto I = CS->body_begin();
6721*67e74705SXin Li       while (I != CS->body_end()) {
6722*67e74705SXin Li         auto OED = dyn_cast<OMPExecutableDirective>(*I);
6723*67e74705SXin Li         if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6724*67e74705SXin Li           OMPTeamsFound = false;
6725*67e74705SXin Li           break;
6726*67e74705SXin Li         }
6727*67e74705SXin Li         ++I;
6728*67e74705SXin Li       }
6729*67e74705SXin Li       assert(I != CS->body_end() && "Not found statement");
6730*67e74705SXin Li       S = *I;
6731*67e74705SXin Li     } else {
6732*67e74705SXin Li       auto *OED = dyn_cast<OMPExecutableDirective>(S);
6733*67e74705SXin Li       OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
6734*67e74705SXin Li     }
6735*67e74705SXin Li     if (!OMPTeamsFound) {
6736*67e74705SXin Li       Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6737*67e74705SXin Li       Diag(DSAStack->getInnerTeamsRegionLoc(),
6738*67e74705SXin Li            diag::note_omp_nested_teams_construct_here);
6739*67e74705SXin Li       Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
6740*67e74705SXin Li           << isa<OMPExecutableDirective>(S);
6741*67e74705SXin Li       return StmtError();
6742*67e74705SXin Li     }
6743*67e74705SXin Li   }
6744*67e74705SXin Li 
6745*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
6746*67e74705SXin Li 
6747*67e74705SXin Li   return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6748*67e74705SXin Li }
6749*67e74705SXin Li 
6750*67e74705SXin Li StmtResult
ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)6751*67e74705SXin Li Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6752*67e74705SXin Li                                          Stmt *AStmt, SourceLocation StartLoc,
6753*67e74705SXin Li                                          SourceLocation EndLoc) {
6754*67e74705SXin Li   if (!AStmt)
6755*67e74705SXin Li     return StmtError();
6756*67e74705SXin Li 
6757*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6758*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
6759*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
6760*67e74705SXin Li   // top and a single exit at the bottom.
6761*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
6762*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
6763*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
6764*67e74705SXin Li 
6765*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
6766*67e74705SXin Li 
6767*67e74705SXin Li   return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6768*67e74705SXin Li                                             AStmt);
6769*67e74705SXin Li }
6770*67e74705SXin Li 
ActOnOpenMPTargetParallelForDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)6771*67e74705SXin Li StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6772*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6773*67e74705SXin Li     SourceLocation EndLoc,
6774*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6775*67e74705SXin Li   if (!AStmt)
6776*67e74705SXin Li     return StmtError();
6777*67e74705SXin Li 
6778*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6779*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
6780*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
6781*67e74705SXin Li   // top and a single exit at the bottom.
6782*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
6783*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
6784*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
6785*67e74705SXin Li 
6786*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
6787*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6788*67e74705SXin Li   // define the nested loops number.
6789*67e74705SXin Li   unsigned NestedLoopCount =
6790*67e74705SXin Li       CheckOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
6791*67e74705SXin Li                       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6792*67e74705SXin Li                       VarsWithImplicitDSA, B);
6793*67e74705SXin Li   if (NestedLoopCount == 0)
6794*67e74705SXin Li     return StmtError();
6795*67e74705SXin Li 
6796*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
6797*67e74705SXin Li          "omp target parallel for loop exprs were not built");
6798*67e74705SXin Li 
6799*67e74705SXin Li   if (!CurContext->isDependentContext()) {
6800*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
6801*67e74705SXin Li     for (auto C : Clauses) {
6802*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
6803*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
6804*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
6805*67e74705SXin Li                                      DSAStack))
6806*67e74705SXin Li           return StmtError();
6807*67e74705SXin Li     }
6808*67e74705SXin Li   }
6809*67e74705SXin Li 
6810*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
6811*67e74705SXin Li   return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6812*67e74705SXin Li                                                NestedLoopCount, Clauses, AStmt,
6813*67e74705SXin Li                                                B, DSAStack->isCancelRegion());
6814*67e74705SXin Li }
6815*67e74705SXin Li 
6816*67e74705SXin Li /// \brief Check for existence of a map clause in the list of clauses.
HasMapClause(ArrayRef<OMPClause * > Clauses)6817*67e74705SXin Li static bool HasMapClause(ArrayRef<OMPClause *> Clauses) {
6818*67e74705SXin Li   for (ArrayRef<OMPClause *>::iterator I = Clauses.begin(), E = Clauses.end();
6819*67e74705SXin Li        I != E; ++I) {
6820*67e74705SXin Li     if (*I != nullptr && (*I)->getClauseKind() == OMPC_map) {
6821*67e74705SXin Li       return true;
6822*67e74705SXin Li     }
6823*67e74705SXin Li   }
6824*67e74705SXin Li 
6825*67e74705SXin Li   return false;
6826*67e74705SXin Li }
6827*67e74705SXin Li 
ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)6828*67e74705SXin Li StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6829*67e74705SXin Li                                                 Stmt *AStmt,
6830*67e74705SXin Li                                                 SourceLocation StartLoc,
6831*67e74705SXin Li                                                 SourceLocation EndLoc) {
6832*67e74705SXin Li   if (!AStmt)
6833*67e74705SXin Li     return StmtError();
6834*67e74705SXin Li 
6835*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6836*67e74705SXin Li 
6837*67e74705SXin Li   // OpenMP [2.10.1, Restrictions, p. 97]
6838*67e74705SXin Li   // At least one map clause must appear on the directive.
6839*67e74705SXin Li   if (!HasMapClause(Clauses)) {
6840*67e74705SXin Li     Diag(StartLoc, diag::err_omp_no_map_for_directive) <<
6841*67e74705SXin Li         getOpenMPDirectiveName(OMPD_target_data);
6842*67e74705SXin Li     return StmtError();
6843*67e74705SXin Li   }
6844*67e74705SXin Li 
6845*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
6846*67e74705SXin Li 
6847*67e74705SXin Li   return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6848*67e74705SXin Li                                         AStmt);
6849*67e74705SXin Li }
6850*67e74705SXin Li 
6851*67e74705SXin Li StmtResult
ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause * > Clauses,SourceLocation StartLoc,SourceLocation EndLoc)6852*67e74705SXin Li Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6853*67e74705SXin Li                                           SourceLocation StartLoc,
6854*67e74705SXin Li                                           SourceLocation EndLoc) {
6855*67e74705SXin Li   // OpenMP [2.10.2, Restrictions, p. 99]
6856*67e74705SXin Li   // At least one map clause must appear on the directive.
6857*67e74705SXin Li   if (!HasMapClause(Clauses)) {
6858*67e74705SXin Li     Diag(StartLoc, diag::err_omp_no_map_for_directive)
6859*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_target_enter_data);
6860*67e74705SXin Li     return StmtError();
6861*67e74705SXin Li   }
6862*67e74705SXin Li 
6863*67e74705SXin Li   return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc,
6864*67e74705SXin Li                                              Clauses);
6865*67e74705SXin Li }
6866*67e74705SXin Li 
6867*67e74705SXin Li StmtResult
ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause * > Clauses,SourceLocation StartLoc,SourceLocation EndLoc)6868*67e74705SXin Li Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6869*67e74705SXin Li                                          SourceLocation StartLoc,
6870*67e74705SXin Li                                          SourceLocation EndLoc) {
6871*67e74705SXin Li   // OpenMP [2.10.3, Restrictions, p. 102]
6872*67e74705SXin Li   // At least one map clause must appear on the directive.
6873*67e74705SXin Li   if (!HasMapClause(Clauses)) {
6874*67e74705SXin Li     Diag(StartLoc, diag::err_omp_no_map_for_directive)
6875*67e74705SXin Li         << getOpenMPDirectiveName(OMPD_target_exit_data);
6876*67e74705SXin Li     return StmtError();
6877*67e74705SXin Li   }
6878*67e74705SXin Li 
6879*67e74705SXin Li   return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses);
6880*67e74705SXin Li }
6881*67e74705SXin Li 
ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause * > Clauses,SourceLocation StartLoc,SourceLocation EndLoc)6882*67e74705SXin Li StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6883*67e74705SXin Li                                                   SourceLocation StartLoc,
6884*67e74705SXin Li                                                   SourceLocation EndLoc) {
6885*67e74705SXin Li   bool seenMotionClause = false;
6886*67e74705SXin Li   for (auto *C : Clauses) {
6887*67e74705SXin Li     if (C->getClauseKind() == OMPC_to || C->getClauseKind() == OMPC_from)
6888*67e74705SXin Li       seenMotionClause = true;
6889*67e74705SXin Li   }
6890*67e74705SXin Li   if (!seenMotionClause) {
6891*67e74705SXin Li     Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
6892*67e74705SXin Li     return StmtError();
6893*67e74705SXin Li   }
6894*67e74705SXin Li   return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses);
6895*67e74705SXin Li }
6896*67e74705SXin Li 
ActOnOpenMPTeamsDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc)6897*67e74705SXin Li StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
6898*67e74705SXin Li                                            Stmt *AStmt, SourceLocation StartLoc,
6899*67e74705SXin Li                                            SourceLocation EndLoc) {
6900*67e74705SXin Li   if (!AStmt)
6901*67e74705SXin Li     return StmtError();
6902*67e74705SXin Li 
6903*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
6904*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
6905*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
6906*67e74705SXin Li   // top and a single exit at the bottom.
6907*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
6908*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
6909*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
6910*67e74705SXin Li 
6911*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
6912*67e74705SXin Li 
6913*67e74705SXin Li   return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6914*67e74705SXin Li }
6915*67e74705SXin Li 
6916*67e74705SXin Li StmtResult
ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,SourceLocation EndLoc,OpenMPDirectiveKind CancelRegion)6917*67e74705SXin Li Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
6918*67e74705SXin Li                                             SourceLocation EndLoc,
6919*67e74705SXin Li                                             OpenMPDirectiveKind CancelRegion) {
6920*67e74705SXin Li   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6921*67e74705SXin Li       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6922*67e74705SXin Li     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6923*67e74705SXin Li         << getOpenMPDirectiveName(CancelRegion);
6924*67e74705SXin Li     return StmtError();
6925*67e74705SXin Li   }
6926*67e74705SXin Li   if (DSAStack->isParentNowaitRegion()) {
6927*67e74705SXin Li     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
6928*67e74705SXin Li     return StmtError();
6929*67e74705SXin Li   }
6930*67e74705SXin Li   if (DSAStack->isParentOrderedRegion()) {
6931*67e74705SXin Li     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
6932*67e74705SXin Li     return StmtError();
6933*67e74705SXin Li   }
6934*67e74705SXin Li   return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
6935*67e74705SXin Li                                                CancelRegion);
6936*67e74705SXin Li }
6937*67e74705SXin Li 
ActOnOpenMPCancelDirective(ArrayRef<OMPClause * > Clauses,SourceLocation StartLoc,SourceLocation EndLoc,OpenMPDirectiveKind CancelRegion)6938*67e74705SXin Li StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
6939*67e74705SXin Li                                             SourceLocation StartLoc,
6940*67e74705SXin Li                                             SourceLocation EndLoc,
6941*67e74705SXin Li                                             OpenMPDirectiveKind CancelRegion) {
6942*67e74705SXin Li   if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
6943*67e74705SXin Li       CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
6944*67e74705SXin Li     Diag(StartLoc, diag::err_omp_wrong_cancel_region)
6945*67e74705SXin Li         << getOpenMPDirectiveName(CancelRegion);
6946*67e74705SXin Li     return StmtError();
6947*67e74705SXin Li   }
6948*67e74705SXin Li   if (DSAStack->isParentNowaitRegion()) {
6949*67e74705SXin Li     Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
6950*67e74705SXin Li     return StmtError();
6951*67e74705SXin Li   }
6952*67e74705SXin Li   if (DSAStack->isParentOrderedRegion()) {
6953*67e74705SXin Li     Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
6954*67e74705SXin Li     return StmtError();
6955*67e74705SXin Li   }
6956*67e74705SXin Li   DSAStack->setParentCancelRegion(/*Cancel=*/true);
6957*67e74705SXin Li   return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6958*67e74705SXin Li                                     CancelRegion);
6959*67e74705SXin Li }
6960*67e74705SXin Li 
checkGrainsizeNumTasksClauses(Sema & S,ArrayRef<OMPClause * > Clauses)6961*67e74705SXin Li static bool checkGrainsizeNumTasksClauses(Sema &S,
6962*67e74705SXin Li                                           ArrayRef<OMPClause *> Clauses) {
6963*67e74705SXin Li   OMPClause *PrevClause = nullptr;
6964*67e74705SXin Li   bool ErrorFound = false;
6965*67e74705SXin Li   for (auto *C : Clauses) {
6966*67e74705SXin Li     if (C->getClauseKind() == OMPC_grainsize ||
6967*67e74705SXin Li         C->getClauseKind() == OMPC_num_tasks) {
6968*67e74705SXin Li       if (!PrevClause)
6969*67e74705SXin Li         PrevClause = C;
6970*67e74705SXin Li       else if (PrevClause->getClauseKind() != C->getClauseKind()) {
6971*67e74705SXin Li         S.Diag(C->getLocStart(),
6972*67e74705SXin Li                diag::err_omp_grainsize_num_tasks_mutually_exclusive)
6973*67e74705SXin Li             << getOpenMPClauseName(C->getClauseKind())
6974*67e74705SXin Li             << getOpenMPClauseName(PrevClause->getClauseKind());
6975*67e74705SXin Li         S.Diag(PrevClause->getLocStart(),
6976*67e74705SXin Li                diag::note_omp_previous_grainsize_num_tasks)
6977*67e74705SXin Li             << getOpenMPClauseName(PrevClause->getClauseKind());
6978*67e74705SXin Li         ErrorFound = true;
6979*67e74705SXin Li       }
6980*67e74705SXin Li     }
6981*67e74705SXin Li   }
6982*67e74705SXin Li   return ErrorFound;
6983*67e74705SXin Li }
6984*67e74705SXin Li 
ActOnOpenMPTaskLoopDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)6985*67e74705SXin Li StmtResult Sema::ActOnOpenMPTaskLoopDirective(
6986*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
6987*67e74705SXin Li     SourceLocation EndLoc,
6988*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
6989*67e74705SXin Li   if (!AStmt)
6990*67e74705SXin Li     return StmtError();
6991*67e74705SXin Li 
6992*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6993*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
6994*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6995*67e74705SXin Li   // define the nested loops number.
6996*67e74705SXin Li   unsigned NestedLoopCount =
6997*67e74705SXin Li       CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
6998*67e74705SXin Li                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
6999*67e74705SXin Li                       VarsWithImplicitDSA, B);
7000*67e74705SXin Li   if (NestedLoopCount == 0)
7001*67e74705SXin Li     return StmtError();
7002*67e74705SXin Li 
7003*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
7004*67e74705SXin Li          "omp for loop exprs were not built");
7005*67e74705SXin Li 
7006*67e74705SXin Li   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7007*67e74705SXin Li   // The grainsize clause and num_tasks clause are mutually exclusive and may
7008*67e74705SXin Li   // not appear on the same taskloop directive.
7009*67e74705SXin Li   if (checkGrainsizeNumTasksClauses(*this, Clauses))
7010*67e74705SXin Li     return StmtError();
7011*67e74705SXin Li 
7012*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
7013*67e74705SXin Li   return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7014*67e74705SXin Li                                       NestedLoopCount, Clauses, AStmt, B);
7015*67e74705SXin Li }
7016*67e74705SXin Li 
ActOnOpenMPTaskLoopSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)7017*67e74705SXin Li StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7018*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7019*67e74705SXin Li     SourceLocation EndLoc,
7020*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7021*67e74705SXin Li   if (!AStmt)
7022*67e74705SXin Li     return StmtError();
7023*67e74705SXin Li 
7024*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7025*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
7026*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7027*67e74705SXin Li   // define the nested loops number.
7028*67e74705SXin Li   unsigned NestedLoopCount =
7029*67e74705SXin Li       CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
7030*67e74705SXin Li                       /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7031*67e74705SXin Li                       VarsWithImplicitDSA, B);
7032*67e74705SXin Li   if (NestedLoopCount == 0)
7033*67e74705SXin Li     return StmtError();
7034*67e74705SXin Li 
7035*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
7036*67e74705SXin Li          "omp for loop exprs were not built");
7037*67e74705SXin Li 
7038*67e74705SXin Li   if (!CurContext->isDependentContext()) {
7039*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
7040*67e74705SXin Li     for (auto C : Clauses) {
7041*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
7042*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7043*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
7044*67e74705SXin Li                                      DSAStack))
7045*67e74705SXin Li           return StmtError();
7046*67e74705SXin Li     }
7047*67e74705SXin Li   }
7048*67e74705SXin Li 
7049*67e74705SXin Li   // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7050*67e74705SXin Li   // The grainsize clause and num_tasks clause are mutually exclusive and may
7051*67e74705SXin Li   // not appear on the same taskloop directive.
7052*67e74705SXin Li   if (checkGrainsizeNumTasksClauses(*this, Clauses))
7053*67e74705SXin Li     return StmtError();
7054*67e74705SXin Li 
7055*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
7056*67e74705SXin Li   return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7057*67e74705SXin Li                                           NestedLoopCount, Clauses, AStmt, B);
7058*67e74705SXin Li }
7059*67e74705SXin Li 
ActOnOpenMPDistributeDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)7060*67e74705SXin Li StmtResult Sema::ActOnOpenMPDistributeDirective(
7061*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7062*67e74705SXin Li     SourceLocation EndLoc,
7063*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7064*67e74705SXin Li   if (!AStmt)
7065*67e74705SXin Li     return StmtError();
7066*67e74705SXin Li 
7067*67e74705SXin Li   assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7068*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
7069*67e74705SXin Li   // In presence of clause 'collapse' with number of loops, it will
7070*67e74705SXin Li   // define the nested loops number.
7071*67e74705SXin Li   unsigned NestedLoopCount =
7072*67e74705SXin Li       CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
7073*67e74705SXin Li                       nullptr /*ordered not a clause on distribute*/, AStmt,
7074*67e74705SXin Li                       *this, *DSAStack, VarsWithImplicitDSA, B);
7075*67e74705SXin Li   if (NestedLoopCount == 0)
7076*67e74705SXin Li     return StmtError();
7077*67e74705SXin Li 
7078*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
7079*67e74705SXin Li          "omp for loop exprs were not built");
7080*67e74705SXin Li 
7081*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
7082*67e74705SXin Li   return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7083*67e74705SXin Li                                         NestedLoopCount, Clauses, AStmt, B);
7084*67e74705SXin Li }
7085*67e74705SXin Li 
ActOnOpenMPDistributeParallelForDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)7086*67e74705SXin Li StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7087*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7088*67e74705SXin Li     SourceLocation EndLoc,
7089*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7090*67e74705SXin Li   if (!AStmt)
7091*67e74705SXin Li     return StmtError();
7092*67e74705SXin Li 
7093*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7094*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
7095*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
7096*67e74705SXin Li   // top and a single exit at the bottom.
7097*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
7098*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
7099*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
7100*67e74705SXin Li 
7101*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
7102*67e74705SXin Li   // In presence of clause 'collapse' with number of loops, it will
7103*67e74705SXin Li   // define the nested loops number.
7104*67e74705SXin Li   unsigned NestedLoopCount = CheckOpenMPLoop(
7105*67e74705SXin Li       OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7106*67e74705SXin Li       nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7107*67e74705SXin Li       VarsWithImplicitDSA, B);
7108*67e74705SXin Li   if (NestedLoopCount == 0)
7109*67e74705SXin Li     return StmtError();
7110*67e74705SXin Li 
7111*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
7112*67e74705SXin Li          "omp for loop exprs were not built");
7113*67e74705SXin Li 
7114*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
7115*67e74705SXin Li   return OMPDistributeParallelForDirective::Create(
7116*67e74705SXin Li       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7117*67e74705SXin Li }
7118*67e74705SXin Li 
ActOnOpenMPDistributeParallelForSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)7119*67e74705SXin Li StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7120*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7121*67e74705SXin Li     SourceLocation EndLoc,
7122*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7123*67e74705SXin Li   if (!AStmt)
7124*67e74705SXin Li     return StmtError();
7125*67e74705SXin Li 
7126*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7127*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
7128*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
7129*67e74705SXin Li   // top and a single exit at the bottom.
7130*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
7131*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
7132*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
7133*67e74705SXin Li 
7134*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
7135*67e74705SXin Li   // In presence of clause 'collapse' with number of loops, it will
7136*67e74705SXin Li   // define the nested loops number.
7137*67e74705SXin Li   unsigned NestedLoopCount = CheckOpenMPLoop(
7138*67e74705SXin Li       OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
7139*67e74705SXin Li       nullptr /*ordered not a clause on distribute*/, AStmt, *this, *DSAStack,
7140*67e74705SXin Li       VarsWithImplicitDSA, B);
7141*67e74705SXin Li   if (NestedLoopCount == 0)
7142*67e74705SXin Li     return StmtError();
7143*67e74705SXin Li 
7144*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
7145*67e74705SXin Li          "omp for loop exprs were not built");
7146*67e74705SXin Li 
7147*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
7148*67e74705SXin Li   return OMPDistributeParallelForSimdDirective::Create(
7149*67e74705SXin Li       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7150*67e74705SXin Li }
7151*67e74705SXin Li 
ActOnOpenMPDistributeSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)7152*67e74705SXin Li StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7153*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7154*67e74705SXin Li     SourceLocation EndLoc,
7155*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7156*67e74705SXin Li   if (!AStmt)
7157*67e74705SXin Li     return StmtError();
7158*67e74705SXin Li 
7159*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7160*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
7161*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
7162*67e74705SXin Li   // top and a single exit at the bottom.
7163*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
7164*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
7165*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
7166*67e74705SXin Li 
7167*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
7168*67e74705SXin Li   // In presence of clause 'collapse' with number of loops, it will
7169*67e74705SXin Li   // define the nested loops number.
7170*67e74705SXin Li   unsigned NestedLoopCount =
7171*67e74705SXin Li       CheckOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
7172*67e74705SXin Li                       nullptr /*ordered not a clause on distribute*/, AStmt,
7173*67e74705SXin Li                       *this, *DSAStack, VarsWithImplicitDSA, B);
7174*67e74705SXin Li   if (NestedLoopCount == 0)
7175*67e74705SXin Li     return StmtError();
7176*67e74705SXin Li 
7177*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
7178*67e74705SXin Li          "omp for loop exprs were not built");
7179*67e74705SXin Li 
7180*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
7181*67e74705SXin Li   return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7182*67e74705SXin Li                                             NestedLoopCount, Clauses, AStmt, B);
7183*67e74705SXin Li }
7184*67e74705SXin Li 
ActOnOpenMPTargetParallelForSimdDirective(ArrayRef<OMPClause * > Clauses,Stmt * AStmt,SourceLocation StartLoc,SourceLocation EndLoc,llvm::DenseMap<ValueDecl *,Expr * > & VarsWithImplicitDSA)7185*67e74705SXin Li StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7186*67e74705SXin Li     ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
7187*67e74705SXin Li     SourceLocation EndLoc,
7188*67e74705SXin Li     llvm::DenseMap<ValueDecl *, Expr *> &VarsWithImplicitDSA) {
7189*67e74705SXin Li   if (!AStmt)
7190*67e74705SXin Li     return StmtError();
7191*67e74705SXin Li 
7192*67e74705SXin Li   CapturedStmt *CS = cast<CapturedStmt>(AStmt);
7193*67e74705SXin Li   // 1.2.2 OpenMP Language Terminology
7194*67e74705SXin Li   // Structured block - An executable statement with a single entry at the
7195*67e74705SXin Li   // top and a single exit at the bottom.
7196*67e74705SXin Li   // The point of exit cannot be a branch out of the structured block.
7197*67e74705SXin Li   // longjmp() and throw() must not violate the entry/exit criteria.
7198*67e74705SXin Li   CS->getCapturedDecl()->setNothrow();
7199*67e74705SXin Li 
7200*67e74705SXin Li   OMPLoopDirective::HelperExprs B;
7201*67e74705SXin Li   // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7202*67e74705SXin Li   // define the nested loops number.
7203*67e74705SXin Li   unsigned NestedLoopCount = CheckOpenMPLoop(
7204*67e74705SXin Li       OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
7205*67e74705SXin Li       getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
7206*67e74705SXin Li       VarsWithImplicitDSA, B);
7207*67e74705SXin Li   if (NestedLoopCount == 0)
7208*67e74705SXin Li     return StmtError();
7209*67e74705SXin Li 
7210*67e74705SXin Li   assert((CurContext->isDependentContext() || B.builtAll()) &&
7211*67e74705SXin Li          "omp target parallel for simd loop exprs were not built");
7212*67e74705SXin Li 
7213*67e74705SXin Li   if (!CurContext->isDependentContext()) {
7214*67e74705SXin Li     // Finalize the clauses that need pre-built expressions for CodeGen.
7215*67e74705SXin Li     for (auto C : Clauses) {
7216*67e74705SXin Li       if (auto LC = dyn_cast<OMPLinearClause>(C))
7217*67e74705SXin Li         if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7218*67e74705SXin Li                                      B.NumIterations, *this, CurScope,
7219*67e74705SXin Li                                      DSAStack))
7220*67e74705SXin Li           return StmtError();
7221*67e74705SXin Li     }
7222*67e74705SXin Li   }
7223*67e74705SXin Li 
7224*67e74705SXin Li   // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
7225*67e74705SXin Li   // If both simdlen and safelen clauses are specified, the value of the simdlen
7226*67e74705SXin Li   // parameter must be less than or equal to the value of the safelen parameter.
7227*67e74705SXin Li   OMPSafelenClause *Safelen = nullptr;
7228*67e74705SXin Li   OMPSimdlenClause *Simdlen = nullptr;
7229*67e74705SXin Li   for (auto *Clause : Clauses) {
7230*67e74705SXin Li     if (Clause->getClauseKind() == OMPC_safelen)
7231*67e74705SXin Li       Safelen = cast<OMPSafelenClause>(Clause);
7232*67e74705SXin Li     else if (Clause->getClauseKind() == OMPC_simdlen)
7233*67e74705SXin Li       Simdlen = cast<OMPSimdlenClause>(Clause);
7234*67e74705SXin Li     if (Safelen && Simdlen)
7235*67e74705SXin Li       break;
7236*67e74705SXin Li   }
7237*67e74705SXin Li   if (Simdlen && Safelen &&
7238*67e74705SXin Li       checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
7239*67e74705SXin Li                                 Safelen->getSafelen()))
7240*67e74705SXin Li     return StmtError();
7241*67e74705SXin Li 
7242*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
7243*67e74705SXin Li   return OMPTargetParallelForSimdDirective::Create(
7244*67e74705SXin Li       Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7245*67e74705SXin Li }
7246*67e74705SXin Li 
ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind,Expr * Expr,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7247*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
7248*67e74705SXin Li                                              SourceLocation StartLoc,
7249*67e74705SXin Li                                              SourceLocation LParenLoc,
7250*67e74705SXin Li                                              SourceLocation EndLoc) {
7251*67e74705SXin Li   OMPClause *Res = nullptr;
7252*67e74705SXin Li   switch (Kind) {
7253*67e74705SXin Li   case OMPC_final:
7254*67e74705SXin Li     Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7255*67e74705SXin Li     break;
7256*67e74705SXin Li   case OMPC_num_threads:
7257*67e74705SXin Li     Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7258*67e74705SXin Li     break;
7259*67e74705SXin Li   case OMPC_safelen:
7260*67e74705SXin Li     Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7261*67e74705SXin Li     break;
7262*67e74705SXin Li   case OMPC_simdlen:
7263*67e74705SXin Li     Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7264*67e74705SXin Li     break;
7265*67e74705SXin Li   case OMPC_collapse:
7266*67e74705SXin Li     Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7267*67e74705SXin Li     break;
7268*67e74705SXin Li   case OMPC_ordered:
7269*67e74705SXin Li     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7270*67e74705SXin Li     break;
7271*67e74705SXin Li   case OMPC_device:
7272*67e74705SXin Li     Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7273*67e74705SXin Li     break;
7274*67e74705SXin Li   case OMPC_num_teams:
7275*67e74705SXin Li     Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7276*67e74705SXin Li     break;
7277*67e74705SXin Li   case OMPC_thread_limit:
7278*67e74705SXin Li     Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7279*67e74705SXin Li     break;
7280*67e74705SXin Li   case OMPC_priority:
7281*67e74705SXin Li     Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7282*67e74705SXin Li     break;
7283*67e74705SXin Li   case OMPC_grainsize:
7284*67e74705SXin Li     Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7285*67e74705SXin Li     break;
7286*67e74705SXin Li   case OMPC_num_tasks:
7287*67e74705SXin Li     Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
7288*67e74705SXin Li     break;
7289*67e74705SXin Li   case OMPC_hint:
7290*67e74705SXin Li     Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
7291*67e74705SXin Li     break;
7292*67e74705SXin Li   case OMPC_if:
7293*67e74705SXin Li   case OMPC_default:
7294*67e74705SXin Li   case OMPC_proc_bind:
7295*67e74705SXin Li   case OMPC_schedule:
7296*67e74705SXin Li   case OMPC_private:
7297*67e74705SXin Li   case OMPC_firstprivate:
7298*67e74705SXin Li   case OMPC_lastprivate:
7299*67e74705SXin Li   case OMPC_shared:
7300*67e74705SXin Li   case OMPC_reduction:
7301*67e74705SXin Li   case OMPC_linear:
7302*67e74705SXin Li   case OMPC_aligned:
7303*67e74705SXin Li   case OMPC_copyin:
7304*67e74705SXin Li   case OMPC_copyprivate:
7305*67e74705SXin Li   case OMPC_nowait:
7306*67e74705SXin Li   case OMPC_untied:
7307*67e74705SXin Li   case OMPC_mergeable:
7308*67e74705SXin Li   case OMPC_threadprivate:
7309*67e74705SXin Li   case OMPC_flush:
7310*67e74705SXin Li   case OMPC_read:
7311*67e74705SXin Li   case OMPC_write:
7312*67e74705SXin Li   case OMPC_update:
7313*67e74705SXin Li   case OMPC_capture:
7314*67e74705SXin Li   case OMPC_seq_cst:
7315*67e74705SXin Li   case OMPC_depend:
7316*67e74705SXin Li   case OMPC_threads:
7317*67e74705SXin Li   case OMPC_simd:
7318*67e74705SXin Li   case OMPC_map:
7319*67e74705SXin Li   case OMPC_nogroup:
7320*67e74705SXin Li   case OMPC_dist_schedule:
7321*67e74705SXin Li   case OMPC_defaultmap:
7322*67e74705SXin Li   case OMPC_unknown:
7323*67e74705SXin Li   case OMPC_uniform:
7324*67e74705SXin Li   case OMPC_to:
7325*67e74705SXin Li   case OMPC_from:
7326*67e74705SXin Li   case OMPC_use_device_ptr:
7327*67e74705SXin Li   case OMPC_is_device_ptr:
7328*67e74705SXin Li     llvm_unreachable("Clause is not allowed.");
7329*67e74705SXin Li   }
7330*67e74705SXin Li   return Res;
7331*67e74705SXin Li }
7332*67e74705SXin Li 
ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,Expr * Condition,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation NameModifierLoc,SourceLocation ColonLoc,SourceLocation EndLoc)7333*67e74705SXin Li OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
7334*67e74705SXin Li                                      Expr *Condition, SourceLocation StartLoc,
7335*67e74705SXin Li                                      SourceLocation LParenLoc,
7336*67e74705SXin Li                                      SourceLocation NameModifierLoc,
7337*67e74705SXin Li                                      SourceLocation ColonLoc,
7338*67e74705SXin Li                                      SourceLocation EndLoc) {
7339*67e74705SXin Li   Expr *ValExpr = Condition;
7340*67e74705SXin Li   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7341*67e74705SXin Li       !Condition->isInstantiationDependent() &&
7342*67e74705SXin Li       !Condition->containsUnexpandedParameterPack()) {
7343*67e74705SXin Li     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7344*67e74705SXin Li     if (Val.isInvalid())
7345*67e74705SXin Li       return nullptr;
7346*67e74705SXin Li 
7347*67e74705SXin Li     ValExpr = MakeFullExpr(Val.get()).get();
7348*67e74705SXin Li   }
7349*67e74705SXin Li 
7350*67e74705SXin Li   return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
7351*67e74705SXin Li                                    NameModifierLoc, ColonLoc, EndLoc);
7352*67e74705SXin Li }
7353*67e74705SXin Li 
ActOnOpenMPFinalClause(Expr * Condition,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7354*67e74705SXin Li OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
7355*67e74705SXin Li                                         SourceLocation StartLoc,
7356*67e74705SXin Li                                         SourceLocation LParenLoc,
7357*67e74705SXin Li                                         SourceLocation EndLoc) {
7358*67e74705SXin Li   Expr *ValExpr = Condition;
7359*67e74705SXin Li   if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
7360*67e74705SXin Li       !Condition->isInstantiationDependent() &&
7361*67e74705SXin Li       !Condition->containsUnexpandedParameterPack()) {
7362*67e74705SXin Li     ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
7363*67e74705SXin Li     if (Val.isInvalid())
7364*67e74705SXin Li       return nullptr;
7365*67e74705SXin Li 
7366*67e74705SXin Li     ValExpr = MakeFullExpr(Val.get()).get();
7367*67e74705SXin Li   }
7368*67e74705SXin Li 
7369*67e74705SXin Li   return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7370*67e74705SXin Li }
PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,Expr * Op)7371*67e74705SXin Li ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
7372*67e74705SXin Li                                                         Expr *Op) {
7373*67e74705SXin Li   if (!Op)
7374*67e74705SXin Li     return ExprError();
7375*67e74705SXin Li 
7376*67e74705SXin Li   class IntConvertDiagnoser : public ICEConvertDiagnoser {
7377*67e74705SXin Li   public:
7378*67e74705SXin Li     IntConvertDiagnoser()
7379*67e74705SXin Li         : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
7380*67e74705SXin Li     SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
7381*67e74705SXin Li                                          QualType T) override {
7382*67e74705SXin Li       return S.Diag(Loc, diag::err_omp_not_integral) << T;
7383*67e74705SXin Li     }
7384*67e74705SXin Li     SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
7385*67e74705SXin Li                                              QualType T) override {
7386*67e74705SXin Li       return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
7387*67e74705SXin Li     }
7388*67e74705SXin Li     SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
7389*67e74705SXin Li                                                QualType T,
7390*67e74705SXin Li                                                QualType ConvTy) override {
7391*67e74705SXin Li       return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
7392*67e74705SXin Li     }
7393*67e74705SXin Li     SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
7394*67e74705SXin Li                                            QualType ConvTy) override {
7395*67e74705SXin Li       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7396*67e74705SXin Li              << ConvTy->isEnumeralType() << ConvTy;
7397*67e74705SXin Li     }
7398*67e74705SXin Li     SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
7399*67e74705SXin Li                                             QualType T) override {
7400*67e74705SXin Li       return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
7401*67e74705SXin Li     }
7402*67e74705SXin Li     SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
7403*67e74705SXin Li                                         QualType ConvTy) override {
7404*67e74705SXin Li       return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
7405*67e74705SXin Li              << ConvTy->isEnumeralType() << ConvTy;
7406*67e74705SXin Li     }
7407*67e74705SXin Li     SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
7408*67e74705SXin Li                                              QualType) override {
7409*67e74705SXin Li       llvm_unreachable("conversion functions are permitted");
7410*67e74705SXin Li     }
7411*67e74705SXin Li   } ConvertDiagnoser;
7412*67e74705SXin Li   return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
7413*67e74705SXin Li }
7414*67e74705SXin Li 
IsNonNegativeIntegerValue(Expr * & ValExpr,Sema & SemaRef,OpenMPClauseKind CKind,bool StrictlyPositive)7415*67e74705SXin Li static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
7416*67e74705SXin Li                                       OpenMPClauseKind CKind,
7417*67e74705SXin Li                                       bool StrictlyPositive) {
7418*67e74705SXin Li   if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7419*67e74705SXin Li       !ValExpr->isInstantiationDependent()) {
7420*67e74705SXin Li     SourceLocation Loc = ValExpr->getExprLoc();
7421*67e74705SXin Li     ExprResult Value =
7422*67e74705SXin Li         SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7423*67e74705SXin Li     if (Value.isInvalid())
7424*67e74705SXin Li       return false;
7425*67e74705SXin Li 
7426*67e74705SXin Li     ValExpr = Value.get();
7427*67e74705SXin Li     // The expression must evaluate to a non-negative integer value.
7428*67e74705SXin Li     llvm::APSInt Result;
7429*67e74705SXin Li     if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
7430*67e74705SXin Li         Result.isSigned() &&
7431*67e74705SXin Li         !((!StrictlyPositive && Result.isNonNegative()) ||
7432*67e74705SXin Li           (StrictlyPositive && Result.isStrictlyPositive()))) {
7433*67e74705SXin Li       SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
7434*67e74705SXin Li           << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7435*67e74705SXin Li           << ValExpr->getSourceRange();
7436*67e74705SXin Li       return false;
7437*67e74705SXin Li     }
7438*67e74705SXin Li   }
7439*67e74705SXin Li   return true;
7440*67e74705SXin Li }
7441*67e74705SXin Li 
ActOnOpenMPNumThreadsClause(Expr * NumThreads,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7442*67e74705SXin Li OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
7443*67e74705SXin Li                                              SourceLocation StartLoc,
7444*67e74705SXin Li                                              SourceLocation LParenLoc,
7445*67e74705SXin Li                                              SourceLocation EndLoc) {
7446*67e74705SXin Li   Expr *ValExpr = NumThreads;
7447*67e74705SXin Li 
7448*67e74705SXin Li   // OpenMP [2.5, Restrictions]
7449*67e74705SXin Li   //  The num_threads expression must evaluate to a positive integer value.
7450*67e74705SXin Li   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
7451*67e74705SXin Li                                  /*StrictlyPositive=*/true))
7452*67e74705SXin Li     return nullptr;
7453*67e74705SXin Li 
7454*67e74705SXin Li   return new (Context)
7455*67e74705SXin Li       OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7456*67e74705SXin Li }
7457*67e74705SXin Li 
VerifyPositiveIntegerConstantInClause(Expr * E,OpenMPClauseKind CKind,bool StrictlyPositive)7458*67e74705SXin Li ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
7459*67e74705SXin Li                                                        OpenMPClauseKind CKind,
7460*67e74705SXin Li                                                        bool StrictlyPositive) {
7461*67e74705SXin Li   if (!E)
7462*67e74705SXin Li     return ExprError();
7463*67e74705SXin Li   if (E->isValueDependent() || E->isTypeDependent() ||
7464*67e74705SXin Li       E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
7465*67e74705SXin Li     return E;
7466*67e74705SXin Li   llvm::APSInt Result;
7467*67e74705SXin Li   ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
7468*67e74705SXin Li   if (ICE.isInvalid())
7469*67e74705SXin Li     return ExprError();
7470*67e74705SXin Li   if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
7471*67e74705SXin Li       (!StrictlyPositive && !Result.isNonNegative())) {
7472*67e74705SXin Li     Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
7473*67e74705SXin Li         << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
7474*67e74705SXin Li         << E->getSourceRange();
7475*67e74705SXin Li     return ExprError();
7476*67e74705SXin Li   }
7477*67e74705SXin Li   if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
7478*67e74705SXin Li     Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
7479*67e74705SXin Li         << E->getSourceRange();
7480*67e74705SXin Li     return ExprError();
7481*67e74705SXin Li   }
7482*67e74705SXin Li   if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
7483*67e74705SXin Li     DSAStack->setAssociatedLoops(Result.getExtValue());
7484*67e74705SXin Li   else if (CKind == OMPC_ordered)
7485*67e74705SXin Li     DSAStack->setAssociatedLoops(Result.getExtValue());
7486*67e74705SXin Li   return ICE;
7487*67e74705SXin Li }
7488*67e74705SXin Li 
ActOnOpenMPSafelenClause(Expr * Len,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7489*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
7490*67e74705SXin Li                                           SourceLocation LParenLoc,
7491*67e74705SXin Li                                           SourceLocation EndLoc) {
7492*67e74705SXin Li   // OpenMP [2.8.1, simd construct, Description]
7493*67e74705SXin Li   // The parameter of the safelen clause must be a constant
7494*67e74705SXin Li   // positive integer expression.
7495*67e74705SXin Li   ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
7496*67e74705SXin Li   if (Safelen.isInvalid())
7497*67e74705SXin Li     return nullptr;
7498*67e74705SXin Li   return new (Context)
7499*67e74705SXin Li       OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
7500*67e74705SXin Li }
7501*67e74705SXin Li 
ActOnOpenMPSimdlenClause(Expr * Len,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7502*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
7503*67e74705SXin Li                                           SourceLocation LParenLoc,
7504*67e74705SXin Li                                           SourceLocation EndLoc) {
7505*67e74705SXin Li   // OpenMP [2.8.1, simd construct, Description]
7506*67e74705SXin Li   // The parameter of the simdlen clause must be a constant
7507*67e74705SXin Li   // positive integer expression.
7508*67e74705SXin Li   ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
7509*67e74705SXin Li   if (Simdlen.isInvalid())
7510*67e74705SXin Li     return nullptr;
7511*67e74705SXin Li   return new (Context)
7512*67e74705SXin Li       OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
7513*67e74705SXin Li }
7514*67e74705SXin Li 
ActOnOpenMPCollapseClause(Expr * NumForLoops,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7515*67e74705SXin Li OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
7516*67e74705SXin Li                                            SourceLocation StartLoc,
7517*67e74705SXin Li                                            SourceLocation LParenLoc,
7518*67e74705SXin Li                                            SourceLocation EndLoc) {
7519*67e74705SXin Li   // OpenMP [2.7.1, loop construct, Description]
7520*67e74705SXin Li   // OpenMP [2.8.1, simd construct, Description]
7521*67e74705SXin Li   // OpenMP [2.9.6, distribute construct, Description]
7522*67e74705SXin Li   // The parameter of the collapse clause must be a constant
7523*67e74705SXin Li   // positive integer expression.
7524*67e74705SXin Li   ExprResult NumForLoopsResult =
7525*67e74705SXin Li       VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
7526*67e74705SXin Li   if (NumForLoopsResult.isInvalid())
7527*67e74705SXin Li     return nullptr;
7528*67e74705SXin Li   return new (Context)
7529*67e74705SXin Li       OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
7530*67e74705SXin Li }
7531*67e74705SXin Li 
ActOnOpenMPOrderedClause(SourceLocation StartLoc,SourceLocation EndLoc,SourceLocation LParenLoc,Expr * NumForLoops)7532*67e74705SXin Li OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
7533*67e74705SXin Li                                           SourceLocation EndLoc,
7534*67e74705SXin Li                                           SourceLocation LParenLoc,
7535*67e74705SXin Li                                           Expr *NumForLoops) {
7536*67e74705SXin Li   // OpenMP [2.7.1, loop construct, Description]
7537*67e74705SXin Li   // OpenMP [2.8.1, simd construct, Description]
7538*67e74705SXin Li   // OpenMP [2.9.6, distribute construct, Description]
7539*67e74705SXin Li   // The parameter of the ordered clause must be a constant
7540*67e74705SXin Li   // positive integer expression if any.
7541*67e74705SXin Li   if (NumForLoops && LParenLoc.isValid()) {
7542*67e74705SXin Li     ExprResult NumForLoopsResult =
7543*67e74705SXin Li         VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
7544*67e74705SXin Li     if (NumForLoopsResult.isInvalid())
7545*67e74705SXin Li       return nullptr;
7546*67e74705SXin Li     NumForLoops = NumForLoopsResult.get();
7547*67e74705SXin Li   } else
7548*67e74705SXin Li     NumForLoops = nullptr;
7549*67e74705SXin Li   DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
7550*67e74705SXin Li   return new (Context)
7551*67e74705SXin Li       OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
7552*67e74705SXin Li }
7553*67e74705SXin Li 
ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,unsigned Argument,SourceLocation ArgumentLoc,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7554*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSimpleClause(
7555*67e74705SXin Li     OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
7556*67e74705SXin Li     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
7557*67e74705SXin Li   OMPClause *Res = nullptr;
7558*67e74705SXin Li   switch (Kind) {
7559*67e74705SXin Li   case OMPC_default:
7560*67e74705SXin Li     Res =
7561*67e74705SXin Li         ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
7562*67e74705SXin Li                                  ArgumentLoc, StartLoc, LParenLoc, EndLoc);
7563*67e74705SXin Li     break;
7564*67e74705SXin Li   case OMPC_proc_bind:
7565*67e74705SXin Li     Res = ActOnOpenMPProcBindClause(
7566*67e74705SXin Li         static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
7567*67e74705SXin Li         LParenLoc, EndLoc);
7568*67e74705SXin Li     break;
7569*67e74705SXin Li   case OMPC_if:
7570*67e74705SXin Li   case OMPC_final:
7571*67e74705SXin Li   case OMPC_num_threads:
7572*67e74705SXin Li   case OMPC_safelen:
7573*67e74705SXin Li   case OMPC_simdlen:
7574*67e74705SXin Li   case OMPC_collapse:
7575*67e74705SXin Li   case OMPC_schedule:
7576*67e74705SXin Li   case OMPC_private:
7577*67e74705SXin Li   case OMPC_firstprivate:
7578*67e74705SXin Li   case OMPC_lastprivate:
7579*67e74705SXin Li   case OMPC_shared:
7580*67e74705SXin Li   case OMPC_reduction:
7581*67e74705SXin Li   case OMPC_linear:
7582*67e74705SXin Li   case OMPC_aligned:
7583*67e74705SXin Li   case OMPC_copyin:
7584*67e74705SXin Li   case OMPC_copyprivate:
7585*67e74705SXin Li   case OMPC_ordered:
7586*67e74705SXin Li   case OMPC_nowait:
7587*67e74705SXin Li   case OMPC_untied:
7588*67e74705SXin Li   case OMPC_mergeable:
7589*67e74705SXin Li   case OMPC_threadprivate:
7590*67e74705SXin Li   case OMPC_flush:
7591*67e74705SXin Li   case OMPC_read:
7592*67e74705SXin Li   case OMPC_write:
7593*67e74705SXin Li   case OMPC_update:
7594*67e74705SXin Li   case OMPC_capture:
7595*67e74705SXin Li   case OMPC_seq_cst:
7596*67e74705SXin Li   case OMPC_depend:
7597*67e74705SXin Li   case OMPC_device:
7598*67e74705SXin Li   case OMPC_threads:
7599*67e74705SXin Li   case OMPC_simd:
7600*67e74705SXin Li   case OMPC_map:
7601*67e74705SXin Li   case OMPC_num_teams:
7602*67e74705SXin Li   case OMPC_thread_limit:
7603*67e74705SXin Li   case OMPC_priority:
7604*67e74705SXin Li   case OMPC_grainsize:
7605*67e74705SXin Li   case OMPC_nogroup:
7606*67e74705SXin Li   case OMPC_num_tasks:
7607*67e74705SXin Li   case OMPC_hint:
7608*67e74705SXin Li   case OMPC_dist_schedule:
7609*67e74705SXin Li   case OMPC_defaultmap:
7610*67e74705SXin Li   case OMPC_unknown:
7611*67e74705SXin Li   case OMPC_uniform:
7612*67e74705SXin Li   case OMPC_to:
7613*67e74705SXin Li   case OMPC_from:
7614*67e74705SXin Li   case OMPC_use_device_ptr:
7615*67e74705SXin Li   case OMPC_is_device_ptr:
7616*67e74705SXin Li     llvm_unreachable("Clause is not allowed.");
7617*67e74705SXin Li   }
7618*67e74705SXin Li   return Res;
7619*67e74705SXin Li }
7620*67e74705SXin Li 
7621*67e74705SXin Li static std::string
getListOfPossibleValues(OpenMPClauseKind K,unsigned First,unsigned Last,ArrayRef<unsigned> Exclude=llvm::None)7622*67e74705SXin Li getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
7623*67e74705SXin Li                         ArrayRef<unsigned> Exclude = llvm::None) {
7624*67e74705SXin Li   std::string Values;
7625*67e74705SXin Li   unsigned Bound = Last >= 2 ? Last - 2 : 0;
7626*67e74705SXin Li   unsigned Skipped = Exclude.size();
7627*67e74705SXin Li   auto S = Exclude.begin(), E = Exclude.end();
7628*67e74705SXin Li   for (unsigned i = First; i < Last; ++i) {
7629*67e74705SXin Li     if (std::find(S, E, i) != E) {
7630*67e74705SXin Li       --Skipped;
7631*67e74705SXin Li       continue;
7632*67e74705SXin Li     }
7633*67e74705SXin Li     Values += "'";
7634*67e74705SXin Li     Values += getOpenMPSimpleClauseTypeName(K, i);
7635*67e74705SXin Li     Values += "'";
7636*67e74705SXin Li     if (i == Bound - Skipped)
7637*67e74705SXin Li       Values += " or ";
7638*67e74705SXin Li     else if (i != Bound + 1 - Skipped)
7639*67e74705SXin Li       Values += ", ";
7640*67e74705SXin Li   }
7641*67e74705SXin Li   return Values;
7642*67e74705SXin Li }
7643*67e74705SXin Li 
ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,SourceLocation KindKwLoc,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7644*67e74705SXin Li OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
7645*67e74705SXin Li                                           SourceLocation KindKwLoc,
7646*67e74705SXin Li                                           SourceLocation StartLoc,
7647*67e74705SXin Li                                           SourceLocation LParenLoc,
7648*67e74705SXin Li                                           SourceLocation EndLoc) {
7649*67e74705SXin Li   if (Kind == OMPC_DEFAULT_unknown) {
7650*67e74705SXin Li     static_assert(OMPC_DEFAULT_unknown > 0,
7651*67e74705SXin Li                   "OMPC_DEFAULT_unknown not greater than 0");
7652*67e74705SXin Li     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7653*67e74705SXin Li         << getListOfPossibleValues(OMPC_default, /*First=*/0,
7654*67e74705SXin Li                                    /*Last=*/OMPC_DEFAULT_unknown)
7655*67e74705SXin Li         << getOpenMPClauseName(OMPC_default);
7656*67e74705SXin Li     return nullptr;
7657*67e74705SXin Li   }
7658*67e74705SXin Li   switch (Kind) {
7659*67e74705SXin Li   case OMPC_DEFAULT_none:
7660*67e74705SXin Li     DSAStack->setDefaultDSANone(KindKwLoc);
7661*67e74705SXin Li     break;
7662*67e74705SXin Li   case OMPC_DEFAULT_shared:
7663*67e74705SXin Li     DSAStack->setDefaultDSAShared(KindKwLoc);
7664*67e74705SXin Li     break;
7665*67e74705SXin Li   case OMPC_DEFAULT_unknown:
7666*67e74705SXin Li     llvm_unreachable("Clause kind is not allowed.");
7667*67e74705SXin Li     break;
7668*67e74705SXin Li   }
7669*67e74705SXin Li   return new (Context)
7670*67e74705SXin Li       OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7671*67e74705SXin Li }
7672*67e74705SXin Li 
ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,SourceLocation KindKwLoc,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)7673*67e74705SXin Li OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
7674*67e74705SXin Li                                            SourceLocation KindKwLoc,
7675*67e74705SXin Li                                            SourceLocation StartLoc,
7676*67e74705SXin Li                                            SourceLocation LParenLoc,
7677*67e74705SXin Li                                            SourceLocation EndLoc) {
7678*67e74705SXin Li   if (Kind == OMPC_PROC_BIND_unknown) {
7679*67e74705SXin Li     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
7680*67e74705SXin Li         << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
7681*67e74705SXin Li                                    /*Last=*/OMPC_PROC_BIND_unknown)
7682*67e74705SXin Li         << getOpenMPClauseName(OMPC_proc_bind);
7683*67e74705SXin Li     return nullptr;
7684*67e74705SXin Li   }
7685*67e74705SXin Li   return new (Context)
7686*67e74705SXin Li       OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
7687*67e74705SXin Li }
7688*67e74705SXin Li 
ActOnOpenMPSingleExprWithArgClause(OpenMPClauseKind Kind,ArrayRef<unsigned> Argument,Expr * Expr,SourceLocation StartLoc,SourceLocation LParenLoc,ArrayRef<SourceLocation> ArgumentLoc,SourceLocation DelimLoc,SourceLocation EndLoc)7689*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
7690*67e74705SXin Li     OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
7691*67e74705SXin Li     SourceLocation StartLoc, SourceLocation LParenLoc,
7692*67e74705SXin Li     ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
7693*67e74705SXin Li     SourceLocation EndLoc) {
7694*67e74705SXin Li   OMPClause *Res = nullptr;
7695*67e74705SXin Li   switch (Kind) {
7696*67e74705SXin Li   case OMPC_schedule:
7697*67e74705SXin Li     enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
7698*67e74705SXin Li     assert(Argument.size() == NumberOfElements &&
7699*67e74705SXin Li            ArgumentLoc.size() == NumberOfElements);
7700*67e74705SXin Li     Res = ActOnOpenMPScheduleClause(
7701*67e74705SXin Li         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
7702*67e74705SXin Li         static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
7703*67e74705SXin Li         static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
7704*67e74705SXin Li         StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
7705*67e74705SXin Li         ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
7706*67e74705SXin Li     break;
7707*67e74705SXin Li   case OMPC_if:
7708*67e74705SXin Li     assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
7709*67e74705SXin Li     Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
7710*67e74705SXin Li                               Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
7711*67e74705SXin Li                               DelimLoc, EndLoc);
7712*67e74705SXin Li     break;
7713*67e74705SXin Li   case OMPC_dist_schedule:
7714*67e74705SXin Li     Res = ActOnOpenMPDistScheduleClause(
7715*67e74705SXin Li         static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
7716*67e74705SXin Li         StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
7717*67e74705SXin Li     break;
7718*67e74705SXin Li   case OMPC_defaultmap:
7719*67e74705SXin Li     enum { Modifier, DefaultmapKind };
7720*67e74705SXin Li     Res = ActOnOpenMPDefaultmapClause(
7721*67e74705SXin Li         static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
7722*67e74705SXin Li         static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
7723*67e74705SXin Li         StartLoc, LParenLoc, ArgumentLoc[Modifier],
7724*67e74705SXin Li         ArgumentLoc[DefaultmapKind], EndLoc);
7725*67e74705SXin Li     break;
7726*67e74705SXin Li   case OMPC_final:
7727*67e74705SXin Li   case OMPC_num_threads:
7728*67e74705SXin Li   case OMPC_safelen:
7729*67e74705SXin Li   case OMPC_simdlen:
7730*67e74705SXin Li   case OMPC_collapse:
7731*67e74705SXin Li   case OMPC_default:
7732*67e74705SXin Li   case OMPC_proc_bind:
7733*67e74705SXin Li   case OMPC_private:
7734*67e74705SXin Li   case OMPC_firstprivate:
7735*67e74705SXin Li   case OMPC_lastprivate:
7736*67e74705SXin Li   case OMPC_shared:
7737*67e74705SXin Li   case OMPC_reduction:
7738*67e74705SXin Li   case OMPC_linear:
7739*67e74705SXin Li   case OMPC_aligned:
7740*67e74705SXin Li   case OMPC_copyin:
7741*67e74705SXin Li   case OMPC_copyprivate:
7742*67e74705SXin Li   case OMPC_ordered:
7743*67e74705SXin Li   case OMPC_nowait:
7744*67e74705SXin Li   case OMPC_untied:
7745*67e74705SXin Li   case OMPC_mergeable:
7746*67e74705SXin Li   case OMPC_threadprivate:
7747*67e74705SXin Li   case OMPC_flush:
7748*67e74705SXin Li   case OMPC_read:
7749*67e74705SXin Li   case OMPC_write:
7750*67e74705SXin Li   case OMPC_update:
7751*67e74705SXin Li   case OMPC_capture:
7752*67e74705SXin Li   case OMPC_seq_cst:
7753*67e74705SXin Li   case OMPC_depend:
7754*67e74705SXin Li   case OMPC_device:
7755*67e74705SXin Li   case OMPC_threads:
7756*67e74705SXin Li   case OMPC_simd:
7757*67e74705SXin Li   case OMPC_map:
7758*67e74705SXin Li   case OMPC_num_teams:
7759*67e74705SXin Li   case OMPC_thread_limit:
7760*67e74705SXin Li   case OMPC_priority:
7761*67e74705SXin Li   case OMPC_grainsize:
7762*67e74705SXin Li   case OMPC_nogroup:
7763*67e74705SXin Li   case OMPC_num_tasks:
7764*67e74705SXin Li   case OMPC_hint:
7765*67e74705SXin Li   case OMPC_unknown:
7766*67e74705SXin Li   case OMPC_uniform:
7767*67e74705SXin Li   case OMPC_to:
7768*67e74705SXin Li   case OMPC_from:
7769*67e74705SXin Li   case OMPC_use_device_ptr:
7770*67e74705SXin Li   case OMPC_is_device_ptr:
7771*67e74705SXin Li     llvm_unreachable("Clause is not allowed.");
7772*67e74705SXin Li   }
7773*67e74705SXin Li   return Res;
7774*67e74705SXin Li }
7775*67e74705SXin Li 
checkScheduleModifiers(Sema & S,OpenMPScheduleClauseModifier M1,OpenMPScheduleClauseModifier M2,SourceLocation M1Loc,SourceLocation M2Loc)7776*67e74705SXin Li static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
7777*67e74705SXin Li                                    OpenMPScheduleClauseModifier M2,
7778*67e74705SXin Li                                    SourceLocation M1Loc, SourceLocation M2Loc) {
7779*67e74705SXin Li   if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
7780*67e74705SXin Li     SmallVector<unsigned, 2> Excluded;
7781*67e74705SXin Li     if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
7782*67e74705SXin Li       Excluded.push_back(M2);
7783*67e74705SXin Li     if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
7784*67e74705SXin Li       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
7785*67e74705SXin Li     if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
7786*67e74705SXin Li       Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
7787*67e74705SXin Li     S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
7788*67e74705SXin Li         << getListOfPossibleValues(OMPC_schedule,
7789*67e74705SXin Li                                    /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
7790*67e74705SXin Li                                    /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7791*67e74705SXin Li                                    Excluded)
7792*67e74705SXin Li         << getOpenMPClauseName(OMPC_schedule);
7793*67e74705SXin Li     return true;
7794*67e74705SXin Li   }
7795*67e74705SXin Li   return false;
7796*67e74705SXin Li }
7797*67e74705SXin Li 
ActOnOpenMPScheduleClause(OpenMPScheduleClauseModifier M1,OpenMPScheduleClauseModifier M2,OpenMPScheduleClauseKind Kind,Expr * ChunkSize,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation M1Loc,SourceLocation M2Loc,SourceLocation KindLoc,SourceLocation CommaLoc,SourceLocation EndLoc)7798*67e74705SXin Li OMPClause *Sema::ActOnOpenMPScheduleClause(
7799*67e74705SXin Li     OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
7800*67e74705SXin Li     OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
7801*67e74705SXin Li     SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
7802*67e74705SXin Li     SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
7803*67e74705SXin Li   if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
7804*67e74705SXin Li       checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
7805*67e74705SXin Li     return nullptr;
7806*67e74705SXin Li   // OpenMP, 2.7.1, Loop Construct, Restrictions
7807*67e74705SXin Li   // Either the monotonic modifier or the nonmonotonic modifier can be specified
7808*67e74705SXin Li   // but not both.
7809*67e74705SXin Li   if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
7810*67e74705SXin Li       (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
7811*67e74705SXin Li        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
7812*67e74705SXin Li       (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
7813*67e74705SXin Li        M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
7814*67e74705SXin Li     Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
7815*67e74705SXin Li         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
7816*67e74705SXin Li         << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
7817*67e74705SXin Li     return nullptr;
7818*67e74705SXin Li   }
7819*67e74705SXin Li   if (Kind == OMPC_SCHEDULE_unknown) {
7820*67e74705SXin Li     std::string Values;
7821*67e74705SXin Li     if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
7822*67e74705SXin Li       unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
7823*67e74705SXin Li       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7824*67e74705SXin Li                                        /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
7825*67e74705SXin Li                                        Exclude);
7826*67e74705SXin Li     } else {
7827*67e74705SXin Li       Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
7828*67e74705SXin Li                                        /*Last=*/OMPC_SCHEDULE_unknown);
7829*67e74705SXin Li     }
7830*67e74705SXin Li     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
7831*67e74705SXin Li         << Values << getOpenMPClauseName(OMPC_schedule);
7832*67e74705SXin Li     return nullptr;
7833*67e74705SXin Li   }
7834*67e74705SXin Li   // OpenMP, 2.7.1, Loop Construct, Restrictions
7835*67e74705SXin Li   // The nonmonotonic modifier can only be specified with schedule(dynamic) or
7836*67e74705SXin Li   // schedule(guided).
7837*67e74705SXin Li   if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
7838*67e74705SXin Li        M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
7839*67e74705SXin Li       Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
7840*67e74705SXin Li     Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
7841*67e74705SXin Li          diag::err_omp_schedule_nonmonotonic_static);
7842*67e74705SXin Li     return nullptr;
7843*67e74705SXin Li   }
7844*67e74705SXin Li   Expr *ValExpr = ChunkSize;
7845*67e74705SXin Li   Stmt *HelperValStmt = nullptr;
7846*67e74705SXin Li   if (ChunkSize) {
7847*67e74705SXin Li     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
7848*67e74705SXin Li         !ChunkSize->isInstantiationDependent() &&
7849*67e74705SXin Li         !ChunkSize->containsUnexpandedParameterPack()) {
7850*67e74705SXin Li       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
7851*67e74705SXin Li       ExprResult Val =
7852*67e74705SXin Li           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
7853*67e74705SXin Li       if (Val.isInvalid())
7854*67e74705SXin Li         return nullptr;
7855*67e74705SXin Li 
7856*67e74705SXin Li       ValExpr = Val.get();
7857*67e74705SXin Li 
7858*67e74705SXin Li       // OpenMP [2.7.1, Restrictions]
7859*67e74705SXin Li       //  chunk_size must be a loop invariant integer expression with a positive
7860*67e74705SXin Li       //  value.
7861*67e74705SXin Li       llvm::APSInt Result;
7862*67e74705SXin Li       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
7863*67e74705SXin Li         if (Result.isSigned() && !Result.isStrictlyPositive()) {
7864*67e74705SXin Li           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
7865*67e74705SXin Li               << "schedule" << 1 << ChunkSize->getSourceRange();
7866*67e74705SXin Li           return nullptr;
7867*67e74705SXin Li         }
7868*67e74705SXin Li       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
7869*67e74705SXin Li                  !CurContext->isDependentContext()) {
7870*67e74705SXin Li         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
7871*67e74705SXin Li         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
7872*67e74705SXin Li         HelperValStmt = buildPreInits(Context, Captures);
7873*67e74705SXin Li       }
7874*67e74705SXin Li     }
7875*67e74705SXin Li   }
7876*67e74705SXin Li 
7877*67e74705SXin Li   return new (Context)
7878*67e74705SXin Li       OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
7879*67e74705SXin Li                         ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
7880*67e74705SXin Li }
7881*67e74705SXin Li 
ActOnOpenMPClause(OpenMPClauseKind Kind,SourceLocation StartLoc,SourceLocation EndLoc)7882*67e74705SXin Li OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
7883*67e74705SXin Li                                    SourceLocation StartLoc,
7884*67e74705SXin Li                                    SourceLocation EndLoc) {
7885*67e74705SXin Li   OMPClause *Res = nullptr;
7886*67e74705SXin Li   switch (Kind) {
7887*67e74705SXin Li   case OMPC_ordered:
7888*67e74705SXin Li     Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
7889*67e74705SXin Li     break;
7890*67e74705SXin Li   case OMPC_nowait:
7891*67e74705SXin Li     Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
7892*67e74705SXin Li     break;
7893*67e74705SXin Li   case OMPC_untied:
7894*67e74705SXin Li     Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
7895*67e74705SXin Li     break;
7896*67e74705SXin Li   case OMPC_mergeable:
7897*67e74705SXin Li     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
7898*67e74705SXin Li     break;
7899*67e74705SXin Li   case OMPC_read:
7900*67e74705SXin Li     Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
7901*67e74705SXin Li     break;
7902*67e74705SXin Li   case OMPC_write:
7903*67e74705SXin Li     Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
7904*67e74705SXin Li     break;
7905*67e74705SXin Li   case OMPC_update:
7906*67e74705SXin Li     Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
7907*67e74705SXin Li     break;
7908*67e74705SXin Li   case OMPC_capture:
7909*67e74705SXin Li     Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
7910*67e74705SXin Li     break;
7911*67e74705SXin Li   case OMPC_seq_cst:
7912*67e74705SXin Li     Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
7913*67e74705SXin Li     break;
7914*67e74705SXin Li   case OMPC_threads:
7915*67e74705SXin Li     Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
7916*67e74705SXin Li     break;
7917*67e74705SXin Li   case OMPC_simd:
7918*67e74705SXin Li     Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
7919*67e74705SXin Li     break;
7920*67e74705SXin Li   case OMPC_nogroup:
7921*67e74705SXin Li     Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
7922*67e74705SXin Li     break;
7923*67e74705SXin Li   case OMPC_if:
7924*67e74705SXin Li   case OMPC_final:
7925*67e74705SXin Li   case OMPC_num_threads:
7926*67e74705SXin Li   case OMPC_safelen:
7927*67e74705SXin Li   case OMPC_simdlen:
7928*67e74705SXin Li   case OMPC_collapse:
7929*67e74705SXin Li   case OMPC_schedule:
7930*67e74705SXin Li   case OMPC_private:
7931*67e74705SXin Li   case OMPC_firstprivate:
7932*67e74705SXin Li   case OMPC_lastprivate:
7933*67e74705SXin Li   case OMPC_shared:
7934*67e74705SXin Li   case OMPC_reduction:
7935*67e74705SXin Li   case OMPC_linear:
7936*67e74705SXin Li   case OMPC_aligned:
7937*67e74705SXin Li   case OMPC_copyin:
7938*67e74705SXin Li   case OMPC_copyprivate:
7939*67e74705SXin Li   case OMPC_default:
7940*67e74705SXin Li   case OMPC_proc_bind:
7941*67e74705SXin Li   case OMPC_threadprivate:
7942*67e74705SXin Li   case OMPC_flush:
7943*67e74705SXin Li   case OMPC_depend:
7944*67e74705SXin Li   case OMPC_device:
7945*67e74705SXin Li   case OMPC_map:
7946*67e74705SXin Li   case OMPC_num_teams:
7947*67e74705SXin Li   case OMPC_thread_limit:
7948*67e74705SXin Li   case OMPC_priority:
7949*67e74705SXin Li   case OMPC_grainsize:
7950*67e74705SXin Li   case OMPC_num_tasks:
7951*67e74705SXin Li   case OMPC_hint:
7952*67e74705SXin Li   case OMPC_dist_schedule:
7953*67e74705SXin Li   case OMPC_defaultmap:
7954*67e74705SXin Li   case OMPC_unknown:
7955*67e74705SXin Li   case OMPC_uniform:
7956*67e74705SXin Li   case OMPC_to:
7957*67e74705SXin Li   case OMPC_from:
7958*67e74705SXin Li   case OMPC_use_device_ptr:
7959*67e74705SXin Li   case OMPC_is_device_ptr:
7960*67e74705SXin Li     llvm_unreachable("Clause is not allowed.");
7961*67e74705SXin Li   }
7962*67e74705SXin Li   return Res;
7963*67e74705SXin Li }
7964*67e74705SXin Li 
ActOnOpenMPNowaitClause(SourceLocation StartLoc,SourceLocation EndLoc)7965*67e74705SXin Li OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
7966*67e74705SXin Li                                          SourceLocation EndLoc) {
7967*67e74705SXin Li   DSAStack->setNowaitRegion();
7968*67e74705SXin Li   return new (Context) OMPNowaitClause(StartLoc, EndLoc);
7969*67e74705SXin Li }
7970*67e74705SXin Li 
ActOnOpenMPUntiedClause(SourceLocation StartLoc,SourceLocation EndLoc)7971*67e74705SXin Li OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
7972*67e74705SXin Li                                          SourceLocation EndLoc) {
7973*67e74705SXin Li   return new (Context) OMPUntiedClause(StartLoc, EndLoc);
7974*67e74705SXin Li }
7975*67e74705SXin Li 
ActOnOpenMPMergeableClause(SourceLocation StartLoc,SourceLocation EndLoc)7976*67e74705SXin Li OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
7977*67e74705SXin Li                                             SourceLocation EndLoc) {
7978*67e74705SXin Li   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
7979*67e74705SXin Li }
7980*67e74705SXin Li 
ActOnOpenMPReadClause(SourceLocation StartLoc,SourceLocation EndLoc)7981*67e74705SXin Li OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
7982*67e74705SXin Li                                        SourceLocation EndLoc) {
7983*67e74705SXin Li   return new (Context) OMPReadClause(StartLoc, EndLoc);
7984*67e74705SXin Li }
7985*67e74705SXin Li 
ActOnOpenMPWriteClause(SourceLocation StartLoc,SourceLocation EndLoc)7986*67e74705SXin Li OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
7987*67e74705SXin Li                                         SourceLocation EndLoc) {
7988*67e74705SXin Li   return new (Context) OMPWriteClause(StartLoc, EndLoc);
7989*67e74705SXin Li }
7990*67e74705SXin Li 
ActOnOpenMPUpdateClause(SourceLocation StartLoc,SourceLocation EndLoc)7991*67e74705SXin Li OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
7992*67e74705SXin Li                                          SourceLocation EndLoc) {
7993*67e74705SXin Li   return new (Context) OMPUpdateClause(StartLoc, EndLoc);
7994*67e74705SXin Li }
7995*67e74705SXin Li 
ActOnOpenMPCaptureClause(SourceLocation StartLoc,SourceLocation EndLoc)7996*67e74705SXin Li OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
7997*67e74705SXin Li                                           SourceLocation EndLoc) {
7998*67e74705SXin Li   return new (Context) OMPCaptureClause(StartLoc, EndLoc);
7999*67e74705SXin Li }
8000*67e74705SXin Li 
ActOnOpenMPSeqCstClause(SourceLocation StartLoc,SourceLocation EndLoc)8001*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
8002*67e74705SXin Li                                          SourceLocation EndLoc) {
8003*67e74705SXin Li   return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
8004*67e74705SXin Li }
8005*67e74705SXin Li 
ActOnOpenMPThreadsClause(SourceLocation StartLoc,SourceLocation EndLoc)8006*67e74705SXin Li OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
8007*67e74705SXin Li                                           SourceLocation EndLoc) {
8008*67e74705SXin Li   return new (Context) OMPThreadsClause(StartLoc, EndLoc);
8009*67e74705SXin Li }
8010*67e74705SXin Li 
ActOnOpenMPSIMDClause(SourceLocation StartLoc,SourceLocation EndLoc)8011*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
8012*67e74705SXin Li                                        SourceLocation EndLoc) {
8013*67e74705SXin Li   return new (Context) OMPSIMDClause(StartLoc, EndLoc);
8014*67e74705SXin Li }
8015*67e74705SXin Li 
ActOnOpenMPNogroupClause(SourceLocation StartLoc,SourceLocation EndLoc)8016*67e74705SXin Li OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
8017*67e74705SXin Li                                           SourceLocation EndLoc) {
8018*67e74705SXin Li   return new (Context) OMPNogroupClause(StartLoc, EndLoc);
8019*67e74705SXin Li }
8020*67e74705SXin Li 
ActOnOpenMPVarListClause(OpenMPClauseKind Kind,ArrayRef<Expr * > VarList,Expr * TailExpr,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc,CXXScopeSpec & ReductionIdScopeSpec,const DeclarationNameInfo & ReductionId,OpenMPDependClauseKind DepKind,OpenMPLinearClauseKind LinKind,OpenMPMapClauseKind MapTypeModifier,OpenMPMapClauseKind MapType,bool IsMapTypeImplicit,SourceLocation DepLinMapLoc)8021*67e74705SXin Li OMPClause *Sema::ActOnOpenMPVarListClause(
8022*67e74705SXin Li     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
8023*67e74705SXin Li     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
8024*67e74705SXin Li     SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
8025*67e74705SXin Li     const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
8026*67e74705SXin Li     OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
8027*67e74705SXin Li     OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
8028*67e74705SXin Li     SourceLocation DepLinMapLoc) {
8029*67e74705SXin Li   OMPClause *Res = nullptr;
8030*67e74705SXin Li   switch (Kind) {
8031*67e74705SXin Li   case OMPC_private:
8032*67e74705SXin Li     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8033*67e74705SXin Li     break;
8034*67e74705SXin Li   case OMPC_firstprivate:
8035*67e74705SXin Li     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8036*67e74705SXin Li     break;
8037*67e74705SXin Li   case OMPC_lastprivate:
8038*67e74705SXin Li     Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8039*67e74705SXin Li     break;
8040*67e74705SXin Li   case OMPC_shared:
8041*67e74705SXin Li     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
8042*67e74705SXin Li     break;
8043*67e74705SXin Li   case OMPC_reduction:
8044*67e74705SXin Li     Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
8045*67e74705SXin Li                                      EndLoc, ReductionIdScopeSpec, ReductionId);
8046*67e74705SXin Li     break;
8047*67e74705SXin Li   case OMPC_linear:
8048*67e74705SXin Li     Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
8049*67e74705SXin Li                                   LinKind, DepLinMapLoc, ColonLoc, EndLoc);
8050*67e74705SXin Li     break;
8051*67e74705SXin Li   case OMPC_aligned:
8052*67e74705SXin Li     Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
8053*67e74705SXin Li                                    ColonLoc, EndLoc);
8054*67e74705SXin Li     break;
8055*67e74705SXin Li   case OMPC_copyin:
8056*67e74705SXin Li     Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
8057*67e74705SXin Li     break;
8058*67e74705SXin Li   case OMPC_copyprivate:
8059*67e74705SXin Li     Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
8060*67e74705SXin Li     break;
8061*67e74705SXin Li   case OMPC_flush:
8062*67e74705SXin Li     Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
8063*67e74705SXin Li     break;
8064*67e74705SXin Li   case OMPC_depend:
8065*67e74705SXin Li     Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
8066*67e74705SXin Li                                   StartLoc, LParenLoc, EndLoc);
8067*67e74705SXin Li     break;
8068*67e74705SXin Li   case OMPC_map:
8069*67e74705SXin Li     Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
8070*67e74705SXin Li                                DepLinMapLoc, ColonLoc, VarList, StartLoc,
8071*67e74705SXin Li                                LParenLoc, EndLoc);
8072*67e74705SXin Li     break;
8073*67e74705SXin Li   case OMPC_to:
8074*67e74705SXin Li     Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
8075*67e74705SXin Li     break;
8076*67e74705SXin Li   case OMPC_from:
8077*67e74705SXin Li     Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
8078*67e74705SXin Li     break;
8079*67e74705SXin Li   case OMPC_use_device_ptr:
8080*67e74705SXin Li     Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8081*67e74705SXin Li     break;
8082*67e74705SXin Li   case OMPC_is_device_ptr:
8083*67e74705SXin Li     Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
8084*67e74705SXin Li     break;
8085*67e74705SXin Li   case OMPC_if:
8086*67e74705SXin Li   case OMPC_final:
8087*67e74705SXin Li   case OMPC_num_threads:
8088*67e74705SXin Li   case OMPC_safelen:
8089*67e74705SXin Li   case OMPC_simdlen:
8090*67e74705SXin Li   case OMPC_collapse:
8091*67e74705SXin Li   case OMPC_default:
8092*67e74705SXin Li   case OMPC_proc_bind:
8093*67e74705SXin Li   case OMPC_schedule:
8094*67e74705SXin Li   case OMPC_ordered:
8095*67e74705SXin Li   case OMPC_nowait:
8096*67e74705SXin Li   case OMPC_untied:
8097*67e74705SXin Li   case OMPC_mergeable:
8098*67e74705SXin Li   case OMPC_threadprivate:
8099*67e74705SXin Li   case OMPC_read:
8100*67e74705SXin Li   case OMPC_write:
8101*67e74705SXin Li   case OMPC_update:
8102*67e74705SXin Li   case OMPC_capture:
8103*67e74705SXin Li   case OMPC_seq_cst:
8104*67e74705SXin Li   case OMPC_device:
8105*67e74705SXin Li   case OMPC_threads:
8106*67e74705SXin Li   case OMPC_simd:
8107*67e74705SXin Li   case OMPC_num_teams:
8108*67e74705SXin Li   case OMPC_thread_limit:
8109*67e74705SXin Li   case OMPC_priority:
8110*67e74705SXin Li   case OMPC_grainsize:
8111*67e74705SXin Li   case OMPC_nogroup:
8112*67e74705SXin Li   case OMPC_num_tasks:
8113*67e74705SXin Li   case OMPC_hint:
8114*67e74705SXin Li   case OMPC_dist_schedule:
8115*67e74705SXin Li   case OMPC_defaultmap:
8116*67e74705SXin Li   case OMPC_unknown:
8117*67e74705SXin Li   case OMPC_uniform:
8118*67e74705SXin Li     llvm_unreachable("Clause is not allowed.");
8119*67e74705SXin Li   }
8120*67e74705SXin Li   return Res;
8121*67e74705SXin Li }
8122*67e74705SXin Li 
getOpenMPCapturedExpr(VarDecl * Capture,ExprValueKind VK,ExprObjectKind OK,SourceLocation Loc)8123*67e74705SXin Li ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
8124*67e74705SXin Li                                        ExprObjectKind OK, SourceLocation Loc) {
8125*67e74705SXin Li   ExprResult Res = BuildDeclRefExpr(
8126*67e74705SXin Li       Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
8127*67e74705SXin Li   if (!Res.isUsable())
8128*67e74705SXin Li     return ExprError();
8129*67e74705SXin Li   if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
8130*67e74705SXin Li     Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
8131*67e74705SXin Li     if (!Res.isUsable())
8132*67e74705SXin Li       return ExprError();
8133*67e74705SXin Li   }
8134*67e74705SXin Li   if (VK != VK_LValue && Res.get()->isGLValue()) {
8135*67e74705SXin Li     Res = DefaultLvalueConversion(Res.get());
8136*67e74705SXin Li     if (!Res.isUsable())
8137*67e74705SXin Li       return ExprError();
8138*67e74705SXin Li   }
8139*67e74705SXin Li   return Res;
8140*67e74705SXin Li }
8141*67e74705SXin Li 
8142*67e74705SXin Li static std::pair<ValueDecl *, bool>
getPrivateItem(Sema & S,Expr * & RefExpr,SourceLocation & ELoc,SourceRange & ERange,bool AllowArraySection=false)8143*67e74705SXin Li getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
8144*67e74705SXin Li                SourceRange &ERange, bool AllowArraySection = false) {
8145*67e74705SXin Li   if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
8146*67e74705SXin Li       RefExpr->containsUnexpandedParameterPack())
8147*67e74705SXin Li     return std::make_pair(nullptr, true);
8148*67e74705SXin Li 
8149*67e74705SXin Li   // OpenMP [3.1, C/C++]
8150*67e74705SXin Li   //  A list item is a variable name.
8151*67e74705SXin Li   // OpenMP  [2.9.3.3, Restrictions, p.1]
8152*67e74705SXin Li   //  A variable that is part of another variable (as an array or
8153*67e74705SXin Li   //  structure element) cannot appear in a private clause.
8154*67e74705SXin Li   RefExpr = RefExpr->IgnoreParens();
8155*67e74705SXin Li   enum {
8156*67e74705SXin Li     NoArrayExpr = -1,
8157*67e74705SXin Li     ArraySubscript = 0,
8158*67e74705SXin Li     OMPArraySection = 1
8159*67e74705SXin Li   } IsArrayExpr = NoArrayExpr;
8160*67e74705SXin Li   if (AllowArraySection) {
8161*67e74705SXin Li     if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
8162*67e74705SXin Li       auto *Base = ASE->getBase()->IgnoreParenImpCasts();
8163*67e74705SXin Li       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8164*67e74705SXin Li         Base = TempASE->getBase()->IgnoreParenImpCasts();
8165*67e74705SXin Li       RefExpr = Base;
8166*67e74705SXin Li       IsArrayExpr = ArraySubscript;
8167*67e74705SXin Li     } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
8168*67e74705SXin Li       auto *Base = OASE->getBase()->IgnoreParenImpCasts();
8169*67e74705SXin Li       while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
8170*67e74705SXin Li         Base = TempOASE->getBase()->IgnoreParenImpCasts();
8171*67e74705SXin Li       while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
8172*67e74705SXin Li         Base = TempASE->getBase()->IgnoreParenImpCasts();
8173*67e74705SXin Li       RefExpr = Base;
8174*67e74705SXin Li       IsArrayExpr = OMPArraySection;
8175*67e74705SXin Li     }
8176*67e74705SXin Li   }
8177*67e74705SXin Li   ELoc = RefExpr->getExprLoc();
8178*67e74705SXin Li   ERange = RefExpr->getSourceRange();
8179*67e74705SXin Li   RefExpr = RefExpr->IgnoreParenImpCasts();
8180*67e74705SXin Li   auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
8181*67e74705SXin Li   auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
8182*67e74705SXin Li   if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
8183*67e74705SXin Li       (S.getCurrentThisType().isNull() || !ME ||
8184*67e74705SXin Li        !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
8185*67e74705SXin Li        !isa<FieldDecl>(ME->getMemberDecl()))) {
8186*67e74705SXin Li     if (IsArrayExpr != NoArrayExpr)
8187*67e74705SXin Li       S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
8188*67e74705SXin Li                                                          << ERange;
8189*67e74705SXin Li     else {
8190*67e74705SXin Li       S.Diag(ELoc,
8191*67e74705SXin Li              AllowArraySection
8192*67e74705SXin Li                  ? diag::err_omp_expected_var_name_member_expr_or_array_item
8193*67e74705SXin Li                  : diag::err_omp_expected_var_name_member_expr)
8194*67e74705SXin Li           << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
8195*67e74705SXin Li     }
8196*67e74705SXin Li     return std::make_pair(nullptr, false);
8197*67e74705SXin Li   }
8198*67e74705SXin Li   return std::make_pair(DE ? DE->getDecl() : ME->getMemberDecl(), false);
8199*67e74705SXin Li }
8200*67e74705SXin Li 
ActOnOpenMPPrivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)8201*67e74705SXin Li OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
8202*67e74705SXin Li                                           SourceLocation StartLoc,
8203*67e74705SXin Li                                           SourceLocation LParenLoc,
8204*67e74705SXin Li                                           SourceLocation EndLoc) {
8205*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
8206*67e74705SXin Li   SmallVector<Expr *, 8> PrivateCopies;
8207*67e74705SXin Li   for (auto &RefExpr : VarList) {
8208*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP private clause.");
8209*67e74705SXin Li     SourceLocation ELoc;
8210*67e74705SXin Li     SourceRange ERange;
8211*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
8212*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8213*67e74705SXin Li     if (Res.second) {
8214*67e74705SXin Li       // It will be analyzed later.
8215*67e74705SXin Li       Vars.push_back(RefExpr);
8216*67e74705SXin Li       PrivateCopies.push_back(nullptr);
8217*67e74705SXin Li     }
8218*67e74705SXin Li     ValueDecl *D = Res.first;
8219*67e74705SXin Li     if (!D)
8220*67e74705SXin Li       continue;
8221*67e74705SXin Li 
8222*67e74705SXin Li     QualType Type = D->getType();
8223*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
8224*67e74705SXin Li 
8225*67e74705SXin Li     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8226*67e74705SXin Li     //  A variable that appears in a private clause must not have an incomplete
8227*67e74705SXin Li     //  type or a reference type.
8228*67e74705SXin Li     if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
8229*67e74705SXin Li       continue;
8230*67e74705SXin Li     Type = Type.getNonReferenceType();
8231*67e74705SXin Li 
8232*67e74705SXin Li     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8233*67e74705SXin Li     // in a Construct]
8234*67e74705SXin Li     //  Variables with the predetermined data-sharing attributes may not be
8235*67e74705SXin Li     //  listed in data-sharing attributes clauses, except for the cases
8236*67e74705SXin Li     //  listed below. For these exceptions only, listing a predetermined
8237*67e74705SXin Li     //  variable in a data-sharing attribute clause is allowed and overrides
8238*67e74705SXin Li     //  the variable's predetermined data-sharing attributes.
8239*67e74705SXin Li     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8240*67e74705SXin Li     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
8241*67e74705SXin Li       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8242*67e74705SXin Li                                           << getOpenMPClauseName(OMPC_private);
8243*67e74705SXin Li       ReportOriginalDSA(*this, DSAStack, D, DVar);
8244*67e74705SXin Li       continue;
8245*67e74705SXin Li     }
8246*67e74705SXin Li 
8247*67e74705SXin Li     // Variably modified types are not supported for tasks.
8248*67e74705SXin Li     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
8249*67e74705SXin Li         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
8250*67e74705SXin Li       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8251*67e74705SXin Li           << getOpenMPClauseName(OMPC_private) << Type
8252*67e74705SXin Li           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8253*67e74705SXin Li       bool IsDecl =
8254*67e74705SXin Li           !VD ||
8255*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8256*67e74705SXin Li       Diag(D->getLocation(),
8257*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8258*67e74705SXin Li           << D;
8259*67e74705SXin Li       continue;
8260*67e74705SXin Li     }
8261*67e74705SXin Li 
8262*67e74705SXin Li     // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8263*67e74705SXin Li     // A list item cannot appear in both a map clause and a data-sharing
8264*67e74705SXin Li     // attribute clause on the same construct
8265*67e74705SXin Li     if (DSAStack->getCurrentDirective() == OMPD_target) {
8266*67e74705SXin Li       if (DSAStack->checkMappableExprComponentListsForDecl(
8267*67e74705SXin Li               VD, /* CurrentRegionOnly = */ true,
8268*67e74705SXin Li               [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8269*67e74705SXin Li                   -> bool { return true; })) {
8270*67e74705SXin Li         Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8271*67e74705SXin Li             << getOpenMPClauseName(OMPC_private)
8272*67e74705SXin Li             << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8273*67e74705SXin Li         ReportOriginalDSA(*this, DSAStack, D, DVar);
8274*67e74705SXin Li         continue;
8275*67e74705SXin Li       }
8276*67e74705SXin Li     }
8277*67e74705SXin Li 
8278*67e74705SXin Li     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
8279*67e74705SXin Li     //  A variable of class type (or array thereof) that appears in a private
8280*67e74705SXin Li     //  clause requires an accessible, unambiguous default constructor for the
8281*67e74705SXin Li     //  class type.
8282*67e74705SXin Li     // Generate helper private variable and initialize it with the default
8283*67e74705SXin Li     // value. The address of the original variable is replaced by the address of
8284*67e74705SXin Li     // the new private variable in CodeGen. This new variable is not added to
8285*67e74705SXin Li     // IdResolver, so the code in the OpenMP region uses original variable for
8286*67e74705SXin Li     // proper diagnostics.
8287*67e74705SXin Li     Type = Type.getUnqualifiedType();
8288*67e74705SXin Li     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8289*67e74705SXin Li                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
8290*67e74705SXin Li     ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
8291*67e74705SXin Li     if (VDPrivate->isInvalidDecl())
8292*67e74705SXin Li       continue;
8293*67e74705SXin Li     auto VDPrivateRefExpr = buildDeclRefExpr(
8294*67e74705SXin Li         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
8295*67e74705SXin Li 
8296*67e74705SXin Li     DeclRefExpr *Ref = nullptr;
8297*67e74705SXin Li     if (!VD && !CurContext->isDependentContext())
8298*67e74705SXin Li       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8299*67e74705SXin Li     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
8300*67e74705SXin Li     Vars.push_back((VD || CurContext->isDependentContext())
8301*67e74705SXin Li                        ? RefExpr->IgnoreParens()
8302*67e74705SXin Li                        : Ref);
8303*67e74705SXin Li     PrivateCopies.push_back(VDPrivateRefExpr);
8304*67e74705SXin Li   }
8305*67e74705SXin Li 
8306*67e74705SXin Li   if (Vars.empty())
8307*67e74705SXin Li     return nullptr;
8308*67e74705SXin Li 
8309*67e74705SXin Li   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8310*67e74705SXin Li                                   PrivateCopies);
8311*67e74705SXin Li }
8312*67e74705SXin Li 
8313*67e74705SXin Li namespace {
8314*67e74705SXin Li class DiagsUninitializedSeveretyRAII {
8315*67e74705SXin Li private:
8316*67e74705SXin Li   DiagnosticsEngine &Diags;
8317*67e74705SXin Li   SourceLocation SavedLoc;
8318*67e74705SXin Li   bool IsIgnored;
8319*67e74705SXin Li 
8320*67e74705SXin Li public:
DiagsUninitializedSeveretyRAII(DiagnosticsEngine & Diags,SourceLocation Loc,bool IsIgnored)8321*67e74705SXin Li   DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
8322*67e74705SXin Li                                  bool IsIgnored)
8323*67e74705SXin Li       : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
8324*67e74705SXin Li     if (!IsIgnored) {
8325*67e74705SXin Li       Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
8326*67e74705SXin Li                         /*Map*/ diag::Severity::Ignored, Loc);
8327*67e74705SXin Li     }
8328*67e74705SXin Li   }
~DiagsUninitializedSeveretyRAII()8329*67e74705SXin Li   ~DiagsUninitializedSeveretyRAII() {
8330*67e74705SXin Li     if (!IsIgnored)
8331*67e74705SXin Li       Diags.popMappings(SavedLoc);
8332*67e74705SXin Li   }
8333*67e74705SXin Li };
8334*67e74705SXin Li }
8335*67e74705SXin Li 
ActOnOpenMPFirstprivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)8336*67e74705SXin Li OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
8337*67e74705SXin Li                                                SourceLocation StartLoc,
8338*67e74705SXin Li                                                SourceLocation LParenLoc,
8339*67e74705SXin Li                                                SourceLocation EndLoc) {
8340*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
8341*67e74705SXin Li   SmallVector<Expr *, 8> PrivateCopies;
8342*67e74705SXin Li   SmallVector<Expr *, 8> Inits;
8343*67e74705SXin Li   SmallVector<Decl *, 4> ExprCaptures;
8344*67e74705SXin Li   bool IsImplicitClause =
8345*67e74705SXin Li       StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
8346*67e74705SXin Li   auto ImplicitClauseLoc = DSAStack->getConstructLoc();
8347*67e74705SXin Li 
8348*67e74705SXin Li   for (auto &RefExpr : VarList) {
8349*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
8350*67e74705SXin Li     SourceLocation ELoc;
8351*67e74705SXin Li     SourceRange ERange;
8352*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
8353*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8354*67e74705SXin Li     if (Res.second) {
8355*67e74705SXin Li       // It will be analyzed later.
8356*67e74705SXin Li       Vars.push_back(RefExpr);
8357*67e74705SXin Li       PrivateCopies.push_back(nullptr);
8358*67e74705SXin Li       Inits.push_back(nullptr);
8359*67e74705SXin Li     }
8360*67e74705SXin Li     ValueDecl *D = Res.first;
8361*67e74705SXin Li     if (!D)
8362*67e74705SXin Li       continue;
8363*67e74705SXin Li 
8364*67e74705SXin Li     ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
8365*67e74705SXin Li     QualType Type = D->getType();
8366*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
8367*67e74705SXin Li 
8368*67e74705SXin Li     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
8369*67e74705SXin Li     //  A variable that appears in a private clause must not have an incomplete
8370*67e74705SXin Li     //  type or a reference type.
8371*67e74705SXin Li     if (RequireCompleteType(ELoc, Type,
8372*67e74705SXin Li                             diag::err_omp_firstprivate_incomplete_type))
8373*67e74705SXin Li       continue;
8374*67e74705SXin Li     Type = Type.getNonReferenceType();
8375*67e74705SXin Li 
8376*67e74705SXin Li     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
8377*67e74705SXin Li     //  A variable of class type (or array thereof) that appears in a private
8378*67e74705SXin Li     //  clause requires an accessible, unambiguous copy constructor for the
8379*67e74705SXin Li     //  class type.
8380*67e74705SXin Li     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
8381*67e74705SXin Li 
8382*67e74705SXin Li     // If an implicit firstprivate variable found it was checked already.
8383*67e74705SXin Li     DSAStackTy::DSAVarData TopDVar;
8384*67e74705SXin Li     if (!IsImplicitClause) {
8385*67e74705SXin Li       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8386*67e74705SXin Li       TopDVar = DVar;
8387*67e74705SXin Li       bool IsConstant = ElemType.isConstant(Context);
8388*67e74705SXin Li       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
8389*67e74705SXin Li       //  A list item that specifies a given variable may not appear in more
8390*67e74705SXin Li       // than one clause on the same directive, except that a variable may be
8391*67e74705SXin Li       //  specified in both firstprivate and lastprivate clauses.
8392*67e74705SXin Li       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
8393*67e74705SXin Li           DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
8394*67e74705SXin Li         Diag(ELoc, diag::err_omp_wrong_dsa)
8395*67e74705SXin Li             << getOpenMPClauseName(DVar.CKind)
8396*67e74705SXin Li             << getOpenMPClauseName(OMPC_firstprivate);
8397*67e74705SXin Li         ReportOriginalDSA(*this, DSAStack, D, DVar);
8398*67e74705SXin Li         continue;
8399*67e74705SXin Li       }
8400*67e74705SXin Li 
8401*67e74705SXin Li       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8402*67e74705SXin Li       // in a Construct]
8403*67e74705SXin Li       //  Variables with the predetermined data-sharing attributes may not be
8404*67e74705SXin Li       //  listed in data-sharing attributes clauses, except for the cases
8405*67e74705SXin Li       //  listed below. For these exceptions only, listing a predetermined
8406*67e74705SXin Li       //  variable in a data-sharing attribute clause is allowed and overrides
8407*67e74705SXin Li       //  the variable's predetermined data-sharing attributes.
8408*67e74705SXin Li       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8409*67e74705SXin Li       // in a Construct, C/C++, p.2]
8410*67e74705SXin Li       //  Variables with const-qualified type having no mutable member may be
8411*67e74705SXin Li       //  listed in a firstprivate clause, even if they are static data members.
8412*67e74705SXin Li       if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
8413*67e74705SXin Li           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
8414*67e74705SXin Li         Diag(ELoc, diag::err_omp_wrong_dsa)
8415*67e74705SXin Li             << getOpenMPClauseName(DVar.CKind)
8416*67e74705SXin Li             << getOpenMPClauseName(OMPC_firstprivate);
8417*67e74705SXin Li         ReportOriginalDSA(*this, DSAStack, D, DVar);
8418*67e74705SXin Li         continue;
8419*67e74705SXin Li       }
8420*67e74705SXin Li 
8421*67e74705SXin Li       OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8422*67e74705SXin Li       // OpenMP [2.9.3.4, Restrictions, p.2]
8423*67e74705SXin Li       //  A list item that is private within a parallel region must not appear
8424*67e74705SXin Li       //  in a firstprivate clause on a worksharing construct if any of the
8425*67e74705SXin Li       //  worksharing regions arising from the worksharing construct ever bind
8426*67e74705SXin Li       //  to any of the parallel regions arising from the parallel construct.
8427*67e74705SXin Li       if (isOpenMPWorksharingDirective(CurrDir) &&
8428*67e74705SXin Li           !isOpenMPParallelDirective(CurrDir)) {
8429*67e74705SXin Li         DVar = DSAStack->getImplicitDSA(D, true);
8430*67e74705SXin Li         if (DVar.CKind != OMPC_shared &&
8431*67e74705SXin Li             (isOpenMPParallelDirective(DVar.DKind) ||
8432*67e74705SXin Li              DVar.DKind == OMPD_unknown)) {
8433*67e74705SXin Li           Diag(ELoc, diag::err_omp_required_access)
8434*67e74705SXin Li               << getOpenMPClauseName(OMPC_firstprivate)
8435*67e74705SXin Li               << getOpenMPClauseName(OMPC_shared);
8436*67e74705SXin Li           ReportOriginalDSA(*this, DSAStack, D, DVar);
8437*67e74705SXin Li           continue;
8438*67e74705SXin Li         }
8439*67e74705SXin Li       }
8440*67e74705SXin Li       // OpenMP [2.9.3.4, Restrictions, p.3]
8441*67e74705SXin Li       //  A list item that appears in a reduction clause of a parallel construct
8442*67e74705SXin Li       //  must not appear in a firstprivate clause on a worksharing or task
8443*67e74705SXin Li       //  construct if any of the worksharing or task regions arising from the
8444*67e74705SXin Li       //  worksharing or task construct ever bind to any of the parallel regions
8445*67e74705SXin Li       //  arising from the parallel construct.
8446*67e74705SXin Li       // OpenMP [2.9.3.4, Restrictions, p.4]
8447*67e74705SXin Li       //  A list item that appears in a reduction clause in worksharing
8448*67e74705SXin Li       //  construct must not appear in a firstprivate clause in a task construct
8449*67e74705SXin Li       //  encountered during execution of any of the worksharing regions arising
8450*67e74705SXin Li       //  from the worksharing construct.
8451*67e74705SXin Li       if (isOpenMPTaskingDirective(CurrDir)) {
8452*67e74705SXin Li         DVar = DSAStack->hasInnermostDSA(
8453*67e74705SXin Li             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8454*67e74705SXin Li             [](OpenMPDirectiveKind K) -> bool {
8455*67e74705SXin Li               return isOpenMPParallelDirective(K) ||
8456*67e74705SXin Li                      isOpenMPWorksharingDirective(K);
8457*67e74705SXin Li             },
8458*67e74705SXin Li             false);
8459*67e74705SXin Li         if (DVar.CKind == OMPC_reduction &&
8460*67e74705SXin Li             (isOpenMPParallelDirective(DVar.DKind) ||
8461*67e74705SXin Li              isOpenMPWorksharingDirective(DVar.DKind))) {
8462*67e74705SXin Li           Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
8463*67e74705SXin Li               << getOpenMPDirectiveName(DVar.DKind);
8464*67e74705SXin Li           ReportOriginalDSA(*this, DSAStack, D, DVar);
8465*67e74705SXin Li           continue;
8466*67e74705SXin Li         }
8467*67e74705SXin Li       }
8468*67e74705SXin Li 
8469*67e74705SXin Li       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8470*67e74705SXin Li       // A list item that is private within a teams region must not appear in a
8471*67e74705SXin Li       // firstprivate clause on a distribute construct if any of the distribute
8472*67e74705SXin Li       // regions arising from the distribute construct ever bind to any of the
8473*67e74705SXin Li       // teams regions arising from the teams construct.
8474*67e74705SXin Li       // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
8475*67e74705SXin Li       // A list item that appears in a reduction clause of a teams construct
8476*67e74705SXin Li       // must not appear in a firstprivate clause on a distribute construct if
8477*67e74705SXin Li       // any of the distribute regions arising from the distribute construct
8478*67e74705SXin Li       // ever bind to any of the teams regions arising from the teams construct.
8479*67e74705SXin Li       // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8480*67e74705SXin Li       // A list item may appear in a firstprivate or lastprivate clause but not
8481*67e74705SXin Li       // both.
8482*67e74705SXin Li       if (CurrDir == OMPD_distribute) {
8483*67e74705SXin Li         DVar = DSAStack->hasInnermostDSA(
8484*67e74705SXin Li             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_private; },
8485*67e74705SXin Li             [](OpenMPDirectiveKind K) -> bool {
8486*67e74705SXin Li               return isOpenMPTeamsDirective(K);
8487*67e74705SXin Li             },
8488*67e74705SXin Li             false);
8489*67e74705SXin Li         if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
8490*67e74705SXin Li           Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
8491*67e74705SXin Li           ReportOriginalDSA(*this, DSAStack, D, DVar);
8492*67e74705SXin Li           continue;
8493*67e74705SXin Li         }
8494*67e74705SXin Li         DVar = DSAStack->hasInnermostDSA(
8495*67e74705SXin Li             D, [](OpenMPClauseKind C) -> bool { return C == OMPC_reduction; },
8496*67e74705SXin Li             [](OpenMPDirectiveKind K) -> bool {
8497*67e74705SXin Li               return isOpenMPTeamsDirective(K);
8498*67e74705SXin Li             },
8499*67e74705SXin Li             false);
8500*67e74705SXin Li         if (DVar.CKind == OMPC_reduction &&
8501*67e74705SXin Li             isOpenMPTeamsDirective(DVar.DKind)) {
8502*67e74705SXin Li           Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
8503*67e74705SXin Li           ReportOriginalDSA(*this, DSAStack, D, DVar);
8504*67e74705SXin Li           continue;
8505*67e74705SXin Li         }
8506*67e74705SXin Li         DVar = DSAStack->getTopDSA(D, false);
8507*67e74705SXin Li         if (DVar.CKind == OMPC_lastprivate) {
8508*67e74705SXin Li           Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8509*67e74705SXin Li           ReportOriginalDSA(*this, DSAStack, D, DVar);
8510*67e74705SXin Li           continue;
8511*67e74705SXin Li         }
8512*67e74705SXin Li       }
8513*67e74705SXin Li       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
8514*67e74705SXin Li       // A list item cannot appear in both a map clause and a data-sharing
8515*67e74705SXin Li       // attribute clause on the same construct
8516*67e74705SXin Li       if (CurrDir == OMPD_target) {
8517*67e74705SXin Li         if (DSAStack->checkMappableExprComponentListsForDecl(
8518*67e74705SXin Li                 VD, /* CurrentRegionOnly = */ true,
8519*67e74705SXin Li                 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef)
8520*67e74705SXin Li                     -> bool { return true; })) {
8521*67e74705SXin Li           Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
8522*67e74705SXin Li               << getOpenMPClauseName(OMPC_firstprivate)
8523*67e74705SXin Li               << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8524*67e74705SXin Li           ReportOriginalDSA(*this, DSAStack, D, DVar);
8525*67e74705SXin Li           continue;
8526*67e74705SXin Li         }
8527*67e74705SXin Li       }
8528*67e74705SXin Li     }
8529*67e74705SXin Li 
8530*67e74705SXin Li     // Variably modified types are not supported for tasks.
8531*67e74705SXin Li     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
8532*67e74705SXin Li         isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
8533*67e74705SXin Li       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
8534*67e74705SXin Li           << getOpenMPClauseName(OMPC_firstprivate) << Type
8535*67e74705SXin Li           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
8536*67e74705SXin Li       bool IsDecl =
8537*67e74705SXin Li           !VD ||
8538*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
8539*67e74705SXin Li       Diag(D->getLocation(),
8540*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
8541*67e74705SXin Li           << D;
8542*67e74705SXin Li       continue;
8543*67e74705SXin Li     }
8544*67e74705SXin Li 
8545*67e74705SXin Li     Type = Type.getUnqualifiedType();
8546*67e74705SXin Li     auto VDPrivate = buildVarDecl(*this, ELoc, Type, D->getName(),
8547*67e74705SXin Li                                   D->hasAttrs() ? &D->getAttrs() : nullptr);
8548*67e74705SXin Li     // Generate helper private variable and initialize it with the value of the
8549*67e74705SXin Li     // original variable. The address of the original variable is replaced by
8550*67e74705SXin Li     // the address of the new private variable in the CodeGen. This new variable
8551*67e74705SXin Li     // is not added to IdResolver, so the code in the OpenMP region uses
8552*67e74705SXin Li     // original variable for proper diagnostics and variable capturing.
8553*67e74705SXin Li     Expr *VDInitRefExpr = nullptr;
8554*67e74705SXin Li     // For arrays generate initializer for single element and replace it by the
8555*67e74705SXin Li     // original array element in CodeGen.
8556*67e74705SXin Li     if (Type->isArrayType()) {
8557*67e74705SXin Li       auto VDInit =
8558*67e74705SXin Li           buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
8559*67e74705SXin Li       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
8560*67e74705SXin Li       auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
8561*67e74705SXin Li       ElemType = ElemType.getUnqualifiedType();
8562*67e74705SXin Li       auto *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
8563*67e74705SXin Li                                       ".firstprivate.temp");
8564*67e74705SXin Li       InitializedEntity Entity =
8565*67e74705SXin Li           InitializedEntity::InitializeVariable(VDInitTemp);
8566*67e74705SXin Li       InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
8567*67e74705SXin Li 
8568*67e74705SXin Li       InitializationSequence InitSeq(*this, Entity, Kind, Init);
8569*67e74705SXin Li       ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
8570*67e74705SXin Li       if (Result.isInvalid())
8571*67e74705SXin Li         VDPrivate->setInvalidDecl();
8572*67e74705SXin Li       else
8573*67e74705SXin Li         VDPrivate->setInit(Result.getAs<Expr>());
8574*67e74705SXin Li       // Remove temp variable declaration.
8575*67e74705SXin Li       Context.Deallocate(VDInitTemp);
8576*67e74705SXin Li     } else {
8577*67e74705SXin Li       auto *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
8578*67e74705SXin Li                                   ".firstprivate.temp");
8579*67e74705SXin Li       VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
8580*67e74705SXin Li                                        RefExpr->getExprLoc());
8581*67e74705SXin Li       AddInitializerToDecl(VDPrivate,
8582*67e74705SXin Li                            DefaultLvalueConversion(VDInitRefExpr).get(),
8583*67e74705SXin Li                            /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
8584*67e74705SXin Li     }
8585*67e74705SXin Li     if (VDPrivate->isInvalidDecl()) {
8586*67e74705SXin Li       if (IsImplicitClause) {
8587*67e74705SXin Li         Diag(RefExpr->getExprLoc(),
8588*67e74705SXin Li              diag::note_omp_task_predetermined_firstprivate_here);
8589*67e74705SXin Li       }
8590*67e74705SXin Li       continue;
8591*67e74705SXin Li     }
8592*67e74705SXin Li     CurContext->addDecl(VDPrivate);
8593*67e74705SXin Li     auto VDPrivateRefExpr = buildDeclRefExpr(
8594*67e74705SXin Li         *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
8595*67e74705SXin Li         RefExpr->getExprLoc());
8596*67e74705SXin Li     DeclRefExpr *Ref = nullptr;
8597*67e74705SXin Li     if (!VD && !CurContext->isDependentContext()) {
8598*67e74705SXin Li       if (TopDVar.CKind == OMPC_lastprivate)
8599*67e74705SXin Li         Ref = TopDVar.PrivateCopy;
8600*67e74705SXin Li       else {
8601*67e74705SXin Li         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8602*67e74705SXin Li         if (!IsOpenMPCapturedDecl(D))
8603*67e74705SXin Li           ExprCaptures.push_back(Ref->getDecl());
8604*67e74705SXin Li       }
8605*67e74705SXin Li     }
8606*67e74705SXin Li     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
8607*67e74705SXin Li     Vars.push_back((VD || CurContext->isDependentContext())
8608*67e74705SXin Li                        ? RefExpr->IgnoreParens()
8609*67e74705SXin Li                        : Ref);
8610*67e74705SXin Li     PrivateCopies.push_back(VDPrivateRefExpr);
8611*67e74705SXin Li     Inits.push_back(VDInitRefExpr);
8612*67e74705SXin Li   }
8613*67e74705SXin Li 
8614*67e74705SXin Li   if (Vars.empty())
8615*67e74705SXin Li     return nullptr;
8616*67e74705SXin Li 
8617*67e74705SXin Li   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8618*67e74705SXin Li                                        Vars, PrivateCopies, Inits,
8619*67e74705SXin Li                                        buildPreInits(Context, ExprCaptures));
8620*67e74705SXin Li }
8621*67e74705SXin Li 
ActOnOpenMPLastprivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)8622*67e74705SXin Li OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
8623*67e74705SXin Li                                               SourceLocation StartLoc,
8624*67e74705SXin Li                                               SourceLocation LParenLoc,
8625*67e74705SXin Li                                               SourceLocation EndLoc) {
8626*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
8627*67e74705SXin Li   SmallVector<Expr *, 8> SrcExprs;
8628*67e74705SXin Li   SmallVector<Expr *, 8> DstExprs;
8629*67e74705SXin Li   SmallVector<Expr *, 8> AssignmentOps;
8630*67e74705SXin Li   SmallVector<Decl *, 4> ExprCaptures;
8631*67e74705SXin Li   SmallVector<Expr *, 4> ExprPostUpdates;
8632*67e74705SXin Li   for (auto &RefExpr : VarList) {
8633*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
8634*67e74705SXin Li     SourceLocation ELoc;
8635*67e74705SXin Li     SourceRange ERange;
8636*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
8637*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8638*67e74705SXin Li     if (Res.second) {
8639*67e74705SXin Li       // It will be analyzed later.
8640*67e74705SXin Li       Vars.push_back(RefExpr);
8641*67e74705SXin Li       SrcExprs.push_back(nullptr);
8642*67e74705SXin Li       DstExprs.push_back(nullptr);
8643*67e74705SXin Li       AssignmentOps.push_back(nullptr);
8644*67e74705SXin Li     }
8645*67e74705SXin Li     ValueDecl *D = Res.first;
8646*67e74705SXin Li     if (!D)
8647*67e74705SXin Li       continue;
8648*67e74705SXin Li 
8649*67e74705SXin Li     QualType Type = D->getType();
8650*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
8651*67e74705SXin Li 
8652*67e74705SXin Li     // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
8653*67e74705SXin Li     //  A variable that appears in a lastprivate clause must not have an
8654*67e74705SXin Li     //  incomplete type or a reference type.
8655*67e74705SXin Li     if (RequireCompleteType(ELoc, Type,
8656*67e74705SXin Li                             diag::err_omp_lastprivate_incomplete_type))
8657*67e74705SXin Li       continue;
8658*67e74705SXin Li     Type = Type.getNonReferenceType();
8659*67e74705SXin Li 
8660*67e74705SXin Li     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
8661*67e74705SXin Li     // in a Construct]
8662*67e74705SXin Li     //  Variables with the predetermined data-sharing attributes may not be
8663*67e74705SXin Li     //  listed in data-sharing attributes clauses, except for the cases
8664*67e74705SXin Li     //  listed below.
8665*67e74705SXin Li     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8666*67e74705SXin Li     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
8667*67e74705SXin Li         DVar.CKind != OMPC_firstprivate &&
8668*67e74705SXin Li         (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
8669*67e74705SXin Li       Diag(ELoc, diag::err_omp_wrong_dsa)
8670*67e74705SXin Li           << getOpenMPClauseName(DVar.CKind)
8671*67e74705SXin Li           << getOpenMPClauseName(OMPC_lastprivate);
8672*67e74705SXin Li       ReportOriginalDSA(*this, DSAStack, D, DVar);
8673*67e74705SXin Li       continue;
8674*67e74705SXin Li     }
8675*67e74705SXin Li 
8676*67e74705SXin Li     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
8677*67e74705SXin Li     // OpenMP [2.14.3.5, Restrictions, p.2]
8678*67e74705SXin Li     // A list item that is private within a parallel region, or that appears in
8679*67e74705SXin Li     // the reduction clause of a parallel construct, must not appear in a
8680*67e74705SXin Li     // lastprivate clause on a worksharing construct if any of the corresponding
8681*67e74705SXin Li     // worksharing regions ever binds to any of the corresponding parallel
8682*67e74705SXin Li     // regions.
8683*67e74705SXin Li     DSAStackTy::DSAVarData TopDVar = DVar;
8684*67e74705SXin Li     if (isOpenMPWorksharingDirective(CurrDir) &&
8685*67e74705SXin Li         !isOpenMPParallelDirective(CurrDir)) {
8686*67e74705SXin Li       DVar = DSAStack->getImplicitDSA(D, true);
8687*67e74705SXin Li       if (DVar.CKind != OMPC_shared) {
8688*67e74705SXin Li         Diag(ELoc, diag::err_omp_required_access)
8689*67e74705SXin Li             << getOpenMPClauseName(OMPC_lastprivate)
8690*67e74705SXin Li             << getOpenMPClauseName(OMPC_shared);
8691*67e74705SXin Li         ReportOriginalDSA(*this, DSAStack, D, DVar);
8692*67e74705SXin Li         continue;
8693*67e74705SXin Li       }
8694*67e74705SXin Li     }
8695*67e74705SXin Li 
8696*67e74705SXin Li     // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
8697*67e74705SXin Li     // A list item may appear in a firstprivate or lastprivate clause but not
8698*67e74705SXin Li     // both.
8699*67e74705SXin Li     if (CurrDir == OMPD_distribute) {
8700*67e74705SXin Li       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8701*67e74705SXin Li       if (DVar.CKind == OMPC_firstprivate) {
8702*67e74705SXin Li         Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
8703*67e74705SXin Li         ReportOriginalDSA(*this, DSAStack, D, DVar);
8704*67e74705SXin Li         continue;
8705*67e74705SXin Li       }
8706*67e74705SXin Li     }
8707*67e74705SXin Li 
8708*67e74705SXin Li     // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
8709*67e74705SXin Li     //  A variable of class type (or array thereof) that appears in a
8710*67e74705SXin Li     //  lastprivate clause requires an accessible, unambiguous default
8711*67e74705SXin Li     //  constructor for the class type, unless the list item is also specified
8712*67e74705SXin Li     //  in a firstprivate clause.
8713*67e74705SXin Li     //  A variable of class type (or array thereof) that appears in a
8714*67e74705SXin Li     //  lastprivate clause requires an accessible, unambiguous copy assignment
8715*67e74705SXin Li     //  operator for the class type.
8716*67e74705SXin Li     Type = Context.getBaseElementType(Type).getNonReferenceType();
8717*67e74705SXin Li     auto *SrcVD = buildVarDecl(*this, ERange.getBegin(),
8718*67e74705SXin Li                                Type.getUnqualifiedType(), ".lastprivate.src",
8719*67e74705SXin Li                                D->hasAttrs() ? &D->getAttrs() : nullptr);
8720*67e74705SXin Li     auto *PseudoSrcExpr =
8721*67e74705SXin Li         buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
8722*67e74705SXin Li     auto *DstVD =
8723*67e74705SXin Li         buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
8724*67e74705SXin Li                      D->hasAttrs() ? &D->getAttrs() : nullptr);
8725*67e74705SXin Li     auto *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
8726*67e74705SXin Li     // For arrays generate assignment operation for single element and replace
8727*67e74705SXin Li     // it by the original array element in CodeGen.
8728*67e74705SXin Li     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
8729*67e74705SXin Li                                    PseudoDstExpr, PseudoSrcExpr);
8730*67e74705SXin Li     if (AssignmentOp.isInvalid())
8731*67e74705SXin Li       continue;
8732*67e74705SXin Li     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
8733*67e74705SXin Li                                        /*DiscardedValue=*/true);
8734*67e74705SXin Li     if (AssignmentOp.isInvalid())
8735*67e74705SXin Li       continue;
8736*67e74705SXin Li 
8737*67e74705SXin Li     DeclRefExpr *Ref = nullptr;
8738*67e74705SXin Li     if (!VD && !CurContext->isDependentContext()) {
8739*67e74705SXin Li       if (TopDVar.CKind == OMPC_firstprivate)
8740*67e74705SXin Li         Ref = TopDVar.PrivateCopy;
8741*67e74705SXin Li       else {
8742*67e74705SXin Li         Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
8743*67e74705SXin Li         if (!IsOpenMPCapturedDecl(D))
8744*67e74705SXin Li           ExprCaptures.push_back(Ref->getDecl());
8745*67e74705SXin Li       }
8746*67e74705SXin Li       if (TopDVar.CKind == OMPC_firstprivate ||
8747*67e74705SXin Li           (!IsOpenMPCapturedDecl(D) &&
8748*67e74705SXin Li            Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
8749*67e74705SXin Li         ExprResult RefRes = DefaultLvalueConversion(Ref);
8750*67e74705SXin Li         if (!RefRes.isUsable())
8751*67e74705SXin Li           continue;
8752*67e74705SXin Li         ExprResult PostUpdateRes =
8753*67e74705SXin Li             BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
8754*67e74705SXin Li                        RefRes.get());
8755*67e74705SXin Li         if (!PostUpdateRes.isUsable())
8756*67e74705SXin Li           continue;
8757*67e74705SXin Li         ExprPostUpdates.push_back(
8758*67e74705SXin Li             IgnoredValueConversions(PostUpdateRes.get()).get());
8759*67e74705SXin Li       }
8760*67e74705SXin Li     }
8761*67e74705SXin Li     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
8762*67e74705SXin Li     Vars.push_back((VD || CurContext->isDependentContext())
8763*67e74705SXin Li                        ? RefExpr->IgnoreParens()
8764*67e74705SXin Li                        : Ref);
8765*67e74705SXin Li     SrcExprs.push_back(PseudoSrcExpr);
8766*67e74705SXin Li     DstExprs.push_back(PseudoDstExpr);
8767*67e74705SXin Li     AssignmentOps.push_back(AssignmentOp.get());
8768*67e74705SXin Li   }
8769*67e74705SXin Li 
8770*67e74705SXin Li   if (Vars.empty())
8771*67e74705SXin Li     return nullptr;
8772*67e74705SXin Li 
8773*67e74705SXin Li   return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
8774*67e74705SXin Li                                       Vars, SrcExprs, DstExprs, AssignmentOps,
8775*67e74705SXin Li                                       buildPreInits(Context, ExprCaptures),
8776*67e74705SXin Li                                       buildPostUpdate(*this, ExprPostUpdates));
8777*67e74705SXin Li }
8778*67e74705SXin Li 
ActOnOpenMPSharedClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)8779*67e74705SXin Li OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
8780*67e74705SXin Li                                          SourceLocation StartLoc,
8781*67e74705SXin Li                                          SourceLocation LParenLoc,
8782*67e74705SXin Li                                          SourceLocation EndLoc) {
8783*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
8784*67e74705SXin Li   for (auto &RefExpr : VarList) {
8785*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
8786*67e74705SXin Li     SourceLocation ELoc;
8787*67e74705SXin Li     SourceRange ERange;
8788*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
8789*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
8790*67e74705SXin Li     if (Res.second) {
8791*67e74705SXin Li       // It will be analyzed later.
8792*67e74705SXin Li       Vars.push_back(RefExpr);
8793*67e74705SXin Li     }
8794*67e74705SXin Li     ValueDecl *D = Res.first;
8795*67e74705SXin Li     if (!D)
8796*67e74705SXin Li       continue;
8797*67e74705SXin Li 
8798*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
8799*67e74705SXin Li     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
8800*67e74705SXin Li     // in a Construct]
8801*67e74705SXin Li     //  Variables with the predetermined data-sharing attributes may not be
8802*67e74705SXin Li     //  listed in data-sharing attributes clauses, except for the cases
8803*67e74705SXin Li     //  listed below. For these exceptions only, listing a predetermined
8804*67e74705SXin Li     //  variable in a data-sharing attribute clause is allowed and overrides
8805*67e74705SXin Li     //  the variable's predetermined data-sharing attributes.
8806*67e74705SXin Li     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
8807*67e74705SXin Li     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
8808*67e74705SXin Li         DVar.RefExpr) {
8809*67e74705SXin Li       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
8810*67e74705SXin Li                                           << getOpenMPClauseName(OMPC_shared);
8811*67e74705SXin Li       ReportOriginalDSA(*this, DSAStack, D, DVar);
8812*67e74705SXin Li       continue;
8813*67e74705SXin Li     }
8814*67e74705SXin Li 
8815*67e74705SXin Li     DeclRefExpr *Ref = nullptr;
8816*67e74705SXin Li     if (!VD && IsOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
8817*67e74705SXin Li       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
8818*67e74705SXin Li     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
8819*67e74705SXin Li     Vars.push_back((VD || !Ref || CurContext->isDependentContext())
8820*67e74705SXin Li                        ? RefExpr->IgnoreParens()
8821*67e74705SXin Li                        : Ref);
8822*67e74705SXin Li   }
8823*67e74705SXin Li 
8824*67e74705SXin Li   if (Vars.empty())
8825*67e74705SXin Li     return nullptr;
8826*67e74705SXin Li 
8827*67e74705SXin Li   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
8828*67e74705SXin Li }
8829*67e74705SXin Li 
8830*67e74705SXin Li namespace {
8831*67e74705SXin Li class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
8832*67e74705SXin Li   DSAStackTy *Stack;
8833*67e74705SXin Li 
8834*67e74705SXin Li public:
VisitDeclRefExpr(DeclRefExpr * E)8835*67e74705SXin Li   bool VisitDeclRefExpr(DeclRefExpr *E) {
8836*67e74705SXin Li     if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
8837*67e74705SXin Li       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
8838*67e74705SXin Li       if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
8839*67e74705SXin Li         return false;
8840*67e74705SXin Li       if (DVar.CKind != OMPC_unknown)
8841*67e74705SXin Li         return true;
8842*67e74705SXin Li       DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
8843*67e74705SXin Li           VD, isOpenMPPrivate, [](OpenMPDirectiveKind) -> bool { return true; },
8844*67e74705SXin Li           false);
8845*67e74705SXin Li       if (DVarPrivate.CKind != OMPC_unknown)
8846*67e74705SXin Li         return true;
8847*67e74705SXin Li       return false;
8848*67e74705SXin Li     }
8849*67e74705SXin Li     return false;
8850*67e74705SXin Li   }
VisitStmt(Stmt * S)8851*67e74705SXin Li   bool VisitStmt(Stmt *S) {
8852*67e74705SXin Li     for (auto Child : S->children()) {
8853*67e74705SXin Li       if (Child && Visit(Child))
8854*67e74705SXin Li         return true;
8855*67e74705SXin Li     }
8856*67e74705SXin Li     return false;
8857*67e74705SXin Li   }
DSARefChecker(DSAStackTy * S)8858*67e74705SXin Li   explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
8859*67e74705SXin Li };
8860*67e74705SXin Li } // namespace
8861*67e74705SXin Li 
8862*67e74705SXin Li namespace {
8863*67e74705SXin Li // Transform MemberExpression for specified FieldDecl of current class to
8864*67e74705SXin Li // DeclRefExpr to specified OMPCapturedExprDecl.
8865*67e74705SXin Li class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
8866*67e74705SXin Li   typedef TreeTransform<TransformExprToCaptures> BaseTransform;
8867*67e74705SXin Li   ValueDecl *Field;
8868*67e74705SXin Li   DeclRefExpr *CapturedExpr;
8869*67e74705SXin Li 
8870*67e74705SXin Li public:
TransformExprToCaptures(Sema & SemaRef,ValueDecl * FieldDecl)8871*67e74705SXin Li   TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
8872*67e74705SXin Li       : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
8873*67e74705SXin Li 
TransformMemberExpr(MemberExpr * E)8874*67e74705SXin Li   ExprResult TransformMemberExpr(MemberExpr *E) {
8875*67e74705SXin Li     if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
8876*67e74705SXin Li         E->getMemberDecl() == Field) {
8877*67e74705SXin Li       CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
8878*67e74705SXin Li       return CapturedExpr;
8879*67e74705SXin Li     }
8880*67e74705SXin Li     return BaseTransform::TransformMemberExpr(E);
8881*67e74705SXin Li   }
getCapturedExpr()8882*67e74705SXin Li   DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
8883*67e74705SXin Li };
8884*67e74705SXin Li } // namespace
8885*67e74705SXin Li 
8886*67e74705SXin Li template <typename T>
filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> & Lookups,const llvm::function_ref<T (ValueDecl *)> & Gen)8887*67e74705SXin Li static T filterLookupForUDR(SmallVectorImpl<UnresolvedSet<8>> &Lookups,
8888*67e74705SXin Li                             const llvm::function_ref<T(ValueDecl *)> &Gen) {
8889*67e74705SXin Li   for (auto &Set : Lookups) {
8890*67e74705SXin Li     for (auto *D : Set) {
8891*67e74705SXin Li       if (auto Res = Gen(cast<ValueDecl>(D)))
8892*67e74705SXin Li         return Res;
8893*67e74705SXin Li     }
8894*67e74705SXin Li   }
8895*67e74705SXin Li   return T();
8896*67e74705SXin Li }
8897*67e74705SXin Li 
8898*67e74705SXin Li static ExprResult
buildDeclareReductionRef(Sema & SemaRef,SourceLocation Loc,SourceRange Range,Scope * S,CXXScopeSpec & ReductionIdScopeSpec,const DeclarationNameInfo & ReductionId,QualType Ty,CXXCastPath & BasePath,Expr * UnresolvedReduction)8899*67e74705SXin Li buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
8900*67e74705SXin Li                          Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
8901*67e74705SXin Li                          const DeclarationNameInfo &ReductionId, QualType Ty,
8902*67e74705SXin Li                          CXXCastPath &BasePath, Expr *UnresolvedReduction) {
8903*67e74705SXin Li   if (ReductionIdScopeSpec.isInvalid())
8904*67e74705SXin Li     return ExprError();
8905*67e74705SXin Li   SmallVector<UnresolvedSet<8>, 4> Lookups;
8906*67e74705SXin Li   if (S) {
8907*67e74705SXin Li     LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
8908*67e74705SXin Li     Lookup.suppressDiagnostics();
8909*67e74705SXin Li     while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
8910*67e74705SXin Li       auto *D = Lookup.getRepresentativeDecl();
8911*67e74705SXin Li       do {
8912*67e74705SXin Li         S = S->getParent();
8913*67e74705SXin Li       } while (S && !S->isDeclScope(D));
8914*67e74705SXin Li       if (S)
8915*67e74705SXin Li         S = S->getParent();
8916*67e74705SXin Li       Lookups.push_back(UnresolvedSet<8>());
8917*67e74705SXin Li       Lookups.back().append(Lookup.begin(), Lookup.end());
8918*67e74705SXin Li       Lookup.clear();
8919*67e74705SXin Li     }
8920*67e74705SXin Li   } else if (auto *ULE =
8921*67e74705SXin Li                  cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
8922*67e74705SXin Li     Lookups.push_back(UnresolvedSet<8>());
8923*67e74705SXin Li     Decl *PrevD = nullptr;
8924*67e74705SXin Li     for(auto *D : ULE->decls()) {
8925*67e74705SXin Li       if (D == PrevD)
8926*67e74705SXin Li         Lookups.push_back(UnresolvedSet<8>());
8927*67e74705SXin Li       else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
8928*67e74705SXin Li         Lookups.back().addDecl(DRD);
8929*67e74705SXin Li       PrevD = D;
8930*67e74705SXin Li     }
8931*67e74705SXin Li   }
8932*67e74705SXin Li   if (Ty->isDependentType() || Ty->isInstantiationDependentType() ||
8933*67e74705SXin Li       Ty->containsUnexpandedParameterPack() ||
8934*67e74705SXin Li       filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) -> bool {
8935*67e74705SXin Li         return !D->isInvalidDecl() &&
8936*67e74705SXin Li                (D->getType()->isDependentType() ||
8937*67e74705SXin Li                 D->getType()->isInstantiationDependentType() ||
8938*67e74705SXin Li                 D->getType()->containsUnexpandedParameterPack());
8939*67e74705SXin Li       })) {
8940*67e74705SXin Li     UnresolvedSet<8> ResSet;
8941*67e74705SXin Li     for (auto &Set : Lookups) {
8942*67e74705SXin Li       ResSet.append(Set.begin(), Set.end());
8943*67e74705SXin Li       // The last item marks the end of all declarations at the specified scope.
8944*67e74705SXin Li       ResSet.addDecl(Set[Set.size() - 1]);
8945*67e74705SXin Li     }
8946*67e74705SXin Li     return UnresolvedLookupExpr::Create(
8947*67e74705SXin Li         SemaRef.Context, /*NamingClass=*/nullptr,
8948*67e74705SXin Li         ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
8949*67e74705SXin Li         /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
8950*67e74705SXin Li   }
8951*67e74705SXin Li   if (auto *VD = filterLookupForUDR<ValueDecl *>(
8952*67e74705SXin Li           Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
8953*67e74705SXin Li             if (!D->isInvalidDecl() &&
8954*67e74705SXin Li                 SemaRef.Context.hasSameType(D->getType(), Ty))
8955*67e74705SXin Li               return D;
8956*67e74705SXin Li             return nullptr;
8957*67e74705SXin Li           }))
8958*67e74705SXin Li     return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8959*67e74705SXin Li   if (auto *VD = filterLookupForUDR<ValueDecl *>(
8960*67e74705SXin Li           Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
8961*67e74705SXin Li             if (!D->isInvalidDecl() &&
8962*67e74705SXin Li                 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
8963*67e74705SXin Li                 !Ty.isMoreQualifiedThan(D->getType()))
8964*67e74705SXin Li               return D;
8965*67e74705SXin Li             return nullptr;
8966*67e74705SXin Li           })) {
8967*67e74705SXin Li     CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
8968*67e74705SXin Li                        /*DetectVirtual=*/false);
8969*67e74705SXin Li     if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
8970*67e74705SXin Li       if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
8971*67e74705SXin Li               VD->getType().getUnqualifiedType()))) {
8972*67e74705SXin Li         if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
8973*67e74705SXin Li                                          /*DiagID=*/0) !=
8974*67e74705SXin Li             Sema::AR_inaccessible) {
8975*67e74705SXin Li           SemaRef.BuildBasePathArray(Paths, BasePath);
8976*67e74705SXin Li           return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
8977*67e74705SXin Li         }
8978*67e74705SXin Li       }
8979*67e74705SXin Li     }
8980*67e74705SXin Li   }
8981*67e74705SXin Li   if (ReductionIdScopeSpec.isSet()) {
8982*67e74705SXin Li     SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
8983*67e74705SXin Li     return ExprError();
8984*67e74705SXin Li   }
8985*67e74705SXin Li   return ExprEmpty();
8986*67e74705SXin Li }
8987*67e74705SXin Li 
ActOnOpenMPReductionClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc,CXXScopeSpec & ReductionIdScopeSpec,const DeclarationNameInfo & ReductionId,ArrayRef<Expr * > UnresolvedReductions)8988*67e74705SXin Li OMPClause *Sema::ActOnOpenMPReductionClause(
8989*67e74705SXin Li     ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
8990*67e74705SXin Li     SourceLocation ColonLoc, SourceLocation EndLoc,
8991*67e74705SXin Li     CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
8992*67e74705SXin Li     ArrayRef<Expr *> UnresolvedReductions) {
8993*67e74705SXin Li   auto DN = ReductionId.getName();
8994*67e74705SXin Li   auto OOK = DN.getCXXOverloadedOperator();
8995*67e74705SXin Li   BinaryOperatorKind BOK = BO_Comma;
8996*67e74705SXin Li 
8997*67e74705SXin Li   // OpenMP [2.14.3.6, reduction clause]
8998*67e74705SXin Li   // C
8999*67e74705SXin Li   // reduction-identifier is either an identifier or one of the following
9000*67e74705SXin Li   // operators: +, -, *,  &, |, ^, && and ||
9001*67e74705SXin Li   // C++
9002*67e74705SXin Li   // reduction-identifier is either an id-expression or one of the following
9003*67e74705SXin Li   // operators: +, -, *, &, |, ^, && and ||
9004*67e74705SXin Li   // FIXME: Only 'min' and 'max' identifiers are supported for now.
9005*67e74705SXin Li   switch (OOK) {
9006*67e74705SXin Li   case OO_Plus:
9007*67e74705SXin Li   case OO_Minus:
9008*67e74705SXin Li     BOK = BO_Add;
9009*67e74705SXin Li     break;
9010*67e74705SXin Li   case OO_Star:
9011*67e74705SXin Li     BOK = BO_Mul;
9012*67e74705SXin Li     break;
9013*67e74705SXin Li   case OO_Amp:
9014*67e74705SXin Li     BOK = BO_And;
9015*67e74705SXin Li     break;
9016*67e74705SXin Li   case OO_Pipe:
9017*67e74705SXin Li     BOK = BO_Or;
9018*67e74705SXin Li     break;
9019*67e74705SXin Li   case OO_Caret:
9020*67e74705SXin Li     BOK = BO_Xor;
9021*67e74705SXin Li     break;
9022*67e74705SXin Li   case OO_AmpAmp:
9023*67e74705SXin Li     BOK = BO_LAnd;
9024*67e74705SXin Li     break;
9025*67e74705SXin Li   case OO_PipePipe:
9026*67e74705SXin Li     BOK = BO_LOr;
9027*67e74705SXin Li     break;
9028*67e74705SXin Li   case OO_New:
9029*67e74705SXin Li   case OO_Delete:
9030*67e74705SXin Li   case OO_Array_New:
9031*67e74705SXin Li   case OO_Array_Delete:
9032*67e74705SXin Li   case OO_Slash:
9033*67e74705SXin Li   case OO_Percent:
9034*67e74705SXin Li   case OO_Tilde:
9035*67e74705SXin Li   case OO_Exclaim:
9036*67e74705SXin Li   case OO_Equal:
9037*67e74705SXin Li   case OO_Less:
9038*67e74705SXin Li   case OO_Greater:
9039*67e74705SXin Li   case OO_LessEqual:
9040*67e74705SXin Li   case OO_GreaterEqual:
9041*67e74705SXin Li   case OO_PlusEqual:
9042*67e74705SXin Li   case OO_MinusEqual:
9043*67e74705SXin Li   case OO_StarEqual:
9044*67e74705SXin Li   case OO_SlashEqual:
9045*67e74705SXin Li   case OO_PercentEqual:
9046*67e74705SXin Li   case OO_CaretEqual:
9047*67e74705SXin Li   case OO_AmpEqual:
9048*67e74705SXin Li   case OO_PipeEqual:
9049*67e74705SXin Li   case OO_LessLess:
9050*67e74705SXin Li   case OO_GreaterGreater:
9051*67e74705SXin Li   case OO_LessLessEqual:
9052*67e74705SXin Li   case OO_GreaterGreaterEqual:
9053*67e74705SXin Li   case OO_EqualEqual:
9054*67e74705SXin Li   case OO_ExclaimEqual:
9055*67e74705SXin Li   case OO_PlusPlus:
9056*67e74705SXin Li   case OO_MinusMinus:
9057*67e74705SXin Li   case OO_Comma:
9058*67e74705SXin Li   case OO_ArrowStar:
9059*67e74705SXin Li   case OO_Arrow:
9060*67e74705SXin Li   case OO_Call:
9061*67e74705SXin Li   case OO_Subscript:
9062*67e74705SXin Li   case OO_Conditional:
9063*67e74705SXin Li   case OO_Coawait:
9064*67e74705SXin Li   case NUM_OVERLOADED_OPERATORS:
9065*67e74705SXin Li     llvm_unreachable("Unexpected reduction identifier");
9066*67e74705SXin Li   case OO_None:
9067*67e74705SXin Li     if (auto II = DN.getAsIdentifierInfo()) {
9068*67e74705SXin Li       if (II->isStr("max"))
9069*67e74705SXin Li         BOK = BO_GT;
9070*67e74705SXin Li       else if (II->isStr("min"))
9071*67e74705SXin Li         BOK = BO_LT;
9072*67e74705SXin Li     }
9073*67e74705SXin Li     break;
9074*67e74705SXin Li   }
9075*67e74705SXin Li   SourceRange ReductionIdRange;
9076*67e74705SXin Li   if (ReductionIdScopeSpec.isValid())
9077*67e74705SXin Li     ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
9078*67e74705SXin Li   ReductionIdRange.setEnd(ReductionId.getEndLoc());
9079*67e74705SXin Li 
9080*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
9081*67e74705SXin Li   SmallVector<Expr *, 8> Privates;
9082*67e74705SXin Li   SmallVector<Expr *, 8> LHSs;
9083*67e74705SXin Li   SmallVector<Expr *, 8> RHSs;
9084*67e74705SXin Li   SmallVector<Expr *, 8> ReductionOps;
9085*67e74705SXin Li   SmallVector<Decl *, 4> ExprCaptures;
9086*67e74705SXin Li   SmallVector<Expr *, 4> ExprPostUpdates;
9087*67e74705SXin Li   auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
9088*67e74705SXin Li   bool FirstIter = true;
9089*67e74705SXin Li   for (auto RefExpr : VarList) {
9090*67e74705SXin Li     assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
9091*67e74705SXin Li     // OpenMP [2.1, C/C++]
9092*67e74705SXin Li     //  A list item is a variable or array section, subject to the restrictions
9093*67e74705SXin Li     //  specified in Section 2.4 on page 42 and in each of the sections
9094*67e74705SXin Li     // describing clauses and directives for which a list appears.
9095*67e74705SXin Li     // OpenMP  [2.14.3.3, Restrictions, p.1]
9096*67e74705SXin Li     //  A variable that is part of another variable (as an array or
9097*67e74705SXin Li     //  structure element) cannot appear in a private clause.
9098*67e74705SXin Li     if (!FirstIter && IR != ER)
9099*67e74705SXin Li       ++IR;
9100*67e74705SXin Li     FirstIter = false;
9101*67e74705SXin Li     SourceLocation ELoc;
9102*67e74705SXin Li     SourceRange ERange;
9103*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
9104*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9105*67e74705SXin Li                               /*AllowArraySection=*/true);
9106*67e74705SXin Li     if (Res.second) {
9107*67e74705SXin Li       // It will be analyzed later.
9108*67e74705SXin Li       Vars.push_back(RefExpr);
9109*67e74705SXin Li       Privates.push_back(nullptr);
9110*67e74705SXin Li       LHSs.push_back(nullptr);
9111*67e74705SXin Li       RHSs.push_back(nullptr);
9112*67e74705SXin Li       // Try to find 'declare reduction' corresponding construct before using
9113*67e74705SXin Li       // builtin/overloaded operators.
9114*67e74705SXin Li       QualType Type = Context.DependentTy;
9115*67e74705SXin Li       CXXCastPath BasePath;
9116*67e74705SXin Li       ExprResult DeclareReductionRef = buildDeclareReductionRef(
9117*67e74705SXin Li           *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9118*67e74705SXin Li           ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9119*67e74705SXin Li       if (CurContext->isDependentContext() &&
9120*67e74705SXin Li           (DeclareReductionRef.isUnset() ||
9121*67e74705SXin Li            isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
9122*67e74705SXin Li         ReductionOps.push_back(DeclareReductionRef.get());
9123*67e74705SXin Li       else
9124*67e74705SXin Li         ReductionOps.push_back(nullptr);
9125*67e74705SXin Li     }
9126*67e74705SXin Li     ValueDecl *D = Res.first;
9127*67e74705SXin Li     if (!D)
9128*67e74705SXin Li       continue;
9129*67e74705SXin Li 
9130*67e74705SXin Li     QualType Type;
9131*67e74705SXin Li     auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
9132*67e74705SXin Li     auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
9133*67e74705SXin Li     if (ASE)
9134*67e74705SXin Li       Type = ASE->getType().getNonReferenceType();
9135*67e74705SXin Li     else if (OASE) {
9136*67e74705SXin Li       auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
9137*67e74705SXin Li       if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
9138*67e74705SXin Li         Type = ATy->getElementType();
9139*67e74705SXin Li       else
9140*67e74705SXin Li         Type = BaseType->getPointeeType();
9141*67e74705SXin Li       Type = Type.getNonReferenceType();
9142*67e74705SXin Li     } else
9143*67e74705SXin Li       Type = Context.getBaseElementType(D->getType().getNonReferenceType());
9144*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
9145*67e74705SXin Li 
9146*67e74705SXin Li     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9147*67e74705SXin Li     //  A variable that appears in a private clause must not have an incomplete
9148*67e74705SXin Li     //  type or a reference type.
9149*67e74705SXin Li     if (RequireCompleteType(ELoc, Type,
9150*67e74705SXin Li                             diag::err_omp_reduction_incomplete_type))
9151*67e74705SXin Li       continue;
9152*67e74705SXin Li     // OpenMP [2.14.3.6, reduction clause, Restrictions]
9153*67e74705SXin Li     // A list item that appears in a reduction clause must not be
9154*67e74705SXin Li     // const-qualified.
9155*67e74705SXin Li     if (Type.getNonReferenceType().isConstant(Context)) {
9156*67e74705SXin Li       Diag(ELoc, diag::err_omp_const_reduction_list_item)
9157*67e74705SXin Li           << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
9158*67e74705SXin Li       if (!ASE && !OASE) {
9159*67e74705SXin Li         bool IsDecl = !VD ||
9160*67e74705SXin Li                       VD->isThisDeclarationADefinition(Context) ==
9161*67e74705SXin Li                           VarDecl::DeclarationOnly;
9162*67e74705SXin Li         Diag(D->getLocation(),
9163*67e74705SXin Li              IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9164*67e74705SXin Li             << D;
9165*67e74705SXin Li       }
9166*67e74705SXin Li       continue;
9167*67e74705SXin Li     }
9168*67e74705SXin Li     // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
9169*67e74705SXin Li     //  If a list-item is a reference type then it must bind to the same object
9170*67e74705SXin Li     //  for all threads of the team.
9171*67e74705SXin Li     if (!ASE && !OASE && VD) {
9172*67e74705SXin Li       VarDecl *VDDef = VD->getDefinition();
9173*67e74705SXin Li       if (VD->getType()->isReferenceType() && VDDef) {
9174*67e74705SXin Li         DSARefChecker Check(DSAStack);
9175*67e74705SXin Li         if (Check.Visit(VDDef->getInit())) {
9176*67e74705SXin Li           Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
9177*67e74705SXin Li           Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
9178*67e74705SXin Li           continue;
9179*67e74705SXin Li         }
9180*67e74705SXin Li       }
9181*67e74705SXin Li     }
9182*67e74705SXin Li 
9183*67e74705SXin Li     // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9184*67e74705SXin Li     // in a Construct]
9185*67e74705SXin Li     //  Variables with the predetermined data-sharing attributes may not be
9186*67e74705SXin Li     //  listed in data-sharing attributes clauses, except for the cases
9187*67e74705SXin Li     //  listed below. For these exceptions only, listing a predetermined
9188*67e74705SXin Li     //  variable in a data-sharing attribute clause is allowed and overrides
9189*67e74705SXin Li     //  the variable's predetermined data-sharing attributes.
9190*67e74705SXin Li     // OpenMP [2.14.3.6, Restrictions, p.3]
9191*67e74705SXin Li     //  Any number of reduction clauses can be specified on the directive,
9192*67e74705SXin Li     //  but a list item can appear only once in the reduction clauses for that
9193*67e74705SXin Li     //  directive.
9194*67e74705SXin Li     DSAStackTy::DSAVarData DVar;
9195*67e74705SXin Li     DVar = DSAStack->getTopDSA(D, false);
9196*67e74705SXin Li     if (DVar.CKind == OMPC_reduction) {
9197*67e74705SXin Li       Diag(ELoc, diag::err_omp_once_referenced)
9198*67e74705SXin Li           << getOpenMPClauseName(OMPC_reduction);
9199*67e74705SXin Li       if (DVar.RefExpr)
9200*67e74705SXin Li         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
9201*67e74705SXin Li     } else if (DVar.CKind != OMPC_unknown) {
9202*67e74705SXin Li       Diag(ELoc, diag::err_omp_wrong_dsa)
9203*67e74705SXin Li           << getOpenMPClauseName(DVar.CKind)
9204*67e74705SXin Li           << getOpenMPClauseName(OMPC_reduction);
9205*67e74705SXin Li       ReportOriginalDSA(*this, DSAStack, D, DVar);
9206*67e74705SXin Li       continue;
9207*67e74705SXin Li     }
9208*67e74705SXin Li 
9209*67e74705SXin Li     // OpenMP [2.14.3.6, Restrictions, p.1]
9210*67e74705SXin Li     //  A list item that appears in a reduction clause of a worksharing
9211*67e74705SXin Li     //  construct must be shared in the parallel regions to which any of the
9212*67e74705SXin Li     //  worksharing regions arising from the worksharing construct bind.
9213*67e74705SXin Li     OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
9214*67e74705SXin Li     if (isOpenMPWorksharingDirective(CurrDir) &&
9215*67e74705SXin Li         !isOpenMPParallelDirective(CurrDir)) {
9216*67e74705SXin Li       DVar = DSAStack->getImplicitDSA(D, true);
9217*67e74705SXin Li       if (DVar.CKind != OMPC_shared) {
9218*67e74705SXin Li         Diag(ELoc, diag::err_omp_required_access)
9219*67e74705SXin Li             << getOpenMPClauseName(OMPC_reduction)
9220*67e74705SXin Li             << getOpenMPClauseName(OMPC_shared);
9221*67e74705SXin Li         ReportOriginalDSA(*this, DSAStack, D, DVar);
9222*67e74705SXin Li         continue;
9223*67e74705SXin Li       }
9224*67e74705SXin Li     }
9225*67e74705SXin Li 
9226*67e74705SXin Li     // Try to find 'declare reduction' corresponding construct before using
9227*67e74705SXin Li     // builtin/overloaded operators.
9228*67e74705SXin Li     CXXCastPath BasePath;
9229*67e74705SXin Li     ExprResult DeclareReductionRef = buildDeclareReductionRef(
9230*67e74705SXin Li         *this, ELoc, ERange, DSAStack->getCurScope(), ReductionIdScopeSpec,
9231*67e74705SXin Li         ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
9232*67e74705SXin Li     if (DeclareReductionRef.isInvalid())
9233*67e74705SXin Li       continue;
9234*67e74705SXin Li     if (CurContext->isDependentContext() &&
9235*67e74705SXin Li         (DeclareReductionRef.isUnset() ||
9236*67e74705SXin Li          isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
9237*67e74705SXin Li       Vars.push_back(RefExpr);
9238*67e74705SXin Li       Privates.push_back(nullptr);
9239*67e74705SXin Li       LHSs.push_back(nullptr);
9240*67e74705SXin Li       RHSs.push_back(nullptr);
9241*67e74705SXin Li       ReductionOps.push_back(DeclareReductionRef.get());
9242*67e74705SXin Li       continue;
9243*67e74705SXin Li     }
9244*67e74705SXin Li     if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
9245*67e74705SXin Li       // Not allowed reduction identifier is found.
9246*67e74705SXin Li       Diag(ReductionId.getLocStart(),
9247*67e74705SXin Li            diag::err_omp_unknown_reduction_identifier)
9248*67e74705SXin Li           << Type << ReductionIdRange;
9249*67e74705SXin Li       continue;
9250*67e74705SXin Li     }
9251*67e74705SXin Li 
9252*67e74705SXin Li     // OpenMP [2.14.3.6, reduction clause, Restrictions]
9253*67e74705SXin Li     // The type of a list item that appears in a reduction clause must be valid
9254*67e74705SXin Li     // for the reduction-identifier. For a max or min reduction in C, the type
9255*67e74705SXin Li     // of the list item must be an allowed arithmetic data type: char, int,
9256*67e74705SXin Li     // float, double, or _Bool, possibly modified with long, short, signed, or
9257*67e74705SXin Li     // unsigned. For a max or min reduction in C++, the type of the list item
9258*67e74705SXin Li     // must be an allowed arithmetic data type: char, wchar_t, int, float,
9259*67e74705SXin Li     // double, or bool, possibly modified with long, short, signed, or unsigned.
9260*67e74705SXin Li     if (DeclareReductionRef.isUnset()) {
9261*67e74705SXin Li       if ((BOK == BO_GT || BOK == BO_LT) &&
9262*67e74705SXin Li           !(Type->isScalarType() ||
9263*67e74705SXin Li             (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
9264*67e74705SXin Li         Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
9265*67e74705SXin Li             << getLangOpts().CPlusPlus;
9266*67e74705SXin Li         if (!ASE && !OASE) {
9267*67e74705SXin Li           bool IsDecl = !VD ||
9268*67e74705SXin Li                         VD->isThisDeclarationADefinition(Context) ==
9269*67e74705SXin Li                             VarDecl::DeclarationOnly;
9270*67e74705SXin Li           Diag(D->getLocation(),
9271*67e74705SXin Li                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9272*67e74705SXin Li               << D;
9273*67e74705SXin Li         }
9274*67e74705SXin Li         continue;
9275*67e74705SXin Li       }
9276*67e74705SXin Li       if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
9277*67e74705SXin Li           !getLangOpts().CPlusPlus && Type->isFloatingType()) {
9278*67e74705SXin Li         Diag(ELoc, diag::err_omp_clause_floating_type_arg);
9279*67e74705SXin Li         if (!ASE && !OASE) {
9280*67e74705SXin Li           bool IsDecl = !VD ||
9281*67e74705SXin Li                         VD->isThisDeclarationADefinition(Context) ==
9282*67e74705SXin Li                             VarDecl::DeclarationOnly;
9283*67e74705SXin Li           Diag(D->getLocation(),
9284*67e74705SXin Li                IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9285*67e74705SXin Li               << D;
9286*67e74705SXin Li         }
9287*67e74705SXin Li         continue;
9288*67e74705SXin Li       }
9289*67e74705SXin Li     }
9290*67e74705SXin Li 
9291*67e74705SXin Li     Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
9292*67e74705SXin Li     auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
9293*67e74705SXin Li                                D->hasAttrs() ? &D->getAttrs() : nullptr);
9294*67e74705SXin Li     auto *RHSVD = buildVarDecl(*this, ELoc, Type, D->getName(),
9295*67e74705SXin Li                                D->hasAttrs() ? &D->getAttrs() : nullptr);
9296*67e74705SXin Li     auto PrivateTy = Type;
9297*67e74705SXin Li     if (OASE ||
9298*67e74705SXin Li         (!ASE &&
9299*67e74705SXin Li          D->getType().getNonReferenceType()->isVariablyModifiedType())) {
9300*67e74705SXin Li       // For arays/array sections only:
9301*67e74705SXin Li       // Create pseudo array type for private copy. The size for this array will
9302*67e74705SXin Li       // be generated during codegen.
9303*67e74705SXin Li       // For array subscripts or single variables Private Ty is the same as Type
9304*67e74705SXin Li       // (type of the variable or single array element).
9305*67e74705SXin Li       PrivateTy = Context.getVariableArrayType(
9306*67e74705SXin Li           Type, new (Context) OpaqueValueExpr(SourceLocation(),
9307*67e74705SXin Li                                               Context.getSizeType(), VK_RValue),
9308*67e74705SXin Li           ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
9309*67e74705SXin Li     } else if (!ASE && !OASE &&
9310*67e74705SXin Li                Context.getAsArrayType(D->getType().getNonReferenceType()))
9311*67e74705SXin Li       PrivateTy = D->getType().getNonReferenceType();
9312*67e74705SXin Li     // Private copy.
9313*67e74705SXin Li     auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, D->getName(),
9314*67e74705SXin Li                                    D->hasAttrs() ? &D->getAttrs() : nullptr);
9315*67e74705SXin Li     // Add initializer for private variable.
9316*67e74705SXin Li     Expr *Init = nullptr;
9317*67e74705SXin Li     auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
9318*67e74705SXin Li     auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
9319*67e74705SXin Li     if (DeclareReductionRef.isUsable()) {
9320*67e74705SXin Li       auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
9321*67e74705SXin Li       auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
9322*67e74705SXin Li       if (DRD->getInitializer()) {
9323*67e74705SXin Li         Init = DRDRef;
9324*67e74705SXin Li         RHSVD->setInit(DRDRef);
9325*67e74705SXin Li         RHSVD->setInitStyle(VarDecl::CallInit);
9326*67e74705SXin Li       }
9327*67e74705SXin Li     } else {
9328*67e74705SXin Li       switch (BOK) {
9329*67e74705SXin Li       case BO_Add:
9330*67e74705SXin Li       case BO_Xor:
9331*67e74705SXin Li       case BO_Or:
9332*67e74705SXin Li       case BO_LOr:
9333*67e74705SXin Li         // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
9334*67e74705SXin Li         if (Type->isScalarType() || Type->isAnyComplexType())
9335*67e74705SXin Li           Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
9336*67e74705SXin Li         break;
9337*67e74705SXin Li       case BO_Mul:
9338*67e74705SXin Li       case BO_LAnd:
9339*67e74705SXin Li         if (Type->isScalarType() || Type->isAnyComplexType()) {
9340*67e74705SXin Li           // '*' and '&&' reduction ops - initializer is '1'.
9341*67e74705SXin Li           Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
9342*67e74705SXin Li         }
9343*67e74705SXin Li         break;
9344*67e74705SXin Li       case BO_And: {
9345*67e74705SXin Li         // '&' reduction op - initializer is '~0'.
9346*67e74705SXin Li         QualType OrigType = Type;
9347*67e74705SXin Li         if (auto *ComplexTy = OrigType->getAs<ComplexType>())
9348*67e74705SXin Li           Type = ComplexTy->getElementType();
9349*67e74705SXin Li         if (Type->isRealFloatingType()) {
9350*67e74705SXin Li           llvm::APFloat InitValue =
9351*67e74705SXin Li               llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
9352*67e74705SXin Li                                              /*isIEEE=*/true);
9353*67e74705SXin Li           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9354*67e74705SXin Li                                          Type, ELoc);
9355*67e74705SXin Li         } else if (Type->isScalarType()) {
9356*67e74705SXin Li           auto Size = Context.getTypeSize(Type);
9357*67e74705SXin Li           QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
9358*67e74705SXin Li           llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
9359*67e74705SXin Li           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9360*67e74705SXin Li         }
9361*67e74705SXin Li         if (Init && OrigType->isAnyComplexType()) {
9362*67e74705SXin Li           // Init = 0xFFFF + 0xFFFFi;
9363*67e74705SXin Li           auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
9364*67e74705SXin Li           Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
9365*67e74705SXin Li         }
9366*67e74705SXin Li         Type = OrigType;
9367*67e74705SXin Li         break;
9368*67e74705SXin Li       }
9369*67e74705SXin Li       case BO_LT:
9370*67e74705SXin Li       case BO_GT: {
9371*67e74705SXin Li         // 'min' reduction op - initializer is 'Largest representable number in
9372*67e74705SXin Li         // the reduction list item type'.
9373*67e74705SXin Li         // 'max' reduction op - initializer is 'Least representable number in
9374*67e74705SXin Li         // the reduction list item type'.
9375*67e74705SXin Li         if (Type->isIntegerType() || Type->isPointerType()) {
9376*67e74705SXin Li           bool IsSigned = Type->hasSignedIntegerRepresentation();
9377*67e74705SXin Li           auto Size = Context.getTypeSize(Type);
9378*67e74705SXin Li           QualType IntTy =
9379*67e74705SXin Li               Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
9380*67e74705SXin Li           llvm::APInt InitValue =
9381*67e74705SXin Li               (BOK != BO_LT)
9382*67e74705SXin Li                   ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
9383*67e74705SXin Li                              : llvm::APInt::getMinValue(Size)
9384*67e74705SXin Li                   : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
9385*67e74705SXin Li                              : llvm::APInt::getMaxValue(Size);
9386*67e74705SXin Li           Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
9387*67e74705SXin Li           if (Type->isPointerType()) {
9388*67e74705SXin Li             // Cast to pointer type.
9389*67e74705SXin Li             auto CastExpr = BuildCStyleCastExpr(
9390*67e74705SXin Li                 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
9391*67e74705SXin Li                 SourceLocation(), Init);
9392*67e74705SXin Li             if (CastExpr.isInvalid())
9393*67e74705SXin Li               continue;
9394*67e74705SXin Li             Init = CastExpr.get();
9395*67e74705SXin Li           }
9396*67e74705SXin Li         } else if (Type->isRealFloatingType()) {
9397*67e74705SXin Li           llvm::APFloat InitValue = llvm::APFloat::getLargest(
9398*67e74705SXin Li               Context.getFloatTypeSemantics(Type), BOK != BO_LT);
9399*67e74705SXin Li           Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
9400*67e74705SXin Li                                          Type, ELoc);
9401*67e74705SXin Li         }
9402*67e74705SXin Li         break;
9403*67e74705SXin Li       }
9404*67e74705SXin Li       case BO_PtrMemD:
9405*67e74705SXin Li       case BO_PtrMemI:
9406*67e74705SXin Li       case BO_MulAssign:
9407*67e74705SXin Li       case BO_Div:
9408*67e74705SXin Li       case BO_Rem:
9409*67e74705SXin Li       case BO_Sub:
9410*67e74705SXin Li       case BO_Shl:
9411*67e74705SXin Li       case BO_Shr:
9412*67e74705SXin Li       case BO_LE:
9413*67e74705SXin Li       case BO_GE:
9414*67e74705SXin Li       case BO_EQ:
9415*67e74705SXin Li       case BO_NE:
9416*67e74705SXin Li       case BO_AndAssign:
9417*67e74705SXin Li       case BO_XorAssign:
9418*67e74705SXin Li       case BO_OrAssign:
9419*67e74705SXin Li       case BO_Assign:
9420*67e74705SXin Li       case BO_AddAssign:
9421*67e74705SXin Li       case BO_SubAssign:
9422*67e74705SXin Li       case BO_DivAssign:
9423*67e74705SXin Li       case BO_RemAssign:
9424*67e74705SXin Li       case BO_ShlAssign:
9425*67e74705SXin Li       case BO_ShrAssign:
9426*67e74705SXin Li       case BO_Comma:
9427*67e74705SXin Li         llvm_unreachable("Unexpected reduction operation");
9428*67e74705SXin Li       }
9429*67e74705SXin Li     }
9430*67e74705SXin Li     if (Init && DeclareReductionRef.isUnset()) {
9431*67e74705SXin Li       AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
9432*67e74705SXin Li                            /*TypeMayContainAuto=*/false);
9433*67e74705SXin Li     } else if (!Init)
9434*67e74705SXin Li       ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
9435*67e74705SXin Li     if (RHSVD->isInvalidDecl())
9436*67e74705SXin Li       continue;
9437*67e74705SXin Li     if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
9438*67e74705SXin Li       Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
9439*67e74705SXin Li                                                             << ReductionIdRange;
9440*67e74705SXin Li       bool IsDecl =
9441*67e74705SXin Li           !VD ||
9442*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9443*67e74705SXin Li       Diag(D->getLocation(),
9444*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9445*67e74705SXin Li           << D;
9446*67e74705SXin Li       continue;
9447*67e74705SXin Li     }
9448*67e74705SXin Li     // Store initializer for single element in private copy. Will be used during
9449*67e74705SXin Li     // codegen.
9450*67e74705SXin Li     PrivateVD->setInit(RHSVD->getInit());
9451*67e74705SXin Li     PrivateVD->setInitStyle(RHSVD->getInitStyle());
9452*67e74705SXin Li     auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
9453*67e74705SXin Li     ExprResult ReductionOp;
9454*67e74705SXin Li     if (DeclareReductionRef.isUsable()) {
9455*67e74705SXin Li       QualType RedTy = DeclareReductionRef.get()->getType();
9456*67e74705SXin Li       QualType PtrRedTy = Context.getPointerType(RedTy);
9457*67e74705SXin Li       ExprResult LHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
9458*67e74705SXin Li       ExprResult RHS = CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
9459*67e74705SXin Li       if (!BasePath.empty()) {
9460*67e74705SXin Li         LHS = DefaultLvalueConversion(LHS.get());
9461*67e74705SXin Li         RHS = DefaultLvalueConversion(RHS.get());
9462*67e74705SXin Li         LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9463*67e74705SXin Li                                        CK_UncheckedDerivedToBase, LHS.get(),
9464*67e74705SXin Li                                        &BasePath, LHS.get()->getValueKind());
9465*67e74705SXin Li         RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
9466*67e74705SXin Li                                        CK_UncheckedDerivedToBase, RHS.get(),
9467*67e74705SXin Li                                        &BasePath, RHS.get()->getValueKind());
9468*67e74705SXin Li       }
9469*67e74705SXin Li       FunctionProtoType::ExtProtoInfo EPI;
9470*67e74705SXin Li       QualType Params[] = {PtrRedTy, PtrRedTy};
9471*67e74705SXin Li       QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
9472*67e74705SXin Li       auto *OVE = new (Context) OpaqueValueExpr(
9473*67e74705SXin Li           ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
9474*67e74705SXin Li           DefaultLvalueConversion(DeclareReductionRef.get()).get());
9475*67e74705SXin Li       Expr *Args[] = {LHS.get(), RHS.get()};
9476*67e74705SXin Li       ReductionOp = new (Context)
9477*67e74705SXin Li           CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
9478*67e74705SXin Li     } else {
9479*67e74705SXin Li       ReductionOp = BuildBinOp(DSAStack->getCurScope(),
9480*67e74705SXin Li                                ReductionId.getLocStart(), BOK, LHSDRE, RHSDRE);
9481*67e74705SXin Li       if (ReductionOp.isUsable()) {
9482*67e74705SXin Li         if (BOK != BO_LT && BOK != BO_GT) {
9483*67e74705SXin Li           ReductionOp =
9484*67e74705SXin Li               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9485*67e74705SXin Li                          BO_Assign, LHSDRE, ReductionOp.get());
9486*67e74705SXin Li         } else {
9487*67e74705SXin Li           auto *ConditionalOp = new (Context) ConditionalOperator(
9488*67e74705SXin Li               ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
9489*67e74705SXin Li               RHSDRE, Type, VK_LValue, OK_Ordinary);
9490*67e74705SXin Li           ReductionOp =
9491*67e74705SXin Li               BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
9492*67e74705SXin Li                          BO_Assign, LHSDRE, ConditionalOp);
9493*67e74705SXin Li         }
9494*67e74705SXin Li         ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
9495*67e74705SXin Li       }
9496*67e74705SXin Li       if (ReductionOp.isInvalid())
9497*67e74705SXin Li         continue;
9498*67e74705SXin Li     }
9499*67e74705SXin Li 
9500*67e74705SXin Li     DeclRefExpr *Ref = nullptr;
9501*67e74705SXin Li     Expr *VarsExpr = RefExpr->IgnoreParens();
9502*67e74705SXin Li     if (!VD && !CurContext->isDependentContext()) {
9503*67e74705SXin Li       if (ASE || OASE) {
9504*67e74705SXin Li         TransformExprToCaptures RebuildToCapture(*this, D);
9505*67e74705SXin Li         VarsExpr =
9506*67e74705SXin Li             RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
9507*67e74705SXin Li         Ref = RebuildToCapture.getCapturedExpr();
9508*67e74705SXin Li       } else {
9509*67e74705SXin Li         VarsExpr = Ref =
9510*67e74705SXin Li             buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9511*67e74705SXin Li       }
9512*67e74705SXin Li       if (!IsOpenMPCapturedDecl(D)) {
9513*67e74705SXin Li         ExprCaptures.push_back(Ref->getDecl());
9514*67e74705SXin Li         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9515*67e74705SXin Li           ExprResult RefRes = DefaultLvalueConversion(Ref);
9516*67e74705SXin Li           if (!RefRes.isUsable())
9517*67e74705SXin Li             continue;
9518*67e74705SXin Li           ExprResult PostUpdateRes =
9519*67e74705SXin Li               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9520*67e74705SXin Li                          SimpleRefExpr, RefRes.get());
9521*67e74705SXin Li           if (!PostUpdateRes.isUsable())
9522*67e74705SXin Li             continue;
9523*67e74705SXin Li           ExprPostUpdates.push_back(
9524*67e74705SXin Li               IgnoredValueConversions(PostUpdateRes.get()).get());
9525*67e74705SXin Li         }
9526*67e74705SXin Li       }
9527*67e74705SXin Li     }
9528*67e74705SXin Li     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
9529*67e74705SXin Li     Vars.push_back(VarsExpr);
9530*67e74705SXin Li     Privates.push_back(PrivateDRE);
9531*67e74705SXin Li     LHSs.push_back(LHSDRE);
9532*67e74705SXin Li     RHSs.push_back(RHSDRE);
9533*67e74705SXin Li     ReductionOps.push_back(ReductionOp.get());
9534*67e74705SXin Li   }
9535*67e74705SXin Li 
9536*67e74705SXin Li   if (Vars.empty())
9537*67e74705SXin Li     return nullptr;
9538*67e74705SXin Li 
9539*67e74705SXin Li   return OMPReductionClause::Create(
9540*67e74705SXin Li       Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
9541*67e74705SXin Li       ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
9542*67e74705SXin Li       LHSs, RHSs, ReductionOps, buildPreInits(Context, ExprCaptures),
9543*67e74705SXin Li       buildPostUpdate(*this, ExprPostUpdates));
9544*67e74705SXin Li }
9545*67e74705SXin Li 
CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,SourceLocation LinLoc)9546*67e74705SXin Li bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
9547*67e74705SXin Li                                      SourceLocation LinLoc) {
9548*67e74705SXin Li   if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
9549*67e74705SXin Li       LinKind == OMPC_LINEAR_unknown) {
9550*67e74705SXin Li     Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
9551*67e74705SXin Li     return true;
9552*67e74705SXin Li   }
9553*67e74705SXin Li   return false;
9554*67e74705SXin Li }
9555*67e74705SXin Li 
CheckOpenMPLinearDecl(ValueDecl * D,SourceLocation ELoc,OpenMPLinearClauseKind LinKind,QualType Type)9556*67e74705SXin Li bool Sema::CheckOpenMPLinearDecl(ValueDecl *D, SourceLocation ELoc,
9557*67e74705SXin Li                                  OpenMPLinearClauseKind LinKind,
9558*67e74705SXin Li                                  QualType Type) {
9559*67e74705SXin Li   auto *VD = dyn_cast_or_null<VarDecl>(D);
9560*67e74705SXin Li   // A variable must not have an incomplete type or a reference type.
9561*67e74705SXin Li   if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
9562*67e74705SXin Li     return true;
9563*67e74705SXin Li   if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
9564*67e74705SXin Li       !Type->isReferenceType()) {
9565*67e74705SXin Li     Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
9566*67e74705SXin Li         << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
9567*67e74705SXin Li     return true;
9568*67e74705SXin Li   }
9569*67e74705SXin Li   Type = Type.getNonReferenceType();
9570*67e74705SXin Li 
9571*67e74705SXin Li   // A list item must not be const-qualified.
9572*67e74705SXin Li   if (Type.isConstant(Context)) {
9573*67e74705SXin Li     Diag(ELoc, diag::err_omp_const_variable)
9574*67e74705SXin Li         << getOpenMPClauseName(OMPC_linear);
9575*67e74705SXin Li     if (D) {
9576*67e74705SXin Li       bool IsDecl =
9577*67e74705SXin Li           !VD ||
9578*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9579*67e74705SXin Li       Diag(D->getLocation(),
9580*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9581*67e74705SXin Li           << D;
9582*67e74705SXin Li     }
9583*67e74705SXin Li     return true;
9584*67e74705SXin Li   }
9585*67e74705SXin Li 
9586*67e74705SXin Li   // A list item must be of integral or pointer type.
9587*67e74705SXin Li   Type = Type.getUnqualifiedType().getCanonicalType();
9588*67e74705SXin Li   const auto *Ty = Type.getTypePtrOrNull();
9589*67e74705SXin Li   if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
9590*67e74705SXin Li               !Ty->isPointerType())) {
9591*67e74705SXin Li     Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
9592*67e74705SXin Li     if (D) {
9593*67e74705SXin Li       bool IsDecl =
9594*67e74705SXin Li           !VD ||
9595*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9596*67e74705SXin Li       Diag(D->getLocation(),
9597*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9598*67e74705SXin Li           << D;
9599*67e74705SXin Li     }
9600*67e74705SXin Li     return true;
9601*67e74705SXin Li   }
9602*67e74705SXin Li   return false;
9603*67e74705SXin Li }
9604*67e74705SXin Li 
ActOnOpenMPLinearClause(ArrayRef<Expr * > VarList,Expr * Step,SourceLocation StartLoc,SourceLocation LParenLoc,OpenMPLinearClauseKind LinKind,SourceLocation LinLoc,SourceLocation ColonLoc,SourceLocation EndLoc)9605*67e74705SXin Li OMPClause *Sema::ActOnOpenMPLinearClause(
9606*67e74705SXin Li     ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
9607*67e74705SXin Li     SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
9608*67e74705SXin Li     SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9609*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
9610*67e74705SXin Li   SmallVector<Expr *, 8> Privates;
9611*67e74705SXin Li   SmallVector<Expr *, 8> Inits;
9612*67e74705SXin Li   SmallVector<Decl *, 4> ExprCaptures;
9613*67e74705SXin Li   SmallVector<Expr *, 4> ExprPostUpdates;
9614*67e74705SXin Li   if (CheckOpenMPLinearModifier(LinKind, LinLoc))
9615*67e74705SXin Li     LinKind = OMPC_LINEAR_val;
9616*67e74705SXin Li   for (auto &RefExpr : VarList) {
9617*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP linear clause.");
9618*67e74705SXin Li     SourceLocation ELoc;
9619*67e74705SXin Li     SourceRange ERange;
9620*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
9621*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9622*67e74705SXin Li                               /*AllowArraySection=*/false);
9623*67e74705SXin Li     if (Res.second) {
9624*67e74705SXin Li       // It will be analyzed later.
9625*67e74705SXin Li       Vars.push_back(RefExpr);
9626*67e74705SXin Li       Privates.push_back(nullptr);
9627*67e74705SXin Li       Inits.push_back(nullptr);
9628*67e74705SXin Li     }
9629*67e74705SXin Li     ValueDecl *D = Res.first;
9630*67e74705SXin Li     if (!D)
9631*67e74705SXin Li       continue;
9632*67e74705SXin Li 
9633*67e74705SXin Li     QualType Type = D->getType();
9634*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
9635*67e74705SXin Li 
9636*67e74705SXin Li     // OpenMP [2.14.3.7, linear clause]
9637*67e74705SXin Li     //  A list-item cannot appear in more than one linear clause.
9638*67e74705SXin Li     //  A list-item that appears in a linear clause cannot appear in any
9639*67e74705SXin Li     //  other data-sharing attribute clause.
9640*67e74705SXin Li     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, false);
9641*67e74705SXin Li     if (DVar.RefExpr) {
9642*67e74705SXin Li       Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9643*67e74705SXin Li                                           << getOpenMPClauseName(OMPC_linear);
9644*67e74705SXin Li       ReportOriginalDSA(*this, DSAStack, D, DVar);
9645*67e74705SXin Li       continue;
9646*67e74705SXin Li     }
9647*67e74705SXin Li 
9648*67e74705SXin Li     if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
9649*67e74705SXin Li       continue;
9650*67e74705SXin Li     Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
9651*67e74705SXin Li 
9652*67e74705SXin Li     // Build private copy of original var.
9653*67e74705SXin Li     auto *Private = buildVarDecl(*this, ELoc, Type, D->getName(),
9654*67e74705SXin Li                                  D->hasAttrs() ? &D->getAttrs() : nullptr);
9655*67e74705SXin Li     auto *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
9656*67e74705SXin Li     // Build var to save initial value.
9657*67e74705SXin Li     VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
9658*67e74705SXin Li     Expr *InitExpr;
9659*67e74705SXin Li     DeclRefExpr *Ref = nullptr;
9660*67e74705SXin Li     if (!VD && !CurContext->isDependentContext()) {
9661*67e74705SXin Li       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
9662*67e74705SXin Li       if (!IsOpenMPCapturedDecl(D)) {
9663*67e74705SXin Li         ExprCaptures.push_back(Ref->getDecl());
9664*67e74705SXin Li         if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
9665*67e74705SXin Li           ExprResult RefRes = DefaultLvalueConversion(Ref);
9666*67e74705SXin Li           if (!RefRes.isUsable())
9667*67e74705SXin Li             continue;
9668*67e74705SXin Li           ExprResult PostUpdateRes =
9669*67e74705SXin Li               BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
9670*67e74705SXin Li                          SimpleRefExpr, RefRes.get());
9671*67e74705SXin Li           if (!PostUpdateRes.isUsable())
9672*67e74705SXin Li             continue;
9673*67e74705SXin Li           ExprPostUpdates.push_back(
9674*67e74705SXin Li               IgnoredValueConversions(PostUpdateRes.get()).get());
9675*67e74705SXin Li         }
9676*67e74705SXin Li       }
9677*67e74705SXin Li     }
9678*67e74705SXin Li     if (LinKind == OMPC_LINEAR_uval)
9679*67e74705SXin Li       InitExpr = VD ? VD->getInit() : SimpleRefExpr;
9680*67e74705SXin Li     else
9681*67e74705SXin Li       InitExpr = VD ? SimpleRefExpr : Ref;
9682*67e74705SXin Li     AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
9683*67e74705SXin Li                          /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
9684*67e74705SXin Li     auto InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
9685*67e74705SXin Li 
9686*67e74705SXin Li     DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
9687*67e74705SXin Li     Vars.push_back((VD || CurContext->isDependentContext())
9688*67e74705SXin Li                        ? RefExpr->IgnoreParens()
9689*67e74705SXin Li                        : Ref);
9690*67e74705SXin Li     Privates.push_back(PrivateRef);
9691*67e74705SXin Li     Inits.push_back(InitRef);
9692*67e74705SXin Li   }
9693*67e74705SXin Li 
9694*67e74705SXin Li   if (Vars.empty())
9695*67e74705SXin Li     return nullptr;
9696*67e74705SXin Li 
9697*67e74705SXin Li   Expr *StepExpr = Step;
9698*67e74705SXin Li   Expr *CalcStepExpr = nullptr;
9699*67e74705SXin Li   if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
9700*67e74705SXin Li       !Step->isInstantiationDependent() &&
9701*67e74705SXin Li       !Step->containsUnexpandedParameterPack()) {
9702*67e74705SXin Li     SourceLocation StepLoc = Step->getLocStart();
9703*67e74705SXin Li     ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
9704*67e74705SXin Li     if (Val.isInvalid())
9705*67e74705SXin Li       return nullptr;
9706*67e74705SXin Li     StepExpr = Val.get();
9707*67e74705SXin Li 
9708*67e74705SXin Li     // Build var to save the step value.
9709*67e74705SXin Li     VarDecl *SaveVar =
9710*67e74705SXin Li         buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
9711*67e74705SXin Li     ExprResult SaveRef =
9712*67e74705SXin Li         buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
9713*67e74705SXin Li     ExprResult CalcStep =
9714*67e74705SXin Li         BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
9715*67e74705SXin Li     CalcStep = ActOnFinishFullExpr(CalcStep.get());
9716*67e74705SXin Li 
9717*67e74705SXin Li     // Warn about zero linear step (it would be probably better specified as
9718*67e74705SXin Li     // making corresponding variables 'const').
9719*67e74705SXin Li     llvm::APSInt Result;
9720*67e74705SXin Li     bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
9721*67e74705SXin Li     if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
9722*67e74705SXin Li       Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
9723*67e74705SXin Li                                                      << (Vars.size() > 1);
9724*67e74705SXin Li     if (!IsConstant && CalcStep.isUsable()) {
9725*67e74705SXin Li       // Calculate the step beforehand instead of doing this on each iteration.
9726*67e74705SXin Li       // (This is not used if the number of iterations may be kfold-ed).
9727*67e74705SXin Li       CalcStepExpr = CalcStep.get();
9728*67e74705SXin Li     }
9729*67e74705SXin Li   }
9730*67e74705SXin Li 
9731*67e74705SXin Li   return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
9732*67e74705SXin Li                                  ColonLoc, EndLoc, Vars, Privates, Inits,
9733*67e74705SXin Li                                  StepExpr, CalcStepExpr,
9734*67e74705SXin Li                                  buildPreInits(Context, ExprCaptures),
9735*67e74705SXin Li                                  buildPostUpdate(*this, ExprPostUpdates));
9736*67e74705SXin Li }
9737*67e74705SXin Li 
FinishOpenMPLinearClause(OMPLinearClause & Clause,DeclRefExpr * IV,Expr * NumIterations,Sema & SemaRef,Scope * S,DSAStackTy * Stack)9738*67e74705SXin Li static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
9739*67e74705SXin Li                                      Expr *NumIterations, Sema &SemaRef,
9740*67e74705SXin Li                                      Scope *S, DSAStackTy *Stack) {
9741*67e74705SXin Li   // Walk the vars and build update/final expressions for the CodeGen.
9742*67e74705SXin Li   SmallVector<Expr *, 8> Updates;
9743*67e74705SXin Li   SmallVector<Expr *, 8> Finals;
9744*67e74705SXin Li   Expr *Step = Clause.getStep();
9745*67e74705SXin Li   Expr *CalcStep = Clause.getCalcStep();
9746*67e74705SXin Li   // OpenMP [2.14.3.7, linear clause]
9747*67e74705SXin Li   // If linear-step is not specified it is assumed to be 1.
9748*67e74705SXin Li   if (Step == nullptr)
9749*67e74705SXin Li     Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
9750*67e74705SXin Li   else if (CalcStep) {
9751*67e74705SXin Li     Step = cast<BinaryOperator>(CalcStep)->getLHS();
9752*67e74705SXin Li   }
9753*67e74705SXin Li   bool HasErrors = false;
9754*67e74705SXin Li   auto CurInit = Clause.inits().begin();
9755*67e74705SXin Li   auto CurPrivate = Clause.privates().begin();
9756*67e74705SXin Li   auto LinKind = Clause.getModifier();
9757*67e74705SXin Li   for (auto &RefExpr : Clause.varlists()) {
9758*67e74705SXin Li     SourceLocation ELoc;
9759*67e74705SXin Li     SourceRange ERange;
9760*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
9761*67e74705SXin Li     auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange,
9762*67e74705SXin Li                               /*AllowArraySection=*/false);
9763*67e74705SXin Li     ValueDecl *D = Res.first;
9764*67e74705SXin Li     if (Res.second || !D) {
9765*67e74705SXin Li       Updates.push_back(nullptr);
9766*67e74705SXin Li       Finals.push_back(nullptr);
9767*67e74705SXin Li       HasErrors = true;
9768*67e74705SXin Li       continue;
9769*67e74705SXin Li     }
9770*67e74705SXin Li     if (auto *CED = dyn_cast<OMPCapturedExprDecl>(D)) {
9771*67e74705SXin Li       D = cast<MemberExpr>(CED->getInit()->IgnoreParenImpCasts())
9772*67e74705SXin Li               ->getMemberDecl();
9773*67e74705SXin Li     }
9774*67e74705SXin Li     auto &&Info = Stack->isLoopControlVariable(D);
9775*67e74705SXin Li     Expr *InitExpr = *CurInit;
9776*67e74705SXin Li 
9777*67e74705SXin Li     // Build privatized reference to the current linear var.
9778*67e74705SXin Li     auto DE = cast<DeclRefExpr>(SimpleRefExpr);
9779*67e74705SXin Li     Expr *CapturedRef;
9780*67e74705SXin Li     if (LinKind == OMPC_LINEAR_uval)
9781*67e74705SXin Li       CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
9782*67e74705SXin Li     else
9783*67e74705SXin Li       CapturedRef =
9784*67e74705SXin Li           buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
9785*67e74705SXin Li                            DE->getType().getUnqualifiedType(), DE->getExprLoc(),
9786*67e74705SXin Li                            /*RefersToCapture=*/true);
9787*67e74705SXin Li 
9788*67e74705SXin Li     // Build update: Var = InitExpr + IV * Step
9789*67e74705SXin Li     ExprResult Update;
9790*67e74705SXin Li     if (!Info.first) {
9791*67e74705SXin Li       Update =
9792*67e74705SXin Li           BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
9793*67e74705SXin Li                              InitExpr, IV, Step, /* Subtract */ false);
9794*67e74705SXin Li     } else
9795*67e74705SXin Li       Update = *CurPrivate;
9796*67e74705SXin Li     Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
9797*67e74705SXin Li                                          /*DiscardedValue=*/true);
9798*67e74705SXin Li 
9799*67e74705SXin Li     // Build final: Var = InitExpr + NumIterations * Step
9800*67e74705SXin Li     ExprResult Final;
9801*67e74705SXin Li     if (!Info.first) {
9802*67e74705SXin Li       Final = BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
9803*67e74705SXin Li                                  InitExpr, NumIterations, Step,
9804*67e74705SXin Li                                  /* Subtract */ false);
9805*67e74705SXin Li     } else
9806*67e74705SXin Li       Final = *CurPrivate;
9807*67e74705SXin Li     Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
9808*67e74705SXin Li                                         /*DiscardedValue=*/true);
9809*67e74705SXin Li 
9810*67e74705SXin Li     if (!Update.isUsable() || !Final.isUsable()) {
9811*67e74705SXin Li       Updates.push_back(nullptr);
9812*67e74705SXin Li       Finals.push_back(nullptr);
9813*67e74705SXin Li       HasErrors = true;
9814*67e74705SXin Li     } else {
9815*67e74705SXin Li       Updates.push_back(Update.get());
9816*67e74705SXin Li       Finals.push_back(Final.get());
9817*67e74705SXin Li     }
9818*67e74705SXin Li     ++CurInit;
9819*67e74705SXin Li     ++CurPrivate;
9820*67e74705SXin Li   }
9821*67e74705SXin Li   Clause.setUpdates(Updates);
9822*67e74705SXin Li   Clause.setFinals(Finals);
9823*67e74705SXin Li   return HasErrors;
9824*67e74705SXin Li }
9825*67e74705SXin Li 
ActOnOpenMPAlignedClause(ArrayRef<Expr * > VarList,Expr * Alignment,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation ColonLoc,SourceLocation EndLoc)9826*67e74705SXin Li OMPClause *Sema::ActOnOpenMPAlignedClause(
9827*67e74705SXin Li     ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
9828*67e74705SXin Li     SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
9829*67e74705SXin Li 
9830*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
9831*67e74705SXin Li   for (auto &RefExpr : VarList) {
9832*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP linear clause.");
9833*67e74705SXin Li     SourceLocation ELoc;
9834*67e74705SXin Li     SourceRange ERange;
9835*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
9836*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
9837*67e74705SXin Li                               /*AllowArraySection=*/false);
9838*67e74705SXin Li     if (Res.second) {
9839*67e74705SXin Li       // It will be analyzed later.
9840*67e74705SXin Li       Vars.push_back(RefExpr);
9841*67e74705SXin Li     }
9842*67e74705SXin Li     ValueDecl *D = Res.first;
9843*67e74705SXin Li     if (!D)
9844*67e74705SXin Li       continue;
9845*67e74705SXin Li 
9846*67e74705SXin Li     QualType QType = D->getType();
9847*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
9848*67e74705SXin Li 
9849*67e74705SXin Li     // OpenMP  [2.8.1, simd construct, Restrictions]
9850*67e74705SXin Li     // The type of list items appearing in the aligned clause must be
9851*67e74705SXin Li     // array, pointer, reference to array, or reference to pointer.
9852*67e74705SXin Li     QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
9853*67e74705SXin Li     const Type *Ty = QType.getTypePtrOrNull();
9854*67e74705SXin Li     if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
9855*67e74705SXin Li       Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
9856*67e74705SXin Li           << QType << getLangOpts().CPlusPlus << ERange;
9857*67e74705SXin Li       bool IsDecl =
9858*67e74705SXin Li           !VD ||
9859*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
9860*67e74705SXin Li       Diag(D->getLocation(),
9861*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
9862*67e74705SXin Li           << D;
9863*67e74705SXin Li       continue;
9864*67e74705SXin Li     }
9865*67e74705SXin Li 
9866*67e74705SXin Li     // OpenMP  [2.8.1, simd construct, Restrictions]
9867*67e74705SXin Li     // A list-item cannot appear in more than one aligned clause.
9868*67e74705SXin Li     if (Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
9869*67e74705SXin Li       Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
9870*67e74705SXin Li       Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
9871*67e74705SXin Li           << getOpenMPClauseName(OMPC_aligned);
9872*67e74705SXin Li       continue;
9873*67e74705SXin Li     }
9874*67e74705SXin Li 
9875*67e74705SXin Li     DeclRefExpr *Ref = nullptr;
9876*67e74705SXin Li     if (!VD && IsOpenMPCapturedDecl(D))
9877*67e74705SXin Li       Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
9878*67e74705SXin Li     Vars.push_back(DefaultFunctionArrayConversion(
9879*67e74705SXin Li                        (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
9880*67e74705SXin Li                        .get());
9881*67e74705SXin Li   }
9882*67e74705SXin Li 
9883*67e74705SXin Li   // OpenMP [2.8.1, simd construct, Description]
9884*67e74705SXin Li   // The parameter of the aligned clause, alignment, must be a constant
9885*67e74705SXin Li   // positive integer expression.
9886*67e74705SXin Li   // If no optional parameter is specified, implementation-defined default
9887*67e74705SXin Li   // alignments for SIMD instructions on the target platforms are assumed.
9888*67e74705SXin Li   if (Alignment != nullptr) {
9889*67e74705SXin Li     ExprResult AlignResult =
9890*67e74705SXin Li         VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
9891*67e74705SXin Li     if (AlignResult.isInvalid())
9892*67e74705SXin Li       return nullptr;
9893*67e74705SXin Li     Alignment = AlignResult.get();
9894*67e74705SXin Li   }
9895*67e74705SXin Li   if (Vars.empty())
9896*67e74705SXin Li     return nullptr;
9897*67e74705SXin Li 
9898*67e74705SXin Li   return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
9899*67e74705SXin Li                                   EndLoc, Vars, Alignment);
9900*67e74705SXin Li }
9901*67e74705SXin Li 
ActOnOpenMPCopyinClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)9902*67e74705SXin Li OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
9903*67e74705SXin Li                                          SourceLocation StartLoc,
9904*67e74705SXin Li                                          SourceLocation LParenLoc,
9905*67e74705SXin Li                                          SourceLocation EndLoc) {
9906*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
9907*67e74705SXin Li   SmallVector<Expr *, 8> SrcExprs;
9908*67e74705SXin Li   SmallVector<Expr *, 8> DstExprs;
9909*67e74705SXin Li   SmallVector<Expr *, 8> AssignmentOps;
9910*67e74705SXin Li   for (auto &RefExpr : VarList) {
9911*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP copyin clause.");
9912*67e74705SXin Li     if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
9913*67e74705SXin Li       // It will be analyzed later.
9914*67e74705SXin Li       Vars.push_back(RefExpr);
9915*67e74705SXin Li       SrcExprs.push_back(nullptr);
9916*67e74705SXin Li       DstExprs.push_back(nullptr);
9917*67e74705SXin Li       AssignmentOps.push_back(nullptr);
9918*67e74705SXin Li       continue;
9919*67e74705SXin Li     }
9920*67e74705SXin Li 
9921*67e74705SXin Li     SourceLocation ELoc = RefExpr->getExprLoc();
9922*67e74705SXin Li     // OpenMP [2.1, C/C++]
9923*67e74705SXin Li     //  A list item is a variable name.
9924*67e74705SXin Li     // OpenMP  [2.14.4.1, Restrictions, p.1]
9925*67e74705SXin Li     //  A list item that appears in a copyin clause must be threadprivate.
9926*67e74705SXin Li     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
9927*67e74705SXin Li     if (!DE || !isa<VarDecl>(DE->getDecl())) {
9928*67e74705SXin Li       Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
9929*67e74705SXin Li           << 0 << RefExpr->getSourceRange();
9930*67e74705SXin Li       continue;
9931*67e74705SXin Li     }
9932*67e74705SXin Li 
9933*67e74705SXin Li     Decl *D = DE->getDecl();
9934*67e74705SXin Li     VarDecl *VD = cast<VarDecl>(D);
9935*67e74705SXin Li 
9936*67e74705SXin Li     QualType Type = VD->getType();
9937*67e74705SXin Li     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
9938*67e74705SXin Li       // It will be analyzed later.
9939*67e74705SXin Li       Vars.push_back(DE);
9940*67e74705SXin Li       SrcExprs.push_back(nullptr);
9941*67e74705SXin Li       DstExprs.push_back(nullptr);
9942*67e74705SXin Li       AssignmentOps.push_back(nullptr);
9943*67e74705SXin Li       continue;
9944*67e74705SXin Li     }
9945*67e74705SXin Li 
9946*67e74705SXin Li     // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
9947*67e74705SXin Li     //  A list item that appears in a copyin clause must be threadprivate.
9948*67e74705SXin Li     if (!DSAStack->isThreadPrivate(VD)) {
9949*67e74705SXin Li       Diag(ELoc, diag::err_omp_required_access)
9950*67e74705SXin Li           << getOpenMPClauseName(OMPC_copyin)
9951*67e74705SXin Li           << getOpenMPDirectiveName(OMPD_threadprivate);
9952*67e74705SXin Li       continue;
9953*67e74705SXin Li     }
9954*67e74705SXin Li 
9955*67e74705SXin Li     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
9956*67e74705SXin Li     //  A variable of class type (or array thereof) that appears in a
9957*67e74705SXin Li     //  copyin clause requires an accessible, unambiguous copy assignment
9958*67e74705SXin Li     //  operator for the class type.
9959*67e74705SXin Li     auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
9960*67e74705SXin Li     auto *SrcVD =
9961*67e74705SXin Li         buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
9962*67e74705SXin Li                      ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9963*67e74705SXin Li     auto *PseudoSrcExpr = buildDeclRefExpr(
9964*67e74705SXin Li         *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
9965*67e74705SXin Li     auto *DstVD =
9966*67e74705SXin Li         buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
9967*67e74705SXin Li                      VD->hasAttrs() ? &VD->getAttrs() : nullptr);
9968*67e74705SXin Li     auto *PseudoDstExpr =
9969*67e74705SXin Li         buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
9970*67e74705SXin Li     // For arrays generate assignment operation for single element and replace
9971*67e74705SXin Li     // it by the original array element in CodeGen.
9972*67e74705SXin Li     auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
9973*67e74705SXin Li                                    PseudoDstExpr, PseudoSrcExpr);
9974*67e74705SXin Li     if (AssignmentOp.isInvalid())
9975*67e74705SXin Li       continue;
9976*67e74705SXin Li     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
9977*67e74705SXin Li                                        /*DiscardedValue=*/true);
9978*67e74705SXin Li     if (AssignmentOp.isInvalid())
9979*67e74705SXin Li       continue;
9980*67e74705SXin Li 
9981*67e74705SXin Li     DSAStack->addDSA(VD, DE, OMPC_copyin);
9982*67e74705SXin Li     Vars.push_back(DE);
9983*67e74705SXin Li     SrcExprs.push_back(PseudoSrcExpr);
9984*67e74705SXin Li     DstExprs.push_back(PseudoDstExpr);
9985*67e74705SXin Li     AssignmentOps.push_back(AssignmentOp.get());
9986*67e74705SXin Li   }
9987*67e74705SXin Li 
9988*67e74705SXin Li   if (Vars.empty())
9989*67e74705SXin Li     return nullptr;
9990*67e74705SXin Li 
9991*67e74705SXin Li   return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9992*67e74705SXin Li                                  SrcExprs, DstExprs, AssignmentOps);
9993*67e74705SXin Li }
9994*67e74705SXin Li 
ActOnOpenMPCopyprivateClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)9995*67e74705SXin Li OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
9996*67e74705SXin Li                                               SourceLocation StartLoc,
9997*67e74705SXin Li                                               SourceLocation LParenLoc,
9998*67e74705SXin Li                                               SourceLocation EndLoc) {
9999*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
10000*67e74705SXin Li   SmallVector<Expr *, 8> SrcExprs;
10001*67e74705SXin Li   SmallVector<Expr *, 8> DstExprs;
10002*67e74705SXin Li   SmallVector<Expr *, 8> AssignmentOps;
10003*67e74705SXin Li   for (auto &RefExpr : VarList) {
10004*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP linear clause.");
10005*67e74705SXin Li     SourceLocation ELoc;
10006*67e74705SXin Li     SourceRange ERange;
10007*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
10008*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange,
10009*67e74705SXin Li                               /*AllowArraySection=*/false);
10010*67e74705SXin Li     if (Res.second) {
10011*67e74705SXin Li       // It will be analyzed later.
10012*67e74705SXin Li       Vars.push_back(RefExpr);
10013*67e74705SXin Li       SrcExprs.push_back(nullptr);
10014*67e74705SXin Li       DstExprs.push_back(nullptr);
10015*67e74705SXin Li       AssignmentOps.push_back(nullptr);
10016*67e74705SXin Li     }
10017*67e74705SXin Li     ValueDecl *D = Res.first;
10018*67e74705SXin Li     if (!D)
10019*67e74705SXin Li       continue;
10020*67e74705SXin Li 
10021*67e74705SXin Li     QualType Type = D->getType();
10022*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(D);
10023*67e74705SXin Li 
10024*67e74705SXin Li     // OpenMP [2.14.4.2, Restrictions, p.2]
10025*67e74705SXin Li     //  A list item that appears in a copyprivate clause may not appear in a
10026*67e74705SXin Li     //  private or firstprivate clause on the single construct.
10027*67e74705SXin Li     if (!VD || !DSAStack->isThreadPrivate(VD)) {
10028*67e74705SXin Li       auto DVar = DSAStack->getTopDSA(D, false);
10029*67e74705SXin Li       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
10030*67e74705SXin Li           DVar.RefExpr) {
10031*67e74705SXin Li         Diag(ELoc, diag::err_omp_wrong_dsa)
10032*67e74705SXin Li             << getOpenMPClauseName(DVar.CKind)
10033*67e74705SXin Li             << getOpenMPClauseName(OMPC_copyprivate);
10034*67e74705SXin Li         ReportOriginalDSA(*this, DSAStack, D, DVar);
10035*67e74705SXin Li         continue;
10036*67e74705SXin Li       }
10037*67e74705SXin Li 
10038*67e74705SXin Li       // OpenMP [2.11.4.2, Restrictions, p.1]
10039*67e74705SXin Li       //  All list items that appear in a copyprivate clause must be either
10040*67e74705SXin Li       //  threadprivate or private in the enclosing context.
10041*67e74705SXin Li       if (DVar.CKind == OMPC_unknown) {
10042*67e74705SXin Li         DVar = DSAStack->getImplicitDSA(D, false);
10043*67e74705SXin Li         if (DVar.CKind == OMPC_shared) {
10044*67e74705SXin Li           Diag(ELoc, diag::err_omp_required_access)
10045*67e74705SXin Li               << getOpenMPClauseName(OMPC_copyprivate)
10046*67e74705SXin Li               << "threadprivate or private in the enclosing context";
10047*67e74705SXin Li           ReportOriginalDSA(*this, DSAStack, D, DVar);
10048*67e74705SXin Li           continue;
10049*67e74705SXin Li         }
10050*67e74705SXin Li       }
10051*67e74705SXin Li     }
10052*67e74705SXin Li 
10053*67e74705SXin Li     // Variably modified types are not supported.
10054*67e74705SXin Li     if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
10055*67e74705SXin Li       Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10056*67e74705SXin Li           << getOpenMPClauseName(OMPC_copyprivate) << Type
10057*67e74705SXin Li           << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10058*67e74705SXin Li       bool IsDecl =
10059*67e74705SXin Li           !VD ||
10060*67e74705SXin Li           VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
10061*67e74705SXin Li       Diag(D->getLocation(),
10062*67e74705SXin Li            IsDecl ? diag::note_previous_decl : diag::note_defined_here)
10063*67e74705SXin Li           << D;
10064*67e74705SXin Li       continue;
10065*67e74705SXin Li     }
10066*67e74705SXin Li 
10067*67e74705SXin Li     // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
10068*67e74705SXin Li     //  A variable of class type (or array thereof) that appears in a
10069*67e74705SXin Li     //  copyin clause requires an accessible, unambiguous copy assignment
10070*67e74705SXin Li     //  operator for the class type.
10071*67e74705SXin Li     Type = Context.getBaseElementType(Type.getNonReferenceType())
10072*67e74705SXin Li                .getUnqualifiedType();
10073*67e74705SXin Li     auto *SrcVD =
10074*67e74705SXin Li         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.src",
10075*67e74705SXin Li                      D->hasAttrs() ? &D->getAttrs() : nullptr);
10076*67e74705SXin Li     auto *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
10077*67e74705SXin Li     auto *DstVD =
10078*67e74705SXin Li         buildVarDecl(*this, RefExpr->getLocStart(), Type, ".copyprivate.dst",
10079*67e74705SXin Li                      D->hasAttrs() ? &D->getAttrs() : nullptr);
10080*67e74705SXin Li     auto *PseudoDstExpr =
10081*67e74705SXin Li         buildDeclRefExpr(*this, DstVD, Type, ELoc);
10082*67e74705SXin Li     auto AssignmentOp = BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
10083*67e74705SXin Li                                    PseudoDstExpr, PseudoSrcExpr);
10084*67e74705SXin Li     if (AssignmentOp.isInvalid())
10085*67e74705SXin Li       continue;
10086*67e74705SXin Li     AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
10087*67e74705SXin Li                                        /*DiscardedValue=*/true);
10088*67e74705SXin Li     if (AssignmentOp.isInvalid())
10089*67e74705SXin Li       continue;
10090*67e74705SXin Li 
10091*67e74705SXin Li     // No need to mark vars as copyprivate, they are already threadprivate or
10092*67e74705SXin Li     // implicitly private.
10093*67e74705SXin Li     assert(VD || IsOpenMPCapturedDecl(D));
10094*67e74705SXin Li     Vars.push_back(
10095*67e74705SXin Li         VD ? RefExpr->IgnoreParens()
10096*67e74705SXin Li            : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
10097*67e74705SXin Li     SrcExprs.push_back(PseudoSrcExpr);
10098*67e74705SXin Li     DstExprs.push_back(PseudoDstExpr);
10099*67e74705SXin Li     AssignmentOps.push_back(AssignmentOp.get());
10100*67e74705SXin Li   }
10101*67e74705SXin Li 
10102*67e74705SXin Li   if (Vars.empty())
10103*67e74705SXin Li     return nullptr;
10104*67e74705SXin Li 
10105*67e74705SXin Li   return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10106*67e74705SXin Li                                       Vars, SrcExprs, DstExprs, AssignmentOps);
10107*67e74705SXin Li }
10108*67e74705SXin Li 
ActOnOpenMPFlushClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)10109*67e74705SXin Li OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
10110*67e74705SXin Li                                         SourceLocation StartLoc,
10111*67e74705SXin Li                                         SourceLocation LParenLoc,
10112*67e74705SXin Li                                         SourceLocation EndLoc) {
10113*67e74705SXin Li   if (VarList.empty())
10114*67e74705SXin Li     return nullptr;
10115*67e74705SXin Li 
10116*67e74705SXin Li   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
10117*67e74705SXin Li }
10118*67e74705SXin Li 
10119*67e74705SXin Li OMPClause *
ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,SourceLocation DepLoc,SourceLocation ColonLoc,ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)10120*67e74705SXin Li Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
10121*67e74705SXin Li                               SourceLocation DepLoc, SourceLocation ColonLoc,
10122*67e74705SXin Li                               ArrayRef<Expr *> VarList, SourceLocation StartLoc,
10123*67e74705SXin Li                               SourceLocation LParenLoc, SourceLocation EndLoc) {
10124*67e74705SXin Li   if (DSAStack->getCurrentDirective() == OMPD_ordered &&
10125*67e74705SXin Li       DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
10126*67e74705SXin Li     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
10127*67e74705SXin Li         << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
10128*67e74705SXin Li     return nullptr;
10129*67e74705SXin Li   }
10130*67e74705SXin Li   if (DSAStack->getCurrentDirective() != OMPD_ordered &&
10131*67e74705SXin Li       (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
10132*67e74705SXin Li        DepKind == OMPC_DEPEND_sink)) {
10133*67e74705SXin Li     unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
10134*67e74705SXin Li     Diag(DepLoc, diag::err_omp_unexpected_clause_value)
10135*67e74705SXin Li         << getListOfPossibleValues(OMPC_depend, /*First=*/0,
10136*67e74705SXin Li                                    /*Last=*/OMPC_DEPEND_unknown, Except)
10137*67e74705SXin Li         << getOpenMPClauseName(OMPC_depend);
10138*67e74705SXin Li     return nullptr;
10139*67e74705SXin Li   }
10140*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
10141*67e74705SXin Li   DSAStackTy::OperatorOffsetTy OpsOffs;
10142*67e74705SXin Li   llvm::APSInt DepCounter(/*BitWidth=*/32);
10143*67e74705SXin Li   llvm::APSInt TotalDepCount(/*BitWidth=*/32);
10144*67e74705SXin Li   if (DepKind == OMPC_DEPEND_sink) {
10145*67e74705SXin Li     if (auto *OrderedCountExpr = DSAStack->getParentOrderedRegionParam()) {
10146*67e74705SXin Li       TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
10147*67e74705SXin Li       TotalDepCount.setIsUnsigned(/*Val=*/true);
10148*67e74705SXin Li     }
10149*67e74705SXin Li   }
10150*67e74705SXin Li   if ((DepKind != OMPC_DEPEND_sink && DepKind != OMPC_DEPEND_source) ||
10151*67e74705SXin Li       DSAStack->getParentOrderedRegionParam()) {
10152*67e74705SXin Li     for (auto &RefExpr : VarList) {
10153*67e74705SXin Li       assert(RefExpr && "NULL expr in OpenMP shared clause.");
10154*67e74705SXin Li       if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
10155*67e74705SXin Li         // It will be analyzed later.
10156*67e74705SXin Li         Vars.push_back(RefExpr);
10157*67e74705SXin Li         continue;
10158*67e74705SXin Li       }
10159*67e74705SXin Li 
10160*67e74705SXin Li       SourceLocation ELoc = RefExpr->getExprLoc();
10161*67e74705SXin Li       auto *SimpleExpr = RefExpr->IgnoreParenCasts();
10162*67e74705SXin Li       if (DepKind == OMPC_DEPEND_sink) {
10163*67e74705SXin Li         if (DepCounter >= TotalDepCount) {
10164*67e74705SXin Li           Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
10165*67e74705SXin Li           continue;
10166*67e74705SXin Li         }
10167*67e74705SXin Li         ++DepCounter;
10168*67e74705SXin Li         // OpenMP  [2.13.9, Summary]
10169*67e74705SXin Li         // depend(dependence-type : vec), where dependence-type is:
10170*67e74705SXin Li         // 'sink' and where vec is the iteration vector, which has the form:
10171*67e74705SXin Li         //  x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
10172*67e74705SXin Li         // where n is the value specified by the ordered clause in the loop
10173*67e74705SXin Li         // directive, xi denotes the loop iteration variable of the i-th nested
10174*67e74705SXin Li         // loop associated with the loop directive, and di is a constant
10175*67e74705SXin Li         // non-negative integer.
10176*67e74705SXin Li         if (CurContext->isDependentContext()) {
10177*67e74705SXin Li           // It will be analyzed later.
10178*67e74705SXin Li           Vars.push_back(RefExpr);
10179*67e74705SXin Li           continue;
10180*67e74705SXin Li         }
10181*67e74705SXin Li         SimpleExpr = SimpleExpr->IgnoreImplicit();
10182*67e74705SXin Li         OverloadedOperatorKind OOK = OO_None;
10183*67e74705SXin Li         SourceLocation OOLoc;
10184*67e74705SXin Li         Expr *LHS = SimpleExpr;
10185*67e74705SXin Li         Expr *RHS = nullptr;
10186*67e74705SXin Li         if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
10187*67e74705SXin Li           OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
10188*67e74705SXin Li           OOLoc = BO->getOperatorLoc();
10189*67e74705SXin Li           LHS = BO->getLHS()->IgnoreParenImpCasts();
10190*67e74705SXin Li           RHS = BO->getRHS()->IgnoreParenImpCasts();
10191*67e74705SXin Li         } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
10192*67e74705SXin Li           OOK = OCE->getOperator();
10193*67e74705SXin Li           OOLoc = OCE->getOperatorLoc();
10194*67e74705SXin Li           LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10195*67e74705SXin Li           RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
10196*67e74705SXin Li         } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
10197*67e74705SXin Li           OOK = MCE->getMethodDecl()
10198*67e74705SXin Li                     ->getNameInfo()
10199*67e74705SXin Li                     .getName()
10200*67e74705SXin Li                     .getCXXOverloadedOperator();
10201*67e74705SXin Li           OOLoc = MCE->getCallee()->getExprLoc();
10202*67e74705SXin Li           LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
10203*67e74705SXin Li           RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
10204*67e74705SXin Li         }
10205*67e74705SXin Li         SourceLocation ELoc;
10206*67e74705SXin Li         SourceRange ERange;
10207*67e74705SXin Li         auto Res = getPrivateItem(*this, LHS, ELoc, ERange,
10208*67e74705SXin Li                                   /*AllowArraySection=*/false);
10209*67e74705SXin Li         if (Res.second) {
10210*67e74705SXin Li           // It will be analyzed later.
10211*67e74705SXin Li           Vars.push_back(RefExpr);
10212*67e74705SXin Li         }
10213*67e74705SXin Li         ValueDecl *D = Res.first;
10214*67e74705SXin Li         if (!D)
10215*67e74705SXin Li           continue;
10216*67e74705SXin Li 
10217*67e74705SXin Li         if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
10218*67e74705SXin Li           Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
10219*67e74705SXin Li           continue;
10220*67e74705SXin Li         }
10221*67e74705SXin Li         if (RHS) {
10222*67e74705SXin Li           ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
10223*67e74705SXin Li               RHS, OMPC_depend, /*StrictlyPositive=*/false);
10224*67e74705SXin Li           if (RHSRes.isInvalid())
10225*67e74705SXin Li             continue;
10226*67e74705SXin Li         }
10227*67e74705SXin Li         if (!CurContext->isDependentContext() &&
10228*67e74705SXin Li             DSAStack->getParentOrderedRegionParam() &&
10229*67e74705SXin Li             DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
10230*67e74705SXin Li           Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
10231*67e74705SXin Li               << DSAStack->getParentLoopControlVariable(
10232*67e74705SXin Li                      DepCounter.getZExtValue());
10233*67e74705SXin Li           continue;
10234*67e74705SXin Li         }
10235*67e74705SXin Li         OpsOffs.push_back({RHS, OOK});
10236*67e74705SXin Li       } else {
10237*67e74705SXin Li         // OpenMP  [2.11.1.1, Restrictions, p.3]
10238*67e74705SXin Li         //  A variable that is part of another variable (such as a field of a
10239*67e74705SXin Li         //  structure) but is not an array element or an array section cannot
10240*67e74705SXin Li         //  appear  in a depend clause.
10241*67e74705SXin Li         auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
10242*67e74705SXin Li         auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
10243*67e74705SXin Li         auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
10244*67e74705SXin Li         if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
10245*67e74705SXin Li             (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
10246*67e74705SXin Li             (ASE &&
10247*67e74705SXin Li              !ASE->getBase()
10248*67e74705SXin Li                   ->getType()
10249*67e74705SXin Li                   .getNonReferenceType()
10250*67e74705SXin Li                   ->isPointerType() &&
10251*67e74705SXin Li              !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
10252*67e74705SXin Li           Diag(ELoc, diag::err_omp_expected_var_name_member_expr_or_array_item)
10253*67e74705SXin Li               << 0 << RefExpr->getSourceRange();
10254*67e74705SXin Li           continue;
10255*67e74705SXin Li         }
10256*67e74705SXin Li       }
10257*67e74705SXin Li       Vars.push_back(RefExpr->IgnoreParenImpCasts());
10258*67e74705SXin Li     }
10259*67e74705SXin Li 
10260*67e74705SXin Li     if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
10261*67e74705SXin Li         TotalDepCount > VarList.size() &&
10262*67e74705SXin Li         DSAStack->getParentOrderedRegionParam()) {
10263*67e74705SXin Li       Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
10264*67e74705SXin Li           << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
10265*67e74705SXin Li     }
10266*67e74705SXin Li     if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
10267*67e74705SXin Li         Vars.empty())
10268*67e74705SXin Li       return nullptr;
10269*67e74705SXin Li   }
10270*67e74705SXin Li   auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
10271*67e74705SXin Li                                     DepKind, DepLoc, ColonLoc, Vars);
10272*67e74705SXin Li   if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source)
10273*67e74705SXin Li     DSAStack->addDoacrossDependClause(C, OpsOffs);
10274*67e74705SXin Li   return C;
10275*67e74705SXin Li }
10276*67e74705SXin Li 
ActOnOpenMPDeviceClause(Expr * Device,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)10277*67e74705SXin Li OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
10278*67e74705SXin Li                                          SourceLocation LParenLoc,
10279*67e74705SXin Li                                          SourceLocation EndLoc) {
10280*67e74705SXin Li   Expr *ValExpr = Device;
10281*67e74705SXin Li 
10282*67e74705SXin Li   // OpenMP [2.9.1, Restrictions]
10283*67e74705SXin Li   // The device expression must evaluate to a non-negative integer value.
10284*67e74705SXin Li   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
10285*67e74705SXin Li                                  /*StrictlyPositive=*/false))
10286*67e74705SXin Li     return nullptr;
10287*67e74705SXin Li 
10288*67e74705SXin Li   return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
10289*67e74705SXin Li }
10290*67e74705SXin Li 
IsCXXRecordForMappable(Sema & SemaRef,SourceLocation Loc,DSAStackTy * Stack,CXXRecordDecl * RD)10291*67e74705SXin Li static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
10292*67e74705SXin Li                                    DSAStackTy *Stack, CXXRecordDecl *RD) {
10293*67e74705SXin Li   if (!RD || RD->isInvalidDecl())
10294*67e74705SXin Li     return true;
10295*67e74705SXin Li 
10296*67e74705SXin Li   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
10297*67e74705SXin Li     if (auto *CTD = CTSD->getSpecializedTemplate())
10298*67e74705SXin Li       RD = CTD->getTemplatedDecl();
10299*67e74705SXin Li   auto QTy = SemaRef.Context.getRecordType(RD);
10300*67e74705SXin Li   if (RD->isDynamicClass()) {
10301*67e74705SXin Li     SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10302*67e74705SXin Li     SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
10303*67e74705SXin Li     return false;
10304*67e74705SXin Li   }
10305*67e74705SXin Li   auto *DC = RD;
10306*67e74705SXin Li   bool IsCorrect = true;
10307*67e74705SXin Li   for (auto *I : DC->decls()) {
10308*67e74705SXin Li     if (I) {
10309*67e74705SXin Li       if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
10310*67e74705SXin Li         if (MD->isStatic()) {
10311*67e74705SXin Li           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10312*67e74705SXin Li           SemaRef.Diag(MD->getLocation(),
10313*67e74705SXin Li                        diag::note_omp_static_member_in_target);
10314*67e74705SXin Li           IsCorrect = false;
10315*67e74705SXin Li         }
10316*67e74705SXin Li       } else if (auto *VD = dyn_cast<VarDecl>(I)) {
10317*67e74705SXin Li         if (VD->isStaticDataMember()) {
10318*67e74705SXin Li           SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
10319*67e74705SXin Li           SemaRef.Diag(VD->getLocation(),
10320*67e74705SXin Li                        diag::note_omp_static_member_in_target);
10321*67e74705SXin Li           IsCorrect = false;
10322*67e74705SXin Li         }
10323*67e74705SXin Li       }
10324*67e74705SXin Li     }
10325*67e74705SXin Li   }
10326*67e74705SXin Li 
10327*67e74705SXin Li   for (auto &I : RD->bases()) {
10328*67e74705SXin Li     if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
10329*67e74705SXin Li                                 I.getType()->getAsCXXRecordDecl()))
10330*67e74705SXin Li       IsCorrect = false;
10331*67e74705SXin Li   }
10332*67e74705SXin Li   return IsCorrect;
10333*67e74705SXin Li }
10334*67e74705SXin Li 
CheckTypeMappable(SourceLocation SL,SourceRange SR,Sema & SemaRef,DSAStackTy * Stack,QualType QTy)10335*67e74705SXin Li static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
10336*67e74705SXin Li                               DSAStackTy *Stack, QualType QTy) {
10337*67e74705SXin Li   NamedDecl *ND;
10338*67e74705SXin Li   if (QTy->isIncompleteType(&ND)) {
10339*67e74705SXin Li     SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
10340*67e74705SXin Li     return false;
10341*67e74705SXin Li   } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
10342*67e74705SXin Li     if (!RD->isInvalidDecl() &&
10343*67e74705SXin Li         !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
10344*67e74705SXin Li       return false;
10345*67e74705SXin Li   }
10346*67e74705SXin Li   return true;
10347*67e74705SXin Li }
10348*67e74705SXin Li 
10349*67e74705SXin Li /// \brief Return true if it can be proven that the provided array expression
10350*67e74705SXin Li /// (array section or array subscript) does NOT specify the whole size of the
10351*67e74705SXin Li /// array whose base type is \a BaseQTy.
CheckArrayExpressionDoesNotReferToWholeSize(Sema & SemaRef,const Expr * E,QualType BaseQTy)10352*67e74705SXin Li static bool CheckArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
10353*67e74705SXin Li                                                         const Expr *E,
10354*67e74705SXin Li                                                         QualType BaseQTy) {
10355*67e74705SXin Li   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10356*67e74705SXin Li 
10357*67e74705SXin Li   // If this is an array subscript, it refers to the whole size if the size of
10358*67e74705SXin Li   // the dimension is constant and equals 1. Also, an array section assumes the
10359*67e74705SXin Li   // format of an array subscript if no colon is used.
10360*67e74705SXin Li   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
10361*67e74705SXin Li     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10362*67e74705SXin Li       return ATy->getSize().getSExtValue() != 1;
10363*67e74705SXin Li     // Size can't be evaluated statically.
10364*67e74705SXin Li     return false;
10365*67e74705SXin Li   }
10366*67e74705SXin Li 
10367*67e74705SXin Li   assert(OASE && "Expecting array section if not an array subscript.");
10368*67e74705SXin Li   auto *LowerBound = OASE->getLowerBound();
10369*67e74705SXin Li   auto *Length = OASE->getLength();
10370*67e74705SXin Li 
10371*67e74705SXin Li   // If there is a lower bound that does not evaluates to zero, we are not
10372*67e74705SXin Li   // convering the whole dimension.
10373*67e74705SXin Li   if (LowerBound) {
10374*67e74705SXin Li     llvm::APSInt ConstLowerBound;
10375*67e74705SXin Li     if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
10376*67e74705SXin Li       return false; // Can't get the integer value as a constant.
10377*67e74705SXin Li     if (ConstLowerBound.getSExtValue())
10378*67e74705SXin Li       return true;
10379*67e74705SXin Li   }
10380*67e74705SXin Li 
10381*67e74705SXin Li   // If we don't have a length we covering the whole dimension.
10382*67e74705SXin Li   if (!Length)
10383*67e74705SXin Li     return false;
10384*67e74705SXin Li 
10385*67e74705SXin Li   // If the base is a pointer, we don't have a way to get the size of the
10386*67e74705SXin Li   // pointee.
10387*67e74705SXin Li   if (BaseQTy->isPointerType())
10388*67e74705SXin Li     return false;
10389*67e74705SXin Li 
10390*67e74705SXin Li   // We can only check if the length is the same as the size of the dimension
10391*67e74705SXin Li   // if we have a constant array.
10392*67e74705SXin Li   auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
10393*67e74705SXin Li   if (!CATy)
10394*67e74705SXin Li     return false;
10395*67e74705SXin Li 
10396*67e74705SXin Li   llvm::APSInt ConstLength;
10397*67e74705SXin Li   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10398*67e74705SXin Li     return false; // Can't get the integer value as a constant.
10399*67e74705SXin Li 
10400*67e74705SXin Li   return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
10401*67e74705SXin Li }
10402*67e74705SXin Li 
10403*67e74705SXin Li // Return true if it can be proven that the provided array expression (array
10404*67e74705SXin Li // section or array subscript) does NOT specify a single element of the array
10405*67e74705SXin Li // whose base type is \a BaseQTy.
CheckArrayExpressionDoesNotReferToUnitySize(Sema & SemaRef,const Expr * E,QualType BaseQTy)10406*67e74705SXin Li static bool CheckArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
10407*67e74705SXin Li                                                        const Expr *E,
10408*67e74705SXin Li                                                        QualType BaseQTy) {
10409*67e74705SXin Li   auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
10410*67e74705SXin Li 
10411*67e74705SXin Li   // An array subscript always refer to a single element. Also, an array section
10412*67e74705SXin Li   // assumes the format of an array subscript if no colon is used.
10413*67e74705SXin Li   if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
10414*67e74705SXin Li     return false;
10415*67e74705SXin Li 
10416*67e74705SXin Li   assert(OASE && "Expecting array section if not an array subscript.");
10417*67e74705SXin Li   auto *Length = OASE->getLength();
10418*67e74705SXin Li 
10419*67e74705SXin Li   // If we don't have a length we have to check if the array has unitary size
10420*67e74705SXin Li   // for this dimension. Also, we should always expect a length if the base type
10421*67e74705SXin Li   // is pointer.
10422*67e74705SXin Li   if (!Length) {
10423*67e74705SXin Li     if (auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
10424*67e74705SXin Li       return ATy->getSize().getSExtValue() != 1;
10425*67e74705SXin Li     // We cannot assume anything.
10426*67e74705SXin Li     return false;
10427*67e74705SXin Li   }
10428*67e74705SXin Li 
10429*67e74705SXin Li   // Check if the length evaluates to 1.
10430*67e74705SXin Li   llvm::APSInt ConstLength;
10431*67e74705SXin Li   if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
10432*67e74705SXin Li     return false; // Can't get the integer value as a constant.
10433*67e74705SXin Li 
10434*67e74705SXin Li   return ConstLength.getSExtValue() != 1;
10435*67e74705SXin Li }
10436*67e74705SXin Li 
10437*67e74705SXin Li // Return the expression of the base of the mappable expression or null if it
10438*67e74705SXin Li // cannot be determined and do all the necessary checks to see if the expression
10439*67e74705SXin Li // is valid as a standalone mappable expression. In the process, record all the
10440*67e74705SXin Li // components of the expression.
CheckMapClauseExpressionBase(Sema & SemaRef,Expr * E,OMPClauseMappableExprCommon::MappableExprComponentList & CurComponents,OpenMPClauseKind CKind)10441*67e74705SXin Li static Expr *CheckMapClauseExpressionBase(
10442*67e74705SXin Li     Sema &SemaRef, Expr *E,
10443*67e74705SXin Li     OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
10444*67e74705SXin Li     OpenMPClauseKind CKind) {
10445*67e74705SXin Li   SourceLocation ELoc = E->getExprLoc();
10446*67e74705SXin Li   SourceRange ERange = E->getSourceRange();
10447*67e74705SXin Li 
10448*67e74705SXin Li   // The base of elements of list in a map clause have to be either:
10449*67e74705SXin Li   //  - a reference to variable or field.
10450*67e74705SXin Li   //  - a member expression.
10451*67e74705SXin Li   //  - an array expression.
10452*67e74705SXin Li   //
10453*67e74705SXin Li   // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
10454*67e74705SXin Li   // reference to 'r'.
10455*67e74705SXin Li   //
10456*67e74705SXin Li   // If we have:
10457*67e74705SXin Li   //
10458*67e74705SXin Li   // struct SS {
10459*67e74705SXin Li   //   Bla S;
10460*67e74705SXin Li   //   foo() {
10461*67e74705SXin Li   //     #pragma omp target map (S.Arr[:12]);
10462*67e74705SXin Li   //   }
10463*67e74705SXin Li   // }
10464*67e74705SXin Li   //
10465*67e74705SXin Li   // We want to retrieve the member expression 'this->S';
10466*67e74705SXin Li 
10467*67e74705SXin Li   Expr *RelevantExpr = nullptr;
10468*67e74705SXin Li 
10469*67e74705SXin Li   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
10470*67e74705SXin Li   //  If a list item is an array section, it must specify contiguous storage.
10471*67e74705SXin Li   //
10472*67e74705SXin Li   // For this restriction it is sufficient that we make sure only references
10473*67e74705SXin Li   // to variables or fields and array expressions, and that no array sections
10474*67e74705SXin Li   // exist except in the rightmost expression (unless they cover the whole
10475*67e74705SXin Li   // dimension of the array). E.g. these would be invalid:
10476*67e74705SXin Li   //
10477*67e74705SXin Li   //   r.ArrS[3:5].Arr[6:7]
10478*67e74705SXin Li   //
10479*67e74705SXin Li   //   r.ArrS[3:5].x
10480*67e74705SXin Li   //
10481*67e74705SXin Li   // but these would be valid:
10482*67e74705SXin Li   //   r.ArrS[3].Arr[6:7]
10483*67e74705SXin Li   //
10484*67e74705SXin Li   //   r.ArrS[3].x
10485*67e74705SXin Li 
10486*67e74705SXin Li   bool AllowUnitySizeArraySection = true;
10487*67e74705SXin Li   bool AllowWholeSizeArraySection = true;
10488*67e74705SXin Li 
10489*67e74705SXin Li   while (!RelevantExpr) {
10490*67e74705SXin Li     E = E->IgnoreParenImpCasts();
10491*67e74705SXin Li 
10492*67e74705SXin Li     if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
10493*67e74705SXin Li       if (!isa<VarDecl>(CurE->getDecl()))
10494*67e74705SXin Li         break;
10495*67e74705SXin Li 
10496*67e74705SXin Li       RelevantExpr = CurE;
10497*67e74705SXin Li 
10498*67e74705SXin Li       // If we got a reference to a declaration, we should not expect any array
10499*67e74705SXin Li       // section before that.
10500*67e74705SXin Li       AllowUnitySizeArraySection = false;
10501*67e74705SXin Li       AllowWholeSizeArraySection = false;
10502*67e74705SXin Li 
10503*67e74705SXin Li       // Record the component.
10504*67e74705SXin Li       CurComponents.push_back(OMPClauseMappableExprCommon::MappableComponent(
10505*67e74705SXin Li           CurE, CurE->getDecl()));
10506*67e74705SXin Li       continue;
10507*67e74705SXin Li     }
10508*67e74705SXin Li 
10509*67e74705SXin Li     if (auto *CurE = dyn_cast<MemberExpr>(E)) {
10510*67e74705SXin Li       auto *BaseE = CurE->getBase()->IgnoreParenImpCasts();
10511*67e74705SXin Li 
10512*67e74705SXin Li       if (isa<CXXThisExpr>(BaseE))
10513*67e74705SXin Li         // We found a base expression: this->Val.
10514*67e74705SXin Li         RelevantExpr = CurE;
10515*67e74705SXin Li       else
10516*67e74705SXin Li         E = BaseE;
10517*67e74705SXin Li 
10518*67e74705SXin Li       if (!isa<FieldDecl>(CurE->getMemberDecl())) {
10519*67e74705SXin Li         SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
10520*67e74705SXin Li             << CurE->getSourceRange();
10521*67e74705SXin Li         break;
10522*67e74705SXin Li       }
10523*67e74705SXin Li 
10524*67e74705SXin Li       auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
10525*67e74705SXin Li 
10526*67e74705SXin Li       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
10527*67e74705SXin Li       //  A bit-field cannot appear in a map clause.
10528*67e74705SXin Li       //
10529*67e74705SXin Li       if (FD->isBitField()) {
10530*67e74705SXin Li         SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
10531*67e74705SXin Li             << CurE->getSourceRange() << getOpenMPClauseName(CKind);
10532*67e74705SXin Li         break;
10533*67e74705SXin Li       }
10534*67e74705SXin Li 
10535*67e74705SXin Li       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10536*67e74705SXin Li       //  If the type of a list item is a reference to a type T then the type
10537*67e74705SXin Li       //  will be considered to be T for all purposes of this clause.
10538*67e74705SXin Li       QualType CurType = BaseE->getType().getNonReferenceType();
10539*67e74705SXin Li 
10540*67e74705SXin Li       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
10541*67e74705SXin Li       //  A list item cannot be a variable that is a member of a structure with
10542*67e74705SXin Li       //  a union type.
10543*67e74705SXin Li       //
10544*67e74705SXin Li       if (auto *RT = CurType->getAs<RecordType>())
10545*67e74705SXin Li         if (RT->isUnionType()) {
10546*67e74705SXin Li           SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
10547*67e74705SXin Li               << CurE->getSourceRange();
10548*67e74705SXin Li           break;
10549*67e74705SXin Li         }
10550*67e74705SXin Li 
10551*67e74705SXin Li       // If we got a member expression, we should not expect any array section
10552*67e74705SXin Li       // before that:
10553*67e74705SXin Li       //
10554*67e74705SXin Li       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
10555*67e74705SXin Li       //  If a list item is an element of a structure, only the rightmost symbol
10556*67e74705SXin Li       //  of the variable reference can be an array section.
10557*67e74705SXin Li       //
10558*67e74705SXin Li       AllowUnitySizeArraySection = false;
10559*67e74705SXin Li       AllowWholeSizeArraySection = false;
10560*67e74705SXin Li 
10561*67e74705SXin Li       // Record the component.
10562*67e74705SXin Li       CurComponents.push_back(
10563*67e74705SXin Li           OMPClauseMappableExprCommon::MappableComponent(CurE, FD));
10564*67e74705SXin Li       continue;
10565*67e74705SXin Li     }
10566*67e74705SXin Li 
10567*67e74705SXin Li     if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
10568*67e74705SXin Li       E = CurE->getBase()->IgnoreParenImpCasts();
10569*67e74705SXin Li 
10570*67e74705SXin Li       if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
10571*67e74705SXin Li         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10572*67e74705SXin Li             << 0 << CurE->getSourceRange();
10573*67e74705SXin Li         break;
10574*67e74705SXin Li       }
10575*67e74705SXin Li 
10576*67e74705SXin Li       // If we got an array subscript that express the whole dimension we
10577*67e74705SXin Li       // can have any array expressions before. If it only expressing part of
10578*67e74705SXin Li       // the dimension, we can only have unitary-size array expressions.
10579*67e74705SXin Li       if (CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
10580*67e74705SXin Li                                                       E->getType()))
10581*67e74705SXin Li         AllowWholeSizeArraySection = false;
10582*67e74705SXin Li 
10583*67e74705SXin Li       // Record the component - we don't have any declaration associated.
10584*67e74705SXin Li       CurComponents.push_back(
10585*67e74705SXin Li           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
10586*67e74705SXin Li       continue;
10587*67e74705SXin Li     }
10588*67e74705SXin Li 
10589*67e74705SXin Li     if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
10590*67e74705SXin Li       E = CurE->getBase()->IgnoreParenImpCasts();
10591*67e74705SXin Li 
10592*67e74705SXin Li       auto CurType =
10593*67e74705SXin Li           OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
10594*67e74705SXin Li 
10595*67e74705SXin Li       // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10596*67e74705SXin Li       //  If the type of a list item is a reference to a type T then the type
10597*67e74705SXin Li       //  will be considered to be T for all purposes of this clause.
10598*67e74705SXin Li       if (CurType->isReferenceType())
10599*67e74705SXin Li         CurType = CurType->getPointeeType();
10600*67e74705SXin Li 
10601*67e74705SXin Li       bool IsPointer = CurType->isAnyPointerType();
10602*67e74705SXin Li 
10603*67e74705SXin Li       if (!IsPointer && !CurType->isArrayType()) {
10604*67e74705SXin Li         SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
10605*67e74705SXin Li             << 0 << CurE->getSourceRange();
10606*67e74705SXin Li         break;
10607*67e74705SXin Li       }
10608*67e74705SXin Li 
10609*67e74705SXin Li       bool NotWhole =
10610*67e74705SXin Li           CheckArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
10611*67e74705SXin Li       bool NotUnity =
10612*67e74705SXin Li           CheckArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
10613*67e74705SXin Li 
10614*67e74705SXin Li       if (AllowWholeSizeArraySection && AllowUnitySizeArraySection) {
10615*67e74705SXin Li         // Any array section is currently allowed.
10616*67e74705SXin Li         //
10617*67e74705SXin Li         // If this array section refers to the whole dimension we can still
10618*67e74705SXin Li         // accept other array sections before this one, except if the base is a
10619*67e74705SXin Li         // pointer. Otherwise, only unitary sections are accepted.
10620*67e74705SXin Li         if (NotWhole || IsPointer)
10621*67e74705SXin Li           AllowWholeSizeArraySection = false;
10622*67e74705SXin Li       } else if ((AllowUnitySizeArraySection && NotUnity) ||
10623*67e74705SXin Li                  (AllowWholeSizeArraySection && NotWhole)) {
10624*67e74705SXin Li         // A unity or whole array section is not allowed and that is not
10625*67e74705SXin Li         // compatible with the properties of the current array section.
10626*67e74705SXin Li         SemaRef.Diag(
10627*67e74705SXin Li             ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
10628*67e74705SXin Li             << CurE->getSourceRange();
10629*67e74705SXin Li         break;
10630*67e74705SXin Li       }
10631*67e74705SXin Li 
10632*67e74705SXin Li       // Record the component - we don't have any declaration associated.
10633*67e74705SXin Li       CurComponents.push_back(
10634*67e74705SXin Li           OMPClauseMappableExprCommon::MappableComponent(CurE, nullptr));
10635*67e74705SXin Li       continue;
10636*67e74705SXin Li     }
10637*67e74705SXin Li 
10638*67e74705SXin Li     // If nothing else worked, this is not a valid map clause expression.
10639*67e74705SXin Li     SemaRef.Diag(ELoc,
10640*67e74705SXin Li                  diag::err_omp_expected_named_var_member_or_array_expression)
10641*67e74705SXin Li         << ERange;
10642*67e74705SXin Li     break;
10643*67e74705SXin Li   }
10644*67e74705SXin Li 
10645*67e74705SXin Li   return RelevantExpr;
10646*67e74705SXin Li }
10647*67e74705SXin Li 
10648*67e74705SXin Li // Return true if expression E associated with value VD has conflicts with other
10649*67e74705SXin Li // map information.
CheckMapConflicts(Sema & SemaRef,DSAStackTy * DSAS,ValueDecl * VD,Expr * E,bool CurrentRegionOnly,OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,OpenMPClauseKind CKind)10650*67e74705SXin Li static bool CheckMapConflicts(
10651*67e74705SXin Li     Sema &SemaRef, DSAStackTy *DSAS, ValueDecl *VD, Expr *E,
10652*67e74705SXin Li     bool CurrentRegionOnly,
10653*67e74705SXin Li     OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
10654*67e74705SXin Li     OpenMPClauseKind CKind) {
10655*67e74705SXin Li   assert(VD && E);
10656*67e74705SXin Li   SourceLocation ELoc = E->getExprLoc();
10657*67e74705SXin Li   SourceRange ERange = E->getSourceRange();
10658*67e74705SXin Li 
10659*67e74705SXin Li   // In order to easily check the conflicts we need to match each component of
10660*67e74705SXin Li   // the expression under test with the components of the expressions that are
10661*67e74705SXin Li   // already in the stack.
10662*67e74705SXin Li 
10663*67e74705SXin Li   assert(!CurComponents.empty() && "Map clause expression with no components!");
10664*67e74705SXin Li   assert(CurComponents.back().getAssociatedDeclaration() == VD &&
10665*67e74705SXin Li          "Map clause expression with unexpected base!");
10666*67e74705SXin Li 
10667*67e74705SXin Li   // Variables to help detecting enclosing problems in data environment nests.
10668*67e74705SXin Li   bool IsEnclosedByDataEnvironmentExpr = false;
10669*67e74705SXin Li   const Expr *EnclosingExpr = nullptr;
10670*67e74705SXin Li 
10671*67e74705SXin Li   bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
10672*67e74705SXin Li       VD, CurrentRegionOnly,
10673*67e74705SXin Li       [&](OMPClauseMappableExprCommon::MappableExprComponentListRef
10674*67e74705SXin Li               StackComponents) -> bool {
10675*67e74705SXin Li 
10676*67e74705SXin Li         assert(!StackComponents.empty() &&
10677*67e74705SXin Li                "Map clause expression with no components!");
10678*67e74705SXin Li         assert(StackComponents.back().getAssociatedDeclaration() == VD &&
10679*67e74705SXin Li                "Map clause expression with unexpected base!");
10680*67e74705SXin Li 
10681*67e74705SXin Li         // The whole expression in the stack.
10682*67e74705SXin Li         auto *RE = StackComponents.front().getAssociatedExpression();
10683*67e74705SXin Li 
10684*67e74705SXin Li         // Expressions must start from the same base. Here we detect at which
10685*67e74705SXin Li         // point both expressions diverge from each other and see if we can
10686*67e74705SXin Li         // detect if the memory referred to both expressions is contiguous and
10687*67e74705SXin Li         // do not overlap.
10688*67e74705SXin Li         auto CI = CurComponents.rbegin();
10689*67e74705SXin Li         auto CE = CurComponents.rend();
10690*67e74705SXin Li         auto SI = StackComponents.rbegin();
10691*67e74705SXin Li         auto SE = StackComponents.rend();
10692*67e74705SXin Li         for (; CI != CE && SI != SE; ++CI, ++SI) {
10693*67e74705SXin Li 
10694*67e74705SXin Li           // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
10695*67e74705SXin Li           //  At most one list item can be an array item derived from a given
10696*67e74705SXin Li           //  variable in map clauses of the same construct.
10697*67e74705SXin Li           if (CurrentRegionOnly &&
10698*67e74705SXin Li               (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
10699*67e74705SXin Li                isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
10700*67e74705SXin Li               (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
10701*67e74705SXin Li                isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
10702*67e74705SXin Li             SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
10703*67e74705SXin Li                          diag::err_omp_multiple_array_items_in_map_clause)
10704*67e74705SXin Li                 << CI->getAssociatedExpression()->getSourceRange();
10705*67e74705SXin Li             SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
10706*67e74705SXin Li                          diag::note_used_here)
10707*67e74705SXin Li                 << SI->getAssociatedExpression()->getSourceRange();
10708*67e74705SXin Li             return true;
10709*67e74705SXin Li           }
10710*67e74705SXin Li 
10711*67e74705SXin Li           // Do both expressions have the same kind?
10712*67e74705SXin Li           if (CI->getAssociatedExpression()->getStmtClass() !=
10713*67e74705SXin Li               SI->getAssociatedExpression()->getStmtClass())
10714*67e74705SXin Li             break;
10715*67e74705SXin Li 
10716*67e74705SXin Li           // Are we dealing with different variables/fields?
10717*67e74705SXin Li           if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
10718*67e74705SXin Li             break;
10719*67e74705SXin Li         }
10720*67e74705SXin Li 
10721*67e74705SXin Li         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10722*67e74705SXin Li         //  List items of map clauses in the same construct must not share
10723*67e74705SXin Li         //  original storage.
10724*67e74705SXin Li         //
10725*67e74705SXin Li         // If the expressions are exactly the same or one is a subset of the
10726*67e74705SXin Li         // other, it means they are sharing storage.
10727*67e74705SXin Li         if (CI == CE && SI == SE) {
10728*67e74705SXin Li           if (CurrentRegionOnly) {
10729*67e74705SXin Li             if (CKind == OMPC_map)
10730*67e74705SXin Li               SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10731*67e74705SXin Li             else {
10732*67e74705SXin Li               assert(CKind == OMPC_to || CKind == OMPC_from);
10733*67e74705SXin Li               SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10734*67e74705SXin Li                   << ERange;
10735*67e74705SXin Li             }
10736*67e74705SXin Li             SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10737*67e74705SXin Li                 << RE->getSourceRange();
10738*67e74705SXin Li             return true;
10739*67e74705SXin Li           } else {
10740*67e74705SXin Li             // If we find the same expression in the enclosing data environment,
10741*67e74705SXin Li             // that is legal.
10742*67e74705SXin Li             IsEnclosedByDataEnvironmentExpr = true;
10743*67e74705SXin Li             return false;
10744*67e74705SXin Li           }
10745*67e74705SXin Li         }
10746*67e74705SXin Li 
10747*67e74705SXin Li         QualType DerivedType =
10748*67e74705SXin Li             std::prev(CI)->getAssociatedDeclaration()->getType();
10749*67e74705SXin Li         SourceLocation DerivedLoc =
10750*67e74705SXin Li             std::prev(CI)->getAssociatedExpression()->getExprLoc();
10751*67e74705SXin Li 
10752*67e74705SXin Li         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10753*67e74705SXin Li         //  If the type of a list item is a reference to a type T then the type
10754*67e74705SXin Li         //  will be considered to be T for all purposes of this clause.
10755*67e74705SXin Li         DerivedType = DerivedType.getNonReferenceType();
10756*67e74705SXin Li 
10757*67e74705SXin Li         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
10758*67e74705SXin Li         //  A variable for which the type is pointer and an array section
10759*67e74705SXin Li         //  derived from that variable must not appear as list items of map
10760*67e74705SXin Li         //  clauses of the same construct.
10761*67e74705SXin Li         //
10762*67e74705SXin Li         // Also, cover one of the cases in:
10763*67e74705SXin Li         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10764*67e74705SXin Li         //  If any part of the original storage of a list item has corresponding
10765*67e74705SXin Li         //  storage in the device data environment, all of the original storage
10766*67e74705SXin Li         //  must have corresponding storage in the device data environment.
10767*67e74705SXin Li         //
10768*67e74705SXin Li         if (DerivedType->isAnyPointerType()) {
10769*67e74705SXin Li           if (CI == CE || SI == SE) {
10770*67e74705SXin Li             SemaRef.Diag(
10771*67e74705SXin Li                 DerivedLoc,
10772*67e74705SXin Li                 diag::err_omp_pointer_mapped_along_with_derived_section)
10773*67e74705SXin Li                 << DerivedLoc;
10774*67e74705SXin Li           } else {
10775*67e74705SXin Li             assert(CI != CE && SI != SE);
10776*67e74705SXin Li             SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_derreferenced)
10777*67e74705SXin Li                 << DerivedLoc;
10778*67e74705SXin Li           }
10779*67e74705SXin Li           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10780*67e74705SXin Li               << RE->getSourceRange();
10781*67e74705SXin Li           return true;
10782*67e74705SXin Li         }
10783*67e74705SXin Li 
10784*67e74705SXin Li         // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
10785*67e74705SXin Li         //  List items of map clauses in the same construct must not share
10786*67e74705SXin Li         //  original storage.
10787*67e74705SXin Li         //
10788*67e74705SXin Li         // An expression is a subset of the other.
10789*67e74705SXin Li         if (CurrentRegionOnly && (CI == CE || SI == SE)) {
10790*67e74705SXin Li           if (CKind == OMPC_map)
10791*67e74705SXin Li             SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
10792*67e74705SXin Li           else {
10793*67e74705SXin Li             assert(CKind == OMPC_to || CKind == OMPC_from);
10794*67e74705SXin Li             SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
10795*67e74705SXin Li                 << ERange;
10796*67e74705SXin Li           }
10797*67e74705SXin Li           SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
10798*67e74705SXin Li               << RE->getSourceRange();
10799*67e74705SXin Li           return true;
10800*67e74705SXin Li         }
10801*67e74705SXin Li 
10802*67e74705SXin Li         // The current expression uses the same base as other expression in the
10803*67e74705SXin Li         // data environment but does not contain it completely.
10804*67e74705SXin Li         if (!CurrentRegionOnly && SI != SE)
10805*67e74705SXin Li           EnclosingExpr = RE;
10806*67e74705SXin Li 
10807*67e74705SXin Li         // The current expression is a subset of the expression in the data
10808*67e74705SXin Li         // environment.
10809*67e74705SXin Li         IsEnclosedByDataEnvironmentExpr |=
10810*67e74705SXin Li             (!CurrentRegionOnly && CI != CE && SI == SE);
10811*67e74705SXin Li 
10812*67e74705SXin Li         return false;
10813*67e74705SXin Li       });
10814*67e74705SXin Li 
10815*67e74705SXin Li   if (CurrentRegionOnly)
10816*67e74705SXin Li     return FoundError;
10817*67e74705SXin Li 
10818*67e74705SXin Li   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
10819*67e74705SXin Li   //  If any part of the original storage of a list item has corresponding
10820*67e74705SXin Li   //  storage in the device data environment, all of the original storage must
10821*67e74705SXin Li   //  have corresponding storage in the device data environment.
10822*67e74705SXin Li   // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
10823*67e74705SXin Li   //  If a list item is an element of a structure, and a different element of
10824*67e74705SXin Li   //  the structure has a corresponding list item in the device data environment
10825*67e74705SXin Li   //  prior to a task encountering the construct associated with the map clause,
10826*67e74705SXin Li   //  then the list item must also have a corresponding list item in the device
10827*67e74705SXin Li   //  data environment prior to the task encountering the construct.
10828*67e74705SXin Li   //
10829*67e74705SXin Li   if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
10830*67e74705SXin Li     SemaRef.Diag(ELoc,
10831*67e74705SXin Li                  diag::err_omp_original_storage_is_shared_and_does_not_contain)
10832*67e74705SXin Li         << ERange;
10833*67e74705SXin Li     SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
10834*67e74705SXin Li         << EnclosingExpr->getSourceRange();
10835*67e74705SXin Li     return true;
10836*67e74705SXin Li   }
10837*67e74705SXin Li 
10838*67e74705SXin Li   return FoundError;
10839*67e74705SXin Li }
10840*67e74705SXin Li 
10841*67e74705SXin Li namespace {
10842*67e74705SXin Li // Utility struct that gathers all the related lists associated with a mappable
10843*67e74705SXin Li // expression.
10844*67e74705SXin Li struct MappableVarListInfo final {
10845*67e74705SXin Li   // The list of expressions.
10846*67e74705SXin Li   ArrayRef<Expr *> VarList;
10847*67e74705SXin Li   // The list of processed expressions.
10848*67e74705SXin Li   SmallVector<Expr *, 16> ProcessedVarList;
10849*67e74705SXin Li   // The mappble components for each expression.
10850*67e74705SXin Li   OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
10851*67e74705SXin Li   // The base declaration of the variable.
10852*67e74705SXin Li   SmallVector<ValueDecl *, 16> VarBaseDeclarations;
10853*67e74705SXin Li 
MappableVarListInfo__anon0fd400dc2d11::MappableVarListInfo10854*67e74705SXin Li   MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
10855*67e74705SXin Li     // We have a list of components and base declarations for each entry in the
10856*67e74705SXin Li     // variable list.
10857*67e74705SXin Li     VarComponents.reserve(VarList.size());
10858*67e74705SXin Li     VarBaseDeclarations.reserve(VarList.size());
10859*67e74705SXin Li   }
10860*67e74705SXin Li };
10861*67e74705SXin Li }
10862*67e74705SXin Li 
10863*67e74705SXin Li // Check the validity of the provided variable list for the provided clause kind
10864*67e74705SXin Li // \a CKind. In the check process the valid expressions, and mappable expression
10865*67e74705SXin Li // components and variables are extracted and used to fill \a Vars,
10866*67e74705SXin Li // \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
10867*67e74705SXin Li // \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
10868*67e74705SXin Li static void
checkMappableExpressionList(Sema & SemaRef,DSAStackTy * DSAS,OpenMPClauseKind CKind,MappableVarListInfo & MVLI,SourceLocation StartLoc,OpenMPMapClauseKind MapType=OMPC_MAP_unknown,bool IsMapTypeImplicit=false)10869*67e74705SXin Li checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
10870*67e74705SXin Li                             OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
10871*67e74705SXin Li                             SourceLocation StartLoc,
10872*67e74705SXin Li                             OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
10873*67e74705SXin Li                             bool IsMapTypeImplicit = false) {
10874*67e74705SXin Li   // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
10875*67e74705SXin Li   assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
10876*67e74705SXin Li          "Unexpected clause kind with mappable expressions!");
10877*67e74705SXin Li 
10878*67e74705SXin Li   // Keep track of the mappable components and base declarations in this clause.
10879*67e74705SXin Li   // Each entry in the list is going to have a list of components associated. We
10880*67e74705SXin Li   // record each set of the components so that we can build the clause later on.
10881*67e74705SXin Li   // In the end we should have the same amount of declarations and component
10882*67e74705SXin Li   // lists.
10883*67e74705SXin Li 
10884*67e74705SXin Li   for (auto &RE : MVLI.VarList) {
10885*67e74705SXin Li     assert(RE && "Null expr in omp to/from/map clause");
10886*67e74705SXin Li     SourceLocation ELoc = RE->getExprLoc();
10887*67e74705SXin Li 
10888*67e74705SXin Li     auto *VE = RE->IgnoreParenLValueCasts();
10889*67e74705SXin Li 
10890*67e74705SXin Li     if (VE->isValueDependent() || VE->isTypeDependent() ||
10891*67e74705SXin Li         VE->isInstantiationDependent() ||
10892*67e74705SXin Li         VE->containsUnexpandedParameterPack()) {
10893*67e74705SXin Li       // We can only analyze this information once the missing information is
10894*67e74705SXin Li       // resolved.
10895*67e74705SXin Li       MVLI.ProcessedVarList.push_back(RE);
10896*67e74705SXin Li       continue;
10897*67e74705SXin Li     }
10898*67e74705SXin Li 
10899*67e74705SXin Li     auto *SimpleExpr = RE->IgnoreParenCasts();
10900*67e74705SXin Li 
10901*67e74705SXin Li     if (!RE->IgnoreParenImpCasts()->isLValue()) {
10902*67e74705SXin Li       SemaRef.Diag(ELoc,
10903*67e74705SXin Li                    diag::err_omp_expected_named_var_member_or_array_expression)
10904*67e74705SXin Li           << RE->getSourceRange();
10905*67e74705SXin Li       continue;
10906*67e74705SXin Li     }
10907*67e74705SXin Li 
10908*67e74705SXin Li     OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
10909*67e74705SXin Li     ValueDecl *CurDeclaration = nullptr;
10910*67e74705SXin Li 
10911*67e74705SXin Li     // Obtain the array or member expression bases if required. Also, fill the
10912*67e74705SXin Li     // components array with all the components identified in the process.
10913*67e74705SXin Li     auto *BE =
10914*67e74705SXin Li         CheckMapClauseExpressionBase(SemaRef, SimpleExpr, CurComponents, CKind);
10915*67e74705SXin Li     if (!BE)
10916*67e74705SXin Li       continue;
10917*67e74705SXin Li 
10918*67e74705SXin Li     assert(!CurComponents.empty() &&
10919*67e74705SXin Li            "Invalid mappable expression information.");
10920*67e74705SXin Li 
10921*67e74705SXin Li     // For the following checks, we rely on the base declaration which is
10922*67e74705SXin Li     // expected to be associated with the last component. The declaration is
10923*67e74705SXin Li     // expected to be a variable or a field (if 'this' is being mapped).
10924*67e74705SXin Li     CurDeclaration = CurComponents.back().getAssociatedDeclaration();
10925*67e74705SXin Li     assert(CurDeclaration && "Null decl on map clause.");
10926*67e74705SXin Li     assert(
10927*67e74705SXin Li         CurDeclaration->isCanonicalDecl() &&
10928*67e74705SXin Li         "Expecting components to have associated only canonical declarations.");
10929*67e74705SXin Li 
10930*67e74705SXin Li     auto *VD = dyn_cast<VarDecl>(CurDeclaration);
10931*67e74705SXin Li     auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
10932*67e74705SXin Li 
10933*67e74705SXin Li     assert((VD || FD) && "Only variables or fields are expected here!");
10934*67e74705SXin Li     (void)FD;
10935*67e74705SXin Li 
10936*67e74705SXin Li     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
10937*67e74705SXin Li     // threadprivate variables cannot appear in a map clause.
10938*67e74705SXin Li     // OpenMP 4.5 [2.10.5, target update Construct]
10939*67e74705SXin Li     // threadprivate variables cannot appear in a from clause.
10940*67e74705SXin Li     if (VD && DSAS->isThreadPrivate(VD)) {
10941*67e74705SXin Li       auto DVar = DSAS->getTopDSA(VD, false);
10942*67e74705SXin Li       SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
10943*67e74705SXin Li           << getOpenMPClauseName(CKind);
10944*67e74705SXin Li       ReportOriginalDSA(SemaRef, DSAS, VD, DVar);
10945*67e74705SXin Li       continue;
10946*67e74705SXin Li     }
10947*67e74705SXin Li 
10948*67e74705SXin Li     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10949*67e74705SXin Li     //  A list item cannot appear in both a map clause and a data-sharing
10950*67e74705SXin Li     //  attribute clause on the same construct.
10951*67e74705SXin Li 
10952*67e74705SXin Li     // Check conflicts with other map clause expressions. We check the conflicts
10953*67e74705SXin Li     // with the current construct separately from the enclosing data
10954*67e74705SXin Li     // environment, because the restrictions are different. We only have to
10955*67e74705SXin Li     // check conflicts across regions for the map clauses.
10956*67e74705SXin Li     if (CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10957*67e74705SXin Li                           /*CurrentRegionOnly=*/true, CurComponents, CKind))
10958*67e74705SXin Li       break;
10959*67e74705SXin Li     if (CKind == OMPC_map &&
10960*67e74705SXin Li         CheckMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
10961*67e74705SXin Li                           /*CurrentRegionOnly=*/false, CurComponents, CKind))
10962*67e74705SXin Li       break;
10963*67e74705SXin Li 
10964*67e74705SXin Li     // OpenMP 4.5 [2.10.5, target update Construct]
10965*67e74705SXin Li     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
10966*67e74705SXin Li     //  If the type of a list item is a reference to a type T then the type will
10967*67e74705SXin Li     //  be considered to be T for all purposes of this clause.
10968*67e74705SXin Li     QualType Type = CurDeclaration->getType().getNonReferenceType();
10969*67e74705SXin Li 
10970*67e74705SXin Li     // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
10971*67e74705SXin Li     // A list item in a to or from clause must have a mappable type.
10972*67e74705SXin Li     // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
10973*67e74705SXin Li     //  A list item must have a mappable type.
10974*67e74705SXin Li     if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
10975*67e74705SXin Li                            DSAS, Type))
10976*67e74705SXin Li       continue;
10977*67e74705SXin Li 
10978*67e74705SXin Li     if (CKind == OMPC_map) {
10979*67e74705SXin Li       // target enter data
10980*67e74705SXin Li       // OpenMP [2.10.2, Restrictions, p. 99]
10981*67e74705SXin Li       // A map-type must be specified in all map clauses and must be either
10982*67e74705SXin Li       // to or alloc.
10983*67e74705SXin Li       OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
10984*67e74705SXin Li       if (DKind == OMPD_target_enter_data &&
10985*67e74705SXin Li           !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
10986*67e74705SXin Li         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
10987*67e74705SXin Li             << (IsMapTypeImplicit ? 1 : 0)
10988*67e74705SXin Li             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
10989*67e74705SXin Li             << getOpenMPDirectiveName(DKind);
10990*67e74705SXin Li         continue;
10991*67e74705SXin Li       }
10992*67e74705SXin Li 
10993*67e74705SXin Li       // target exit_data
10994*67e74705SXin Li       // OpenMP [2.10.3, Restrictions, p. 102]
10995*67e74705SXin Li       // A map-type must be specified in all map clauses and must be either
10996*67e74705SXin Li       // from, release, or delete.
10997*67e74705SXin Li       if (DKind == OMPD_target_exit_data &&
10998*67e74705SXin Li           !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
10999*67e74705SXin Li             MapType == OMPC_MAP_delete)) {
11000*67e74705SXin Li         SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
11001*67e74705SXin Li             << (IsMapTypeImplicit ? 1 : 0)
11002*67e74705SXin Li             << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
11003*67e74705SXin Li             << getOpenMPDirectiveName(DKind);
11004*67e74705SXin Li         continue;
11005*67e74705SXin Li       }
11006*67e74705SXin Li 
11007*67e74705SXin Li       // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
11008*67e74705SXin Li       // A list item cannot appear in both a map clause and a data-sharing
11009*67e74705SXin Li       // attribute clause on the same construct
11010*67e74705SXin Li       if (DKind == OMPD_target && VD) {
11011*67e74705SXin Li         auto DVar = DSAS->getTopDSA(VD, false);
11012*67e74705SXin Li         if (isOpenMPPrivate(DVar.CKind)) {
11013*67e74705SXin Li           SemaRef.Diag(ELoc, diag::err_omp_variable_in_map_and_dsa)
11014*67e74705SXin Li               << getOpenMPClauseName(DVar.CKind)
11015*67e74705SXin Li               << getOpenMPDirectiveName(DSAS->getCurrentDirective());
11016*67e74705SXin Li           ReportOriginalDSA(SemaRef, DSAS, CurDeclaration, DVar);
11017*67e74705SXin Li           continue;
11018*67e74705SXin Li         }
11019*67e74705SXin Li       }
11020*67e74705SXin Li     }
11021*67e74705SXin Li 
11022*67e74705SXin Li     // Save the current expression.
11023*67e74705SXin Li     MVLI.ProcessedVarList.push_back(RE);
11024*67e74705SXin Li 
11025*67e74705SXin Li     // Store the components in the stack so that they can be used to check
11026*67e74705SXin Li     // against other clauses later on.
11027*67e74705SXin Li     DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents);
11028*67e74705SXin Li 
11029*67e74705SXin Li     // Save the components and declaration to create the clause. For purposes of
11030*67e74705SXin Li     // the clause creation, any component list that has has base 'this' uses
11031*67e74705SXin Li     // null as base declaration.
11032*67e74705SXin Li     MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
11033*67e74705SXin Li     MVLI.VarComponents.back().append(CurComponents.begin(),
11034*67e74705SXin Li                                      CurComponents.end());
11035*67e74705SXin Li     MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
11036*67e74705SXin Li                                                            : CurDeclaration);
11037*67e74705SXin Li   }
11038*67e74705SXin Li }
11039*67e74705SXin Li 
11040*67e74705SXin Li OMPClause *
ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,OpenMPMapClauseKind MapType,bool IsMapTypeImplicit,SourceLocation MapLoc,SourceLocation ColonLoc,ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11041*67e74705SXin Li Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
11042*67e74705SXin Li                            OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
11043*67e74705SXin Li                            SourceLocation MapLoc, SourceLocation ColonLoc,
11044*67e74705SXin Li                            ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11045*67e74705SXin Li                            SourceLocation LParenLoc, SourceLocation EndLoc) {
11046*67e74705SXin Li   MappableVarListInfo MVLI(VarList);
11047*67e74705SXin Li   checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
11048*67e74705SXin Li                               MapType, IsMapTypeImplicit);
11049*67e74705SXin Li 
11050*67e74705SXin Li   // We need to produce a map clause even if we don't have variables so that
11051*67e74705SXin Li   // other diagnostics related with non-existing map clauses are accurate.
11052*67e74705SXin Li   return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11053*67e74705SXin Li                               MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11054*67e74705SXin Li                               MVLI.VarComponents, MapTypeModifier, MapType,
11055*67e74705SXin Li                               IsMapTypeImplicit, MapLoc);
11056*67e74705SXin Li }
11057*67e74705SXin Li 
ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,TypeResult ParsedType)11058*67e74705SXin Li QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
11059*67e74705SXin Li                                                TypeResult ParsedType) {
11060*67e74705SXin Li   assert(ParsedType.isUsable());
11061*67e74705SXin Li 
11062*67e74705SXin Li   QualType ReductionType = GetTypeFromParser(ParsedType.get());
11063*67e74705SXin Li   if (ReductionType.isNull())
11064*67e74705SXin Li     return QualType();
11065*67e74705SXin Li 
11066*67e74705SXin Li   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
11067*67e74705SXin Li   // A type name in a declare reduction directive cannot be a function type, an
11068*67e74705SXin Li   // array type, a reference type, or a type qualified with const, volatile or
11069*67e74705SXin Li   // restrict.
11070*67e74705SXin Li   if (ReductionType.hasQualifiers()) {
11071*67e74705SXin Li     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
11072*67e74705SXin Li     return QualType();
11073*67e74705SXin Li   }
11074*67e74705SXin Li 
11075*67e74705SXin Li   if (ReductionType->isFunctionType()) {
11076*67e74705SXin Li     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
11077*67e74705SXin Li     return QualType();
11078*67e74705SXin Li   }
11079*67e74705SXin Li   if (ReductionType->isReferenceType()) {
11080*67e74705SXin Li     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
11081*67e74705SXin Li     return QualType();
11082*67e74705SXin Li   }
11083*67e74705SXin Li   if (ReductionType->isArrayType()) {
11084*67e74705SXin Li     Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
11085*67e74705SXin Li     return QualType();
11086*67e74705SXin Li   }
11087*67e74705SXin Li   return ReductionType;
11088*67e74705SXin Li }
11089*67e74705SXin Li 
ActOnOpenMPDeclareReductionDirectiveStart(Scope * S,DeclContext * DC,DeclarationName Name,ArrayRef<std::pair<QualType,SourceLocation>> ReductionTypes,AccessSpecifier AS,Decl * PrevDeclInScope)11090*67e74705SXin Li Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
11091*67e74705SXin Li     Scope *S, DeclContext *DC, DeclarationName Name,
11092*67e74705SXin Li     ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
11093*67e74705SXin Li     AccessSpecifier AS, Decl *PrevDeclInScope) {
11094*67e74705SXin Li   SmallVector<Decl *, 8> Decls;
11095*67e74705SXin Li   Decls.reserve(ReductionTypes.size());
11096*67e74705SXin Li 
11097*67e74705SXin Li   LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
11098*67e74705SXin Li                       ForRedeclaration);
11099*67e74705SXin Li   // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
11100*67e74705SXin Li   // A reduction-identifier may not be re-declared in the current scope for the
11101*67e74705SXin Li   // same type or for a type that is compatible according to the base language
11102*67e74705SXin Li   // rules.
11103*67e74705SXin Li   llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
11104*67e74705SXin Li   OMPDeclareReductionDecl *PrevDRD = nullptr;
11105*67e74705SXin Li   bool InCompoundScope = true;
11106*67e74705SXin Li   if (S != nullptr) {
11107*67e74705SXin Li     // Find previous declaration with the same name not referenced in other
11108*67e74705SXin Li     // declarations.
11109*67e74705SXin Li     FunctionScopeInfo *ParentFn = getEnclosingFunction();
11110*67e74705SXin Li     InCompoundScope =
11111*67e74705SXin Li         (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
11112*67e74705SXin Li     LookupName(Lookup, S);
11113*67e74705SXin Li     FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
11114*67e74705SXin Li                          /*AllowInlineNamespace=*/false);
11115*67e74705SXin Li     llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
11116*67e74705SXin Li     auto Filter = Lookup.makeFilter();
11117*67e74705SXin Li     while (Filter.hasNext()) {
11118*67e74705SXin Li       auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
11119*67e74705SXin Li       if (InCompoundScope) {
11120*67e74705SXin Li         auto I = UsedAsPrevious.find(PrevDecl);
11121*67e74705SXin Li         if (I == UsedAsPrevious.end())
11122*67e74705SXin Li           UsedAsPrevious[PrevDecl] = false;
11123*67e74705SXin Li         if (auto *D = PrevDecl->getPrevDeclInScope())
11124*67e74705SXin Li           UsedAsPrevious[D] = true;
11125*67e74705SXin Li       }
11126*67e74705SXin Li       PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
11127*67e74705SXin Li           PrevDecl->getLocation();
11128*67e74705SXin Li     }
11129*67e74705SXin Li     Filter.done();
11130*67e74705SXin Li     if (InCompoundScope) {
11131*67e74705SXin Li       for (auto &PrevData : UsedAsPrevious) {
11132*67e74705SXin Li         if (!PrevData.second) {
11133*67e74705SXin Li           PrevDRD = PrevData.first;
11134*67e74705SXin Li           break;
11135*67e74705SXin Li         }
11136*67e74705SXin Li       }
11137*67e74705SXin Li     }
11138*67e74705SXin Li   } else if (PrevDeclInScope != nullptr) {
11139*67e74705SXin Li     auto *PrevDRDInScope = PrevDRD =
11140*67e74705SXin Li         cast<OMPDeclareReductionDecl>(PrevDeclInScope);
11141*67e74705SXin Li     do {
11142*67e74705SXin Li       PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
11143*67e74705SXin Li           PrevDRDInScope->getLocation();
11144*67e74705SXin Li       PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
11145*67e74705SXin Li     } while (PrevDRDInScope != nullptr);
11146*67e74705SXin Li   }
11147*67e74705SXin Li   for (auto &TyData : ReductionTypes) {
11148*67e74705SXin Li     auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
11149*67e74705SXin Li     bool Invalid = false;
11150*67e74705SXin Li     if (I != PreviousRedeclTypes.end()) {
11151*67e74705SXin Li       Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
11152*67e74705SXin Li           << TyData.first;
11153*67e74705SXin Li       Diag(I->second, diag::note_previous_definition);
11154*67e74705SXin Li       Invalid = true;
11155*67e74705SXin Li     }
11156*67e74705SXin Li     PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
11157*67e74705SXin Li     auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
11158*67e74705SXin Li                                                 Name, TyData.first, PrevDRD);
11159*67e74705SXin Li     DC->addDecl(DRD);
11160*67e74705SXin Li     DRD->setAccess(AS);
11161*67e74705SXin Li     Decls.push_back(DRD);
11162*67e74705SXin Li     if (Invalid)
11163*67e74705SXin Li       DRD->setInvalidDecl();
11164*67e74705SXin Li     else
11165*67e74705SXin Li       PrevDRD = DRD;
11166*67e74705SXin Li   }
11167*67e74705SXin Li 
11168*67e74705SXin Li   return DeclGroupPtrTy::make(
11169*67e74705SXin Li       DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
11170*67e74705SXin Li }
11171*67e74705SXin Li 
ActOnOpenMPDeclareReductionCombinerStart(Scope * S,Decl * D)11172*67e74705SXin Li void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
11173*67e74705SXin Li   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11174*67e74705SXin Li 
11175*67e74705SXin Li   // Enter new function scope.
11176*67e74705SXin Li   PushFunctionScope();
11177*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
11178*67e74705SXin Li   getCurFunction()->setHasOMPDeclareReductionCombiner();
11179*67e74705SXin Li 
11180*67e74705SXin Li   if (S != nullptr)
11181*67e74705SXin Li     PushDeclContext(S, DRD);
11182*67e74705SXin Li   else
11183*67e74705SXin Li     CurContext = DRD;
11184*67e74705SXin Li 
11185*67e74705SXin Li   PushExpressionEvaluationContext(PotentiallyEvaluated);
11186*67e74705SXin Li 
11187*67e74705SXin Li   QualType ReductionType = DRD->getType();
11188*67e74705SXin Li   // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
11189*67e74705SXin Li   // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
11190*67e74705SXin Li   // uses semantics of argument handles by value, but it should be passed by
11191*67e74705SXin Li   // reference. C lang does not support references, so pass all parameters as
11192*67e74705SXin Li   // pointers.
11193*67e74705SXin Li   // Create 'T omp_in;' variable.
11194*67e74705SXin Li   auto *OmpInParm =
11195*67e74705SXin Li       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
11196*67e74705SXin Li   // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
11197*67e74705SXin Li   // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
11198*67e74705SXin Li   // uses semantics of argument handles by value, but it should be passed by
11199*67e74705SXin Li   // reference. C lang does not support references, so pass all parameters as
11200*67e74705SXin Li   // pointers.
11201*67e74705SXin Li   // Create 'T omp_out;' variable.
11202*67e74705SXin Li   auto *OmpOutParm =
11203*67e74705SXin Li       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
11204*67e74705SXin Li   if (S != nullptr) {
11205*67e74705SXin Li     PushOnScopeChains(OmpInParm, S);
11206*67e74705SXin Li     PushOnScopeChains(OmpOutParm, S);
11207*67e74705SXin Li   } else {
11208*67e74705SXin Li     DRD->addDecl(OmpInParm);
11209*67e74705SXin Li     DRD->addDecl(OmpOutParm);
11210*67e74705SXin Li   }
11211*67e74705SXin Li }
11212*67e74705SXin Li 
ActOnOpenMPDeclareReductionCombinerEnd(Decl * D,Expr * Combiner)11213*67e74705SXin Li void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
11214*67e74705SXin Li   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11215*67e74705SXin Li   DiscardCleanupsInEvaluationContext();
11216*67e74705SXin Li   PopExpressionEvaluationContext();
11217*67e74705SXin Li 
11218*67e74705SXin Li   PopDeclContext();
11219*67e74705SXin Li   PopFunctionScopeInfo();
11220*67e74705SXin Li 
11221*67e74705SXin Li   if (Combiner != nullptr)
11222*67e74705SXin Li     DRD->setCombiner(Combiner);
11223*67e74705SXin Li   else
11224*67e74705SXin Li     DRD->setInvalidDecl();
11225*67e74705SXin Li }
11226*67e74705SXin Li 
ActOnOpenMPDeclareReductionInitializerStart(Scope * S,Decl * D)11227*67e74705SXin Li void Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
11228*67e74705SXin Li   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11229*67e74705SXin Li 
11230*67e74705SXin Li   // Enter new function scope.
11231*67e74705SXin Li   PushFunctionScope();
11232*67e74705SXin Li   getCurFunction()->setHasBranchProtectedScope();
11233*67e74705SXin Li 
11234*67e74705SXin Li   if (S != nullptr)
11235*67e74705SXin Li     PushDeclContext(S, DRD);
11236*67e74705SXin Li   else
11237*67e74705SXin Li     CurContext = DRD;
11238*67e74705SXin Li 
11239*67e74705SXin Li   PushExpressionEvaluationContext(PotentiallyEvaluated);
11240*67e74705SXin Li 
11241*67e74705SXin Li   QualType ReductionType = DRD->getType();
11242*67e74705SXin Li   // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
11243*67e74705SXin Li   // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
11244*67e74705SXin Li   // uses semantics of argument handles by value, but it should be passed by
11245*67e74705SXin Li   // reference. C lang does not support references, so pass all parameters as
11246*67e74705SXin Li   // pointers.
11247*67e74705SXin Li   // Create 'T omp_priv;' variable.
11248*67e74705SXin Li   auto *OmpPrivParm =
11249*67e74705SXin Li       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
11250*67e74705SXin Li   // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
11251*67e74705SXin Li   // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
11252*67e74705SXin Li   // uses semantics of argument handles by value, but it should be passed by
11253*67e74705SXin Li   // reference. C lang does not support references, so pass all parameters as
11254*67e74705SXin Li   // pointers.
11255*67e74705SXin Li   // Create 'T omp_orig;' variable.
11256*67e74705SXin Li   auto *OmpOrigParm =
11257*67e74705SXin Li       buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
11258*67e74705SXin Li   if (S != nullptr) {
11259*67e74705SXin Li     PushOnScopeChains(OmpPrivParm, S);
11260*67e74705SXin Li     PushOnScopeChains(OmpOrigParm, S);
11261*67e74705SXin Li   } else {
11262*67e74705SXin Li     DRD->addDecl(OmpPrivParm);
11263*67e74705SXin Li     DRD->addDecl(OmpOrigParm);
11264*67e74705SXin Li   }
11265*67e74705SXin Li }
11266*67e74705SXin Li 
ActOnOpenMPDeclareReductionInitializerEnd(Decl * D,Expr * Initializer)11267*67e74705SXin Li void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D,
11268*67e74705SXin Li                                                      Expr *Initializer) {
11269*67e74705SXin Li   auto *DRD = cast<OMPDeclareReductionDecl>(D);
11270*67e74705SXin Li   DiscardCleanupsInEvaluationContext();
11271*67e74705SXin Li   PopExpressionEvaluationContext();
11272*67e74705SXin Li 
11273*67e74705SXin Li   PopDeclContext();
11274*67e74705SXin Li   PopFunctionScopeInfo();
11275*67e74705SXin Li 
11276*67e74705SXin Li   if (Initializer != nullptr)
11277*67e74705SXin Li     DRD->setInitializer(Initializer);
11278*67e74705SXin Li   else
11279*67e74705SXin Li     DRD->setInvalidDecl();
11280*67e74705SXin Li }
11281*67e74705SXin Li 
ActOnOpenMPDeclareReductionDirectiveEnd(Scope * S,DeclGroupPtrTy DeclReductions,bool IsValid)11282*67e74705SXin Li Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
11283*67e74705SXin Li     Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
11284*67e74705SXin Li   for (auto *D : DeclReductions.get()) {
11285*67e74705SXin Li     if (IsValid) {
11286*67e74705SXin Li       auto *DRD = cast<OMPDeclareReductionDecl>(D);
11287*67e74705SXin Li       if (S != nullptr)
11288*67e74705SXin Li         PushOnScopeChains(DRD, S, /*AddToContext=*/false);
11289*67e74705SXin Li     } else
11290*67e74705SXin Li       D->setInvalidDecl();
11291*67e74705SXin Li   }
11292*67e74705SXin Li   return DeclReductions;
11293*67e74705SXin Li }
11294*67e74705SXin Li 
ActOnOpenMPNumTeamsClause(Expr * NumTeams,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11295*67e74705SXin Li OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
11296*67e74705SXin Li                                            SourceLocation StartLoc,
11297*67e74705SXin Li                                            SourceLocation LParenLoc,
11298*67e74705SXin Li                                            SourceLocation EndLoc) {
11299*67e74705SXin Li   Expr *ValExpr = NumTeams;
11300*67e74705SXin Li 
11301*67e74705SXin Li   // OpenMP [teams Constrcut, Restrictions]
11302*67e74705SXin Li   // The num_teams expression must evaluate to a positive integer value.
11303*67e74705SXin Li   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
11304*67e74705SXin Li                                  /*StrictlyPositive=*/true))
11305*67e74705SXin Li     return nullptr;
11306*67e74705SXin Li 
11307*67e74705SXin Li   return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11308*67e74705SXin Li }
11309*67e74705SXin Li 
ActOnOpenMPThreadLimitClause(Expr * ThreadLimit,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11310*67e74705SXin Li OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
11311*67e74705SXin Li                                               SourceLocation StartLoc,
11312*67e74705SXin Li                                               SourceLocation LParenLoc,
11313*67e74705SXin Li                                               SourceLocation EndLoc) {
11314*67e74705SXin Li   Expr *ValExpr = ThreadLimit;
11315*67e74705SXin Li 
11316*67e74705SXin Li   // OpenMP [teams Constrcut, Restrictions]
11317*67e74705SXin Li   // The thread_limit expression must evaluate to a positive integer value.
11318*67e74705SXin Li   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
11319*67e74705SXin Li                                  /*StrictlyPositive=*/true))
11320*67e74705SXin Li     return nullptr;
11321*67e74705SXin Li 
11322*67e74705SXin Li   return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
11323*67e74705SXin Li                                             EndLoc);
11324*67e74705SXin Li }
11325*67e74705SXin Li 
ActOnOpenMPPriorityClause(Expr * Priority,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11326*67e74705SXin Li OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
11327*67e74705SXin Li                                            SourceLocation StartLoc,
11328*67e74705SXin Li                                            SourceLocation LParenLoc,
11329*67e74705SXin Li                                            SourceLocation EndLoc) {
11330*67e74705SXin Li   Expr *ValExpr = Priority;
11331*67e74705SXin Li 
11332*67e74705SXin Li   // OpenMP [2.9.1, task Constrcut]
11333*67e74705SXin Li   // The priority-value is a non-negative numerical scalar expression.
11334*67e74705SXin Li   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
11335*67e74705SXin Li                                  /*StrictlyPositive=*/false))
11336*67e74705SXin Li     return nullptr;
11337*67e74705SXin Li 
11338*67e74705SXin Li   return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11339*67e74705SXin Li }
11340*67e74705SXin Li 
ActOnOpenMPGrainsizeClause(Expr * Grainsize,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11341*67e74705SXin Li OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
11342*67e74705SXin Li                                             SourceLocation StartLoc,
11343*67e74705SXin Li                                             SourceLocation LParenLoc,
11344*67e74705SXin Li                                             SourceLocation EndLoc) {
11345*67e74705SXin Li   Expr *ValExpr = Grainsize;
11346*67e74705SXin Li 
11347*67e74705SXin Li   // OpenMP [2.9.2, taskloop Constrcut]
11348*67e74705SXin Li   // The parameter of the grainsize clause must be a positive integer
11349*67e74705SXin Li   // expression.
11350*67e74705SXin Li   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
11351*67e74705SXin Li                                  /*StrictlyPositive=*/true))
11352*67e74705SXin Li     return nullptr;
11353*67e74705SXin Li 
11354*67e74705SXin Li   return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11355*67e74705SXin Li }
11356*67e74705SXin Li 
ActOnOpenMPNumTasksClause(Expr * NumTasks,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11357*67e74705SXin Li OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
11358*67e74705SXin Li                                            SourceLocation StartLoc,
11359*67e74705SXin Li                                            SourceLocation LParenLoc,
11360*67e74705SXin Li                                            SourceLocation EndLoc) {
11361*67e74705SXin Li   Expr *ValExpr = NumTasks;
11362*67e74705SXin Li 
11363*67e74705SXin Li   // OpenMP [2.9.2, taskloop Constrcut]
11364*67e74705SXin Li   // The parameter of the num_tasks clause must be a positive integer
11365*67e74705SXin Li   // expression.
11366*67e74705SXin Li   if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
11367*67e74705SXin Li                                  /*StrictlyPositive=*/true))
11368*67e74705SXin Li     return nullptr;
11369*67e74705SXin Li 
11370*67e74705SXin Li   return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
11371*67e74705SXin Li }
11372*67e74705SXin Li 
ActOnOpenMPHintClause(Expr * Hint,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11373*67e74705SXin Li OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
11374*67e74705SXin Li                                        SourceLocation LParenLoc,
11375*67e74705SXin Li                                        SourceLocation EndLoc) {
11376*67e74705SXin Li   // OpenMP [2.13.2, critical construct, Description]
11377*67e74705SXin Li   // ... where hint-expression is an integer constant expression that evaluates
11378*67e74705SXin Li   // to a valid lock hint.
11379*67e74705SXin Li   ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
11380*67e74705SXin Li   if (HintExpr.isInvalid())
11381*67e74705SXin Li     return nullptr;
11382*67e74705SXin Li   return new (Context)
11383*67e74705SXin Li       OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
11384*67e74705SXin Li }
11385*67e74705SXin Li 
ActOnOpenMPDistScheduleClause(OpenMPDistScheduleClauseKind Kind,Expr * ChunkSize,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation KindLoc,SourceLocation CommaLoc,SourceLocation EndLoc)11386*67e74705SXin Li OMPClause *Sema::ActOnOpenMPDistScheduleClause(
11387*67e74705SXin Li     OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
11388*67e74705SXin Li     SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
11389*67e74705SXin Li     SourceLocation EndLoc) {
11390*67e74705SXin Li   if (Kind == OMPC_DIST_SCHEDULE_unknown) {
11391*67e74705SXin Li     std::string Values;
11392*67e74705SXin Li     Values += "'";
11393*67e74705SXin Li     Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
11394*67e74705SXin Li     Values += "'";
11395*67e74705SXin Li     Diag(KindLoc, diag::err_omp_unexpected_clause_value)
11396*67e74705SXin Li         << Values << getOpenMPClauseName(OMPC_dist_schedule);
11397*67e74705SXin Li     return nullptr;
11398*67e74705SXin Li   }
11399*67e74705SXin Li   Expr *ValExpr = ChunkSize;
11400*67e74705SXin Li   Stmt *HelperValStmt = nullptr;
11401*67e74705SXin Li   if (ChunkSize) {
11402*67e74705SXin Li     if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
11403*67e74705SXin Li         !ChunkSize->isInstantiationDependent() &&
11404*67e74705SXin Li         !ChunkSize->containsUnexpandedParameterPack()) {
11405*67e74705SXin Li       SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
11406*67e74705SXin Li       ExprResult Val =
11407*67e74705SXin Li           PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
11408*67e74705SXin Li       if (Val.isInvalid())
11409*67e74705SXin Li         return nullptr;
11410*67e74705SXin Li 
11411*67e74705SXin Li       ValExpr = Val.get();
11412*67e74705SXin Li 
11413*67e74705SXin Li       // OpenMP [2.7.1, Restrictions]
11414*67e74705SXin Li       //  chunk_size must be a loop invariant integer expression with a positive
11415*67e74705SXin Li       //  value.
11416*67e74705SXin Li       llvm::APSInt Result;
11417*67e74705SXin Li       if (ValExpr->isIntegerConstantExpr(Result, Context)) {
11418*67e74705SXin Li         if (Result.isSigned() && !Result.isStrictlyPositive()) {
11419*67e74705SXin Li           Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
11420*67e74705SXin Li               << "dist_schedule" << ChunkSize->getSourceRange();
11421*67e74705SXin Li           return nullptr;
11422*67e74705SXin Li         }
11423*67e74705SXin Li       } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
11424*67e74705SXin Li                  !CurContext->isDependentContext()) {
11425*67e74705SXin Li         llvm::MapVector<Expr *, DeclRefExpr *> Captures;
11426*67e74705SXin Li         ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11427*67e74705SXin Li         HelperValStmt = buildPreInits(Context, Captures);
11428*67e74705SXin Li       }
11429*67e74705SXin Li     }
11430*67e74705SXin Li   }
11431*67e74705SXin Li 
11432*67e74705SXin Li   return new (Context)
11433*67e74705SXin Li       OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
11434*67e74705SXin Li                             Kind, ValExpr, HelperValStmt);
11435*67e74705SXin Li }
11436*67e74705SXin Li 
ActOnOpenMPDefaultmapClause(OpenMPDefaultmapClauseModifier M,OpenMPDefaultmapClauseKind Kind,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation MLoc,SourceLocation KindLoc,SourceLocation EndLoc)11437*67e74705SXin Li OMPClause *Sema::ActOnOpenMPDefaultmapClause(
11438*67e74705SXin Li     OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
11439*67e74705SXin Li     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
11440*67e74705SXin Li     SourceLocation KindLoc, SourceLocation EndLoc) {
11441*67e74705SXin Li   // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
11442*67e74705SXin Li   if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom ||
11443*67e74705SXin Li       Kind != OMPC_DEFAULTMAP_scalar) {
11444*67e74705SXin Li     std::string Value;
11445*67e74705SXin Li     SourceLocation Loc;
11446*67e74705SXin Li     Value += "'";
11447*67e74705SXin Li     if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
11448*67e74705SXin Li       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11449*67e74705SXin Li                  OMPC_DEFAULTMAP_MODIFIER_tofrom);
11450*67e74705SXin Li       Loc = MLoc;
11451*67e74705SXin Li     } else {
11452*67e74705SXin Li       Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
11453*67e74705SXin Li                  OMPC_DEFAULTMAP_scalar);
11454*67e74705SXin Li       Loc = KindLoc;
11455*67e74705SXin Li     }
11456*67e74705SXin Li     Value += "'";
11457*67e74705SXin Li     Diag(Loc, diag::err_omp_unexpected_clause_value)
11458*67e74705SXin Li         << Value << getOpenMPClauseName(OMPC_defaultmap);
11459*67e74705SXin Li     return nullptr;
11460*67e74705SXin Li   }
11461*67e74705SXin Li 
11462*67e74705SXin Li   return new (Context)
11463*67e74705SXin Li       OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
11464*67e74705SXin Li }
11465*67e74705SXin Li 
ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc)11466*67e74705SXin Li bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
11467*67e74705SXin Li   DeclContext *CurLexicalContext = getCurLexicalContext();
11468*67e74705SXin Li   if (!CurLexicalContext->isFileContext() &&
11469*67e74705SXin Li       !CurLexicalContext->isExternCContext() &&
11470*67e74705SXin Li       !CurLexicalContext->isExternCXXContext()) {
11471*67e74705SXin Li     Diag(Loc, diag::err_omp_region_not_file_context);
11472*67e74705SXin Li     return false;
11473*67e74705SXin Li   }
11474*67e74705SXin Li   if (IsInOpenMPDeclareTargetContext) {
11475*67e74705SXin Li     Diag(Loc, diag::err_omp_enclosed_declare_target);
11476*67e74705SXin Li     return false;
11477*67e74705SXin Li   }
11478*67e74705SXin Li 
11479*67e74705SXin Li   IsInOpenMPDeclareTargetContext = true;
11480*67e74705SXin Li   return true;
11481*67e74705SXin Li }
11482*67e74705SXin Li 
ActOnFinishOpenMPDeclareTargetDirective()11483*67e74705SXin Li void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
11484*67e74705SXin Li   assert(IsInOpenMPDeclareTargetContext &&
11485*67e74705SXin Li          "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
11486*67e74705SXin Li 
11487*67e74705SXin Li   IsInOpenMPDeclareTargetContext = false;
11488*67e74705SXin Li }
11489*67e74705SXin Li 
11490*67e74705SXin Li void
ActOnOpenMPDeclareTargetName(Scope * CurScope,CXXScopeSpec & ScopeSpec,const DeclarationNameInfo & Id,OMPDeclareTargetDeclAttr::MapTypeTy MT,NamedDeclSetType & SameDirectiveDecls)11491*67e74705SXin Li Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope, CXXScopeSpec &ScopeSpec,
11492*67e74705SXin Li                                    const DeclarationNameInfo &Id,
11493*67e74705SXin Li                                    OMPDeclareTargetDeclAttr::MapTypeTy MT,
11494*67e74705SXin Li                                    NamedDeclSetType &SameDirectiveDecls) {
11495*67e74705SXin Li   LookupResult Lookup(*this, Id, LookupOrdinaryName);
11496*67e74705SXin Li   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
11497*67e74705SXin Li 
11498*67e74705SXin Li   if (Lookup.isAmbiguous())
11499*67e74705SXin Li     return;
11500*67e74705SXin Li   Lookup.suppressDiagnostics();
11501*67e74705SXin Li 
11502*67e74705SXin Li   if (!Lookup.isSingleResult()) {
11503*67e74705SXin Li     if (TypoCorrection Corrected =
11504*67e74705SXin Li             CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
11505*67e74705SXin Li                         llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
11506*67e74705SXin Li                         CTK_ErrorRecovery)) {
11507*67e74705SXin Li       diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
11508*67e74705SXin Li                                   << Id.getName());
11509*67e74705SXin Li       checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
11510*67e74705SXin Li       return;
11511*67e74705SXin Li     }
11512*67e74705SXin Li 
11513*67e74705SXin Li     Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
11514*67e74705SXin Li     return;
11515*67e74705SXin Li   }
11516*67e74705SXin Li 
11517*67e74705SXin Li   NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
11518*67e74705SXin Li   if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND)) {
11519*67e74705SXin Li     if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
11520*67e74705SXin Li       Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
11521*67e74705SXin Li 
11522*67e74705SXin Li     if (!ND->hasAttr<OMPDeclareTargetDeclAttr>()) {
11523*67e74705SXin Li       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
11524*67e74705SXin Li       ND->addAttr(A);
11525*67e74705SXin Li       if (ASTMutationListener *ML = Context.getASTMutationListener())
11526*67e74705SXin Li         ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
11527*67e74705SXin Li       checkDeclIsAllowedInOpenMPTarget(nullptr, ND);
11528*67e74705SXin Li     } else if (ND->getAttr<OMPDeclareTargetDeclAttr>()->getMapType() != MT) {
11529*67e74705SXin Li       Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
11530*67e74705SXin Li           << Id.getName();
11531*67e74705SXin Li     }
11532*67e74705SXin Li   } else
11533*67e74705SXin Li     Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
11534*67e74705SXin Li }
11535*67e74705SXin Li 
checkDeclInTargetContext(SourceLocation SL,SourceRange SR,Sema & SemaRef,Decl * D)11536*67e74705SXin Li static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
11537*67e74705SXin Li                                      Sema &SemaRef, Decl *D) {
11538*67e74705SXin Li   if (!D)
11539*67e74705SXin Li     return;
11540*67e74705SXin Li   Decl *LD = nullptr;
11541*67e74705SXin Li   if (isa<TagDecl>(D)) {
11542*67e74705SXin Li     LD = cast<TagDecl>(D)->getDefinition();
11543*67e74705SXin Li   } else if (isa<VarDecl>(D)) {
11544*67e74705SXin Li     LD = cast<VarDecl>(D)->getDefinition();
11545*67e74705SXin Li 
11546*67e74705SXin Li     // If this is an implicit variable that is legal and we do not need to do
11547*67e74705SXin Li     // anything.
11548*67e74705SXin Li     if (cast<VarDecl>(D)->isImplicit()) {
11549*67e74705SXin Li       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11550*67e74705SXin Li           SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11551*67e74705SXin Li       D->addAttr(A);
11552*67e74705SXin Li       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11553*67e74705SXin Li         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11554*67e74705SXin Li       return;
11555*67e74705SXin Li     }
11556*67e74705SXin Li 
11557*67e74705SXin Li   } else if (isa<FunctionDecl>(D)) {
11558*67e74705SXin Li     const FunctionDecl *FD = nullptr;
11559*67e74705SXin Li     if (cast<FunctionDecl>(D)->hasBody(FD))
11560*67e74705SXin Li       LD = const_cast<FunctionDecl *>(FD);
11561*67e74705SXin Li 
11562*67e74705SXin Li     // If the definition is associated with the current declaration in the
11563*67e74705SXin Li     // target region (it can be e.g. a lambda) that is legal and we do not need
11564*67e74705SXin Li     // to do anything else.
11565*67e74705SXin Li     if (LD == D) {
11566*67e74705SXin Li       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11567*67e74705SXin Li           SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11568*67e74705SXin Li       D->addAttr(A);
11569*67e74705SXin Li       if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11570*67e74705SXin Li         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11571*67e74705SXin Li       return;
11572*67e74705SXin Li     }
11573*67e74705SXin Li   }
11574*67e74705SXin Li   if (!LD)
11575*67e74705SXin Li     LD = D;
11576*67e74705SXin Li   if (LD && !LD->hasAttr<OMPDeclareTargetDeclAttr>() &&
11577*67e74705SXin Li       (isa<VarDecl>(LD) || isa<FunctionDecl>(LD))) {
11578*67e74705SXin Li     // Outlined declaration is not declared target.
11579*67e74705SXin Li     if (LD->isOutOfLine()) {
11580*67e74705SXin Li       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11581*67e74705SXin Li       SemaRef.Diag(SL, diag::note_used_here) << SR;
11582*67e74705SXin Li     } else {
11583*67e74705SXin Li       DeclContext *DC = LD->getDeclContext();
11584*67e74705SXin Li       while (DC) {
11585*67e74705SXin Li         if (isa<FunctionDecl>(DC) &&
11586*67e74705SXin Li             cast<FunctionDecl>(DC)->hasAttr<OMPDeclareTargetDeclAttr>())
11587*67e74705SXin Li           break;
11588*67e74705SXin Li         DC = DC->getParent();
11589*67e74705SXin Li       }
11590*67e74705SXin Li       if (DC)
11591*67e74705SXin Li         return;
11592*67e74705SXin Li 
11593*67e74705SXin Li       // Is not declared in target context.
11594*67e74705SXin Li       SemaRef.Diag(LD->getLocation(), diag::warn_omp_not_in_target_context);
11595*67e74705SXin Li       SemaRef.Diag(SL, diag::note_used_here) << SR;
11596*67e74705SXin Li     }
11597*67e74705SXin Li     // Mark decl as declared target to prevent further diagnostic.
11598*67e74705SXin Li     Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11599*67e74705SXin Li         SemaRef.Context, OMPDeclareTargetDeclAttr::MT_To);
11600*67e74705SXin Li     D->addAttr(A);
11601*67e74705SXin Li     if (ASTMutationListener *ML = SemaRef.Context.getASTMutationListener())
11602*67e74705SXin Li       ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11603*67e74705SXin Li   }
11604*67e74705SXin Li }
11605*67e74705SXin Li 
checkValueDeclInTarget(SourceLocation SL,SourceRange SR,Sema & SemaRef,DSAStackTy * Stack,ValueDecl * VD)11606*67e74705SXin Li static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
11607*67e74705SXin Li                                    Sema &SemaRef, DSAStackTy *Stack,
11608*67e74705SXin Li                                    ValueDecl *VD) {
11609*67e74705SXin Li   if (VD->hasAttr<OMPDeclareTargetDeclAttr>())
11610*67e74705SXin Li     return true;
11611*67e74705SXin Li   if (!CheckTypeMappable(SL, SR, SemaRef, Stack, VD->getType()))
11612*67e74705SXin Li     return false;
11613*67e74705SXin Li   return true;
11614*67e74705SXin Li }
11615*67e74705SXin Li 
checkDeclIsAllowedInOpenMPTarget(Expr * E,Decl * D)11616*67e74705SXin Li void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D) {
11617*67e74705SXin Li   if (!D || D->isInvalidDecl())
11618*67e74705SXin Li     return;
11619*67e74705SXin Li   SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
11620*67e74705SXin Li   SourceLocation SL = E ? E->getLocStart() : D->getLocation();
11621*67e74705SXin Li   // 2.10.6: threadprivate variable cannot appear in a declare target directive.
11622*67e74705SXin Li   if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
11623*67e74705SXin Li     if (DSAStack->isThreadPrivate(VD)) {
11624*67e74705SXin Li       Diag(SL, diag::err_omp_threadprivate_in_target);
11625*67e74705SXin Li       ReportOriginalDSA(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
11626*67e74705SXin Li       return;
11627*67e74705SXin Li     }
11628*67e74705SXin Li   }
11629*67e74705SXin Li   if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
11630*67e74705SXin Li     // Problem if any with var declared with incomplete type will be reported
11631*67e74705SXin Li     // as normal, so no need to check it here.
11632*67e74705SXin Li     if ((E || !VD->getType()->isIncompleteType()) &&
11633*67e74705SXin Li         !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD)) {
11634*67e74705SXin Li       // Mark decl as declared target to prevent further diagnostic.
11635*67e74705SXin Li       if (isa<VarDecl>(VD) || isa<FunctionDecl>(VD)) {
11636*67e74705SXin Li         Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11637*67e74705SXin Li             Context, OMPDeclareTargetDeclAttr::MT_To);
11638*67e74705SXin Li         VD->addAttr(A);
11639*67e74705SXin Li         if (ASTMutationListener *ML = Context.getASTMutationListener())
11640*67e74705SXin Li           ML->DeclarationMarkedOpenMPDeclareTarget(VD, A);
11641*67e74705SXin Li       }
11642*67e74705SXin Li       return;
11643*67e74705SXin Li     }
11644*67e74705SXin Li   }
11645*67e74705SXin Li   if (!E) {
11646*67e74705SXin Li     // Checking declaration inside declare target region.
11647*67e74705SXin Li     if (!D->hasAttr<OMPDeclareTargetDeclAttr>() &&
11648*67e74705SXin Li         (isa<VarDecl>(D) || isa<FunctionDecl>(D))) {
11649*67e74705SXin Li       Attr *A = OMPDeclareTargetDeclAttr::CreateImplicit(
11650*67e74705SXin Li           Context, OMPDeclareTargetDeclAttr::MT_To);
11651*67e74705SXin Li       D->addAttr(A);
11652*67e74705SXin Li       if (ASTMutationListener *ML = Context.getASTMutationListener())
11653*67e74705SXin Li         ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
11654*67e74705SXin Li     }
11655*67e74705SXin Li     return;
11656*67e74705SXin Li   }
11657*67e74705SXin Li   checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
11658*67e74705SXin Li }
11659*67e74705SXin Li 
ActOnOpenMPToClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11660*67e74705SXin Li OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
11661*67e74705SXin Li                                      SourceLocation StartLoc,
11662*67e74705SXin Li                                      SourceLocation LParenLoc,
11663*67e74705SXin Li                                      SourceLocation EndLoc) {
11664*67e74705SXin Li   MappableVarListInfo MVLI(VarList);
11665*67e74705SXin Li   checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
11666*67e74705SXin Li   if (MVLI.ProcessedVarList.empty())
11667*67e74705SXin Li     return nullptr;
11668*67e74705SXin Li 
11669*67e74705SXin Li   return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11670*67e74705SXin Li                              MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11671*67e74705SXin Li                              MVLI.VarComponents);
11672*67e74705SXin Li }
11673*67e74705SXin Li 
ActOnOpenMPFromClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11674*67e74705SXin Li OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
11675*67e74705SXin Li                                        SourceLocation StartLoc,
11676*67e74705SXin Li                                        SourceLocation LParenLoc,
11677*67e74705SXin Li                                        SourceLocation EndLoc) {
11678*67e74705SXin Li   MappableVarListInfo MVLI(VarList);
11679*67e74705SXin Li   checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
11680*67e74705SXin Li   if (MVLI.ProcessedVarList.empty())
11681*67e74705SXin Li     return nullptr;
11682*67e74705SXin Li 
11683*67e74705SXin Li   return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11684*67e74705SXin Li                                MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
11685*67e74705SXin Li                                MVLI.VarComponents);
11686*67e74705SXin Li }
11687*67e74705SXin Li 
ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11688*67e74705SXin Li OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
11689*67e74705SXin Li                                                SourceLocation StartLoc,
11690*67e74705SXin Li                                                SourceLocation LParenLoc,
11691*67e74705SXin Li                                                SourceLocation EndLoc) {
11692*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
11693*67e74705SXin Li   for (auto &RefExpr : VarList) {
11694*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11695*67e74705SXin Li     SourceLocation ELoc;
11696*67e74705SXin Li     SourceRange ERange;
11697*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
11698*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11699*67e74705SXin Li     if (Res.second) {
11700*67e74705SXin Li       // It will be analyzed later.
11701*67e74705SXin Li       Vars.push_back(RefExpr);
11702*67e74705SXin Li     }
11703*67e74705SXin Li     ValueDecl *D = Res.first;
11704*67e74705SXin Li     if (!D)
11705*67e74705SXin Li       continue;
11706*67e74705SXin Li 
11707*67e74705SXin Li     QualType Type = D->getType();
11708*67e74705SXin Li     // item should be a pointer or reference to pointer
11709*67e74705SXin Li     if (!Type.getNonReferenceType()->isPointerType()) {
11710*67e74705SXin Li       Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
11711*67e74705SXin Li           << 0 << RefExpr->getSourceRange();
11712*67e74705SXin Li       continue;
11713*67e74705SXin Li     }
11714*67e74705SXin Li     Vars.push_back(RefExpr->IgnoreParens());
11715*67e74705SXin Li   }
11716*67e74705SXin Li 
11717*67e74705SXin Li   if (Vars.empty())
11718*67e74705SXin Li     return nullptr;
11719*67e74705SXin Li 
11720*67e74705SXin Li   return OMPUseDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11721*67e74705SXin Li                                        Vars);
11722*67e74705SXin Li }
11723*67e74705SXin Li 
ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr * > VarList,SourceLocation StartLoc,SourceLocation LParenLoc,SourceLocation EndLoc)11724*67e74705SXin Li OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
11725*67e74705SXin Li                                               SourceLocation StartLoc,
11726*67e74705SXin Li                                               SourceLocation LParenLoc,
11727*67e74705SXin Li                                               SourceLocation EndLoc) {
11728*67e74705SXin Li   SmallVector<Expr *, 8> Vars;
11729*67e74705SXin Li   for (auto &RefExpr : VarList) {
11730*67e74705SXin Li     assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
11731*67e74705SXin Li     SourceLocation ELoc;
11732*67e74705SXin Li     SourceRange ERange;
11733*67e74705SXin Li     Expr *SimpleRefExpr = RefExpr;
11734*67e74705SXin Li     auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
11735*67e74705SXin Li     if (Res.second) {
11736*67e74705SXin Li       // It will be analyzed later.
11737*67e74705SXin Li       Vars.push_back(RefExpr);
11738*67e74705SXin Li     }
11739*67e74705SXin Li     ValueDecl *D = Res.first;
11740*67e74705SXin Li     if (!D)
11741*67e74705SXin Li       continue;
11742*67e74705SXin Li 
11743*67e74705SXin Li     QualType Type = D->getType();
11744*67e74705SXin Li     // item should be a pointer or array or reference to pointer or array
11745*67e74705SXin Li     if (!Type.getNonReferenceType()->isPointerType() &&
11746*67e74705SXin Li         !Type.getNonReferenceType()->isArrayType()) {
11747*67e74705SXin Li       Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
11748*67e74705SXin Li           << 0 << RefExpr->getSourceRange();
11749*67e74705SXin Li       continue;
11750*67e74705SXin Li     }
11751*67e74705SXin Li     Vars.push_back(RefExpr->IgnoreParens());
11752*67e74705SXin Li   }
11753*67e74705SXin Li 
11754*67e74705SXin Li   if (Vars.empty())
11755*67e74705SXin Li     return nullptr;
11756*67e74705SXin Li 
11757*67e74705SXin Li   return OMPIsDevicePtrClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11758*67e74705SXin Li                                       Vars);
11759*67e74705SXin Li }
11760