blob: 1721bea7693268ba9e58b20fc67393aa47ce623d [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "clang/Basic/OpenMPKinds.h"
23#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000025#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000028#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000029using namespace clang;
30
Alexey Bataev758e55e2013-09-06 18:03:48 +000031//===----------------------------------------------------------------------===//
32// Stack of data-sharing attributes for variables
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// \brief Default data sharing attributes, which can be applied to directive.
37enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000038 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
39 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
40 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000041};
Alexey Bataev7ff55242014-06-19 09:13:45 +000042
Alexey Bataevf29276e2014-06-18 04:14:57 +000043template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000044 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000045 bool operator()(T Kind) {
46 for (auto KindEl : Arr)
47 if (KindEl == Kind)
48 return true;
49 return false;
50 }
51
52private:
53 ArrayRef<T> Arr;
54};
Alexey Bataev23b69422014-06-18 07:08:49 +000055struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000056 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000057 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000058};
59
60typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
61typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000062
63/// \brief Stack for tracking declarations used in OpenMP directives and
64/// clauses and their data-sharing attributes.
65class DSAStackTy {
66public:
67 struct DSAVarData {
68 OpenMPDirectiveKind DKind;
69 OpenMPClauseKind CKind;
70 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000071 SourceLocation ImplicitDSALoc;
72 DSAVarData()
73 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
74 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000075 };
Alexey Bataeved09d242014-05-28 05:53:51 +000076
Alexey Bataev758e55e2013-09-06 18:03:48 +000077private:
78 struct DSAInfo {
79 OpenMPClauseKind Attributes;
80 DeclRefExpr *RefExpr;
81 };
82 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000083 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000084
85 struct SharingMapTy {
86 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000087 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 OpenMPDirectiveKind Directive;
91 DeclarationNameInfo DirectiveName;
92 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000094 bool OrderedRegion;
Alexey Bataeved09d242014-05-28 05:53:51 +000095 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000096 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000097 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataevdea47612014-07-23 07:46:59 +000099 ConstructLoc(Loc), OrderedRegion(false) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000100 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000101 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataevdea47612014-07-23 07:46:59 +0000103 ConstructLoc(), OrderedRegion(false) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 };
105
106 typedef SmallVector<SharingMapTy, 64> StackTy;
107
108 /// \brief Stack of used declaration and their data-sharing attributes.
109 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000110 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111
112 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
113
114 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000115
116 /// \brief Checks if the variable is a local for OpenMP region.
117 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000118
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121
122 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 Scope *CurScope, SourceLocation Loc) {
124 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
125 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 }
127
128 void pop() {
129 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
130 Stack.pop_back();
131 }
132
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000133 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000134 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000135 /// for diagnostics.
136 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
137
Alexey Bataev758e55e2013-09-06 18:03:48 +0000138 /// \brief Adds explicit data sharing attribute to the specified declaration.
139 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
140
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141 /// \brief Returns data sharing attributes from top of the stack for the
142 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000143 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000145 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000146 /// \brief Checks if the specified variables has data-sharing attributes which
147 /// match specified \a CPred predicate in any directive which matches \a DPred
148 /// predicate.
149 template <class ClausesPredicate, class DirectivesPredicate>
150 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000151 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000152 /// \brief Checks if the specified variables has data-sharing attributes which
153 /// match specified \a CPred predicate in any innermost directive which
154 /// matches \a DPred predicate.
155 template <class ClausesPredicate, class DirectivesPredicate>
156 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000157 DirectivesPredicate DPred,
158 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000159 /// \brief Finds a directive which matches specified \a DPred predicate.
160 template <class NamedDirectivesPredicate>
161 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000162
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163 /// \brief Returns currently analyzed directive.
164 OpenMPDirectiveKind getCurrentDirective() const {
165 return Stack.back().Directive;
166 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000167 /// \brief Returns parent directive.
168 OpenMPDirectiveKind getParentDirective() const {
169 if (Stack.size() > 2)
170 return Stack[Stack.size() - 2].Directive;
171 return OMPD_unknown;
172 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173
174 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000175 void setDefaultDSANone(SourceLocation Loc) {
176 Stack.back().DefaultAttr = DSA_none;
177 Stack.back().DefaultAttrLoc = Loc;
178 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000179 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000180 void setDefaultDSAShared(SourceLocation Loc) {
181 Stack.back().DefaultAttr = DSA_shared;
182 Stack.back().DefaultAttrLoc = Loc;
183 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000184
185 DefaultDataSharingAttributes getDefaultDSA() const {
186 return Stack.back().DefaultAttr;
187 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000188 SourceLocation getDefaultDSALocation() const {
189 return Stack.back().DefaultAttrLoc;
190 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000191
Alexey Bataevf29276e2014-06-18 04:14:57 +0000192 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000193 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000194 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000195 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000196 }
197
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000198 /// \brief Marks current region as ordered (it has an 'ordered' clause).
199 void setOrderedRegion(bool IsOrdered = true) {
200 Stack.back().OrderedRegion = IsOrdered;
201 }
202 /// \brief Returns true, if parent region is ordered (has associated
203 /// 'ordered' clause), false - otherwise.
204 bool isParentOrderedRegion() const {
205 if (Stack.size() > 2)
206 return Stack[Stack.size() - 2].OrderedRegion;
207 return false;
208 }
209
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000210 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000211 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000212 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000213};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000214bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
215 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
216 DKind == OMPD_unknown;
217}
Alexey Bataeved09d242014-05-28 05:53:51 +0000218} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000219
220DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
221 VarDecl *D) {
222 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000223 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000224 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
225 // in a region but not in construct]
226 // File-scope or namespace-scope variables referenced in called routines
227 // in the region are shared unless they appear in a threadprivate
228 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000229 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000230 DVar.CKind = OMPC_shared;
231
232 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
233 // in a region but not in construct]
234 // Variables with static storage duration that are declared in called
235 // routines in the region are shared.
236 if (D->hasGlobalStorage())
237 DVar.CKind = OMPC_shared;
238
Alexey Bataev758e55e2013-09-06 18:03:48 +0000239 return DVar;
240 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000241
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
244 // in a Construct, C/C++, predetermined, p.1]
245 // Variables with automatic storage duration that are declared in a scope
246 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000247 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
248 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
249 DVar.CKind = OMPC_private;
250 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000251 }
252
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253 // Explicitly specified attributes and local variables with predetermined
254 // attributes.
255 if (Iter->SharingMap.count(D)) {
256 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
257 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000258 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000259 return DVar;
260 }
261
262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263 // in a Construct, C/C++, implicitly determined, p.1]
264 // In a parallel or task construct, the data-sharing attributes of these
265 // variables are determined by the default clause, if present.
266 switch (Iter->DefaultAttr) {
267 case DSA_shared:
268 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000269 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270 return DVar;
271 case DSA_none:
272 return DVar;
273 case DSA_unspecified:
274 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
275 // in a Construct, implicitly determined, p.2]
276 // In a parallel construct, if no default clause is present, these
277 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000278 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000279 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 DVar.CKind = OMPC_shared;
281 return DVar;
282 }
283
284 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
285 // in a Construct, implicitly determined, p.4]
286 // In a task construct, if no default clause is present, a variable that in
287 // the enclosing context is determined to be shared by all implicit tasks
288 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 if (DVar.DKind == OMPD_task) {
290 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000291 for (StackTy::reverse_iterator I = std::next(Iter),
292 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000294 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
295 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000296 // in a Construct, implicitly determined, p.6]
297 // In a task construct, if no default clause is present, a variable
298 // whose data-sharing attribute is not determined by the rules above is
299 // firstprivate.
300 DVarTemp = getDSA(I, D);
301 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000302 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000303 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000304 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305 return DVar;
306 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000307 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000308 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 }
310 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000311 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000312 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000313 return DVar;
314 }
315 }
316 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
317 // in a Construct, implicitly determined, p.3]
318 // For constructs other than task, if no default clause is present, these
319 // variables inherit their data-sharing attributes from the enclosing
320 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000321 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322}
323
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000324DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
325 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
326 auto It = Stack.back().AlignedMap.find(D);
327 if (It == Stack.back().AlignedMap.end()) {
328 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
329 Stack.back().AlignedMap[D] = NewDE;
330 return nullptr;
331 } else {
332 assert(It->second && "Unexpected nullptr expr in the aligned map");
333 return It->second;
334 }
335 return nullptr;
336}
337
Alexey Bataev758e55e2013-09-06 18:03:48 +0000338void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
339 if (A == OMPC_threadprivate) {
340 Stack[0].SharingMap[D].Attributes = A;
341 Stack[0].SharingMap[D].RefExpr = E;
342 } else {
343 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
344 Stack.back().SharingMap[D].Attributes = A;
345 Stack.back().SharingMap[D].RefExpr = E;
346 }
347}
348
Alexey Bataeved09d242014-05-28 05:53:51 +0000349bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000350 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000351 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000352 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000353 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000354 ++I;
355 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000356 if (I == E)
357 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000358 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000359 Scope *CurScope = getCurScope();
360 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000361 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000362 }
363 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000364 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000365 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366}
367
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000368DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000369 DSAVarData DVar;
370
371 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
372 // in a Construct, C/C++, predetermined, p.1]
373 // Variables appearing in threadprivate directives are threadprivate.
374 if (D->getTLSKind() != VarDecl::TLS_None) {
375 DVar.CKind = OMPC_threadprivate;
376 return DVar;
377 }
378 if (Stack[0].SharingMap.count(D)) {
379 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
380 DVar.CKind = OMPC_threadprivate;
381 return DVar;
382 }
383
384 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
385 // in a Construct, C/C++, predetermined, p.1]
386 // Variables with automatic storage duration that are declared in a scope
387 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000388 OpenMPDirectiveKind Kind =
389 FromParent ? getParentDirective() : getCurrentDirective();
390 auto StartI = std::next(Stack.rbegin());
391 auto EndI = std::prev(Stack.rend());
392 if (FromParent && StartI != EndI) {
393 StartI = std::next(StartI);
394 }
395 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000396 if (isOpenMPLocal(D, StartI) &&
397 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
398 D->getStorageClass() == SC_None)) ||
399 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000400 DVar.CKind = OMPC_private;
401 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000402 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403 }
404
405 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
406 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000407 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000408 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000409 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000410 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000411 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
412 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000413 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
414 return DVar;
415
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 DVar.CKind = OMPC_shared;
417 return DVar;
418 }
419
420 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000421 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 while (Type->isArrayType()) {
423 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
424 Type = ElemType.getNonReferenceType().getCanonicalType();
425 }
426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
427 // in a Construct, C/C++, predetermined, p.6]
428 // Variables with const qualified type having no mutable member are
429 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000430 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000431 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000433 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000434 // Variables with const-qualified type having no mutable member may be
435 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000436 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
437 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000438 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
439 return DVar;
440
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 DVar.CKind = OMPC_shared;
442 return DVar;
443 }
444
445 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
446 // in a Construct, C/C++, predetermined, p.7]
447 // Variables with static storage duration that are declared in a scope
448 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000449 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450 DVar.CKind = OMPC_shared;
451 return DVar;
452 }
453
454 // Explicitly specified attributes and local variables with predetermined
455 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000456 auto I = std::prev(StartI);
457 if (I->SharingMap.count(D)) {
458 DVar.RefExpr = I->SharingMap[D].RefExpr;
459 DVar.CKind = I->SharingMap[D].Attributes;
460 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000461 }
462
463 return DVar;
464}
465
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000466DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
467 auto StartI = Stack.rbegin();
468 auto EndI = std::prev(Stack.rend());
469 if (FromParent && StartI != EndI) {
470 StartI = std::next(StartI);
471 }
472 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000473}
474
Alexey Bataevf29276e2014-06-18 04:14:57 +0000475template <class ClausesPredicate, class DirectivesPredicate>
476DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000477 DirectivesPredicate DPred,
478 bool FromParent) {
479 auto StartI = std::next(Stack.rbegin());
480 auto EndI = std::prev(Stack.rend());
481 if (FromParent && StartI != EndI) {
482 StartI = std::next(StartI);
483 }
484 for (auto I = StartI, EE = EndI; I != EE; ++I) {
485 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000486 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000487 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000488 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000489 return DVar;
490 }
491 return DSAVarData();
492}
493
Alexey Bataevf29276e2014-06-18 04:14:57 +0000494template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000495DSAStackTy::DSAVarData
496DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
497 DirectivesPredicate DPred, bool FromParent) {
498 auto StartI = std::next(Stack.rbegin());
499 auto EndI = std::prev(Stack.rend());
500 if (FromParent && StartI != EndI) {
501 StartI = std::next(StartI);
502 }
503 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000504 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000505 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000506 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000507 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000508 return DVar;
509 return DSAVarData();
510 }
511 return DSAVarData();
512}
513
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000514template <class NamedDirectivesPredicate>
515bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
516 auto StartI = std::next(Stack.rbegin());
517 auto EndI = std::prev(Stack.rend());
518 if (FromParent && StartI != EndI) {
519 StartI = std::next(StartI);
520 }
521 for (auto I = StartI, EE = EndI; I != EE; ++I) {
522 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
523 return true;
524 }
525 return false;
526}
527
Alexey Bataev758e55e2013-09-06 18:03:48 +0000528void Sema::InitDataSharingAttributesStack() {
529 VarDataSharingAttributesStack = new DSAStackTy(*this);
530}
531
532#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
533
Alexey Bataeved09d242014-05-28 05:53:51 +0000534void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000535
536void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
537 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000538 Scope *CurScope, SourceLocation Loc) {
539 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000540 PushExpressionEvaluationContext(PotentiallyEvaluated);
541}
542
543void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000544 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
545 // A variable of class type (or array thereof) that appears in a lastprivate
546 // clause requires an accessible, unambiguous default constructor for the
547 // class type, unless the list item is also specified in a firstprivate
548 // clause.
549 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
550 for (auto C : D->clauses()) {
551 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
552 for (auto VarRef : Clause->varlists()) {
553 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
554 continue;
555 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000556 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000557 if (DVar.CKind == OMPC_lastprivate) {
558 SourceLocation ELoc = VarRef->getExprLoc();
559 auto Type = VarRef->getType();
560 if (Type->isArrayType())
561 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
562 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000563 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
564 // FIXME This code must be replaced by actual constructing of the
565 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000566 if (RD) {
567 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
568 PartialDiagnostic PD =
569 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
570 if (!CD ||
571 CheckConstructorAccess(
572 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
573 CD->getAccess(), PD) == AR_inaccessible ||
574 CD->isDeleted()) {
575 Diag(ELoc, diag::err_omp_required_method)
576 << getOpenMPClauseName(OMPC_lastprivate) << 0;
577 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
578 VarDecl::DeclarationOnly;
579 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
580 : diag::note_defined_here)
581 << VD;
582 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
583 continue;
584 }
585 MarkFunctionReferenced(ELoc, CD);
586 DiagnoseUseOfDecl(CD, ELoc);
587 }
588 }
589 }
590 }
591 }
592 }
593
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 DSAStack->pop();
595 DiscardCleanupsInEvaluationContext();
596 PopExpressionEvaluationContext();
597}
598
Alexey Bataeva769e072013-03-22 06:34:35 +0000599namespace {
600
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000601class VarDeclFilterCCC : public CorrectionCandidateCallback {
602private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000603 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000604
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000605public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000606 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000607 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000608 NamedDecl *ND = Candidate.getCorrectionDecl();
609 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
610 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000611 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
612 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000615 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000616};
Alexey Bataeved09d242014-05-28 05:53:51 +0000617} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000618
619ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
620 CXXScopeSpec &ScopeSpec,
621 const DeclarationNameInfo &Id) {
622 LookupResult Lookup(*this, Id, LookupOrdinaryName);
623 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
624
625 if (Lookup.isAmbiguous())
626 return ExprError();
627
628 VarDecl *VD;
629 if (!Lookup.isSingleResult()) {
630 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000631 if (TypoCorrection Corrected =
632 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
633 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000634 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000635 PDiag(Lookup.empty()
636 ? diag::err_undeclared_var_use_suggest
637 : diag::err_omp_expected_var_arg_suggest)
638 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000639 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000640 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000641 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
642 : diag::err_omp_expected_var_arg)
643 << Id.getName();
644 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000645 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000646 } else {
647 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000648 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000649 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
650 return ExprError();
651 }
652 }
653 Lookup.suppressDiagnostics();
654
655 // OpenMP [2.9.2, Syntax, C/C++]
656 // Variables must be file-scope, namespace-scope, or static block-scope.
657 if (!VD->hasGlobalStorage()) {
658 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000659 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
660 bool IsDecl =
661 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000662 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
664 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000665 return ExprError();
666 }
667
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000668 VarDecl *CanonicalVD = VD->getCanonicalDecl();
669 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000670 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
671 // A threadprivate directive for file-scope variables must appear outside
672 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000673 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
674 !getCurLexicalContext()->isTranslationUnit()) {
675 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000676 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
677 bool IsDecl =
678 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
679 Diag(VD->getLocation(),
680 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
681 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000682 return ExprError();
683 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000684 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
685 // A threadprivate directive for static class member variables must appear
686 // in the class definition, in the same scope in which the member
687 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000688 if (CanonicalVD->isStaticDataMember() &&
689 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
690 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000691 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
692 bool IsDecl =
693 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
694 Diag(VD->getLocation(),
695 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
696 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000697 return ExprError();
698 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000699 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
700 // A threadprivate directive for namespace-scope variables must appear
701 // outside any definition or declaration other than the namespace
702 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000703 if (CanonicalVD->getDeclContext()->isNamespace() &&
704 (!getCurLexicalContext()->isFileContext() ||
705 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
706 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000707 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
708 bool IsDecl =
709 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
710 Diag(VD->getLocation(),
711 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
712 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000713 return ExprError();
714 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000715 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
716 // A threadprivate directive for static block-scope variables must appear
717 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000718 if (CanonicalVD->isStaticLocal() && CurScope &&
719 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000720 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000721 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
722 bool IsDecl =
723 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
724 Diag(VD->getLocation(),
725 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
726 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000727 return ExprError();
728 }
729
730 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
731 // A threadprivate directive must lexically precede all references to any
732 // of the variables in its list.
733 if (VD->isUsed()) {
734 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000735 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000736 return ExprError();
737 }
738
739 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000740 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000741 return DE;
742}
743
Alexey Bataeved09d242014-05-28 05:53:51 +0000744Sema::DeclGroupPtrTy
745Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
746 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000747 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000748 CurContext->addDecl(D);
749 return DeclGroupPtrTy::make(DeclGroupRef(D));
750 }
751 return DeclGroupPtrTy();
752}
753
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000754namespace {
755class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
756 Sema &SemaRef;
757
758public:
759 bool VisitDeclRefExpr(const DeclRefExpr *E) {
760 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
761 if (VD->hasLocalStorage()) {
762 SemaRef.Diag(E->getLocStart(),
763 diag::err_omp_local_var_in_threadprivate_init)
764 << E->getSourceRange();
765 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
766 << VD << VD->getSourceRange();
767 return true;
768 }
769 }
770 return false;
771 }
772 bool VisitStmt(const Stmt *S) {
773 for (auto Child : S->children()) {
774 if (Child && Visit(Child))
775 return true;
776 }
777 return false;
778 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000779 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000780};
781} // namespace
782
Alexey Bataeved09d242014-05-28 05:53:51 +0000783OMPThreadPrivateDecl *
784Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000785 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000786 for (auto &RefExpr : VarList) {
787 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000788 VarDecl *VD = cast<VarDecl>(DE->getDecl());
789 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000790
791 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
792 // A threadprivate variable must not have an incomplete type.
793 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000794 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000795 continue;
796 }
797
798 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
799 // A threadprivate variable must not have a reference type.
800 if (VD->getType()->isReferenceType()) {
801 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000802 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
803 bool IsDecl =
804 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
805 Diag(VD->getLocation(),
806 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
807 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000808 continue;
809 }
810
Richard Smithfd3834f2013-04-13 02:43:54 +0000811 // Check if this is a TLS variable.
812 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000813 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000814 bool IsDecl =
815 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
816 Diag(VD->getLocation(),
817 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
818 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000819 continue;
820 }
821
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000822 // Check if initial value of threadprivate variable reference variable with
823 // local storage (it is not supported by runtime).
824 if (auto Init = VD->getAnyInitializer()) {
825 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000826 if (Checker.Visit(Init))
827 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000828 }
829
Alexey Bataeved09d242014-05-28 05:53:51 +0000830 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000831 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000832 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000833 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000834 if (!Vars.empty()) {
835 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
836 Vars);
837 D->setAccess(AS_public);
838 }
839 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000840}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000841
Alexey Bataev7ff55242014-06-19 09:13:45 +0000842static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
843 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
844 bool IsLoopIterVar = false) {
845 if (DVar.RefExpr) {
846 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
847 << getOpenMPClauseName(DVar.CKind);
848 return;
849 }
850 enum {
851 PDSA_StaticMemberShared,
852 PDSA_StaticLocalVarShared,
853 PDSA_LoopIterVarPrivate,
854 PDSA_LoopIterVarLinear,
855 PDSA_LoopIterVarLastprivate,
856 PDSA_ConstVarShared,
857 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000858 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000859 PDSA_LocalVarPrivate,
860 PDSA_Implicit
861 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000862 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000863 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000864 if (IsLoopIterVar) {
865 if (DVar.CKind == OMPC_private)
866 Reason = PDSA_LoopIterVarPrivate;
867 else if (DVar.CKind == OMPC_lastprivate)
868 Reason = PDSA_LoopIterVarLastprivate;
869 else
870 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000871 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
872 Reason = PDSA_TaskVarFirstprivate;
873 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000874 } else if (VD->isStaticLocal())
875 Reason = PDSA_StaticLocalVarShared;
876 else if (VD->isStaticDataMember())
877 Reason = PDSA_StaticMemberShared;
878 else if (VD->isFileVarDecl())
879 Reason = PDSA_GlobalVarShared;
880 else if (VD->getType().isConstant(SemaRef.getASTContext()))
881 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000882 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000883 ReportHint = true;
884 Reason = PDSA_LocalVarPrivate;
885 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000886 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000887 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000888 << Reason << ReportHint
889 << getOpenMPDirectiveName(Stack->getCurrentDirective());
890 } else if (DVar.ImplicitDSALoc.isValid()) {
891 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
892 << getOpenMPClauseName(DVar.CKind);
893 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000894}
895
Alexey Bataev758e55e2013-09-06 18:03:48 +0000896namespace {
897class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
898 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000899 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900 bool ErrorFound;
901 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000902 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000903 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000904
Alexey Bataev758e55e2013-09-06 18:03:48 +0000905public:
906 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000907 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000908 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000909 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
910 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000911
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000912 auto DVar = Stack->getTopDSA(VD, false);
913 // Check if the variable has explicit DSA set and stop analysis if it so.
914 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000915
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000916 auto ELoc = E->getExprLoc();
917 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000918 // The default(none) clause requires that each variable that is referenced
919 // in the construct, and does not have a predetermined data-sharing
920 // attribute, must have its data-sharing attribute explicitly determined
921 // by being listed in a data-sharing attribute clause.
922 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000923 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000924 VarsWithInheritedDSA.count(VD) == 0) {
925 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000926 return;
927 }
928
929 // OpenMP [2.9.3.6, Restrictions, p.2]
930 // A list item that appears in a reduction clause of the innermost
931 // enclosing worksharing or parallel construct may not be accessed in an
932 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000933 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000934 [](OpenMPDirectiveKind K) -> bool {
935 return isOpenMPParallelDirective(K) ||
936 isOpenMPWorksharingDirective(K);
937 },
938 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000939 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
940 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000941 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
942 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000943 return;
944 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000945
946 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000947 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000948 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000949 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000950 }
951 }
952 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000953 for (auto *C : S->clauses()) {
954 // Skip analysis of arguments of implicitly defined firstprivate clause
955 // for task directives.
956 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
957 for (auto *CC : C->children()) {
958 if (CC)
959 Visit(CC);
960 }
961 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962 }
963 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000964 for (auto *C : S->children()) {
965 if (C && !isa<OMPExecutableDirective>(C))
966 Visit(C);
967 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000968 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969
970 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000971 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000972 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
973 return VarsWithInheritedDSA;
974 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000975
Alexey Bataev7ff55242014-06-19 09:13:45 +0000976 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
977 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978};
Alexey Bataeved09d242014-05-28 05:53:51 +0000979} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000980
Alexey Bataevbae9a792014-06-27 10:37:06 +0000981void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000982 switch (DKind) {
983 case OMPD_parallel: {
984 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
985 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000986 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000987 std::make_pair(".global_tid.", KmpInt32PtrTy),
988 std::make_pair(".bound_tid.", KmpInt32PtrTy),
989 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000990 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000991 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
992 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000993 break;
994 }
995 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000996 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 std::make_pair(StringRef(), QualType()) // __context with shared vars
998 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000999 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1000 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001001 break;
1002 }
1003 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001004 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001005 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001006 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001007 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1008 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001009 break;
1010 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001011 case OMPD_sections: {
1012 Sema::CapturedParamNameType Params[] = {
1013 std::make_pair(StringRef(), QualType()) // __context with shared vars
1014 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001015 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1016 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001017 break;
1018 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001019 case OMPD_section: {
1020 Sema::CapturedParamNameType Params[] = {
1021 std::make_pair(StringRef(), QualType()) // __context with shared vars
1022 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001023 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1024 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001025 break;
1026 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001027 case OMPD_single: {
1028 Sema::CapturedParamNameType Params[] = {
1029 std::make_pair(StringRef(), QualType()) // __context with shared vars
1030 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001031 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1032 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001033 break;
1034 }
Alexander Musman80c22892014-07-17 08:54:58 +00001035 case OMPD_master: {
1036 Sema::CapturedParamNameType Params[] = {
1037 std::make_pair(StringRef(), QualType()) // __context with shared vars
1038 };
1039 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1040 Params);
1041 break;
1042 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001043 case OMPD_critical: {
1044 Sema::CapturedParamNameType Params[] = {
1045 std::make_pair(StringRef(), QualType()) // __context with shared vars
1046 };
1047 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1048 Params);
1049 break;
1050 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001051 case OMPD_parallel_for: {
1052 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1053 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1054 Sema::CapturedParamNameType Params[] = {
1055 std::make_pair(".global_tid.", KmpInt32PtrTy),
1056 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1057 std::make_pair(StringRef(), QualType()) // __context with shared vars
1058 };
1059 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1060 Params);
1061 break;
1062 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001063 case OMPD_parallel_sections: {
1064 Sema::CapturedParamNameType Params[] = {
1065 std::make_pair(StringRef(), QualType()) // __context with shared vars
1066 };
1067 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1068 Params);
1069 break;
1070 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001071 case OMPD_task: {
1072 Sema::CapturedParamNameType Params[] = {
1073 std::make_pair(StringRef(), QualType()) // __context with shared vars
1074 };
1075 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1076 Params);
1077 break;
1078 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001079 case OMPD_taskyield: {
1080 Sema::CapturedParamNameType Params[] = {
1081 std::make_pair(StringRef(), QualType()) // __context with shared vars
1082 };
1083 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1084 Params);
1085 break;
1086 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001087 case OMPD_barrier: {
1088 Sema::CapturedParamNameType Params[] = {
1089 std::make_pair(StringRef(), QualType()) // __context with shared vars
1090 };
1091 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1092 Params);
1093 break;
1094 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001095 case OMPD_taskwait: {
1096 Sema::CapturedParamNameType Params[] = {
1097 std::make_pair(StringRef(), QualType()) // __context with shared vars
1098 };
1099 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1100 Params);
1101 break;
1102 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001103 case OMPD_flush: {
1104 Sema::CapturedParamNameType Params[] = {
1105 std::make_pair(StringRef(), QualType()) // __context with shared vars
1106 };
1107 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1108 Params);
1109 break;
1110 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001111 case OMPD_ordered: {
1112 Sema::CapturedParamNameType Params[] = {
1113 std::make_pair(StringRef(), QualType()) // __context with shared vars
1114 };
1115 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1116 Params);
1117 break;
1118 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001119 case OMPD_atomic: {
1120 Sema::CapturedParamNameType Params[] = {
1121 std::make_pair(StringRef(), QualType()) // __context with shared vars
1122 };
1123 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1124 Params);
1125 break;
1126 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001127 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001128 llvm_unreachable("OpenMP Directive is not allowed");
1129 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001130 llvm_unreachable("Unknown OpenMP directive");
1131 }
1132}
1133
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001134static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1135 OpenMPDirectiveKind CurrentRegion,
1136 const DeclarationNameInfo &CurrentName,
1137 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001138 // Allowed nesting of constructs
1139 // +------------------+-----------------+------------------------------------+
1140 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1141 // +------------------+-----------------+------------------------------------+
1142 // | parallel | parallel | * |
1143 // | parallel | for | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001144 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001145 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001146 // | parallel | simd | * |
1147 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001148 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001149 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001150 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001151 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001152 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001153 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001154 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001155 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001156 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001157 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001158 // | parallel | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001159 // +------------------+-----------------+------------------------------------+
1160 // | for | parallel | * |
1161 // | for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001162 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001163 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001164 // | for | simd | * |
1165 // | for | sections | + |
1166 // | for | section | + |
1167 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001168 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001169 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001170 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001171 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001172 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001173 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001174 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001175 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001176 // | for | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001177 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001178 // | master | parallel | * |
1179 // | master | for | + |
1180 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001181 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001182 // | master | simd | * |
1183 // | master | sections | + |
1184 // | master | section | + |
1185 // | master | single | + |
1186 // | master | parallel for | * |
1187 // | master |parallel sections| * |
1188 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001189 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001190 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001191 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001192 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001193 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001194 // | master | atomic | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001195 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001196 // | critical | parallel | * |
1197 // | critical | for | + |
1198 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001199 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001200 // | critical | simd | * |
1201 // | critical | sections | + |
1202 // | critical | section | + |
1203 // | critical | single | + |
1204 // | critical | parallel for | * |
1205 // | critical |parallel sections| * |
1206 // | critical | task | * |
1207 // | critical | taskyield | * |
1208 // | critical | barrier | + |
1209 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001210 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001211 // | critical | atomic | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001212 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001213 // | simd | parallel | |
1214 // | simd | for | |
Alexander Musman80c22892014-07-17 08:54:58 +00001215 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001216 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001217 // | simd | simd | |
1218 // | simd | sections | |
1219 // | simd | section | |
1220 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001221 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001222 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001223 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001224 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001225 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001226 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001227 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001228 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001229 // | simd | atomic | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001230 // +------------------+-----------------+------------------------------------+
1231 // | sections | parallel | * |
1232 // | sections | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001233 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001234 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001235 // | sections | simd | * |
1236 // | sections | sections | + |
1237 // | sections | section | * |
1238 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001239 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001240 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001241 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001242 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001243 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001244 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001245 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001246 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001247 // | sections | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001248 // +------------------+-----------------+------------------------------------+
1249 // | section | parallel | * |
1250 // | section | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001251 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001252 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001253 // | section | simd | * |
1254 // | section | sections | + |
1255 // | section | section | + |
1256 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001257 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001258 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001259 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001260 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001261 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001262 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001263 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001264 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001265 // | section | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001266 // +------------------+-----------------+------------------------------------+
1267 // | single | parallel | * |
1268 // | single | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001269 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001270 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001271 // | single | simd | * |
1272 // | single | sections | + |
1273 // | single | section | + |
1274 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001275 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001276 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001277 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001278 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001279 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001280 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001281 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001282 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001283 // | single | atomic | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001284 // +------------------+-----------------+------------------------------------+
1285 // | parallel for | parallel | * |
1286 // | parallel for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001287 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001288 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001289 // | parallel for | simd | * |
1290 // | parallel for | sections | + |
1291 // | parallel for | section | + |
1292 // | parallel for | single | + |
1293 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001294 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001295 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001296 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001297 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001298 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001299 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001300 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001301 // | parallel for | atomic | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001302 // +------------------+-----------------+------------------------------------+
1303 // | parallel sections| parallel | * |
1304 // | parallel sections| for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001305 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001306 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001307 // | parallel sections| simd | * |
1308 // | parallel sections| sections | + |
1309 // | parallel sections| section | * |
1310 // | parallel sections| single | + |
1311 // | parallel sections| parallel for | * |
1312 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001313 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001314 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001315 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001316 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001317 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001318 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001319 // | parallel sections| atomic | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001320 // +------------------+-----------------+------------------------------------+
1321 // | task | parallel | * |
1322 // | task | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001323 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001324 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001325 // | task | simd | * |
1326 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001327 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001328 // | task | single | + |
1329 // | task | parallel for | * |
1330 // | task |parallel sections| * |
1331 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001332 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001333 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001334 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001335 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001336 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001337 // | task | atomic | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001338 // +------------------+-----------------+------------------------------------+
1339 // | ordered | parallel | * |
1340 // | ordered | for | + |
1341 // | ordered | master | * |
1342 // | ordered | critical | * |
1343 // | ordered | simd | * |
1344 // | ordered | sections | + |
1345 // | ordered | section | + |
1346 // | ordered | single | + |
1347 // | ordered | parallel for | * |
1348 // | ordered |parallel sections| * |
1349 // | ordered | task | * |
1350 // | ordered | taskyield | * |
1351 // | ordered | barrier | + |
1352 // | ordered | taskwait | * |
1353 // | ordered | flush | * |
1354 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001355 // | ordered | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001356 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001357 if (Stack->getCurScope()) {
1358 auto ParentRegion = Stack->getParentDirective();
1359 bool NestingProhibited = false;
1360 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001361 enum {
1362 NoRecommend,
1363 ShouldBeInParallelRegion,
1364 ShouldBeInOrderedRegion
1365 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001366 if (isOpenMPSimdDirective(ParentRegion)) {
1367 // OpenMP [2.16, Nesting of Regions]
1368 // OpenMP constructs may not be nested inside a simd region.
1369 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1370 return true;
1371 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001372 if (ParentRegion == OMPD_atomic) {
1373 // OpenMP [2.16, Nesting of Regions]
1374 // OpenMP constructs may not be nested inside an atomic region.
1375 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1376 return true;
1377 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001378 if (CurrentRegion == OMPD_section) {
1379 // OpenMP [2.7.2, sections Construct, Restrictions]
1380 // Orphaned section directives are prohibited. That is, the section
1381 // directives must appear within the sections construct and must not be
1382 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001383 if (ParentRegion != OMPD_sections &&
1384 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001385 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1386 << (ParentRegion != OMPD_unknown)
1387 << getOpenMPDirectiveName(ParentRegion);
1388 return true;
1389 }
1390 return false;
1391 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001392 // Allow some constructs to be orphaned (they could be used in functions,
1393 // called from OpenMP regions with the required preconditions).
1394 if (ParentRegion == OMPD_unknown)
1395 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001396 if (CurrentRegion == OMPD_master) {
1397 // OpenMP [2.16, Nesting of Regions]
1398 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001399 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001400 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1401 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001402 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1403 // OpenMP [2.16, Nesting of Regions]
1404 // A critical region may not be nested (closely or otherwise) inside a
1405 // critical region with the same name. Note that this restriction is not
1406 // sufficient to prevent deadlock.
1407 SourceLocation PreviousCriticalLoc;
1408 bool DeadLock =
1409 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1410 OpenMPDirectiveKind K,
1411 const DeclarationNameInfo &DNI,
1412 SourceLocation Loc)
1413 ->bool {
1414 if (K == OMPD_critical &&
1415 DNI.getName() == CurrentName.getName()) {
1416 PreviousCriticalLoc = Loc;
1417 return true;
1418 } else
1419 return false;
1420 },
1421 false /* skip top directive */);
1422 if (DeadLock) {
1423 SemaRef.Diag(StartLoc,
1424 diag::err_omp_prohibited_region_critical_same_name)
1425 << CurrentName.getName();
1426 if (PreviousCriticalLoc.isValid())
1427 SemaRef.Diag(PreviousCriticalLoc,
1428 diag::note_omp_previous_critical_region);
1429 return true;
1430 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001431 } else if (CurrentRegion == OMPD_barrier) {
1432 // OpenMP [2.16, Nesting of Regions]
1433 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001434 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001435 NestingProhibited =
1436 isOpenMPWorksharingDirective(ParentRegion) ||
1437 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1438 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001439 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1440 !isOpenMPParallelDirective(CurrentRegion) &&
1441 !isOpenMPSimdDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001442 // OpenMP [2.16, Nesting of Regions]
1443 // A worksharing region may not be closely nested inside a worksharing,
1444 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001445 NestingProhibited =
1446 (isOpenMPWorksharingDirective(ParentRegion) &&
1447 !isOpenMPSimdDirective(ParentRegion)) ||
1448 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1449 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1450 Recommend = ShouldBeInParallelRegion;
1451 } else if (CurrentRegion == OMPD_ordered) {
1452 // OpenMP [2.16, Nesting of Regions]
1453 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001454 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001455 // An ordered region must be closely nested inside a loop region (or
1456 // parallel loop region) with an ordered clause.
1457 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001458 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001459 !Stack->isParentOrderedRegion();
1460 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001461 }
1462 if (NestingProhibited) {
1463 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001464 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1465 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001466 return true;
1467 }
1468 }
1469 return false;
1470}
1471
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001472StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001473 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001474 ArrayRef<OMPClause *> Clauses,
1475 Stmt *AStmt,
1476 SourceLocation StartLoc,
1477 SourceLocation EndLoc) {
1478 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001479 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001480 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001482 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001483 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001484 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001485 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001486 if (AStmt) {
1487 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1488
1489 // Check default data sharing attributes for referenced variables.
1490 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1491 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1492 if (DSAChecker.isErrorFound())
1493 return StmtError();
1494 // Generate list of implicitly defined firstprivate variables.
1495 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001496
1497 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1498 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1499 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1500 SourceLocation(), SourceLocation())) {
1501 ClausesWithImplicit.push_back(Implicit);
1502 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1503 DSAChecker.getImplicitFirstprivate().size();
1504 } else
1505 ErrorFound = true;
1506 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001507 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001508
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001509 switch (Kind) {
1510 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001511 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1512 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001513 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001514 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001515 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1516 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001517 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001518 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001519 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1520 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001521 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001522 case OMPD_sections:
1523 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1524 EndLoc);
1525 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001526 case OMPD_section:
1527 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001528 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001529 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1530 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001531 case OMPD_single:
1532 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1533 EndLoc);
1534 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001535 case OMPD_master:
1536 assert(ClausesWithImplicit.empty() &&
1537 "No clauses are allowed for 'omp master' directive");
1538 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1539 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001540 case OMPD_critical:
1541 assert(ClausesWithImplicit.empty() &&
1542 "No clauses are allowed for 'omp critical' directive");
1543 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1544 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001545 case OMPD_parallel_for:
1546 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1547 EndLoc, VarsWithInheritedDSA);
1548 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001549 case OMPD_parallel_sections:
1550 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1551 StartLoc, EndLoc);
1552 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001553 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001554 Res =
1555 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1556 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001557 case OMPD_taskyield:
1558 assert(ClausesWithImplicit.empty() &&
1559 "No clauses are allowed for 'omp taskyield' directive");
1560 assert(AStmt == nullptr &&
1561 "No associated statement allowed for 'omp taskyield' directive");
1562 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1563 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001564 case OMPD_barrier:
1565 assert(ClausesWithImplicit.empty() &&
1566 "No clauses are allowed for 'omp barrier' directive");
1567 assert(AStmt == nullptr &&
1568 "No associated statement allowed for 'omp barrier' directive");
1569 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1570 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001571 case OMPD_taskwait:
1572 assert(ClausesWithImplicit.empty() &&
1573 "No clauses are allowed for 'omp taskwait' directive");
1574 assert(AStmt == nullptr &&
1575 "No associated statement allowed for 'omp taskwait' directive");
1576 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1577 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001578 case OMPD_flush:
1579 assert(AStmt == nullptr &&
1580 "No associated statement allowed for 'omp flush' directive");
1581 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1582 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001583 case OMPD_ordered:
1584 assert(ClausesWithImplicit.empty() &&
1585 "No clauses are allowed for 'omp ordered' directive");
1586 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1587 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001588 case OMPD_atomic:
1589 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1590 EndLoc);
1591 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001592 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001593 llvm_unreachable("OpenMP Directive is not allowed");
1594 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001595 llvm_unreachable("Unknown OpenMP directive");
1596 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001597
Alexey Bataev4acb8592014-07-07 13:01:15 +00001598 for (auto P : VarsWithInheritedDSA) {
1599 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1600 << P.first << P.second->getSourceRange();
1601 }
1602 if (!VarsWithInheritedDSA.empty())
1603 return StmtError();
1604
Alexey Bataeved09d242014-05-28 05:53:51 +00001605 if (ErrorFound)
1606 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001607 return Res;
1608}
1609
1610StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1611 Stmt *AStmt,
1612 SourceLocation StartLoc,
1613 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001614 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1615 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1616 // 1.2.2 OpenMP Language Terminology
1617 // Structured block - An executable statement with a single entry at the
1618 // top and a single exit at the bottom.
1619 // The point of exit cannot be a branch out of the structured block.
1620 // longjmp() and throw() must not violate the entry/exit criteria.
1621 CS->getCapturedDecl()->setNothrow();
1622
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001623 getCurFunction()->setHasBranchProtectedScope();
1624
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001625 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1626 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001627}
1628
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001629namespace {
1630/// \brief Helper class for checking canonical form of the OpenMP loops and
1631/// extracting iteration space of each loop in the loop nest, that will be used
1632/// for IR generation.
1633class OpenMPIterationSpaceChecker {
1634 /// \brief Reference to Sema.
1635 Sema &SemaRef;
1636 /// \brief A location for diagnostics (when there is no some better location).
1637 SourceLocation DefaultLoc;
1638 /// \brief A location for diagnostics (when increment is not compatible).
1639 SourceLocation ConditionLoc;
1640 /// \brief A source location for referring to condition later.
1641 SourceRange ConditionSrcRange;
1642 /// \brief Loop variable.
1643 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001644 /// \brief Reference to loop variable.
1645 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001646 /// \brief Lower bound (initializer for the var).
1647 Expr *LB;
1648 /// \brief Upper bound.
1649 Expr *UB;
1650 /// \brief Loop step (increment).
1651 Expr *Step;
1652 /// \brief This flag is true when condition is one of:
1653 /// Var < UB
1654 /// Var <= UB
1655 /// UB > Var
1656 /// UB >= Var
1657 bool TestIsLessOp;
1658 /// \brief This flag is true when condition is strict ( < or > ).
1659 bool TestIsStrictOp;
1660 /// \brief This flag is true when step is subtracted on each iteration.
1661 bool SubtractStep;
1662
1663public:
1664 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1665 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001666 ConditionSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
1667 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1668 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001669 /// \brief Check init-expr for canonical loop form and save loop counter
1670 /// variable - #Var and its initialization value - #LB.
1671 bool CheckInit(Stmt *S);
1672 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1673 /// for less/greater and for strict/non-strict comparison.
1674 bool CheckCond(Expr *S);
1675 /// \brief Check incr-expr for canonical loop form and return true if it
1676 /// does not conform, otherwise save loop step (#Step).
1677 bool CheckInc(Expr *S);
1678 /// \brief Return the loop counter variable.
1679 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001680 /// \brief Return the reference expression to loop counter variable.
1681 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001682 /// \brief Return true if any expression is dependent.
1683 bool Dependent() const;
1684
1685private:
1686 /// \brief Check the right-hand side of an assignment in the increment
1687 /// expression.
1688 bool CheckIncRHS(Expr *RHS);
1689 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001690 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001691 /// \brief Helper to set upper bound.
1692 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1693 const SourceLocation &SL);
1694 /// \brief Helper to set loop increment.
1695 bool SetStep(Expr *NewStep, bool Subtract);
1696};
1697
1698bool OpenMPIterationSpaceChecker::Dependent() const {
1699 if (!Var) {
1700 assert(!LB && !UB && !Step);
1701 return false;
1702 }
1703 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1704 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1705}
1706
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001707bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1708 DeclRefExpr *NewVarRefExpr,
1709 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001710 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001711 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1712 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001713 if (!NewVar || !NewLB)
1714 return true;
1715 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001716 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001717 LB = NewLB;
1718 return false;
1719}
1720
1721bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1722 const SourceRange &SR,
1723 const SourceLocation &SL) {
1724 // State consistency checking to ensure correct usage.
1725 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1726 !TestIsLessOp && !TestIsStrictOp);
1727 if (!NewUB)
1728 return true;
1729 UB = NewUB;
1730 TestIsLessOp = LessOp;
1731 TestIsStrictOp = StrictOp;
1732 ConditionSrcRange = SR;
1733 ConditionLoc = SL;
1734 return false;
1735}
1736
1737bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1738 // State consistency checking to ensure correct usage.
1739 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1740 if (!NewStep)
1741 return true;
1742 if (!NewStep->isValueDependent()) {
1743 // Check that the step is integer expression.
1744 SourceLocation StepLoc = NewStep->getLocStart();
1745 ExprResult Val =
1746 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1747 if (Val.isInvalid())
1748 return true;
1749 NewStep = Val.get();
1750
1751 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1752 // If test-expr is of form var relational-op b and relational-op is < or
1753 // <= then incr-expr must cause var to increase on each iteration of the
1754 // loop. If test-expr is of form var relational-op b and relational-op is
1755 // > or >= then incr-expr must cause var to decrease on each iteration of
1756 // the loop.
1757 // If test-expr is of form b relational-op var and relational-op is < or
1758 // <= then incr-expr must cause var to decrease on each iteration of the
1759 // loop. If test-expr is of form b relational-op var and relational-op is
1760 // > or >= then incr-expr must cause var to increase on each iteration of
1761 // the loop.
1762 llvm::APSInt Result;
1763 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1764 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1765 bool IsConstNeg =
1766 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1767 bool IsConstZero = IsConstant && !Result.getBoolValue();
1768 if (UB && (IsConstZero ||
1769 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1770 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1771 SemaRef.Diag(NewStep->getExprLoc(),
1772 diag::err_omp_loop_incr_not_compatible)
1773 << Var << TestIsLessOp << NewStep->getSourceRange();
1774 SemaRef.Diag(ConditionLoc,
1775 diag::note_omp_loop_cond_requres_compatible_incr)
1776 << TestIsLessOp << ConditionSrcRange;
1777 return true;
1778 }
1779 }
1780
1781 Step = NewStep;
1782 SubtractStep = Subtract;
1783 return false;
1784}
1785
1786bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1787 // Check init-expr for canonical loop form and save loop counter
1788 // variable - #Var and its initialization value - #LB.
1789 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1790 // var = lb
1791 // integer-type var = lb
1792 // random-access-iterator-type var = lb
1793 // pointer-type var = lb
1794 //
1795 if (!S) {
1796 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1797 return true;
1798 }
1799 if (Expr *E = dyn_cast<Expr>(S))
1800 S = E->IgnoreParens();
1801 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1802 if (BO->getOpcode() == BO_Assign)
1803 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001804 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1805 BO->getLHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001806 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1807 if (DS->isSingleDecl()) {
1808 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1809 if (Var->hasInit()) {
1810 // Accept non-canonical init form here but emit ext. warning.
1811 if (Var->getInitStyle() != VarDecl::CInit)
1812 SemaRef.Diag(S->getLocStart(),
1813 diag::ext_omp_loop_not_canonical_init)
1814 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001815 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001816 }
1817 }
1818 }
1819 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1820 if (CE->getOperator() == OO_Equal)
1821 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001822 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1823 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001824
1825 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1826 << S->getSourceRange();
1827 return true;
1828}
1829
Alexey Bataev23b69422014-06-18 07:08:49 +00001830/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001831/// variable (which may be the loop variable) if possible.
1832static const VarDecl *GetInitVarDecl(const Expr *E) {
1833 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001834 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001835 E = E->IgnoreParenImpCasts();
1836 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1837 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1838 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1839 CE->getArg(0) != nullptr)
1840 E = CE->getArg(0)->IgnoreParenImpCasts();
1841 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1842 if (!DRE)
1843 return nullptr;
1844 return dyn_cast<VarDecl>(DRE->getDecl());
1845}
1846
1847bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1848 // Check test-expr for canonical form, save upper-bound UB, flags for
1849 // less/greater and for strict/non-strict comparison.
1850 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1851 // var relational-op b
1852 // b relational-op var
1853 //
1854 if (!S) {
1855 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1856 return true;
1857 }
1858 S = S->IgnoreParenImpCasts();
1859 SourceLocation CondLoc = S->getLocStart();
1860 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1861 if (BO->isRelationalOp()) {
1862 if (GetInitVarDecl(BO->getLHS()) == Var)
1863 return SetUB(BO->getRHS(),
1864 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1865 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1866 BO->getSourceRange(), BO->getOperatorLoc());
1867 if (GetInitVarDecl(BO->getRHS()) == Var)
1868 return SetUB(BO->getLHS(),
1869 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1870 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1871 BO->getSourceRange(), BO->getOperatorLoc());
1872 }
1873 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1874 if (CE->getNumArgs() == 2) {
1875 auto Op = CE->getOperator();
1876 switch (Op) {
1877 case OO_Greater:
1878 case OO_GreaterEqual:
1879 case OO_Less:
1880 case OO_LessEqual:
1881 if (GetInitVarDecl(CE->getArg(0)) == Var)
1882 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1883 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1884 CE->getOperatorLoc());
1885 if (GetInitVarDecl(CE->getArg(1)) == Var)
1886 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1887 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1888 CE->getOperatorLoc());
1889 break;
1890 default:
1891 break;
1892 }
1893 }
1894 }
1895 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1896 << S->getSourceRange() << Var;
1897 return true;
1898}
1899
1900bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1901 // RHS of canonical loop form increment can be:
1902 // var + incr
1903 // incr + var
1904 // var - incr
1905 //
1906 RHS = RHS->IgnoreParenImpCasts();
1907 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1908 if (BO->isAdditiveOp()) {
1909 bool IsAdd = BO->getOpcode() == BO_Add;
1910 if (GetInitVarDecl(BO->getLHS()) == Var)
1911 return SetStep(BO->getRHS(), !IsAdd);
1912 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1913 return SetStep(BO->getLHS(), false);
1914 }
1915 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1916 bool IsAdd = CE->getOperator() == OO_Plus;
1917 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1918 if (GetInitVarDecl(CE->getArg(0)) == Var)
1919 return SetStep(CE->getArg(1), !IsAdd);
1920 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1921 return SetStep(CE->getArg(0), false);
1922 }
1923 }
1924 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1925 << RHS->getSourceRange() << Var;
1926 return true;
1927}
1928
1929bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1930 // Check incr-expr for canonical loop form and return true if it
1931 // does not conform.
1932 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1933 // ++var
1934 // var++
1935 // --var
1936 // var--
1937 // var += incr
1938 // var -= incr
1939 // var = var + incr
1940 // var = incr + var
1941 // var = var - incr
1942 //
1943 if (!S) {
1944 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1945 return true;
1946 }
1947 S = S->IgnoreParens();
1948 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1949 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1950 return SetStep(
1951 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1952 (UO->isDecrementOp() ? -1 : 1)).get(),
1953 false);
1954 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1955 switch (BO->getOpcode()) {
1956 case BO_AddAssign:
1957 case BO_SubAssign:
1958 if (GetInitVarDecl(BO->getLHS()) == Var)
1959 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1960 break;
1961 case BO_Assign:
1962 if (GetInitVarDecl(BO->getLHS()) == Var)
1963 return CheckIncRHS(BO->getRHS());
1964 break;
1965 default:
1966 break;
1967 }
1968 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1969 switch (CE->getOperator()) {
1970 case OO_PlusPlus:
1971 case OO_MinusMinus:
1972 if (GetInitVarDecl(CE->getArg(0)) == Var)
1973 return SetStep(
1974 SemaRef.ActOnIntegerConstant(
1975 CE->getLocStart(),
1976 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1977 false);
1978 break;
1979 case OO_PlusEqual:
1980 case OO_MinusEqual:
1981 if (GetInitVarDecl(CE->getArg(0)) == Var)
1982 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1983 break;
1984 case OO_Equal:
1985 if (GetInitVarDecl(CE->getArg(0)) == Var)
1986 return CheckIncRHS(CE->getArg(1));
1987 break;
1988 default:
1989 break;
1990 }
1991 }
1992 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1993 << S->getSourceRange() << Var;
1994 return true;
1995}
Alexey Bataev23b69422014-06-18 07:08:49 +00001996} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001997
1998/// \brief Called on a for stmt to check and extract its iteration space
1999/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002000static bool CheckOpenMPIterationSpace(
2001 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2002 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2003 Expr *NestedLoopCountExpr,
2004 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002005 // OpenMP [2.6, Canonical Loop Form]
2006 // for (init-expr; test-expr; incr-expr) structured-block
2007 auto For = dyn_cast_or_null<ForStmt>(S);
2008 if (!For) {
2009 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002010 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2011 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2012 << CurrentNestedLoopCount;
2013 if (NestedLoopCount > 1)
2014 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2015 diag::note_omp_collapse_expr)
2016 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002017 return true;
2018 }
2019 assert(For->getBody());
2020
2021 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2022
2023 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002024 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002025 if (ISC.CheckInit(Init)) {
2026 return true;
2027 }
2028
2029 bool HasErrors = false;
2030
2031 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002032 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002033
2034 // OpenMP [2.6, Canonical Loop Form]
2035 // Var is one of the following:
2036 // A variable of signed or unsigned integer type.
2037 // For C++, a variable of a random access iterator type.
2038 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002039 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002040 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2041 !VarType->isPointerType() &&
2042 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2043 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2044 << SemaRef.getLangOpts().CPlusPlus;
2045 HasErrors = true;
2046 }
2047
Alexey Bataev4acb8592014-07-07 13:01:15 +00002048 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2049 // Construct
2050 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2051 // parallel for construct is (are) private.
2052 // The loop iteration variable in the associated for-loop of a simd construct
2053 // with just one associated for-loop is linear with a constant-linear-step
2054 // that is the increment of the associated for-loop.
2055 // Exclude loop var from the list of variables with implicitly defined data
2056 // sharing attributes.
2057 while (VarsWithImplicitDSA.count(Var) > 0)
2058 VarsWithImplicitDSA.erase(Var);
2059
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002060 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2061 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002062 // The loop iteration variable in the associated for-loop of a simd construct
2063 // with just one associated for-loop may be listed in a linear clause with a
2064 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002065 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2066 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002067 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002068 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2069 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2070 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002071 auto PredeterminedCKind =
2072 isOpenMPSimdDirective(DKind)
2073 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2074 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002075 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002076 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00002077 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
2078 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002079 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002080 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002081 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2082 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002083 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002084 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002085 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002086 // Make the loop iteration variable private (for worksharing constructs),
2087 // linear (for simd directives with the only one associated loop) or
2088 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002089 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002090 }
2091
Alexey Bataev7ff55242014-06-19 09:13:45 +00002092 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002093
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002094 // Check test-expr.
2095 HasErrors |= ISC.CheckCond(For->getCond());
2096
2097 // Check incr-expr.
2098 HasErrors |= ISC.CheckInc(For->getInc());
2099
2100 if (ISC.Dependent())
2101 return HasErrors;
2102
2103 // FIXME: Build loop's iteration space representation.
2104 return HasErrors;
2105}
2106
2107/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
2108/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
2109/// to get the first for loop.
2110static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
2111 if (IgnoreCaptured)
2112 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
2113 S = CapS->getCapturedStmt();
2114 // OpenMP [2.8.1, simd construct, Restrictions]
2115 // All loops associated with the construct must be perfectly nested; that is,
2116 // there must be no intervening code nor any OpenMP directive between any two
2117 // loops.
2118 while (true) {
2119 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
2120 S = AS->getSubStmt();
2121 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
2122 if (CS->size() != 1)
2123 break;
2124 S = CS->body_back();
2125 } else
2126 break;
2127 }
2128 return S;
2129}
2130
2131/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002132/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2133/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002134static unsigned
2135CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2136 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2137 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002138 unsigned NestedLoopCount = 1;
2139 if (NestedLoopCountExpr) {
2140 // Found 'collapse' clause - calculate collapse number.
2141 llvm::APSInt Result;
2142 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2143 NestedLoopCount = Result.getLimitedValue();
2144 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002145 // This is helper routine for loop directives (e.g., 'for', 'simd',
2146 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002147 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
2148 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002149 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002150 NestedLoopCount, NestedLoopCountExpr,
2151 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002152 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002153 // Move on to the next nested for loop, or to the loop body.
2154 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
2155 }
2156
2157 // FIXME: Build resulting iteration space for IR generation (collapsing
2158 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002159 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002160}
2161
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002162static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002163 auto CollapseFilter = [](const OMPClause *C) -> bool {
2164 return C->getClauseKind() == OMPC_collapse;
2165 };
2166 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2167 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002168 if (I)
2169 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2170 return nullptr;
2171}
2172
Alexey Bataev4acb8592014-07-07 13:01:15 +00002173StmtResult Sema::ActOnOpenMPSimdDirective(
2174 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2175 SourceLocation EndLoc,
2176 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002177 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002178 unsigned NestedLoopCount =
2179 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2180 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002181 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002182 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002183
2184 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002185 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2186 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002187}
2188
Alexey Bataev4acb8592014-07-07 13:01:15 +00002189StmtResult Sema::ActOnOpenMPForDirective(
2190 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2191 SourceLocation EndLoc,
2192 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002193 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002194 unsigned NestedLoopCount =
2195 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2196 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002197 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002198 return StmtError();
2199
2200 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002201 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2202 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002203}
2204
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002205StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2206 Stmt *AStmt,
2207 SourceLocation StartLoc,
2208 SourceLocation EndLoc) {
2209 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2210 auto BaseStmt = AStmt;
2211 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2212 BaseStmt = CS->getCapturedStmt();
2213 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2214 auto S = C->children();
2215 if (!S)
2216 return StmtError();
2217 // All associated statements must be '#pragma omp section' except for
2218 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002219 for (++S; S; ++S) {
2220 auto SectionStmt = *S;
2221 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2222 if (SectionStmt)
2223 Diag(SectionStmt->getLocStart(),
2224 diag::err_omp_sections_substmt_not_section);
2225 return StmtError();
2226 }
2227 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002228 } else {
2229 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2230 return StmtError();
2231 }
2232
2233 getCurFunction()->setHasBranchProtectedScope();
2234
2235 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2236 AStmt);
2237}
2238
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002239StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2240 SourceLocation StartLoc,
2241 SourceLocation EndLoc) {
2242 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2243
2244 getCurFunction()->setHasBranchProtectedScope();
2245
2246 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2247}
2248
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002249StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2250 Stmt *AStmt,
2251 SourceLocation StartLoc,
2252 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002253 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2254
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002255 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002256
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002257 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2258}
2259
Alexander Musman80c22892014-07-17 08:54:58 +00002260StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2261 SourceLocation StartLoc,
2262 SourceLocation EndLoc) {
2263 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2264
2265 getCurFunction()->setHasBranchProtectedScope();
2266
2267 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2268}
2269
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002270StmtResult
2271Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2272 Stmt *AStmt, SourceLocation StartLoc,
2273 SourceLocation EndLoc) {
2274 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2275
2276 getCurFunction()->setHasBranchProtectedScope();
2277
2278 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2279 AStmt);
2280}
2281
Alexey Bataev4acb8592014-07-07 13:01:15 +00002282StmtResult Sema::ActOnOpenMPParallelForDirective(
2283 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2284 SourceLocation EndLoc,
2285 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2286 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2287 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2288 // 1.2.2 OpenMP Language Terminology
2289 // Structured block - An executable statement with a single entry at the
2290 // top and a single exit at the bottom.
2291 // The point of exit cannot be a branch out of the structured block.
2292 // longjmp() and throw() must not violate the entry/exit criteria.
2293 CS->getCapturedDecl()->setNothrow();
2294
2295 // In presence of clause 'collapse', it will define the nested loops number.
2296 unsigned NestedLoopCount =
2297 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2298 *this, *DSAStack, VarsWithImplicitDSA);
2299 if (NestedLoopCount == 0)
2300 return StmtError();
2301
2302 getCurFunction()->setHasBranchProtectedScope();
2303 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2304 NestedLoopCount, Clauses, AStmt);
2305}
2306
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002307StmtResult
2308Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2309 Stmt *AStmt, SourceLocation StartLoc,
2310 SourceLocation EndLoc) {
2311 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2312 auto BaseStmt = AStmt;
2313 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2314 BaseStmt = CS->getCapturedStmt();
2315 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2316 auto S = C->children();
2317 if (!S)
2318 return StmtError();
2319 // All associated statements must be '#pragma omp section' except for
2320 // the first one.
2321 for (++S; S; ++S) {
2322 auto SectionStmt = *S;
2323 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2324 if (SectionStmt)
2325 Diag(SectionStmt->getLocStart(),
2326 diag::err_omp_parallel_sections_substmt_not_section);
2327 return StmtError();
2328 }
2329 }
2330 } else {
2331 Diag(AStmt->getLocStart(),
2332 diag::err_omp_parallel_sections_not_compound_stmt);
2333 return StmtError();
2334 }
2335
2336 getCurFunction()->setHasBranchProtectedScope();
2337
2338 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2339 Clauses, AStmt);
2340}
2341
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002342StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2343 Stmt *AStmt, SourceLocation StartLoc,
2344 SourceLocation EndLoc) {
2345 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2346 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2347 // 1.2.2 OpenMP Language Terminology
2348 // Structured block - An executable statement with a single entry at the
2349 // top and a single exit at the bottom.
2350 // The point of exit cannot be a branch out of the structured block.
2351 // longjmp() and throw() must not violate the entry/exit criteria.
2352 CS->getCapturedDecl()->setNothrow();
2353
2354 getCurFunction()->setHasBranchProtectedScope();
2355
2356 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2357}
2358
Alexey Bataev68446b72014-07-18 07:47:19 +00002359StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2360 SourceLocation EndLoc) {
2361 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2362}
2363
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002364StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
2365 SourceLocation EndLoc) {
2366 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
2367}
2368
Alexey Bataev2df347a2014-07-18 10:17:07 +00002369StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
2370 SourceLocation EndLoc) {
2371 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
2372}
2373
Alexey Bataev6125da92014-07-21 11:26:11 +00002374StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
2375 SourceLocation StartLoc,
2376 SourceLocation EndLoc) {
2377 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
2378 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
2379}
2380
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002381StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
2382 SourceLocation StartLoc,
2383 SourceLocation EndLoc) {
2384 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2385
2386 getCurFunction()->setHasBranchProtectedScope();
2387
2388 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
2389}
2390
Alexey Bataev0162e452014-07-22 10:10:35 +00002391StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
2392 Stmt *AStmt,
2393 SourceLocation StartLoc,
2394 SourceLocation EndLoc) {
2395 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002396 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00002397 // 1.2.2 OpenMP Language Terminology
2398 // Structured block - An executable statement with a single entry at the
2399 // top and a single exit at the bottom.
2400 // The point of exit cannot be a branch out of the structured block.
2401 // longjmp() and throw() must not violate the entry/exit criteria.
2402 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00002403 OpenMPClauseKind AtomicKind = OMPC_unknown;
2404 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002405 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00002406 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00002407 C->getClauseKind() == OMPC_update ||
2408 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00002409 if (AtomicKind != OMPC_unknown) {
2410 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
2411 << SourceRange(C->getLocStart(), C->getLocEnd());
2412 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
2413 << getOpenMPClauseName(AtomicKind);
2414 } else {
2415 AtomicKind = C->getClauseKind();
2416 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002417 }
2418 }
2419 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002420 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00002421 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002422 if (!isa<Expr>(Body)) {
2423 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002424 diag::err_omp_atomic_read_not_expression_statement);
2425 return StmtError();
2426 }
2427 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002428 if (!isa<Expr>(Body)) {
2429 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002430 diag::err_omp_atomic_write_not_expression_statement);
2431 return StmtError();
2432 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00002433 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002434 if (!isa<Expr>(Body)) {
2435 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00002436 diag::err_omp_atomic_update_not_expression_statement)
2437 << (AtomicKind == OMPC_update);
2438 return StmtError();
2439 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002440 } else if (AtomicKind == OMPC_capture) {
2441 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
2442 Diag(Body->getLocStart(),
2443 diag::err_omp_atomic_capture_not_expression_statement);
2444 return StmtError();
2445 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
2446 Diag(Body->getLocStart(),
2447 diag::err_omp_atomic_capture_not_compound_statement);
2448 return StmtError();
2449 }
Alexey Bataevdea47612014-07-23 07:46:59 +00002450 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002451
2452 getCurFunction()->setHasBranchProtectedScope();
2453
2454 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2455}
2456
Alexey Bataeved09d242014-05-28 05:53:51 +00002457OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002458 SourceLocation StartLoc,
2459 SourceLocation LParenLoc,
2460 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002461 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002462 switch (Kind) {
2463 case OMPC_if:
2464 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2465 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002466 case OMPC_final:
2467 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2468 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002469 case OMPC_num_threads:
2470 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2471 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002472 case OMPC_safelen:
2473 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2474 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002475 case OMPC_collapse:
2476 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2477 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002478 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002479 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002480 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002481 case OMPC_private:
2482 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002483 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002484 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002485 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002486 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002487 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002488 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002489 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002490 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002491 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002492 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002493 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002494 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002495 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002496 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002497 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002498 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002499 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002500 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002501 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002502 llvm_unreachable("Clause is not allowed.");
2503 }
2504 return Res;
2505}
2506
Alexey Bataeved09d242014-05-28 05:53:51 +00002507OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002508 SourceLocation LParenLoc,
2509 SourceLocation EndLoc) {
2510 Expr *ValExpr = Condition;
2511 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2512 !Condition->isInstantiationDependent() &&
2513 !Condition->containsUnexpandedParameterPack()) {
2514 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002515 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002516 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002517 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002518
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002519 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002520 }
2521
2522 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2523}
2524
Alexey Bataev3778b602014-07-17 07:32:53 +00002525OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2526 SourceLocation StartLoc,
2527 SourceLocation LParenLoc,
2528 SourceLocation EndLoc) {
2529 Expr *ValExpr = Condition;
2530 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2531 !Condition->isInstantiationDependent() &&
2532 !Condition->containsUnexpandedParameterPack()) {
2533 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2534 Condition->getExprLoc(), Condition);
2535 if (Val.isInvalid())
2536 return nullptr;
2537
2538 ValExpr = Val.get();
2539 }
2540
2541 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2542}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002543ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2544 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002545 if (!Op)
2546 return ExprError();
2547
2548 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2549 public:
2550 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002551 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002552 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2553 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002554 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2555 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002556 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2557 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002558 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2559 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002560 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2561 QualType T,
2562 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002563 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2564 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002565 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2566 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002567 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002568 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002569 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002570 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2571 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002572 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2573 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002574 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2575 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002576 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002577 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002578 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002579 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2580 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002581 llvm_unreachable("conversion functions are permitted");
2582 }
2583 } ConvertDiagnoser;
2584 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2585}
2586
2587OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2588 SourceLocation StartLoc,
2589 SourceLocation LParenLoc,
2590 SourceLocation EndLoc) {
2591 Expr *ValExpr = NumThreads;
2592 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2593 !NumThreads->isInstantiationDependent() &&
2594 !NumThreads->containsUnexpandedParameterPack()) {
2595 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2596 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002597 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002598 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002599 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002600
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002601 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002602
2603 // OpenMP [2.5, Restrictions]
2604 // The num_threads expression must evaluate to a positive integer value.
2605 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002606 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2607 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002608 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2609 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002610 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002611 }
2612 }
2613
Alexey Bataeved09d242014-05-28 05:53:51 +00002614 return new (Context)
2615 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002616}
2617
Alexey Bataev62c87d22014-03-21 04:51:18 +00002618ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2619 OpenMPClauseKind CKind) {
2620 if (!E)
2621 return ExprError();
2622 if (E->isValueDependent() || E->isTypeDependent() ||
2623 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002624 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002625 llvm::APSInt Result;
2626 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2627 if (ICE.isInvalid())
2628 return ExprError();
2629 if (!Result.isStrictlyPositive()) {
2630 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2631 << getOpenMPClauseName(CKind) << E->getSourceRange();
2632 return ExprError();
2633 }
2634 return ICE;
2635}
2636
2637OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2638 SourceLocation LParenLoc,
2639 SourceLocation EndLoc) {
2640 // OpenMP [2.8.1, simd construct, Description]
2641 // The parameter of the safelen clause must be a constant
2642 // positive integer expression.
2643 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2644 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002645 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002646 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002647 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002648}
2649
Alexander Musman64d33f12014-06-04 07:53:32 +00002650OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2651 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002652 SourceLocation LParenLoc,
2653 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002654 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002655 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002656 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002657 // The parameter of the collapse clause must be a constant
2658 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002659 ExprResult NumForLoopsResult =
2660 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2661 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002662 return nullptr;
2663 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002664 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002665}
2666
Alexey Bataeved09d242014-05-28 05:53:51 +00002667OMPClause *Sema::ActOnOpenMPSimpleClause(
2668 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2669 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002670 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002671 switch (Kind) {
2672 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002673 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002674 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2675 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002676 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002677 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002678 Res = ActOnOpenMPProcBindClause(
2679 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2680 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002681 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002682 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002683 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002684 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002685 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002686 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002687 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002688 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002689 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002690 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002691 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002692 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002693 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002694 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002695 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002696 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002697 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002698 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002699 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002700 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002701 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002702 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002703 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002704 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002705 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002706 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002707 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002708 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002709 llvm_unreachable("Clause is not allowed.");
2710 }
2711 return Res;
2712}
2713
2714OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2715 SourceLocation KindKwLoc,
2716 SourceLocation StartLoc,
2717 SourceLocation LParenLoc,
2718 SourceLocation EndLoc) {
2719 if (Kind == OMPC_DEFAULT_unknown) {
2720 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002721 static_assert(OMPC_DEFAULT_unknown > 0,
2722 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002723 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002724 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002725 Values += "'";
2726 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2727 Values += "'";
2728 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002729 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002730 Values += " or ";
2731 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002732 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002733 break;
2734 default:
2735 Values += Sep;
2736 break;
2737 }
2738 }
2739 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002740 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002741 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002742 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002743 switch (Kind) {
2744 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002745 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002746 break;
2747 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002748 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002749 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002750 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002751 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002752 break;
2753 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002754 return new (Context)
2755 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002756}
2757
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002758OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2759 SourceLocation KindKwLoc,
2760 SourceLocation StartLoc,
2761 SourceLocation LParenLoc,
2762 SourceLocation EndLoc) {
2763 if (Kind == OMPC_PROC_BIND_unknown) {
2764 std::string Values;
2765 std::string Sep(", ");
2766 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2767 Values += "'";
2768 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2769 Values += "'";
2770 switch (i) {
2771 case OMPC_PROC_BIND_unknown - 2:
2772 Values += " or ";
2773 break;
2774 case OMPC_PROC_BIND_unknown - 1:
2775 break;
2776 default:
2777 Values += Sep;
2778 break;
2779 }
2780 }
2781 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002782 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002783 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002784 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002785 return new (Context)
2786 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002787}
2788
Alexey Bataev56dafe82014-06-20 07:16:17 +00002789OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2790 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2791 SourceLocation StartLoc, SourceLocation LParenLoc,
2792 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2793 SourceLocation EndLoc) {
2794 OMPClause *Res = nullptr;
2795 switch (Kind) {
2796 case OMPC_schedule:
2797 Res = ActOnOpenMPScheduleClause(
2798 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2799 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2800 break;
2801 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002802 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002803 case OMPC_num_threads:
2804 case OMPC_safelen:
2805 case OMPC_collapse:
2806 case OMPC_default:
2807 case OMPC_proc_bind:
2808 case OMPC_private:
2809 case OMPC_firstprivate:
2810 case OMPC_lastprivate:
2811 case OMPC_shared:
2812 case OMPC_reduction:
2813 case OMPC_linear:
2814 case OMPC_aligned:
2815 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002816 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002817 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002818 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002819 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002820 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002821 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002822 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002823 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002824 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002825 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002826 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002827 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002828 case OMPC_unknown:
2829 llvm_unreachable("Clause is not allowed.");
2830 }
2831 return Res;
2832}
2833
2834OMPClause *Sema::ActOnOpenMPScheduleClause(
2835 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2836 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2837 SourceLocation EndLoc) {
2838 if (Kind == OMPC_SCHEDULE_unknown) {
2839 std::string Values;
2840 std::string Sep(", ");
2841 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2842 Values += "'";
2843 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2844 Values += "'";
2845 switch (i) {
2846 case OMPC_SCHEDULE_unknown - 2:
2847 Values += " or ";
2848 break;
2849 case OMPC_SCHEDULE_unknown - 1:
2850 break;
2851 default:
2852 Values += Sep;
2853 break;
2854 }
2855 }
2856 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2857 << Values << getOpenMPClauseName(OMPC_schedule);
2858 return nullptr;
2859 }
2860 Expr *ValExpr = ChunkSize;
2861 if (ChunkSize) {
2862 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2863 !ChunkSize->isInstantiationDependent() &&
2864 !ChunkSize->containsUnexpandedParameterPack()) {
2865 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2866 ExprResult Val =
2867 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2868 if (Val.isInvalid())
2869 return nullptr;
2870
2871 ValExpr = Val.get();
2872
2873 // OpenMP [2.7.1, Restrictions]
2874 // chunk_size must be a loop invariant integer expression with a positive
2875 // value.
2876 llvm::APSInt Result;
2877 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2878 Result.isSigned() && !Result.isStrictlyPositive()) {
2879 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2880 << "schedule" << ChunkSize->getSourceRange();
2881 return nullptr;
2882 }
2883 }
2884 }
2885
2886 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2887 EndLoc, Kind, ValExpr);
2888}
2889
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002890OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2891 SourceLocation StartLoc,
2892 SourceLocation EndLoc) {
2893 OMPClause *Res = nullptr;
2894 switch (Kind) {
2895 case OMPC_ordered:
2896 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2897 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002898 case OMPC_nowait:
2899 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2900 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002901 case OMPC_untied:
2902 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
2903 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002904 case OMPC_mergeable:
2905 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
2906 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002907 case OMPC_read:
2908 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
2909 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00002910 case OMPC_write:
2911 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
2912 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00002913 case OMPC_update:
2914 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
2915 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00002916 case OMPC_capture:
2917 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
2918 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002919 case OMPC_seq_cst:
2920 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
2921 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002922 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002923 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002924 case OMPC_num_threads:
2925 case OMPC_safelen:
2926 case OMPC_collapse:
2927 case OMPC_schedule:
2928 case OMPC_private:
2929 case OMPC_firstprivate:
2930 case OMPC_lastprivate:
2931 case OMPC_shared:
2932 case OMPC_reduction:
2933 case OMPC_linear:
2934 case OMPC_aligned:
2935 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002936 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002937 case OMPC_default:
2938 case OMPC_proc_bind:
2939 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002940 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002941 case OMPC_unknown:
2942 llvm_unreachable("Clause is not allowed.");
2943 }
2944 return Res;
2945}
2946
2947OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2948 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002949 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002950 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2951}
2952
Alexey Bataev236070f2014-06-20 11:19:47 +00002953OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2954 SourceLocation EndLoc) {
2955 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2956}
2957
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002958OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
2959 SourceLocation EndLoc) {
2960 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
2961}
2962
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002963OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
2964 SourceLocation EndLoc) {
2965 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
2966}
2967
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002968OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
2969 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002970 return new (Context) OMPReadClause(StartLoc, EndLoc);
2971}
2972
Alexey Bataevdea47612014-07-23 07:46:59 +00002973OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
2974 SourceLocation EndLoc) {
2975 return new (Context) OMPWriteClause(StartLoc, EndLoc);
2976}
2977
Alexey Bataev67a4f222014-07-23 10:25:33 +00002978OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
2979 SourceLocation EndLoc) {
2980 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
2981}
2982
Alexey Bataev459dec02014-07-24 06:46:57 +00002983OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
2984 SourceLocation EndLoc) {
2985 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
2986}
2987
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002988OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
2989 SourceLocation EndLoc) {
2990 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
2991}
2992
Alexey Bataevc5e02582014-06-16 07:08:35 +00002993OMPClause *Sema::ActOnOpenMPVarListClause(
2994 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2995 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2996 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2997 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002998 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002999 switch (Kind) {
3000 case OMPC_private:
3001 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3002 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003003 case OMPC_firstprivate:
3004 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3005 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003006 case OMPC_lastprivate:
3007 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3008 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003009 case OMPC_shared:
3010 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3011 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003012 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003013 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3014 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003015 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003016 case OMPC_linear:
3017 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3018 ColonLoc, EndLoc);
3019 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003020 case OMPC_aligned:
3021 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3022 ColonLoc, EndLoc);
3023 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003024 case OMPC_copyin:
3025 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3026 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003027 case OMPC_copyprivate:
3028 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3029 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003030 case OMPC_flush:
3031 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3032 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003033 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003034 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003035 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003036 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003037 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003038 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003039 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003040 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003041 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003042 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003043 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003044 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003045 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003046 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003047 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003048 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003049 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003050 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003051 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003052 llvm_unreachable("Clause is not allowed.");
3053 }
3054 return Res;
3055}
3056
3057OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3058 SourceLocation StartLoc,
3059 SourceLocation LParenLoc,
3060 SourceLocation EndLoc) {
3061 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003062 for (auto &RefExpr : VarList) {
3063 assert(RefExpr && "NULL expr in OpenMP private clause.");
3064 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003065 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003066 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003067 continue;
3068 }
3069
Alexey Bataeved09d242014-05-28 05:53:51 +00003070 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003071 // OpenMP [2.1, C/C++]
3072 // A list item is a variable name.
3073 // OpenMP [2.9.3.3, Restrictions, p.1]
3074 // A variable that is part of another variable (as an array or
3075 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003076 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003077 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003078 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003079 continue;
3080 }
3081 Decl *D = DE->getDecl();
3082 VarDecl *VD = cast<VarDecl>(D);
3083
3084 QualType Type = VD->getType();
3085 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3086 // It will be analyzed later.
3087 Vars.push_back(DE);
3088 continue;
3089 }
3090
3091 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3092 // A variable that appears in a private clause must not have an incomplete
3093 // type or a reference type.
3094 if (RequireCompleteType(ELoc, Type,
3095 diag::err_omp_private_incomplete_type)) {
3096 continue;
3097 }
3098 if (Type->isReferenceType()) {
3099 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003100 << getOpenMPClauseName(OMPC_private) << Type;
3101 bool IsDecl =
3102 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3103 Diag(VD->getLocation(),
3104 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3105 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003106 continue;
3107 }
3108
3109 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3110 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003111 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003112 // class type.
3113 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003114 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3115 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003116 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003117 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3118 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3119 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003120 // FIXME This code must be replaced by actual constructing/destructing of
3121 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003122 if (RD) {
3123 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3124 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003125 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003126 if (!CD ||
3127 CheckConstructorAccess(ELoc, CD,
3128 InitializedEntity::InitializeTemporary(Type),
3129 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003130 CD->isDeleted()) {
3131 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003132 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003133 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3134 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003135 Diag(VD->getLocation(),
3136 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3137 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003138 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3139 continue;
3140 }
3141 MarkFunctionReferenced(ELoc, CD);
3142 DiagnoseUseOfDecl(CD, ELoc);
3143
3144 CXXDestructorDecl *DD = RD->getDestructor();
3145 if (DD) {
3146 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3147 DD->isDeleted()) {
3148 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003149 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003150 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3151 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003152 Diag(VD->getLocation(),
3153 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3154 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003155 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3156 continue;
3157 }
3158 MarkFunctionReferenced(ELoc, DD);
3159 DiagnoseUseOfDecl(DD, ELoc);
3160 }
3161 }
3162
Alexey Bataev758e55e2013-09-06 18:03:48 +00003163 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3164 // in a Construct]
3165 // Variables with the predetermined data-sharing attributes may not be
3166 // listed in data-sharing attributes clauses, except for the cases
3167 // listed below. For these exceptions only, listing a predetermined
3168 // variable in a data-sharing attribute clause is allowed and overrides
3169 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003170 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003171 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003172 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3173 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003174 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003175 continue;
3176 }
3177
3178 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003179 Vars.push_back(DE);
3180 }
3181
Alexey Bataeved09d242014-05-28 05:53:51 +00003182 if (Vars.empty())
3183 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003184
3185 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3186}
3187
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003188OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3189 SourceLocation StartLoc,
3190 SourceLocation LParenLoc,
3191 SourceLocation EndLoc) {
3192 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003193 bool IsImplicitClause =
3194 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3195 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3196
Alexey Bataeved09d242014-05-28 05:53:51 +00003197 for (auto &RefExpr : VarList) {
3198 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3199 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003200 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003201 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003202 continue;
3203 }
3204
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003205 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3206 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003207 // OpenMP [2.1, C/C++]
3208 // A list item is a variable name.
3209 // OpenMP [2.9.3.3, Restrictions, p.1]
3210 // A variable that is part of another variable (as an array or
3211 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003212 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003213 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003214 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003215 continue;
3216 }
3217 Decl *D = DE->getDecl();
3218 VarDecl *VD = cast<VarDecl>(D);
3219
3220 QualType Type = VD->getType();
3221 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3222 // It will be analyzed later.
3223 Vars.push_back(DE);
3224 continue;
3225 }
3226
3227 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3228 // A variable that appears in a private clause must not have an incomplete
3229 // type or a reference type.
3230 if (RequireCompleteType(ELoc, Type,
3231 diag::err_omp_firstprivate_incomplete_type)) {
3232 continue;
3233 }
3234 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003235 if (IsImplicitClause) {
3236 Diag(ImplicitClauseLoc,
3237 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3238 << Type;
3239 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3240 } else {
3241 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3242 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3243 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003244 bool IsDecl =
3245 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3246 Diag(VD->getLocation(),
3247 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3248 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003249 continue;
3250 }
3251
3252 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3253 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003254 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003255 // class type.
3256 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003257 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3258 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3259 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003260 // FIXME This code must be replaced by actual constructing/destructing of
3261 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003262 if (RD) {
3263 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3264 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003265 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003266 if (!CD ||
3267 CheckConstructorAccess(ELoc, CD,
3268 InitializedEntity::InitializeTemporary(Type),
3269 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003270 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003271 if (IsImplicitClause) {
3272 Diag(ImplicitClauseLoc,
3273 diag::err_omp_task_predetermined_firstprivate_required_method)
3274 << 0;
3275 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3276 } else {
3277 Diag(ELoc, diag::err_omp_required_method)
3278 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3279 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003280 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3281 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003282 Diag(VD->getLocation(),
3283 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3284 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003285 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3286 continue;
3287 }
3288 MarkFunctionReferenced(ELoc, CD);
3289 DiagnoseUseOfDecl(CD, ELoc);
3290
3291 CXXDestructorDecl *DD = RD->getDestructor();
3292 if (DD) {
3293 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3294 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003295 if (IsImplicitClause) {
3296 Diag(ImplicitClauseLoc,
3297 diag::err_omp_task_predetermined_firstprivate_required_method)
3298 << 1;
3299 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3300 } else {
3301 Diag(ELoc, diag::err_omp_required_method)
3302 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3303 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003304 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3305 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003306 Diag(VD->getLocation(),
3307 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3308 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003309 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3310 continue;
3311 }
3312 MarkFunctionReferenced(ELoc, DD);
3313 DiagnoseUseOfDecl(DD, ELoc);
3314 }
3315 }
3316
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003317 // If an implicit firstprivate variable found it was checked already.
3318 if (!IsImplicitClause) {
3319 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003320 Type = Type.getNonReferenceType().getCanonicalType();
3321 bool IsConstant = Type.isConstant(Context);
3322 Type = Context.getBaseElementType(Type);
3323 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
3324 // A list item that specifies a given variable may not appear in more
3325 // than one clause on the same directive, except that a variable may be
3326 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003327 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00003328 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003329 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003330 << getOpenMPClauseName(DVar.CKind)
3331 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003332 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003333 continue;
3334 }
3335
3336 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3337 // in a Construct]
3338 // Variables with the predetermined data-sharing attributes may not be
3339 // listed in data-sharing attributes clauses, except for the cases
3340 // listed below. For these exceptions only, listing a predetermined
3341 // variable in a data-sharing attribute clause is allowed and overrides
3342 // the variable's predetermined data-sharing attributes.
3343 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3344 // in a Construct, C/C++, p.2]
3345 // Variables with const-qualified type having no mutable member may be
3346 // listed in a firstprivate clause, even if they are static data members.
3347 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
3348 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
3349 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003350 << getOpenMPClauseName(DVar.CKind)
3351 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003352 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003353 continue;
3354 }
3355
Alexey Bataevf29276e2014-06-18 04:14:57 +00003356 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003357 // OpenMP [2.9.3.4, Restrictions, p.2]
3358 // A list item that is private within a parallel region must not appear
3359 // in a firstprivate clause on a worksharing construct if any of the
3360 // worksharing regions arising from the worksharing construct ever bind
3361 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00003362 if (isOpenMPWorksharingDirective(CurrDir) &&
3363 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003364 DVar = DSAStack->getImplicitDSA(VD, true);
3365 if (DVar.CKind != OMPC_shared &&
3366 (isOpenMPParallelDirective(DVar.DKind) ||
3367 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003368 Diag(ELoc, diag::err_omp_required_access)
3369 << getOpenMPClauseName(OMPC_firstprivate)
3370 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003371 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003372 continue;
3373 }
3374 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003375 // OpenMP [2.9.3.4, Restrictions, p.3]
3376 // A list item that appears in a reduction clause of a parallel construct
3377 // must not appear in a firstprivate clause on a worksharing or task
3378 // construct if any of the worksharing or task regions arising from the
3379 // worksharing or task construct ever bind to any of the parallel regions
3380 // arising from the parallel construct.
3381 // OpenMP [2.9.3.4, Restrictions, p.4]
3382 // A list item that appears in a reduction clause in worksharing
3383 // construct must not appear in a firstprivate clause in a task construct
3384 // encountered during execution of any of the worksharing regions arising
3385 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003386 if (CurrDir == OMPD_task) {
3387 DVar =
3388 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
3389 [](OpenMPDirectiveKind K) -> bool {
3390 return isOpenMPParallelDirective(K) ||
3391 isOpenMPWorksharingDirective(K);
3392 },
3393 false);
3394 if (DVar.CKind == OMPC_reduction &&
3395 (isOpenMPParallelDirective(DVar.DKind) ||
3396 isOpenMPWorksharingDirective(DVar.DKind))) {
3397 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
3398 << getOpenMPDirectiveName(DVar.DKind);
3399 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3400 continue;
3401 }
3402 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003403 }
3404
3405 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
3406 Vars.push_back(DE);
3407 }
3408
Alexey Bataeved09d242014-05-28 05:53:51 +00003409 if (Vars.empty())
3410 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003411
3412 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3413 Vars);
3414}
3415
Alexander Musman1bb328c2014-06-04 13:06:39 +00003416OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
3417 SourceLocation StartLoc,
3418 SourceLocation LParenLoc,
3419 SourceLocation EndLoc) {
3420 SmallVector<Expr *, 8> Vars;
3421 for (auto &RefExpr : VarList) {
3422 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
3423 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3424 // It will be analyzed later.
3425 Vars.push_back(RefExpr);
3426 continue;
3427 }
3428
3429 SourceLocation ELoc = RefExpr->getExprLoc();
3430 // OpenMP [2.1, C/C++]
3431 // A list item is a variable name.
3432 // OpenMP [2.14.3.5, Restrictions, p.1]
3433 // A variable that is part of another variable (as an array or structure
3434 // element) cannot appear in a lastprivate clause.
3435 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3436 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3437 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3438 continue;
3439 }
3440 Decl *D = DE->getDecl();
3441 VarDecl *VD = cast<VarDecl>(D);
3442
3443 QualType Type = VD->getType();
3444 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3445 // It will be analyzed later.
3446 Vars.push_back(DE);
3447 continue;
3448 }
3449
3450 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
3451 // A variable that appears in a lastprivate clause must not have an
3452 // incomplete type or a reference type.
3453 if (RequireCompleteType(ELoc, Type,
3454 diag::err_omp_lastprivate_incomplete_type)) {
3455 continue;
3456 }
3457 if (Type->isReferenceType()) {
3458 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3459 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3460 bool IsDecl =
3461 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3462 Diag(VD->getLocation(),
3463 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3464 << VD;
3465 continue;
3466 }
3467
3468 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3469 // in a Construct]
3470 // Variables with the predetermined data-sharing attributes may not be
3471 // listed in data-sharing attributes clauses, except for the cases
3472 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003473 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003474 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3475 DVar.CKind != OMPC_firstprivate &&
3476 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3477 Diag(ELoc, diag::err_omp_wrong_dsa)
3478 << getOpenMPClauseName(DVar.CKind)
3479 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003480 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003481 continue;
3482 }
3483
Alexey Bataevf29276e2014-06-18 04:14:57 +00003484 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3485 // OpenMP [2.14.3.5, Restrictions, p.2]
3486 // A list item that is private within a parallel region, or that appears in
3487 // the reduction clause of a parallel construct, must not appear in a
3488 // lastprivate clause on a worksharing construct if any of the corresponding
3489 // worksharing regions ever binds to any of the corresponding parallel
3490 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003491 if (isOpenMPWorksharingDirective(CurrDir) &&
3492 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003493 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003494 if (DVar.CKind != OMPC_shared) {
3495 Diag(ELoc, diag::err_omp_required_access)
3496 << getOpenMPClauseName(OMPC_lastprivate)
3497 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003498 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003499 continue;
3500 }
3501 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003502 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003503 // A variable of class type (or array thereof) that appears in a
3504 // lastprivate clause requires an accessible, unambiguous default
3505 // constructor for the class type, unless the list item is also specified
3506 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003507 // A variable of class type (or array thereof) that appears in a
3508 // lastprivate clause requires an accessible, unambiguous copy assignment
3509 // operator for the class type.
3510 while (Type.getNonReferenceType()->isArrayType())
3511 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3512 ->getElementType();
3513 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3514 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3515 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003516 // FIXME This code must be replaced by actual copying and destructing of the
3517 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003518 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003519 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3520 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003521 if (MD) {
3522 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3523 MD->isDeleted()) {
3524 Diag(ELoc, diag::err_omp_required_method)
3525 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3526 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3527 VarDecl::DeclarationOnly;
3528 Diag(VD->getLocation(),
3529 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3530 << VD;
3531 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3532 continue;
3533 }
3534 MarkFunctionReferenced(ELoc, MD);
3535 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003536 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003537
3538 CXXDestructorDecl *DD = RD->getDestructor();
3539 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003540 PartialDiagnostic PD =
3541 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003542 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3543 DD->isDeleted()) {
3544 Diag(ELoc, diag::err_omp_required_method)
3545 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3546 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3547 VarDecl::DeclarationOnly;
3548 Diag(VD->getLocation(),
3549 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3550 << VD;
3551 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3552 continue;
3553 }
3554 MarkFunctionReferenced(ELoc, DD);
3555 DiagnoseUseOfDecl(DD, ELoc);
3556 }
3557 }
3558
Alexey Bataevf29276e2014-06-18 04:14:57 +00003559 if (DVar.CKind != OMPC_firstprivate)
3560 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003561 Vars.push_back(DE);
3562 }
3563
3564 if (Vars.empty())
3565 return nullptr;
3566
3567 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3568 Vars);
3569}
3570
Alexey Bataev758e55e2013-09-06 18:03:48 +00003571OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3572 SourceLocation StartLoc,
3573 SourceLocation LParenLoc,
3574 SourceLocation EndLoc) {
3575 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003576 for (auto &RefExpr : VarList) {
3577 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3578 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003579 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003580 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003581 continue;
3582 }
3583
Alexey Bataeved09d242014-05-28 05:53:51 +00003584 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003585 // OpenMP [2.1, C/C++]
3586 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003587 // OpenMP [2.14.3.2, Restrictions, p.1]
3588 // A variable that is part of another variable (as an array or structure
3589 // element) cannot appear in a shared unless it is a static data member
3590 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003591 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003592 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003593 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003594 continue;
3595 }
3596 Decl *D = DE->getDecl();
3597 VarDecl *VD = cast<VarDecl>(D);
3598
3599 QualType Type = VD->getType();
3600 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3601 // It will be analyzed later.
3602 Vars.push_back(DE);
3603 continue;
3604 }
3605
3606 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3607 // in a Construct]
3608 // Variables with the predetermined data-sharing attributes may not be
3609 // listed in data-sharing attributes clauses, except for the cases
3610 // listed below. For these exceptions only, listing a predetermined
3611 // variable in a data-sharing attribute clause is allowed and overrides
3612 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003613 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003614 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3615 DVar.RefExpr) {
3616 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3617 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003618 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003619 continue;
3620 }
3621
3622 DSAStack->addDSA(VD, DE, OMPC_shared);
3623 Vars.push_back(DE);
3624 }
3625
Alexey Bataeved09d242014-05-28 05:53:51 +00003626 if (Vars.empty())
3627 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003628
3629 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3630}
3631
Alexey Bataevc5e02582014-06-16 07:08:35 +00003632namespace {
3633class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3634 DSAStackTy *Stack;
3635
3636public:
3637 bool VisitDeclRefExpr(DeclRefExpr *E) {
3638 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003639 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003640 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3641 return false;
3642 if (DVar.CKind != OMPC_unknown)
3643 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003644 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003645 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003646 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003647 return true;
3648 return false;
3649 }
3650 return false;
3651 }
3652 bool VisitStmt(Stmt *S) {
3653 for (auto Child : S->children()) {
3654 if (Child && Visit(Child))
3655 return true;
3656 }
3657 return false;
3658 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003659 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003660};
Alexey Bataev23b69422014-06-18 07:08:49 +00003661} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003662
3663OMPClause *Sema::ActOnOpenMPReductionClause(
3664 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3665 SourceLocation ColonLoc, SourceLocation EndLoc,
3666 CXXScopeSpec &ReductionIdScopeSpec,
3667 const DeclarationNameInfo &ReductionId) {
3668 // TODO: Allow scope specification search when 'declare reduction' is
3669 // supported.
3670 assert(ReductionIdScopeSpec.isEmpty() &&
3671 "No support for scoped reduction identifiers yet.");
3672
3673 auto DN = ReductionId.getName();
3674 auto OOK = DN.getCXXOverloadedOperator();
3675 BinaryOperatorKind BOK = BO_Comma;
3676
3677 // OpenMP [2.14.3.6, reduction clause]
3678 // C
3679 // reduction-identifier is either an identifier or one of the following
3680 // operators: +, -, *, &, |, ^, && and ||
3681 // C++
3682 // reduction-identifier is either an id-expression or one of the following
3683 // operators: +, -, *, &, |, ^, && and ||
3684 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3685 switch (OOK) {
3686 case OO_Plus:
3687 case OO_Minus:
3688 BOK = BO_AddAssign;
3689 break;
3690 case OO_Star:
3691 BOK = BO_MulAssign;
3692 break;
3693 case OO_Amp:
3694 BOK = BO_AndAssign;
3695 break;
3696 case OO_Pipe:
3697 BOK = BO_OrAssign;
3698 break;
3699 case OO_Caret:
3700 BOK = BO_XorAssign;
3701 break;
3702 case OO_AmpAmp:
3703 BOK = BO_LAnd;
3704 break;
3705 case OO_PipePipe:
3706 BOK = BO_LOr;
3707 break;
3708 default:
3709 if (auto II = DN.getAsIdentifierInfo()) {
3710 if (II->isStr("max"))
3711 BOK = BO_GT;
3712 else if (II->isStr("min"))
3713 BOK = BO_LT;
3714 }
3715 break;
3716 }
3717 SourceRange ReductionIdRange;
3718 if (ReductionIdScopeSpec.isValid()) {
3719 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3720 }
3721 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3722 if (BOK == BO_Comma) {
3723 // Not allowed reduction identifier is found.
3724 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3725 << ReductionIdRange;
3726 return nullptr;
3727 }
3728
3729 SmallVector<Expr *, 8> Vars;
3730 for (auto RefExpr : VarList) {
3731 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3732 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3733 // It will be analyzed later.
3734 Vars.push_back(RefExpr);
3735 continue;
3736 }
3737
3738 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3739 RefExpr->isInstantiationDependent() ||
3740 RefExpr->containsUnexpandedParameterPack()) {
3741 // It will be analyzed later.
3742 Vars.push_back(RefExpr);
3743 continue;
3744 }
3745
3746 auto ELoc = RefExpr->getExprLoc();
3747 auto ERange = RefExpr->getSourceRange();
3748 // OpenMP [2.1, C/C++]
3749 // A list item is a variable or array section, subject to the restrictions
3750 // specified in Section 2.4 on page 42 and in each of the sections
3751 // describing clauses and directives for which a list appears.
3752 // OpenMP [2.14.3.3, Restrictions, p.1]
3753 // A variable that is part of another variable (as an array or
3754 // structure element) cannot appear in a private clause.
3755 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3756 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3757 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3758 continue;
3759 }
3760 auto D = DE->getDecl();
3761 auto VD = cast<VarDecl>(D);
3762 auto Type = VD->getType();
3763 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3764 // A variable that appears in a private clause must not have an incomplete
3765 // type or a reference type.
3766 if (RequireCompleteType(ELoc, Type,
3767 diag::err_omp_reduction_incomplete_type))
3768 continue;
3769 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3770 // Arrays may not appear in a reduction clause.
3771 if (Type.getNonReferenceType()->isArrayType()) {
3772 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3773 bool IsDecl =
3774 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3775 Diag(VD->getLocation(),
3776 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3777 << VD;
3778 continue;
3779 }
3780 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3781 // A list item that appears in a reduction clause must not be
3782 // const-qualified.
3783 if (Type.getNonReferenceType().isConstant(Context)) {
3784 Diag(ELoc, diag::err_omp_const_variable)
3785 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3786 bool IsDecl =
3787 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3788 Diag(VD->getLocation(),
3789 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3790 << VD;
3791 continue;
3792 }
3793 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3794 // If a list-item is a reference type then it must bind to the same object
3795 // for all threads of the team.
3796 VarDecl *VDDef = VD->getDefinition();
3797 if (Type->isReferenceType() && VDDef) {
3798 DSARefChecker Check(DSAStack);
3799 if (Check.Visit(VDDef->getInit())) {
3800 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3801 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3802 continue;
3803 }
3804 }
3805 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3806 // The type of a list item that appears in a reduction clause must be valid
3807 // for the reduction-identifier. For a max or min reduction in C, the type
3808 // of the list item must be an allowed arithmetic data type: char, int,
3809 // float, double, or _Bool, possibly modified with long, short, signed, or
3810 // unsigned. For a max or min reduction in C++, the type of the list item
3811 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3812 // double, or bool, possibly modified with long, short, signed, or unsigned.
3813 if ((BOK == BO_GT || BOK == BO_LT) &&
3814 !(Type->isScalarType() ||
3815 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3816 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3817 << getLangOpts().CPlusPlus;
3818 bool IsDecl =
3819 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3820 Diag(VD->getLocation(),
3821 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3822 << VD;
3823 continue;
3824 }
3825 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3826 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3827 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3828 bool IsDecl =
3829 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3830 Diag(VD->getLocation(),
3831 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3832 << VD;
3833 continue;
3834 }
3835 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3836 getDiagnostics().setSuppressAllDiagnostics(true);
3837 ExprResult ReductionOp =
3838 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3839 RefExpr, RefExpr);
3840 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3841 if (ReductionOp.isInvalid()) {
3842 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003843 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003844 bool IsDecl =
3845 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3846 Diag(VD->getLocation(),
3847 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3848 << VD;
3849 continue;
3850 }
3851
3852 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3853 // in a Construct]
3854 // Variables with the predetermined data-sharing attributes may not be
3855 // listed in data-sharing attributes clauses, except for the cases
3856 // listed below. For these exceptions only, listing a predetermined
3857 // variable in a data-sharing attribute clause is allowed and overrides
3858 // the variable's predetermined data-sharing attributes.
3859 // OpenMP [2.14.3.6, Restrictions, p.3]
3860 // Any number of reduction clauses can be specified on the directive,
3861 // but a list item can appear only once in the reduction clauses for that
3862 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003863 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003864 if (DVar.CKind == OMPC_reduction) {
3865 Diag(ELoc, diag::err_omp_once_referenced)
3866 << getOpenMPClauseName(OMPC_reduction);
3867 if (DVar.RefExpr) {
3868 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3869 }
3870 } else if (DVar.CKind != OMPC_unknown) {
3871 Diag(ELoc, diag::err_omp_wrong_dsa)
3872 << getOpenMPClauseName(DVar.CKind)
3873 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003874 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003875 continue;
3876 }
3877
3878 // OpenMP [2.14.3.6, Restrictions, p.1]
3879 // A list item that appears in a reduction clause of a worksharing
3880 // construct must be shared in the parallel regions to which any of the
3881 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003882 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003883 if (isOpenMPWorksharingDirective(CurrDir) &&
3884 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003885 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003886 if (DVar.CKind != OMPC_shared) {
3887 Diag(ELoc, diag::err_omp_required_access)
3888 << getOpenMPClauseName(OMPC_reduction)
3889 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003890 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003891 continue;
3892 }
3893 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003894
3895 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3896 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3897 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003898 // FIXME This code must be replaced by actual constructing/destructing of
3899 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003900 if (RD) {
3901 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3902 PartialDiagnostic PD =
3903 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003904 if (!CD ||
3905 CheckConstructorAccess(ELoc, CD,
3906 InitializedEntity::InitializeTemporary(Type),
3907 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003908 CD->isDeleted()) {
3909 Diag(ELoc, diag::err_omp_required_method)
3910 << getOpenMPClauseName(OMPC_reduction) << 0;
3911 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3912 VarDecl::DeclarationOnly;
3913 Diag(VD->getLocation(),
3914 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3915 << VD;
3916 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3917 continue;
3918 }
3919 MarkFunctionReferenced(ELoc, CD);
3920 DiagnoseUseOfDecl(CD, ELoc);
3921
3922 CXXDestructorDecl *DD = RD->getDestructor();
3923 if (DD) {
3924 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3925 DD->isDeleted()) {
3926 Diag(ELoc, diag::err_omp_required_method)
3927 << getOpenMPClauseName(OMPC_reduction) << 4;
3928 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3929 VarDecl::DeclarationOnly;
3930 Diag(VD->getLocation(),
3931 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3932 << VD;
3933 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3934 continue;
3935 }
3936 MarkFunctionReferenced(ELoc, DD);
3937 DiagnoseUseOfDecl(DD, ELoc);
3938 }
3939 }
3940
3941 DSAStack->addDSA(VD, DE, OMPC_reduction);
3942 Vars.push_back(DE);
3943 }
3944
3945 if (Vars.empty())
3946 return nullptr;
3947
3948 return OMPReductionClause::Create(
3949 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3950 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3951}
3952
Alexander Musman8dba6642014-04-22 13:09:42 +00003953OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3954 SourceLocation StartLoc,
3955 SourceLocation LParenLoc,
3956 SourceLocation ColonLoc,
3957 SourceLocation EndLoc) {
3958 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003959 for (auto &RefExpr : VarList) {
3960 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3961 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003962 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003963 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003964 continue;
3965 }
3966
3967 // OpenMP [2.14.3.7, linear clause]
3968 // A list item that appears in a linear clause is subject to the private
3969 // clause semantics described in Section 2.14.3.3 on page 159 except as
3970 // noted. In addition, the value of the new list item on each iteration
3971 // of the associated loop(s) corresponds to the value of the original
3972 // list item before entering the construct plus the logical number of
3973 // the iteration times linear-step.
3974
Alexey Bataeved09d242014-05-28 05:53:51 +00003975 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003976 // OpenMP [2.1, C/C++]
3977 // A list item is a variable name.
3978 // OpenMP [2.14.3.3, Restrictions, p.1]
3979 // A variable that is part of another variable (as an array or
3980 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003981 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003982 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003983 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003984 continue;
3985 }
3986
3987 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3988
3989 // OpenMP [2.14.3.7, linear clause]
3990 // A list-item cannot appear in more than one linear clause.
3991 // A list-item that appears in a linear clause cannot appear in any
3992 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003993 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003994 if (DVar.RefExpr) {
3995 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3996 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003997 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003998 continue;
3999 }
4000
4001 QualType QType = VD->getType();
4002 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4003 // It will be analyzed later.
4004 Vars.push_back(DE);
4005 continue;
4006 }
4007
4008 // A variable must not have an incomplete type or a reference type.
4009 if (RequireCompleteType(ELoc, QType,
4010 diag::err_omp_linear_incomplete_type)) {
4011 continue;
4012 }
4013 if (QType->isReferenceType()) {
4014 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4015 << getOpenMPClauseName(OMPC_linear) << QType;
4016 bool IsDecl =
4017 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4018 Diag(VD->getLocation(),
4019 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4020 << VD;
4021 continue;
4022 }
4023
4024 // A list item must not be const-qualified.
4025 if (QType.isConstant(Context)) {
4026 Diag(ELoc, diag::err_omp_const_variable)
4027 << getOpenMPClauseName(OMPC_linear);
4028 bool IsDecl =
4029 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4030 Diag(VD->getLocation(),
4031 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4032 << VD;
4033 continue;
4034 }
4035
4036 // A list item must be of integral or pointer type.
4037 QType = QType.getUnqualifiedType().getCanonicalType();
4038 const Type *Ty = QType.getTypePtrOrNull();
4039 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4040 !Ty->isPointerType())) {
4041 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4042 bool IsDecl =
4043 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4044 Diag(VD->getLocation(),
4045 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4046 << VD;
4047 continue;
4048 }
4049
4050 DSAStack->addDSA(VD, DE, OMPC_linear);
4051 Vars.push_back(DE);
4052 }
4053
4054 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004055 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004056
4057 Expr *StepExpr = Step;
4058 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4059 !Step->isInstantiationDependent() &&
4060 !Step->containsUnexpandedParameterPack()) {
4061 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004062 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004063 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004064 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004065 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004066
4067 // Warn about zero linear step (it would be probably better specified as
4068 // making corresponding variables 'const').
4069 llvm::APSInt Result;
4070 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4071 !Result.isNegative() && !Result.isStrictlyPositive())
4072 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4073 << (Vars.size() > 1);
4074 }
4075
4076 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4077 Vars, StepExpr);
4078}
4079
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004080OMPClause *Sema::ActOnOpenMPAlignedClause(
4081 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4082 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4083
4084 SmallVector<Expr *, 8> Vars;
4085 for (auto &RefExpr : VarList) {
4086 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4087 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4088 // It will be analyzed later.
4089 Vars.push_back(RefExpr);
4090 continue;
4091 }
4092
4093 SourceLocation ELoc = RefExpr->getExprLoc();
4094 // OpenMP [2.1, C/C++]
4095 // A list item is a variable name.
4096 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4097 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4098 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4099 continue;
4100 }
4101
4102 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4103
4104 // OpenMP [2.8.1, simd construct, Restrictions]
4105 // The type of list items appearing in the aligned clause must be
4106 // array, pointer, reference to array, or reference to pointer.
4107 QualType QType = DE->getType()
4108 .getNonReferenceType()
4109 .getUnqualifiedType()
4110 .getCanonicalType();
4111 const Type *Ty = QType.getTypePtrOrNull();
4112 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4113 !Ty->isPointerType())) {
4114 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4115 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4116 bool IsDecl =
4117 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4118 Diag(VD->getLocation(),
4119 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4120 << VD;
4121 continue;
4122 }
4123
4124 // OpenMP [2.8.1, simd construct, Restrictions]
4125 // A list-item cannot appear in more than one aligned clause.
4126 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4127 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4128 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4129 << getOpenMPClauseName(OMPC_aligned);
4130 continue;
4131 }
4132
4133 Vars.push_back(DE);
4134 }
4135
4136 // OpenMP [2.8.1, simd construct, Description]
4137 // The parameter of the aligned clause, alignment, must be a constant
4138 // positive integer expression.
4139 // If no optional parameter is specified, implementation-defined default
4140 // alignments for SIMD instructions on the target platforms are assumed.
4141 if (Alignment != nullptr) {
4142 ExprResult AlignResult =
4143 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4144 if (AlignResult.isInvalid())
4145 return nullptr;
4146 Alignment = AlignResult.get();
4147 }
4148 if (Vars.empty())
4149 return nullptr;
4150
4151 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
4152 EndLoc, Vars, Alignment);
4153}
4154
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004155OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
4156 SourceLocation StartLoc,
4157 SourceLocation LParenLoc,
4158 SourceLocation EndLoc) {
4159 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004160 for (auto &RefExpr : VarList) {
4161 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
4162 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004163 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004164 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004165 continue;
4166 }
4167
Alexey Bataeved09d242014-05-28 05:53:51 +00004168 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004169 // OpenMP [2.1, C/C++]
4170 // A list item is a variable name.
4171 // OpenMP [2.14.4.1, Restrictions, p.1]
4172 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004173 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004174 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004175 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004176 continue;
4177 }
4178
4179 Decl *D = DE->getDecl();
4180 VarDecl *VD = cast<VarDecl>(D);
4181
4182 QualType Type = VD->getType();
4183 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4184 // It will be analyzed later.
4185 Vars.push_back(DE);
4186 continue;
4187 }
4188
4189 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4190 // A list item that appears in a copyin clause must be threadprivate.
4191 if (!DSAStack->isThreadPrivate(VD)) {
4192 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004193 << getOpenMPClauseName(OMPC_copyin)
4194 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004195 continue;
4196 }
4197
4198 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4199 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004200 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004201 // operator for the class type.
4202 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004203 CXXRecordDecl *RD =
4204 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004205 // FIXME This code must be replaced by actual assignment of the
4206 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004207 if (RD) {
4208 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4209 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004210 if (MD) {
4211 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4212 MD->isDeleted()) {
4213 Diag(ELoc, diag::err_omp_required_method)
4214 << getOpenMPClauseName(OMPC_copyin) << 2;
4215 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4216 VarDecl::DeclarationOnly;
4217 Diag(VD->getLocation(),
4218 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4219 << VD;
4220 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4221 continue;
4222 }
4223 MarkFunctionReferenced(ELoc, MD);
4224 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004225 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004226 }
4227
4228 DSAStack->addDSA(VD, DE, OMPC_copyin);
4229 Vars.push_back(DE);
4230 }
4231
Alexey Bataeved09d242014-05-28 05:53:51 +00004232 if (Vars.empty())
4233 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004234
4235 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4236}
4237
Alexey Bataevbae9a792014-06-27 10:37:06 +00004238OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4239 SourceLocation StartLoc,
4240 SourceLocation LParenLoc,
4241 SourceLocation EndLoc) {
4242 SmallVector<Expr *, 8> Vars;
4243 for (auto &RefExpr : VarList) {
4244 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4245 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4246 // It will be analyzed later.
4247 Vars.push_back(RefExpr);
4248 continue;
4249 }
4250
4251 SourceLocation ELoc = RefExpr->getExprLoc();
4252 // OpenMP [2.1, C/C++]
4253 // A list item is a variable name.
4254 // OpenMP [2.14.4.1, Restrictions, p.1]
4255 // A list item that appears in a copyin clause must be threadprivate.
4256 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4257 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4258 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4259 continue;
4260 }
4261
4262 Decl *D = DE->getDecl();
4263 VarDecl *VD = cast<VarDecl>(D);
4264
4265 QualType Type = VD->getType();
4266 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4267 // It will be analyzed later.
4268 Vars.push_back(DE);
4269 continue;
4270 }
4271
4272 // OpenMP [2.14.4.2, Restrictions, p.2]
4273 // A list item that appears in a copyprivate clause may not appear in a
4274 // private or firstprivate clause on the single construct.
4275 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004276 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004277 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4278 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4279 Diag(ELoc, diag::err_omp_wrong_dsa)
4280 << getOpenMPClauseName(DVar.CKind)
4281 << getOpenMPClauseName(OMPC_copyprivate);
4282 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4283 continue;
4284 }
4285
4286 // OpenMP [2.11.4.2, Restrictions, p.1]
4287 // All list items that appear in a copyprivate clause must be either
4288 // threadprivate or private in the enclosing context.
4289 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004290 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004291 if (DVar.CKind == OMPC_shared) {
4292 Diag(ELoc, diag::err_omp_required_access)
4293 << getOpenMPClauseName(OMPC_copyprivate)
4294 << "threadprivate or private in the enclosing context";
4295 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4296 continue;
4297 }
4298 }
4299 }
4300
4301 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4302 // A variable of class type (or array thereof) that appears in a
4303 // copyin clause requires an accessible, unambiguous copy assignment
4304 // operator for the class type.
4305 Type = Context.getBaseElementType(Type);
4306 CXXRecordDecl *RD =
4307 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
4308 // FIXME This code must be replaced by actual assignment of the
4309 // threadprivate variable.
4310 if (RD) {
4311 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4312 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4313 if (MD) {
4314 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4315 MD->isDeleted()) {
4316 Diag(ELoc, diag::err_omp_required_method)
4317 << getOpenMPClauseName(OMPC_copyprivate) << 2;
4318 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4319 VarDecl::DeclarationOnly;
4320 Diag(VD->getLocation(),
4321 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4322 << VD;
4323 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4324 continue;
4325 }
4326 MarkFunctionReferenced(ELoc, MD);
4327 DiagnoseUseOfDecl(MD, ELoc);
4328 }
4329 }
4330
4331 // No need to mark vars as copyprivate, they are already threadprivate or
4332 // implicitly private.
4333 Vars.push_back(DE);
4334 }
4335
4336 if (Vars.empty())
4337 return nullptr;
4338
4339 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4340}
4341
Alexey Bataev6125da92014-07-21 11:26:11 +00004342OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
4343 SourceLocation StartLoc,
4344 SourceLocation LParenLoc,
4345 SourceLocation EndLoc) {
4346 if (VarList.empty())
4347 return nullptr;
4348
4349 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
4350}
Alexey Bataevdea47612014-07-23 07:46:59 +00004351