blob: ece5316187d0311415c0b6ab5706f785724bc607 [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 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001011 case OMPD_for_simd: {
1012 Sema::CapturedParamNameType Params[] = {
1013 std::make_pair(StringRef(), QualType()) // __context with shared vars
1014 };
1015 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1016 Params);
1017 break;
1018 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001019 case OMPD_sections: {
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 Bataevd3f8dd22014-06-25 11:44:49 +00001025 break;
1026 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001027 case OMPD_section: {
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 Bataev1e0498a2014-06-26 08:21:58 +00001033 break;
1034 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001035 case OMPD_single: {
1036 Sema::CapturedParamNameType Params[] = {
1037 std::make_pair(StringRef(), QualType()) // __context with shared vars
1038 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001039 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1040 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001041 break;
1042 }
Alexander Musman80c22892014-07-17 08:54:58 +00001043 case OMPD_master: {
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 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001051 case OMPD_critical: {
1052 Sema::CapturedParamNameType Params[] = {
1053 std::make_pair(StringRef(), QualType()) // __context with shared vars
1054 };
1055 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1056 Params);
1057 break;
1058 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001059 case OMPD_parallel_for: {
1060 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1061 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1062 Sema::CapturedParamNameType Params[] = {
1063 std::make_pair(".global_tid.", KmpInt32PtrTy),
1064 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1065 std::make_pair(StringRef(), QualType()) // __context with shared vars
1066 };
1067 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1068 Params);
1069 break;
1070 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001071 case OMPD_parallel_for_simd: {
1072 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1073 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1074 Sema::CapturedParamNameType Params[] = {
1075 std::make_pair(".global_tid.", KmpInt32PtrTy),
1076 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1077 std::make_pair(StringRef(), QualType()) // __context with shared vars
1078 };
1079 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1080 Params);
1081 break;
1082 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001083 case OMPD_parallel_sections: {
1084 Sema::CapturedParamNameType Params[] = {
1085 std::make_pair(StringRef(), QualType()) // __context with shared vars
1086 };
1087 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1088 Params);
1089 break;
1090 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001091 case OMPD_task: {
1092 Sema::CapturedParamNameType Params[] = {
1093 std::make_pair(StringRef(), QualType()) // __context with shared vars
1094 };
1095 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1096 Params);
1097 break;
1098 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001099 case OMPD_taskyield: {
1100 Sema::CapturedParamNameType Params[] = {
1101 std::make_pair(StringRef(), QualType()) // __context with shared vars
1102 };
1103 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1104 Params);
1105 break;
1106 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001107 case OMPD_barrier: {
1108 Sema::CapturedParamNameType Params[] = {
1109 std::make_pair(StringRef(), QualType()) // __context with shared vars
1110 };
1111 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1112 Params);
1113 break;
1114 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001115 case OMPD_taskwait: {
1116 Sema::CapturedParamNameType Params[] = {
1117 std::make_pair(StringRef(), QualType()) // __context with shared vars
1118 };
1119 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1120 Params);
1121 break;
1122 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001123 case OMPD_flush: {
1124 Sema::CapturedParamNameType Params[] = {
1125 std::make_pair(StringRef(), QualType()) // __context with shared vars
1126 };
1127 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1128 Params);
1129 break;
1130 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001131 case OMPD_ordered: {
1132 Sema::CapturedParamNameType Params[] = {
1133 std::make_pair(StringRef(), QualType()) // __context with shared vars
1134 };
1135 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1136 Params);
1137 break;
1138 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001139 case OMPD_atomic: {
1140 Sema::CapturedParamNameType Params[] = {
1141 std::make_pair(StringRef(), QualType()) // __context with shared vars
1142 };
1143 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1144 Params);
1145 break;
1146 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001147 case OMPD_target: {
1148 Sema::CapturedParamNameType Params[] = {
1149 std::make_pair(StringRef(), QualType()) // __context with shared vars
1150 };
1151 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1152 Params);
1153 break;
1154 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001155 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001156 llvm_unreachable("OpenMP Directive is not allowed");
1157 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001158 llvm_unreachable("Unknown OpenMP directive");
1159 }
1160}
1161
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001162static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1163 OpenMPDirectiveKind CurrentRegion,
1164 const DeclarationNameInfo &CurrentName,
1165 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001166 // Allowed nesting of constructs
1167 // +------------------+-----------------+------------------------------------+
1168 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1169 // +------------------+-----------------+------------------------------------+
1170 // | parallel | parallel | * |
1171 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001172 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001173 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001174 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001175 // | parallel | simd | * |
1176 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001177 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001178 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001179 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001180 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001181 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001182 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001183 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001184 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001185 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001186 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001187 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001188 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001189 // | parallel | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001190 // +------------------+-----------------+------------------------------------+
1191 // | for | parallel | * |
1192 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001193 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001194 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001195 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001196 // | for | simd | * |
1197 // | for | sections | + |
1198 // | for | section | + |
1199 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001200 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001201 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001202 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001203 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001204 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001205 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001206 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001207 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001208 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001209 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001210 // | for | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001211 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001212 // | master | parallel | * |
1213 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001214 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001215 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001216 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001217 // | master | simd | * |
1218 // | master | sections | + |
1219 // | master | section | + |
1220 // | master | single | + |
1221 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001222 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001223 // | master |parallel sections| * |
1224 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001225 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001226 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001227 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001228 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001229 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001230 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001231 // | master | target | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001232 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001233 // | critical | parallel | * |
1234 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001235 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001236 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001237 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001238 // | critical | simd | * |
1239 // | critical | sections | + |
1240 // | critical | section | + |
1241 // | critical | single | + |
1242 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001243 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001244 // | critical |parallel sections| * |
1245 // | critical | task | * |
1246 // | critical | taskyield | * |
1247 // | critical | barrier | + |
1248 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001249 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001250 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001251 // | critical | target | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001252 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001253 // | simd | parallel | |
1254 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001255 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001256 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001257 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001258 // | simd | simd | |
1259 // | simd | sections | |
1260 // | simd | section | |
1261 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001262 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001263 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001264 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001265 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001266 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001267 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001268 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001269 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001270 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001271 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001272 // | simd | target | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001273 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001274 // | for simd | parallel | |
1275 // | for simd | for | |
1276 // | for simd | for simd | |
1277 // | for simd | master | |
1278 // | for simd | critical | |
1279 // | for simd | simd | |
1280 // | for simd | sections | |
1281 // | for simd | section | |
1282 // | for simd | single | |
1283 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001284 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001285 // | for simd |parallel sections| |
1286 // | for simd | task | |
1287 // | for simd | taskyield | |
1288 // | for simd | barrier | |
1289 // | for simd | taskwait | |
1290 // | for simd | flush | |
1291 // | for simd | ordered | |
1292 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001293 // | for simd | target | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001294 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001295 // | parallel for simd| parallel | |
1296 // | parallel for simd| for | |
1297 // | parallel for simd| for simd | |
1298 // | parallel for simd| master | |
1299 // | parallel for simd| critical | |
1300 // | parallel for simd| simd | |
1301 // | parallel for simd| sections | |
1302 // | parallel for simd| section | |
1303 // | parallel for simd| single | |
1304 // | parallel for simd| parallel for | |
1305 // | parallel for simd|parallel for simd| |
1306 // | parallel for simd|parallel sections| |
1307 // | parallel for simd| task | |
1308 // | parallel for simd| taskyield | |
1309 // | parallel for simd| barrier | |
1310 // | parallel for simd| taskwait | |
1311 // | parallel for simd| flush | |
1312 // | parallel for simd| ordered | |
1313 // | parallel for simd| atomic | |
1314 // | parallel for simd| target | |
1315 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001316 // | sections | parallel | * |
1317 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001318 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001319 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001320 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001321 // | sections | simd | * |
1322 // | sections | sections | + |
1323 // | sections | section | * |
1324 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001325 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001326 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001327 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001328 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001329 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001330 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001331 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001332 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001333 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001334 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001335 // | sections | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001336 // +------------------+-----------------+------------------------------------+
1337 // | section | parallel | * |
1338 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001339 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001340 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001341 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001342 // | section | simd | * |
1343 // | section | sections | + |
1344 // | section | section | + |
1345 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001346 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001347 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001348 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001349 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001350 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001351 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001352 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001353 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001354 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001355 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001356 // | section | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001357 // +------------------+-----------------+------------------------------------+
1358 // | single | parallel | * |
1359 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001360 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001361 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001362 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001363 // | single | simd | * |
1364 // | single | sections | + |
1365 // | single | section | + |
1366 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001367 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001368 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001369 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001370 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001371 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001372 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001373 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001374 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001375 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001376 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001377 // | single | target | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001378 // +------------------+-----------------+------------------------------------+
1379 // | parallel for | parallel | * |
1380 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001381 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001382 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001383 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001384 // | parallel for | simd | * |
1385 // | parallel for | sections | + |
1386 // | parallel for | section | + |
1387 // | parallel for | single | + |
1388 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001389 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001390 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001391 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001392 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001393 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001394 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001395 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001396 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001397 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001398 // | parallel for | target | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001399 // +------------------+-----------------+------------------------------------+
1400 // | parallel sections| parallel | * |
1401 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001402 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001403 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001404 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001405 // | parallel sections| simd | * |
1406 // | parallel sections| sections | + |
1407 // | parallel sections| section | * |
1408 // | parallel sections| single | + |
1409 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001410 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001411 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001412 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001413 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001414 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001415 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001416 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001417 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001418 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001419 // | parallel sections| target | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001420 // +------------------+-----------------+------------------------------------+
1421 // | task | parallel | * |
1422 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001423 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001424 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001425 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001426 // | task | simd | * |
1427 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001428 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001429 // | task | single | + |
1430 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001431 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001432 // | task |parallel sections| * |
1433 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001434 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001435 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001436 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001437 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001438 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001439 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001440 // | task | target | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001441 // +------------------+-----------------+------------------------------------+
1442 // | ordered | parallel | * |
1443 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001444 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001445 // | ordered | master | * |
1446 // | ordered | critical | * |
1447 // | ordered | simd | * |
1448 // | ordered | sections | + |
1449 // | ordered | section | + |
1450 // | ordered | single | + |
1451 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001452 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001453 // | ordered |parallel sections| * |
1454 // | ordered | task | * |
1455 // | ordered | taskyield | * |
1456 // | ordered | barrier | + |
1457 // | ordered | taskwait | * |
1458 // | ordered | flush | * |
1459 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001460 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001461 // | ordered | target | * |
1462 // +------------------+-----------------+------------------------------------+
1463 // | atomic | parallel | |
1464 // | atomic | for | |
1465 // | atomic | for simd | |
1466 // | atomic | master | |
1467 // | atomic | critical | |
1468 // | atomic | simd | |
1469 // | atomic | sections | |
1470 // | atomic | section | |
1471 // | atomic | single | |
1472 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001473 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001474 // | atomic |parallel sections| |
1475 // | atomic | task | |
1476 // | atomic | taskyield | |
1477 // | atomic | barrier | |
1478 // | atomic | taskwait | |
1479 // | atomic | flush | |
1480 // | atomic | ordered | |
1481 // | atomic | atomic | |
1482 // | atomic | target | |
1483 // +------------------+-----------------+------------------------------------+
1484 // | target | parallel | * |
1485 // | target | for | * |
1486 // | target | for simd | * |
1487 // | target | master | * |
1488 // | target | critical | * |
1489 // | target | simd | * |
1490 // | target | sections | * |
1491 // | target | section | * |
1492 // | target | single | * |
1493 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001494 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001495 // | target |parallel sections| * |
1496 // | target | task | * |
1497 // | target | taskyield | * |
1498 // | target | barrier | * |
1499 // | target | taskwait | * |
1500 // | target | flush | * |
1501 // | target | ordered | * |
1502 // | target | atomic | * |
1503 // | target | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001504 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001505 if (Stack->getCurScope()) {
1506 auto ParentRegion = Stack->getParentDirective();
1507 bool NestingProhibited = false;
1508 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001509 enum {
1510 NoRecommend,
1511 ShouldBeInParallelRegion,
1512 ShouldBeInOrderedRegion
1513 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001514 if (isOpenMPSimdDirective(ParentRegion)) {
1515 // OpenMP [2.16, Nesting of Regions]
1516 // OpenMP constructs may not be nested inside a simd region.
1517 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1518 return true;
1519 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001520 if (ParentRegion == OMPD_atomic) {
1521 // OpenMP [2.16, Nesting of Regions]
1522 // OpenMP constructs may not be nested inside an atomic region.
1523 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1524 return true;
1525 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001526 if (CurrentRegion == OMPD_section) {
1527 // OpenMP [2.7.2, sections Construct, Restrictions]
1528 // Orphaned section directives are prohibited. That is, the section
1529 // directives must appear within the sections construct and must not be
1530 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001531 if (ParentRegion != OMPD_sections &&
1532 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001533 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1534 << (ParentRegion != OMPD_unknown)
1535 << getOpenMPDirectiveName(ParentRegion);
1536 return true;
1537 }
1538 return false;
1539 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001540 // Allow some constructs to be orphaned (they could be used in functions,
1541 // called from OpenMP regions with the required preconditions).
1542 if (ParentRegion == OMPD_unknown)
1543 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001544 if (CurrentRegion == OMPD_master) {
1545 // OpenMP [2.16, Nesting of Regions]
1546 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001547 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001548 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1549 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001550 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1551 // OpenMP [2.16, Nesting of Regions]
1552 // A critical region may not be nested (closely or otherwise) inside a
1553 // critical region with the same name. Note that this restriction is not
1554 // sufficient to prevent deadlock.
1555 SourceLocation PreviousCriticalLoc;
1556 bool DeadLock =
1557 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1558 OpenMPDirectiveKind K,
1559 const DeclarationNameInfo &DNI,
1560 SourceLocation Loc)
1561 ->bool {
1562 if (K == OMPD_critical &&
1563 DNI.getName() == CurrentName.getName()) {
1564 PreviousCriticalLoc = Loc;
1565 return true;
1566 } else
1567 return false;
1568 },
1569 false /* skip top directive */);
1570 if (DeadLock) {
1571 SemaRef.Diag(StartLoc,
1572 diag::err_omp_prohibited_region_critical_same_name)
1573 << CurrentName.getName();
1574 if (PreviousCriticalLoc.isValid())
1575 SemaRef.Diag(PreviousCriticalLoc,
1576 diag::note_omp_previous_critical_region);
1577 return true;
1578 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001579 } else if (CurrentRegion == OMPD_barrier) {
1580 // OpenMP [2.16, Nesting of Regions]
1581 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001582 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001583 NestingProhibited =
1584 isOpenMPWorksharingDirective(ParentRegion) ||
1585 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1586 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001587 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001588 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001589 // OpenMP [2.16, Nesting of Regions]
1590 // A worksharing region may not be closely nested inside a worksharing,
1591 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001592 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001593 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001594 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1595 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1596 Recommend = ShouldBeInParallelRegion;
1597 } else if (CurrentRegion == OMPD_ordered) {
1598 // OpenMP [2.16, Nesting of Regions]
1599 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001600 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001601 // An ordered region must be closely nested inside a loop region (or
1602 // parallel loop region) with an ordered clause.
1603 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001604 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001605 !Stack->isParentOrderedRegion();
1606 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001607 }
1608 if (NestingProhibited) {
1609 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001610 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1611 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001612 return true;
1613 }
1614 }
1615 return false;
1616}
1617
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001618StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001619 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001620 ArrayRef<OMPClause *> Clauses,
1621 Stmt *AStmt,
1622 SourceLocation StartLoc,
1623 SourceLocation EndLoc) {
1624 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001625 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001626 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001627
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001628 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001629 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001630 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001631 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001632 if (AStmt) {
1633 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1634
1635 // Check default data sharing attributes for referenced variables.
1636 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1637 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1638 if (DSAChecker.isErrorFound())
1639 return StmtError();
1640 // Generate list of implicitly defined firstprivate variables.
1641 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001642
1643 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1644 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1645 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1646 SourceLocation(), SourceLocation())) {
1647 ClausesWithImplicit.push_back(Implicit);
1648 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1649 DSAChecker.getImplicitFirstprivate().size();
1650 } else
1651 ErrorFound = true;
1652 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001653 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001654
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001655 switch (Kind) {
1656 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001657 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1658 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001659 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001660 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001661 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1662 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001663 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001664 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001665 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1666 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001667 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001668 case OMPD_for_simd:
1669 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1670 EndLoc, VarsWithInheritedDSA);
1671 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001672 case OMPD_sections:
1673 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1674 EndLoc);
1675 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001676 case OMPD_section:
1677 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001678 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001679 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1680 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001681 case OMPD_single:
1682 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1683 EndLoc);
1684 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001685 case OMPD_master:
1686 assert(ClausesWithImplicit.empty() &&
1687 "No clauses are allowed for 'omp master' directive");
1688 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1689 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001690 case OMPD_critical:
1691 assert(ClausesWithImplicit.empty() &&
1692 "No clauses are allowed for 'omp critical' directive");
1693 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1694 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001695 case OMPD_parallel_for:
1696 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1697 EndLoc, VarsWithInheritedDSA);
1698 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001699 case OMPD_parallel_for_simd:
1700 Res = ActOnOpenMPParallelForSimdDirective(
1701 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1702 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001703 case OMPD_parallel_sections:
1704 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1705 StartLoc, EndLoc);
1706 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001707 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001708 Res =
1709 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1710 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001711 case OMPD_taskyield:
1712 assert(ClausesWithImplicit.empty() &&
1713 "No clauses are allowed for 'omp taskyield' directive");
1714 assert(AStmt == nullptr &&
1715 "No associated statement allowed for 'omp taskyield' directive");
1716 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1717 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001718 case OMPD_barrier:
1719 assert(ClausesWithImplicit.empty() &&
1720 "No clauses are allowed for 'omp barrier' directive");
1721 assert(AStmt == nullptr &&
1722 "No associated statement allowed for 'omp barrier' directive");
1723 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1724 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001725 case OMPD_taskwait:
1726 assert(ClausesWithImplicit.empty() &&
1727 "No clauses are allowed for 'omp taskwait' directive");
1728 assert(AStmt == nullptr &&
1729 "No associated statement allowed for 'omp taskwait' directive");
1730 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1731 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001732 case OMPD_flush:
1733 assert(AStmt == nullptr &&
1734 "No associated statement allowed for 'omp flush' directive");
1735 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1736 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001737 case OMPD_ordered:
1738 assert(ClausesWithImplicit.empty() &&
1739 "No clauses are allowed for 'omp ordered' directive");
1740 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1741 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001742 case OMPD_atomic:
1743 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1744 EndLoc);
1745 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001746 case OMPD_target:
1747 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1748 EndLoc);
1749 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001750 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001751 llvm_unreachable("OpenMP Directive is not allowed");
1752 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001753 llvm_unreachable("Unknown OpenMP directive");
1754 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001755
Alexey Bataev4acb8592014-07-07 13:01:15 +00001756 for (auto P : VarsWithInheritedDSA) {
1757 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1758 << P.first << P.second->getSourceRange();
1759 }
1760 if (!VarsWithInheritedDSA.empty())
1761 return StmtError();
1762
Alexey Bataeved09d242014-05-28 05:53:51 +00001763 if (ErrorFound)
1764 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001765 return Res;
1766}
1767
1768StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1769 Stmt *AStmt,
1770 SourceLocation StartLoc,
1771 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001772 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1773 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1774 // 1.2.2 OpenMP Language Terminology
1775 // Structured block - An executable statement with a single entry at the
1776 // top and a single exit at the bottom.
1777 // The point of exit cannot be a branch out of the structured block.
1778 // longjmp() and throw() must not violate the entry/exit criteria.
1779 CS->getCapturedDecl()->setNothrow();
1780
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001781 getCurFunction()->setHasBranchProtectedScope();
1782
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001783 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1784 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001785}
1786
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001787namespace {
1788/// \brief Helper class for checking canonical form of the OpenMP loops and
1789/// extracting iteration space of each loop in the loop nest, that will be used
1790/// for IR generation.
1791class OpenMPIterationSpaceChecker {
1792 /// \brief Reference to Sema.
1793 Sema &SemaRef;
1794 /// \brief A location for diagnostics (when there is no some better location).
1795 SourceLocation DefaultLoc;
1796 /// \brief A location for diagnostics (when increment is not compatible).
1797 SourceLocation ConditionLoc;
1798 /// \brief A source location for referring to condition later.
1799 SourceRange ConditionSrcRange;
1800 /// \brief Loop variable.
1801 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001802 /// \brief Reference to loop variable.
1803 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001804 /// \brief Lower bound (initializer for the var).
1805 Expr *LB;
1806 /// \brief Upper bound.
1807 Expr *UB;
1808 /// \brief Loop step (increment).
1809 Expr *Step;
1810 /// \brief This flag is true when condition is one of:
1811 /// Var < UB
1812 /// Var <= UB
1813 /// UB > Var
1814 /// UB >= Var
1815 bool TestIsLessOp;
1816 /// \brief This flag is true when condition is strict ( < or > ).
1817 bool TestIsStrictOp;
1818 /// \brief This flag is true when step is subtracted on each iteration.
1819 bool SubtractStep;
1820
1821public:
1822 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1823 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001824 ConditionSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
1825 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1826 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001827 /// \brief Check init-expr for canonical loop form and save loop counter
1828 /// variable - #Var and its initialization value - #LB.
1829 bool CheckInit(Stmt *S);
1830 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1831 /// for less/greater and for strict/non-strict comparison.
1832 bool CheckCond(Expr *S);
1833 /// \brief Check incr-expr for canonical loop form and return true if it
1834 /// does not conform, otherwise save loop step (#Step).
1835 bool CheckInc(Expr *S);
1836 /// \brief Return the loop counter variable.
1837 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001838 /// \brief Return the reference expression to loop counter variable.
1839 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001840 /// \brief Return true if any expression is dependent.
1841 bool Dependent() const;
1842
1843private:
1844 /// \brief Check the right-hand side of an assignment in the increment
1845 /// expression.
1846 bool CheckIncRHS(Expr *RHS);
1847 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001848 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001849 /// \brief Helper to set upper bound.
1850 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1851 const SourceLocation &SL);
1852 /// \brief Helper to set loop increment.
1853 bool SetStep(Expr *NewStep, bool Subtract);
1854};
1855
1856bool OpenMPIterationSpaceChecker::Dependent() const {
1857 if (!Var) {
1858 assert(!LB && !UB && !Step);
1859 return false;
1860 }
1861 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1862 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1863}
1864
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001865bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1866 DeclRefExpr *NewVarRefExpr,
1867 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001868 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001869 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1870 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001871 if (!NewVar || !NewLB)
1872 return true;
1873 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001874 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001875 LB = NewLB;
1876 return false;
1877}
1878
1879bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1880 const SourceRange &SR,
1881 const SourceLocation &SL) {
1882 // State consistency checking to ensure correct usage.
1883 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1884 !TestIsLessOp && !TestIsStrictOp);
1885 if (!NewUB)
1886 return true;
1887 UB = NewUB;
1888 TestIsLessOp = LessOp;
1889 TestIsStrictOp = StrictOp;
1890 ConditionSrcRange = SR;
1891 ConditionLoc = SL;
1892 return false;
1893}
1894
1895bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1896 // State consistency checking to ensure correct usage.
1897 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1898 if (!NewStep)
1899 return true;
1900 if (!NewStep->isValueDependent()) {
1901 // Check that the step is integer expression.
1902 SourceLocation StepLoc = NewStep->getLocStart();
1903 ExprResult Val =
1904 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1905 if (Val.isInvalid())
1906 return true;
1907 NewStep = Val.get();
1908
1909 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1910 // If test-expr is of form var relational-op b and relational-op is < or
1911 // <= then incr-expr must cause var to increase on each iteration of the
1912 // loop. If test-expr is of form var relational-op b and relational-op is
1913 // > or >= then incr-expr must cause var to decrease on each iteration of
1914 // the loop.
1915 // If test-expr is of form b relational-op var and relational-op is < or
1916 // <= then incr-expr must cause var to decrease on each iteration of the
1917 // loop. If test-expr is of form b relational-op var and relational-op is
1918 // > or >= then incr-expr must cause var to increase on each iteration of
1919 // the loop.
1920 llvm::APSInt Result;
1921 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1922 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1923 bool IsConstNeg =
1924 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1925 bool IsConstZero = IsConstant && !Result.getBoolValue();
1926 if (UB && (IsConstZero ||
1927 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1928 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1929 SemaRef.Diag(NewStep->getExprLoc(),
1930 diag::err_omp_loop_incr_not_compatible)
1931 << Var << TestIsLessOp << NewStep->getSourceRange();
1932 SemaRef.Diag(ConditionLoc,
1933 diag::note_omp_loop_cond_requres_compatible_incr)
1934 << TestIsLessOp << ConditionSrcRange;
1935 return true;
1936 }
1937 }
1938
1939 Step = NewStep;
1940 SubtractStep = Subtract;
1941 return false;
1942}
1943
1944bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1945 // Check init-expr for canonical loop form and save loop counter
1946 // variable - #Var and its initialization value - #LB.
1947 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1948 // var = lb
1949 // integer-type var = lb
1950 // random-access-iterator-type var = lb
1951 // pointer-type var = lb
1952 //
1953 if (!S) {
1954 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1955 return true;
1956 }
1957 if (Expr *E = dyn_cast<Expr>(S))
1958 S = E->IgnoreParens();
1959 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1960 if (BO->getOpcode() == BO_Assign)
1961 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001962 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1963 BO->getLHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001964 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1965 if (DS->isSingleDecl()) {
1966 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1967 if (Var->hasInit()) {
1968 // Accept non-canonical init form here but emit ext. warning.
1969 if (Var->getInitStyle() != VarDecl::CInit)
1970 SemaRef.Diag(S->getLocStart(),
1971 diag::ext_omp_loop_not_canonical_init)
1972 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001973 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001974 }
1975 }
1976 }
1977 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1978 if (CE->getOperator() == OO_Equal)
1979 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001980 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1981 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001982
1983 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1984 << S->getSourceRange();
1985 return true;
1986}
1987
Alexey Bataev23b69422014-06-18 07:08:49 +00001988/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001989/// variable (which may be the loop variable) if possible.
1990static const VarDecl *GetInitVarDecl(const Expr *E) {
1991 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001992 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001993 E = E->IgnoreParenImpCasts();
1994 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1995 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1996 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1997 CE->getArg(0) != nullptr)
1998 E = CE->getArg(0)->IgnoreParenImpCasts();
1999 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2000 if (!DRE)
2001 return nullptr;
2002 return dyn_cast<VarDecl>(DRE->getDecl());
2003}
2004
2005bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2006 // Check test-expr for canonical form, save upper-bound UB, flags for
2007 // less/greater and for strict/non-strict comparison.
2008 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2009 // var relational-op b
2010 // b relational-op var
2011 //
2012 if (!S) {
2013 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2014 return true;
2015 }
2016 S = S->IgnoreParenImpCasts();
2017 SourceLocation CondLoc = S->getLocStart();
2018 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2019 if (BO->isRelationalOp()) {
2020 if (GetInitVarDecl(BO->getLHS()) == Var)
2021 return SetUB(BO->getRHS(),
2022 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2023 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2024 BO->getSourceRange(), BO->getOperatorLoc());
2025 if (GetInitVarDecl(BO->getRHS()) == Var)
2026 return SetUB(BO->getLHS(),
2027 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2028 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2029 BO->getSourceRange(), BO->getOperatorLoc());
2030 }
2031 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2032 if (CE->getNumArgs() == 2) {
2033 auto Op = CE->getOperator();
2034 switch (Op) {
2035 case OO_Greater:
2036 case OO_GreaterEqual:
2037 case OO_Less:
2038 case OO_LessEqual:
2039 if (GetInitVarDecl(CE->getArg(0)) == Var)
2040 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2041 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2042 CE->getOperatorLoc());
2043 if (GetInitVarDecl(CE->getArg(1)) == Var)
2044 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2045 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2046 CE->getOperatorLoc());
2047 break;
2048 default:
2049 break;
2050 }
2051 }
2052 }
2053 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2054 << S->getSourceRange() << Var;
2055 return true;
2056}
2057
2058bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2059 // RHS of canonical loop form increment can be:
2060 // var + incr
2061 // incr + var
2062 // var - incr
2063 //
2064 RHS = RHS->IgnoreParenImpCasts();
2065 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2066 if (BO->isAdditiveOp()) {
2067 bool IsAdd = BO->getOpcode() == BO_Add;
2068 if (GetInitVarDecl(BO->getLHS()) == Var)
2069 return SetStep(BO->getRHS(), !IsAdd);
2070 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2071 return SetStep(BO->getLHS(), false);
2072 }
2073 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2074 bool IsAdd = CE->getOperator() == OO_Plus;
2075 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2076 if (GetInitVarDecl(CE->getArg(0)) == Var)
2077 return SetStep(CE->getArg(1), !IsAdd);
2078 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2079 return SetStep(CE->getArg(0), false);
2080 }
2081 }
2082 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2083 << RHS->getSourceRange() << Var;
2084 return true;
2085}
2086
2087bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2088 // Check incr-expr for canonical loop form and return true if it
2089 // does not conform.
2090 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2091 // ++var
2092 // var++
2093 // --var
2094 // var--
2095 // var += incr
2096 // var -= incr
2097 // var = var + incr
2098 // var = incr + var
2099 // var = var - incr
2100 //
2101 if (!S) {
2102 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2103 return true;
2104 }
2105 S = S->IgnoreParens();
2106 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2107 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2108 return SetStep(
2109 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2110 (UO->isDecrementOp() ? -1 : 1)).get(),
2111 false);
2112 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2113 switch (BO->getOpcode()) {
2114 case BO_AddAssign:
2115 case BO_SubAssign:
2116 if (GetInitVarDecl(BO->getLHS()) == Var)
2117 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2118 break;
2119 case BO_Assign:
2120 if (GetInitVarDecl(BO->getLHS()) == Var)
2121 return CheckIncRHS(BO->getRHS());
2122 break;
2123 default:
2124 break;
2125 }
2126 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2127 switch (CE->getOperator()) {
2128 case OO_PlusPlus:
2129 case OO_MinusMinus:
2130 if (GetInitVarDecl(CE->getArg(0)) == Var)
2131 return SetStep(
2132 SemaRef.ActOnIntegerConstant(
2133 CE->getLocStart(),
2134 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2135 false);
2136 break;
2137 case OO_PlusEqual:
2138 case OO_MinusEqual:
2139 if (GetInitVarDecl(CE->getArg(0)) == Var)
2140 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2141 break;
2142 case OO_Equal:
2143 if (GetInitVarDecl(CE->getArg(0)) == Var)
2144 return CheckIncRHS(CE->getArg(1));
2145 break;
2146 default:
2147 break;
2148 }
2149 }
2150 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2151 << S->getSourceRange() << Var;
2152 return true;
2153}
Alexey Bataev23b69422014-06-18 07:08:49 +00002154} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002155
2156/// \brief Called on a for stmt to check and extract its iteration space
2157/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002158static bool CheckOpenMPIterationSpace(
2159 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2160 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2161 Expr *NestedLoopCountExpr,
2162 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002163 // OpenMP [2.6, Canonical Loop Form]
2164 // for (init-expr; test-expr; incr-expr) structured-block
2165 auto For = dyn_cast_or_null<ForStmt>(S);
2166 if (!For) {
2167 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002168 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2169 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2170 << CurrentNestedLoopCount;
2171 if (NestedLoopCount > 1)
2172 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2173 diag::note_omp_collapse_expr)
2174 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002175 return true;
2176 }
2177 assert(For->getBody());
2178
2179 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2180
2181 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002182 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002183 if (ISC.CheckInit(Init)) {
2184 return true;
2185 }
2186
2187 bool HasErrors = false;
2188
2189 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002190 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002191
2192 // OpenMP [2.6, Canonical Loop Form]
2193 // Var is one of the following:
2194 // A variable of signed or unsigned integer type.
2195 // For C++, a variable of a random access iterator type.
2196 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002197 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002198 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2199 !VarType->isPointerType() &&
2200 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2201 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2202 << SemaRef.getLangOpts().CPlusPlus;
2203 HasErrors = true;
2204 }
2205
Alexey Bataev4acb8592014-07-07 13:01:15 +00002206 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2207 // Construct
2208 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2209 // parallel for construct is (are) private.
2210 // The loop iteration variable in the associated for-loop of a simd construct
2211 // with just one associated for-loop is linear with a constant-linear-step
2212 // that is the increment of the associated for-loop.
2213 // Exclude loop var from the list of variables with implicitly defined data
2214 // sharing attributes.
2215 while (VarsWithImplicitDSA.count(Var) > 0)
2216 VarsWithImplicitDSA.erase(Var);
2217
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002218 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2219 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002220 // The loop iteration variable in the associated for-loop of a simd construct
2221 // with just one associated for-loop may be listed in a linear clause with a
2222 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002223 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2224 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002225 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002226 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2227 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2228 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002229 auto PredeterminedCKind =
2230 isOpenMPSimdDirective(DKind)
2231 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2232 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002233 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002234 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002235 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2236 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2237 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002238 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002239 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002240 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2241 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002242 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002243 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002244 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002245 // Make the loop iteration variable private (for worksharing constructs),
2246 // linear (for simd directives with the only one associated loop) or
2247 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002248 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002249 }
2250
Alexey Bataev7ff55242014-06-19 09:13:45 +00002251 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002252
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002253 // Check test-expr.
2254 HasErrors |= ISC.CheckCond(For->getCond());
2255
2256 // Check incr-expr.
2257 HasErrors |= ISC.CheckInc(For->getInc());
2258
2259 if (ISC.Dependent())
2260 return HasErrors;
2261
2262 // FIXME: Build loop's iteration space representation.
2263 return HasErrors;
2264}
2265
2266/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
2267/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
2268/// to get the first for loop.
2269static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
2270 if (IgnoreCaptured)
2271 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
2272 S = CapS->getCapturedStmt();
2273 // OpenMP [2.8.1, simd construct, Restrictions]
2274 // All loops associated with the construct must be perfectly nested; that is,
2275 // there must be no intervening code nor any OpenMP directive between any two
2276 // loops.
2277 while (true) {
2278 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
2279 S = AS->getSubStmt();
2280 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
2281 if (CS->size() != 1)
2282 break;
2283 S = CS->body_back();
2284 } else
2285 break;
2286 }
2287 return S;
2288}
2289
2290/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002291/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2292/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002293static unsigned
2294CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2295 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2296 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002297 unsigned NestedLoopCount = 1;
2298 if (NestedLoopCountExpr) {
2299 // Found 'collapse' clause - calculate collapse number.
2300 llvm::APSInt Result;
2301 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2302 NestedLoopCount = Result.getLimitedValue();
2303 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002304 // This is helper routine for loop directives (e.g., 'for', 'simd',
2305 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002306 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
2307 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002308 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002309 NestedLoopCount, NestedLoopCountExpr,
2310 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002311 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002312 // Move on to the next nested for loop, or to the loop body.
2313 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
2314 }
2315
2316 // FIXME: Build resulting iteration space for IR generation (collapsing
2317 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002318 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002319}
2320
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002321static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002322 auto CollapseFilter = [](const OMPClause *C) -> bool {
2323 return C->getClauseKind() == OMPC_collapse;
2324 };
2325 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2326 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002327 if (I)
2328 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2329 return nullptr;
2330}
2331
Alexey Bataev4acb8592014-07-07 13:01:15 +00002332StmtResult Sema::ActOnOpenMPSimdDirective(
2333 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2334 SourceLocation EndLoc,
2335 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002336 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002337 unsigned NestedLoopCount =
2338 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2339 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002340 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002341 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002342
2343 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002344 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2345 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002346}
2347
Alexey Bataev4acb8592014-07-07 13:01:15 +00002348StmtResult Sema::ActOnOpenMPForDirective(
2349 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2350 SourceLocation EndLoc,
2351 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002352 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002353 unsigned NestedLoopCount =
2354 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2355 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002356 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002357 return StmtError();
2358
2359 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002360 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2361 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002362}
2363
Alexander Musmanf82886e2014-09-18 05:12:34 +00002364StmtResult Sema::ActOnOpenMPForSimdDirective(
2365 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2366 SourceLocation EndLoc,
2367 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2368 // In presence of clause 'collapse', it will define the nested loops number.
2369 unsigned NestedLoopCount =
2370 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
2371 *this, *DSAStack, VarsWithImplicitDSA);
2372 if (NestedLoopCount == 0)
2373 return StmtError();
2374
2375 getCurFunction()->setHasBranchProtectedScope();
2376 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2377 Clauses, AStmt);
2378}
2379
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002380StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2381 Stmt *AStmt,
2382 SourceLocation StartLoc,
2383 SourceLocation EndLoc) {
2384 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2385 auto BaseStmt = AStmt;
2386 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2387 BaseStmt = CS->getCapturedStmt();
2388 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2389 auto S = C->children();
2390 if (!S)
2391 return StmtError();
2392 // All associated statements must be '#pragma omp section' except for
2393 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002394 for (++S; S; ++S) {
2395 auto SectionStmt = *S;
2396 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2397 if (SectionStmt)
2398 Diag(SectionStmt->getLocStart(),
2399 diag::err_omp_sections_substmt_not_section);
2400 return StmtError();
2401 }
2402 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002403 } else {
2404 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2405 return StmtError();
2406 }
2407
2408 getCurFunction()->setHasBranchProtectedScope();
2409
2410 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2411 AStmt);
2412}
2413
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002414StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2415 SourceLocation StartLoc,
2416 SourceLocation EndLoc) {
2417 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2418
2419 getCurFunction()->setHasBranchProtectedScope();
2420
2421 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2422}
2423
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002424StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2425 Stmt *AStmt,
2426 SourceLocation StartLoc,
2427 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002428 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2429
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002430 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002431
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002432 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2433}
2434
Alexander Musman80c22892014-07-17 08:54:58 +00002435StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2436 SourceLocation StartLoc,
2437 SourceLocation EndLoc) {
2438 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2439
2440 getCurFunction()->setHasBranchProtectedScope();
2441
2442 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2443}
2444
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002445StmtResult
2446Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2447 Stmt *AStmt, SourceLocation StartLoc,
2448 SourceLocation EndLoc) {
2449 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2450
2451 getCurFunction()->setHasBranchProtectedScope();
2452
2453 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2454 AStmt);
2455}
2456
Alexey Bataev4acb8592014-07-07 13:01:15 +00002457StmtResult Sema::ActOnOpenMPParallelForDirective(
2458 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2459 SourceLocation EndLoc,
2460 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2461 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2462 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2463 // 1.2.2 OpenMP Language Terminology
2464 // Structured block - An executable statement with a single entry at the
2465 // top and a single exit at the bottom.
2466 // The point of exit cannot be a branch out of the structured block.
2467 // longjmp() and throw() must not violate the entry/exit criteria.
2468 CS->getCapturedDecl()->setNothrow();
2469
2470 // In presence of clause 'collapse', it will define the nested loops number.
2471 unsigned NestedLoopCount =
2472 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2473 *this, *DSAStack, VarsWithImplicitDSA);
2474 if (NestedLoopCount == 0)
2475 return StmtError();
2476
2477 getCurFunction()->setHasBranchProtectedScope();
2478 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2479 NestedLoopCount, Clauses, AStmt);
2480}
2481
Alexander Musmane4e893b2014-09-23 09:33:00 +00002482StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
2483 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2484 SourceLocation EndLoc,
2485 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2486 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2487 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2488 // 1.2.2 OpenMP Language Terminology
2489 // Structured block - An executable statement with a single entry at the
2490 // top and a single exit at the bottom.
2491 // The point of exit cannot be a branch out of the structured block.
2492 // longjmp() and throw() must not violate the entry/exit criteria.
2493 CS->getCapturedDecl()->setNothrow();
2494
2495 // In presence of clause 'collapse', it will define the nested loops number.
2496 unsigned NestedLoopCount =
2497 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
2498 AStmt, *this, *DSAStack, VarsWithImplicitDSA);
2499 if (NestedLoopCount == 0)
2500 return StmtError();
2501
2502 getCurFunction()->setHasBranchProtectedScope();
2503 return OMPParallelForSimdDirective::Create(Context, StartLoc, EndLoc,
2504 NestedLoopCount, Clauses, AStmt);
2505}
2506
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002507StmtResult
2508Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2509 Stmt *AStmt, SourceLocation StartLoc,
2510 SourceLocation EndLoc) {
2511 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2512 auto BaseStmt = AStmt;
2513 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2514 BaseStmt = CS->getCapturedStmt();
2515 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2516 auto S = C->children();
2517 if (!S)
2518 return StmtError();
2519 // All associated statements must be '#pragma omp section' except for
2520 // the first one.
2521 for (++S; S; ++S) {
2522 auto SectionStmt = *S;
2523 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2524 if (SectionStmt)
2525 Diag(SectionStmt->getLocStart(),
2526 diag::err_omp_parallel_sections_substmt_not_section);
2527 return StmtError();
2528 }
2529 }
2530 } else {
2531 Diag(AStmt->getLocStart(),
2532 diag::err_omp_parallel_sections_not_compound_stmt);
2533 return StmtError();
2534 }
2535
2536 getCurFunction()->setHasBranchProtectedScope();
2537
2538 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2539 Clauses, AStmt);
2540}
2541
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002542StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2543 Stmt *AStmt, SourceLocation StartLoc,
2544 SourceLocation EndLoc) {
2545 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2546 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2547 // 1.2.2 OpenMP Language Terminology
2548 // Structured block - An executable statement with a single entry at the
2549 // top and a single exit at the bottom.
2550 // The point of exit cannot be a branch out of the structured block.
2551 // longjmp() and throw() must not violate the entry/exit criteria.
2552 CS->getCapturedDecl()->setNothrow();
2553
2554 getCurFunction()->setHasBranchProtectedScope();
2555
2556 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2557}
2558
Alexey Bataev68446b72014-07-18 07:47:19 +00002559StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2560 SourceLocation EndLoc) {
2561 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2562}
2563
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002564StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
2565 SourceLocation EndLoc) {
2566 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
2567}
2568
Alexey Bataev2df347a2014-07-18 10:17:07 +00002569StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
2570 SourceLocation EndLoc) {
2571 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
2572}
2573
Alexey Bataev6125da92014-07-21 11:26:11 +00002574StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
2575 SourceLocation StartLoc,
2576 SourceLocation EndLoc) {
2577 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
2578 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
2579}
2580
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002581StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
2582 SourceLocation StartLoc,
2583 SourceLocation EndLoc) {
2584 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2585
2586 getCurFunction()->setHasBranchProtectedScope();
2587
2588 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
2589}
2590
Alexey Bataev0162e452014-07-22 10:10:35 +00002591StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
2592 Stmt *AStmt,
2593 SourceLocation StartLoc,
2594 SourceLocation EndLoc) {
2595 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002596 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00002597 // 1.2.2 OpenMP Language Terminology
2598 // Structured block - An executable statement with a single entry at the
2599 // top and a single exit at the bottom.
2600 // The point of exit cannot be a branch out of the structured block.
2601 // longjmp() and throw() must not violate the entry/exit criteria.
2602 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00002603 OpenMPClauseKind AtomicKind = OMPC_unknown;
2604 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002605 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00002606 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00002607 C->getClauseKind() == OMPC_update ||
2608 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00002609 if (AtomicKind != OMPC_unknown) {
2610 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
2611 << SourceRange(C->getLocStart(), C->getLocEnd());
2612 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
2613 << getOpenMPClauseName(AtomicKind);
2614 } else {
2615 AtomicKind = C->getClauseKind();
2616 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002617 }
2618 }
2619 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002620 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00002621 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002622 if (!isa<Expr>(Body)) {
2623 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002624 diag::err_omp_atomic_read_not_expression_statement);
2625 return StmtError();
2626 }
2627 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002628 if (!isa<Expr>(Body)) {
2629 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002630 diag::err_omp_atomic_write_not_expression_statement);
2631 return StmtError();
2632 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00002633 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002634 if (!isa<Expr>(Body)) {
2635 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00002636 diag::err_omp_atomic_update_not_expression_statement)
2637 << (AtomicKind == OMPC_update);
2638 return StmtError();
2639 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002640 } else if (AtomicKind == OMPC_capture) {
2641 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
2642 Diag(Body->getLocStart(),
2643 diag::err_omp_atomic_capture_not_expression_statement);
2644 return StmtError();
2645 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
2646 Diag(Body->getLocStart(),
2647 diag::err_omp_atomic_capture_not_compound_statement);
2648 return StmtError();
2649 }
Alexey Bataevdea47612014-07-23 07:46:59 +00002650 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002651
2652 getCurFunction()->setHasBranchProtectedScope();
2653
2654 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2655}
2656
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002657StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
2658 Stmt *AStmt,
2659 SourceLocation StartLoc,
2660 SourceLocation EndLoc) {
2661 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2662
2663 getCurFunction()->setHasBranchProtectedScope();
2664
2665 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2666}
2667
Alexey Bataeved09d242014-05-28 05:53:51 +00002668OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002669 SourceLocation StartLoc,
2670 SourceLocation LParenLoc,
2671 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002672 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002673 switch (Kind) {
2674 case OMPC_if:
2675 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2676 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002677 case OMPC_final:
2678 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2679 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002680 case OMPC_num_threads:
2681 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2682 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002683 case OMPC_safelen:
2684 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2685 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002686 case OMPC_collapse:
2687 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2688 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002689 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002690 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002691 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002692 case OMPC_private:
2693 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002694 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002695 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002696 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002697 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002698 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002699 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002700 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002701 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002702 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002703 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002704 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002705 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002706 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002707 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002708 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002709 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002710 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002711 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002712 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002713 llvm_unreachable("Clause is not allowed.");
2714 }
2715 return Res;
2716}
2717
Alexey Bataeved09d242014-05-28 05:53:51 +00002718OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002719 SourceLocation LParenLoc,
2720 SourceLocation EndLoc) {
2721 Expr *ValExpr = Condition;
2722 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2723 !Condition->isInstantiationDependent() &&
2724 !Condition->containsUnexpandedParameterPack()) {
2725 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002726 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002727 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002728 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002729
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002730 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002731 }
2732
2733 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2734}
2735
Alexey Bataev3778b602014-07-17 07:32:53 +00002736OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2737 SourceLocation StartLoc,
2738 SourceLocation LParenLoc,
2739 SourceLocation EndLoc) {
2740 Expr *ValExpr = Condition;
2741 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2742 !Condition->isInstantiationDependent() &&
2743 !Condition->containsUnexpandedParameterPack()) {
2744 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2745 Condition->getExprLoc(), Condition);
2746 if (Val.isInvalid())
2747 return nullptr;
2748
2749 ValExpr = Val.get();
2750 }
2751
2752 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2753}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002754ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2755 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002756 if (!Op)
2757 return ExprError();
2758
2759 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2760 public:
2761 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002762 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002763 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2764 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002765 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2766 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002767 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2768 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002769 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2770 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002771 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2772 QualType T,
2773 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002774 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2775 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002776 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2777 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002778 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002779 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002780 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002781 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2782 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002783 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2784 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002785 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2786 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002787 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002788 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002789 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002790 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2791 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002792 llvm_unreachable("conversion functions are permitted");
2793 }
2794 } ConvertDiagnoser;
2795 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2796}
2797
2798OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2799 SourceLocation StartLoc,
2800 SourceLocation LParenLoc,
2801 SourceLocation EndLoc) {
2802 Expr *ValExpr = NumThreads;
2803 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2804 !NumThreads->isInstantiationDependent() &&
2805 !NumThreads->containsUnexpandedParameterPack()) {
2806 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2807 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002808 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002809 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002810 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002811
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002812 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002813
2814 // OpenMP [2.5, Restrictions]
2815 // The num_threads expression must evaluate to a positive integer value.
2816 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002817 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2818 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002819 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2820 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002821 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002822 }
2823 }
2824
Alexey Bataeved09d242014-05-28 05:53:51 +00002825 return new (Context)
2826 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002827}
2828
Alexey Bataev62c87d22014-03-21 04:51:18 +00002829ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2830 OpenMPClauseKind CKind) {
2831 if (!E)
2832 return ExprError();
2833 if (E->isValueDependent() || E->isTypeDependent() ||
2834 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002835 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002836 llvm::APSInt Result;
2837 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2838 if (ICE.isInvalid())
2839 return ExprError();
2840 if (!Result.isStrictlyPositive()) {
2841 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2842 << getOpenMPClauseName(CKind) << E->getSourceRange();
2843 return ExprError();
2844 }
2845 return ICE;
2846}
2847
2848OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2849 SourceLocation LParenLoc,
2850 SourceLocation EndLoc) {
2851 // OpenMP [2.8.1, simd construct, Description]
2852 // The parameter of the safelen clause must be a constant
2853 // positive integer expression.
2854 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2855 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002856 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002857 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002858 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002859}
2860
Alexander Musman64d33f12014-06-04 07:53:32 +00002861OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2862 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002863 SourceLocation LParenLoc,
2864 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002865 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002866 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002867 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002868 // The parameter of the collapse clause must be a constant
2869 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002870 ExprResult NumForLoopsResult =
2871 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2872 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002873 return nullptr;
2874 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002875 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002876}
2877
Alexey Bataeved09d242014-05-28 05:53:51 +00002878OMPClause *Sema::ActOnOpenMPSimpleClause(
2879 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2880 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002881 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002882 switch (Kind) {
2883 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002884 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002885 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2886 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002887 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002888 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002889 Res = ActOnOpenMPProcBindClause(
2890 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2891 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002892 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002893 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002894 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002895 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002896 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002897 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002898 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002899 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002900 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002901 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002902 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002903 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002904 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002905 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002906 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002907 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002908 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002909 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002910 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002911 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002912 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002913 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002914 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002915 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002916 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002917 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002918 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002919 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002920 llvm_unreachable("Clause is not allowed.");
2921 }
2922 return Res;
2923}
2924
2925OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2926 SourceLocation KindKwLoc,
2927 SourceLocation StartLoc,
2928 SourceLocation LParenLoc,
2929 SourceLocation EndLoc) {
2930 if (Kind == OMPC_DEFAULT_unknown) {
2931 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002932 static_assert(OMPC_DEFAULT_unknown > 0,
2933 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002934 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002935 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002936 Values += "'";
2937 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2938 Values += "'";
2939 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002940 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002941 Values += " or ";
2942 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002943 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002944 break;
2945 default:
2946 Values += Sep;
2947 break;
2948 }
2949 }
2950 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002951 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002952 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002953 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002954 switch (Kind) {
2955 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002956 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002957 break;
2958 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002959 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002960 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002961 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002962 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002963 break;
2964 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002965 return new (Context)
2966 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002967}
2968
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002969OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2970 SourceLocation KindKwLoc,
2971 SourceLocation StartLoc,
2972 SourceLocation LParenLoc,
2973 SourceLocation EndLoc) {
2974 if (Kind == OMPC_PROC_BIND_unknown) {
2975 std::string Values;
2976 std::string Sep(", ");
2977 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2978 Values += "'";
2979 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2980 Values += "'";
2981 switch (i) {
2982 case OMPC_PROC_BIND_unknown - 2:
2983 Values += " or ";
2984 break;
2985 case OMPC_PROC_BIND_unknown - 1:
2986 break;
2987 default:
2988 Values += Sep;
2989 break;
2990 }
2991 }
2992 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002993 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002994 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002995 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002996 return new (Context)
2997 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002998}
2999
Alexey Bataev56dafe82014-06-20 07:16:17 +00003000OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3001 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3002 SourceLocation StartLoc, SourceLocation LParenLoc,
3003 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3004 SourceLocation EndLoc) {
3005 OMPClause *Res = nullptr;
3006 switch (Kind) {
3007 case OMPC_schedule:
3008 Res = ActOnOpenMPScheduleClause(
3009 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3010 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3011 break;
3012 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003013 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003014 case OMPC_num_threads:
3015 case OMPC_safelen:
3016 case OMPC_collapse:
3017 case OMPC_default:
3018 case OMPC_proc_bind:
3019 case OMPC_private:
3020 case OMPC_firstprivate:
3021 case OMPC_lastprivate:
3022 case OMPC_shared:
3023 case OMPC_reduction:
3024 case OMPC_linear:
3025 case OMPC_aligned:
3026 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003027 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003028 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003029 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003030 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003031 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003032 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003033 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003034 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003035 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003036 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003037 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003038 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003039 case OMPC_unknown:
3040 llvm_unreachable("Clause is not allowed.");
3041 }
3042 return Res;
3043}
3044
3045OMPClause *Sema::ActOnOpenMPScheduleClause(
3046 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3047 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3048 SourceLocation EndLoc) {
3049 if (Kind == OMPC_SCHEDULE_unknown) {
3050 std::string Values;
3051 std::string Sep(", ");
3052 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3053 Values += "'";
3054 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3055 Values += "'";
3056 switch (i) {
3057 case OMPC_SCHEDULE_unknown - 2:
3058 Values += " or ";
3059 break;
3060 case OMPC_SCHEDULE_unknown - 1:
3061 break;
3062 default:
3063 Values += Sep;
3064 break;
3065 }
3066 }
3067 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3068 << Values << getOpenMPClauseName(OMPC_schedule);
3069 return nullptr;
3070 }
3071 Expr *ValExpr = ChunkSize;
3072 if (ChunkSize) {
3073 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3074 !ChunkSize->isInstantiationDependent() &&
3075 !ChunkSize->containsUnexpandedParameterPack()) {
3076 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3077 ExprResult Val =
3078 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3079 if (Val.isInvalid())
3080 return nullptr;
3081
3082 ValExpr = Val.get();
3083
3084 // OpenMP [2.7.1, Restrictions]
3085 // chunk_size must be a loop invariant integer expression with a positive
3086 // value.
3087 llvm::APSInt Result;
3088 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3089 Result.isSigned() && !Result.isStrictlyPositive()) {
3090 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3091 << "schedule" << ChunkSize->getSourceRange();
3092 return nullptr;
3093 }
3094 }
3095 }
3096
3097 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3098 EndLoc, Kind, ValExpr);
3099}
3100
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003101OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3102 SourceLocation StartLoc,
3103 SourceLocation EndLoc) {
3104 OMPClause *Res = nullptr;
3105 switch (Kind) {
3106 case OMPC_ordered:
3107 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3108 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003109 case OMPC_nowait:
3110 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3111 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003112 case OMPC_untied:
3113 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3114 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003115 case OMPC_mergeable:
3116 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3117 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003118 case OMPC_read:
3119 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3120 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003121 case OMPC_write:
3122 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3123 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003124 case OMPC_update:
3125 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3126 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003127 case OMPC_capture:
3128 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3129 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003130 case OMPC_seq_cst:
3131 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3132 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003133 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003134 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003135 case OMPC_num_threads:
3136 case OMPC_safelen:
3137 case OMPC_collapse:
3138 case OMPC_schedule:
3139 case OMPC_private:
3140 case OMPC_firstprivate:
3141 case OMPC_lastprivate:
3142 case OMPC_shared:
3143 case OMPC_reduction:
3144 case OMPC_linear:
3145 case OMPC_aligned:
3146 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003147 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003148 case OMPC_default:
3149 case OMPC_proc_bind:
3150 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003151 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003152 case OMPC_unknown:
3153 llvm_unreachable("Clause is not allowed.");
3154 }
3155 return Res;
3156}
3157
3158OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3159 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003160 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003161 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3162}
3163
Alexey Bataev236070f2014-06-20 11:19:47 +00003164OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3165 SourceLocation EndLoc) {
3166 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3167}
3168
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003169OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3170 SourceLocation EndLoc) {
3171 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3172}
3173
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003174OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3175 SourceLocation EndLoc) {
3176 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3177}
3178
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003179OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3180 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003181 return new (Context) OMPReadClause(StartLoc, EndLoc);
3182}
3183
Alexey Bataevdea47612014-07-23 07:46:59 +00003184OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3185 SourceLocation EndLoc) {
3186 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3187}
3188
Alexey Bataev67a4f222014-07-23 10:25:33 +00003189OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3190 SourceLocation EndLoc) {
3191 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3192}
3193
Alexey Bataev459dec02014-07-24 06:46:57 +00003194OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3195 SourceLocation EndLoc) {
3196 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3197}
3198
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003199OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3200 SourceLocation EndLoc) {
3201 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3202}
3203
Alexey Bataevc5e02582014-06-16 07:08:35 +00003204OMPClause *Sema::ActOnOpenMPVarListClause(
3205 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3206 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3207 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3208 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003209 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003210 switch (Kind) {
3211 case OMPC_private:
3212 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3213 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003214 case OMPC_firstprivate:
3215 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3216 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003217 case OMPC_lastprivate:
3218 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3219 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003220 case OMPC_shared:
3221 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3222 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003223 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003224 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3225 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003226 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003227 case OMPC_linear:
3228 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3229 ColonLoc, EndLoc);
3230 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003231 case OMPC_aligned:
3232 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3233 ColonLoc, EndLoc);
3234 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003235 case OMPC_copyin:
3236 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3237 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003238 case OMPC_copyprivate:
3239 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3240 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003241 case OMPC_flush:
3242 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3243 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003244 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003245 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003246 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003247 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003248 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003249 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003250 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003251 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003252 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003253 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003254 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003255 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003256 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003257 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003258 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003259 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003260 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003261 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003262 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003263 llvm_unreachable("Clause is not allowed.");
3264 }
3265 return Res;
3266}
3267
3268OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3269 SourceLocation StartLoc,
3270 SourceLocation LParenLoc,
3271 SourceLocation EndLoc) {
3272 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003273 for (auto &RefExpr : VarList) {
3274 assert(RefExpr && "NULL expr in OpenMP private clause.");
3275 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003276 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003277 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003278 continue;
3279 }
3280
Alexey Bataeved09d242014-05-28 05:53:51 +00003281 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003282 // OpenMP [2.1, C/C++]
3283 // A list item is a variable name.
3284 // OpenMP [2.9.3.3, Restrictions, p.1]
3285 // A variable that is part of another variable (as an array or
3286 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003287 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003288 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003289 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003290 continue;
3291 }
3292 Decl *D = DE->getDecl();
3293 VarDecl *VD = cast<VarDecl>(D);
3294
3295 QualType Type = VD->getType();
3296 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3297 // It will be analyzed later.
3298 Vars.push_back(DE);
3299 continue;
3300 }
3301
3302 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3303 // A variable that appears in a private clause must not have an incomplete
3304 // type or a reference type.
3305 if (RequireCompleteType(ELoc, Type,
3306 diag::err_omp_private_incomplete_type)) {
3307 continue;
3308 }
3309 if (Type->isReferenceType()) {
3310 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003311 << getOpenMPClauseName(OMPC_private) << Type;
3312 bool IsDecl =
3313 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3314 Diag(VD->getLocation(),
3315 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3316 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003317 continue;
3318 }
3319
3320 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3321 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003322 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003323 // class type.
3324 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003325 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3326 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003327 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003328 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3329 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3330 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003331 // FIXME This code must be replaced by actual constructing/destructing of
3332 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003333 if (RD) {
3334 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3335 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003336 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003337 if (!CD ||
3338 CheckConstructorAccess(ELoc, CD,
3339 InitializedEntity::InitializeTemporary(Type),
3340 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003341 CD->isDeleted()) {
3342 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003343 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003344 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3345 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003346 Diag(VD->getLocation(),
3347 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3348 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003349 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3350 continue;
3351 }
3352 MarkFunctionReferenced(ELoc, CD);
3353 DiagnoseUseOfDecl(CD, ELoc);
3354
3355 CXXDestructorDecl *DD = RD->getDestructor();
3356 if (DD) {
3357 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3358 DD->isDeleted()) {
3359 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003360 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003361 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3362 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003363 Diag(VD->getLocation(),
3364 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3365 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003366 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3367 continue;
3368 }
3369 MarkFunctionReferenced(ELoc, DD);
3370 DiagnoseUseOfDecl(DD, ELoc);
3371 }
3372 }
3373
Alexey Bataev758e55e2013-09-06 18:03:48 +00003374 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3375 // in a Construct]
3376 // Variables with the predetermined data-sharing attributes may not be
3377 // listed in data-sharing attributes clauses, except for the cases
3378 // listed below. For these exceptions only, listing a predetermined
3379 // variable in a data-sharing attribute clause is allowed and overrides
3380 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003381 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003382 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003383 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3384 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003385 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003386 continue;
3387 }
3388
3389 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003390 Vars.push_back(DE);
3391 }
3392
Alexey Bataeved09d242014-05-28 05:53:51 +00003393 if (Vars.empty())
3394 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003395
3396 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3397}
3398
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003399OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3400 SourceLocation StartLoc,
3401 SourceLocation LParenLoc,
3402 SourceLocation EndLoc) {
3403 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003404 bool IsImplicitClause =
3405 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3406 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3407
Alexey Bataeved09d242014-05-28 05:53:51 +00003408 for (auto &RefExpr : VarList) {
3409 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3410 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003411 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003412 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003413 continue;
3414 }
3415
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003416 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3417 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003418 // OpenMP [2.1, C/C++]
3419 // A list item is a variable name.
3420 // OpenMP [2.9.3.3, Restrictions, p.1]
3421 // A variable that is part of another variable (as an array or
3422 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003423 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003424 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003425 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003426 continue;
3427 }
3428 Decl *D = DE->getDecl();
3429 VarDecl *VD = cast<VarDecl>(D);
3430
3431 QualType Type = VD->getType();
3432 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3433 // It will be analyzed later.
3434 Vars.push_back(DE);
3435 continue;
3436 }
3437
3438 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3439 // A variable that appears in a private clause must not have an incomplete
3440 // type or a reference type.
3441 if (RequireCompleteType(ELoc, Type,
3442 diag::err_omp_firstprivate_incomplete_type)) {
3443 continue;
3444 }
3445 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003446 if (IsImplicitClause) {
3447 Diag(ImplicitClauseLoc,
3448 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3449 << Type;
3450 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3451 } else {
3452 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3453 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3454 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003455 bool IsDecl =
3456 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3457 Diag(VD->getLocation(),
3458 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3459 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003460 continue;
3461 }
3462
3463 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3464 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003465 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003466 // class type.
3467 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003468 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3469 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3470 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003471 // FIXME This code must be replaced by actual constructing/destructing of
3472 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003473 if (RD) {
3474 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3475 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003476 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003477 if (!CD ||
3478 CheckConstructorAccess(ELoc, CD,
3479 InitializedEntity::InitializeTemporary(Type),
3480 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003481 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003482 if (IsImplicitClause) {
3483 Diag(ImplicitClauseLoc,
3484 diag::err_omp_task_predetermined_firstprivate_required_method)
3485 << 0;
3486 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3487 } else {
3488 Diag(ELoc, diag::err_omp_required_method)
3489 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3490 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003491 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3492 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003493 Diag(VD->getLocation(),
3494 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3495 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003496 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3497 continue;
3498 }
3499 MarkFunctionReferenced(ELoc, CD);
3500 DiagnoseUseOfDecl(CD, ELoc);
3501
3502 CXXDestructorDecl *DD = RD->getDestructor();
3503 if (DD) {
3504 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3505 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003506 if (IsImplicitClause) {
3507 Diag(ImplicitClauseLoc,
3508 diag::err_omp_task_predetermined_firstprivate_required_method)
3509 << 1;
3510 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3511 } else {
3512 Diag(ELoc, diag::err_omp_required_method)
3513 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3514 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003515 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3516 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003517 Diag(VD->getLocation(),
3518 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3519 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003520 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3521 continue;
3522 }
3523 MarkFunctionReferenced(ELoc, DD);
3524 DiagnoseUseOfDecl(DD, ELoc);
3525 }
3526 }
3527
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003528 // If an implicit firstprivate variable found it was checked already.
3529 if (!IsImplicitClause) {
3530 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003531 Type = Type.getNonReferenceType().getCanonicalType();
3532 bool IsConstant = Type.isConstant(Context);
3533 Type = Context.getBaseElementType(Type);
3534 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
3535 // A list item that specifies a given variable may not appear in more
3536 // than one clause on the same directive, except that a variable may be
3537 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003538 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00003539 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003540 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003541 << getOpenMPClauseName(DVar.CKind)
3542 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003543 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003544 continue;
3545 }
3546
3547 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3548 // in a Construct]
3549 // Variables with the predetermined data-sharing attributes may not be
3550 // listed in data-sharing attributes clauses, except for the cases
3551 // listed below. For these exceptions only, listing a predetermined
3552 // variable in a data-sharing attribute clause is allowed and overrides
3553 // the variable's predetermined data-sharing attributes.
3554 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3555 // in a Construct, C/C++, p.2]
3556 // Variables with const-qualified type having no mutable member may be
3557 // listed in a firstprivate clause, even if they are static data members.
3558 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
3559 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
3560 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003561 << getOpenMPClauseName(DVar.CKind)
3562 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003563 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003564 continue;
3565 }
3566
Alexey Bataevf29276e2014-06-18 04:14:57 +00003567 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003568 // OpenMP [2.9.3.4, Restrictions, p.2]
3569 // A list item that is private within a parallel region must not appear
3570 // in a firstprivate clause on a worksharing construct if any of the
3571 // worksharing regions arising from the worksharing construct ever bind
3572 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00003573 if (isOpenMPWorksharingDirective(CurrDir) &&
3574 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003575 DVar = DSAStack->getImplicitDSA(VD, true);
3576 if (DVar.CKind != OMPC_shared &&
3577 (isOpenMPParallelDirective(DVar.DKind) ||
3578 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003579 Diag(ELoc, diag::err_omp_required_access)
3580 << getOpenMPClauseName(OMPC_firstprivate)
3581 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003582 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003583 continue;
3584 }
3585 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003586 // OpenMP [2.9.3.4, Restrictions, p.3]
3587 // A list item that appears in a reduction clause of a parallel construct
3588 // must not appear in a firstprivate clause on a worksharing or task
3589 // construct if any of the worksharing or task regions arising from the
3590 // worksharing or task construct ever bind to any of the parallel regions
3591 // arising from the parallel construct.
3592 // OpenMP [2.9.3.4, Restrictions, p.4]
3593 // A list item that appears in a reduction clause in worksharing
3594 // construct must not appear in a firstprivate clause in a task construct
3595 // encountered during execution of any of the worksharing regions arising
3596 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003597 if (CurrDir == OMPD_task) {
3598 DVar =
3599 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
3600 [](OpenMPDirectiveKind K) -> bool {
3601 return isOpenMPParallelDirective(K) ||
3602 isOpenMPWorksharingDirective(K);
3603 },
3604 false);
3605 if (DVar.CKind == OMPC_reduction &&
3606 (isOpenMPParallelDirective(DVar.DKind) ||
3607 isOpenMPWorksharingDirective(DVar.DKind))) {
3608 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
3609 << getOpenMPDirectiveName(DVar.DKind);
3610 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3611 continue;
3612 }
3613 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003614 }
3615
3616 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
3617 Vars.push_back(DE);
3618 }
3619
Alexey Bataeved09d242014-05-28 05:53:51 +00003620 if (Vars.empty())
3621 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003622
3623 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3624 Vars);
3625}
3626
Alexander Musman1bb328c2014-06-04 13:06:39 +00003627OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
3628 SourceLocation StartLoc,
3629 SourceLocation LParenLoc,
3630 SourceLocation EndLoc) {
3631 SmallVector<Expr *, 8> Vars;
3632 for (auto &RefExpr : VarList) {
3633 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
3634 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3635 // It will be analyzed later.
3636 Vars.push_back(RefExpr);
3637 continue;
3638 }
3639
3640 SourceLocation ELoc = RefExpr->getExprLoc();
3641 // OpenMP [2.1, C/C++]
3642 // A list item is a variable name.
3643 // OpenMP [2.14.3.5, Restrictions, p.1]
3644 // A variable that is part of another variable (as an array or structure
3645 // element) cannot appear in a lastprivate clause.
3646 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3647 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3648 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3649 continue;
3650 }
3651 Decl *D = DE->getDecl();
3652 VarDecl *VD = cast<VarDecl>(D);
3653
3654 QualType Type = VD->getType();
3655 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3656 // It will be analyzed later.
3657 Vars.push_back(DE);
3658 continue;
3659 }
3660
3661 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
3662 // A variable that appears in a lastprivate clause must not have an
3663 // incomplete type or a reference type.
3664 if (RequireCompleteType(ELoc, Type,
3665 diag::err_omp_lastprivate_incomplete_type)) {
3666 continue;
3667 }
3668 if (Type->isReferenceType()) {
3669 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3670 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3671 bool IsDecl =
3672 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3673 Diag(VD->getLocation(),
3674 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3675 << VD;
3676 continue;
3677 }
3678
3679 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3680 // in a Construct]
3681 // Variables with the predetermined data-sharing attributes may not be
3682 // listed in data-sharing attributes clauses, except for the cases
3683 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003684 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003685 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3686 DVar.CKind != OMPC_firstprivate &&
3687 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3688 Diag(ELoc, diag::err_omp_wrong_dsa)
3689 << getOpenMPClauseName(DVar.CKind)
3690 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003691 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003692 continue;
3693 }
3694
Alexey Bataevf29276e2014-06-18 04:14:57 +00003695 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3696 // OpenMP [2.14.3.5, Restrictions, p.2]
3697 // A list item that is private within a parallel region, or that appears in
3698 // the reduction clause of a parallel construct, must not appear in a
3699 // lastprivate clause on a worksharing construct if any of the corresponding
3700 // worksharing regions ever binds to any of the corresponding parallel
3701 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003702 if (isOpenMPWorksharingDirective(CurrDir) &&
3703 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003704 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003705 if (DVar.CKind != OMPC_shared) {
3706 Diag(ELoc, diag::err_omp_required_access)
3707 << getOpenMPClauseName(OMPC_lastprivate)
3708 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003709 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003710 continue;
3711 }
3712 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003713 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003714 // A variable of class type (or array thereof) that appears in a
3715 // lastprivate clause requires an accessible, unambiguous default
3716 // constructor for the class type, unless the list item is also specified
3717 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003718 // A variable of class type (or array thereof) that appears in a
3719 // lastprivate clause requires an accessible, unambiguous copy assignment
3720 // operator for the class type.
3721 while (Type.getNonReferenceType()->isArrayType())
3722 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3723 ->getElementType();
3724 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3725 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3726 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003727 // FIXME This code must be replaced by actual copying and destructing of the
3728 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003729 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003730 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3731 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003732 if (MD) {
3733 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3734 MD->isDeleted()) {
3735 Diag(ELoc, diag::err_omp_required_method)
3736 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3737 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3738 VarDecl::DeclarationOnly;
3739 Diag(VD->getLocation(),
3740 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3741 << VD;
3742 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3743 continue;
3744 }
3745 MarkFunctionReferenced(ELoc, MD);
3746 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003747 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003748
3749 CXXDestructorDecl *DD = RD->getDestructor();
3750 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003751 PartialDiagnostic PD =
3752 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003753 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3754 DD->isDeleted()) {
3755 Diag(ELoc, diag::err_omp_required_method)
3756 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3757 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3758 VarDecl::DeclarationOnly;
3759 Diag(VD->getLocation(),
3760 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3761 << VD;
3762 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3763 continue;
3764 }
3765 MarkFunctionReferenced(ELoc, DD);
3766 DiagnoseUseOfDecl(DD, ELoc);
3767 }
3768 }
3769
Alexey Bataevf29276e2014-06-18 04:14:57 +00003770 if (DVar.CKind != OMPC_firstprivate)
3771 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003772 Vars.push_back(DE);
3773 }
3774
3775 if (Vars.empty())
3776 return nullptr;
3777
3778 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3779 Vars);
3780}
3781
Alexey Bataev758e55e2013-09-06 18:03:48 +00003782OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3783 SourceLocation StartLoc,
3784 SourceLocation LParenLoc,
3785 SourceLocation EndLoc) {
3786 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003787 for (auto &RefExpr : VarList) {
3788 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3789 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003790 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003791 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003792 continue;
3793 }
3794
Alexey Bataeved09d242014-05-28 05:53:51 +00003795 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003796 // OpenMP [2.1, C/C++]
3797 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003798 // OpenMP [2.14.3.2, Restrictions, p.1]
3799 // A variable that is part of another variable (as an array or structure
3800 // element) cannot appear in a shared unless it is a static data member
3801 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003802 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003803 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003804 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003805 continue;
3806 }
3807 Decl *D = DE->getDecl();
3808 VarDecl *VD = cast<VarDecl>(D);
3809
3810 QualType Type = VD->getType();
3811 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3812 // It will be analyzed later.
3813 Vars.push_back(DE);
3814 continue;
3815 }
3816
3817 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3818 // in a Construct]
3819 // Variables with the predetermined data-sharing attributes may not be
3820 // listed in data-sharing attributes clauses, except for the cases
3821 // listed below. For these exceptions only, listing a predetermined
3822 // variable in a data-sharing attribute clause is allowed and overrides
3823 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003824 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003825 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3826 DVar.RefExpr) {
3827 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3828 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003829 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003830 continue;
3831 }
3832
3833 DSAStack->addDSA(VD, DE, OMPC_shared);
3834 Vars.push_back(DE);
3835 }
3836
Alexey Bataeved09d242014-05-28 05:53:51 +00003837 if (Vars.empty())
3838 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003839
3840 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3841}
3842
Alexey Bataevc5e02582014-06-16 07:08:35 +00003843namespace {
3844class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3845 DSAStackTy *Stack;
3846
3847public:
3848 bool VisitDeclRefExpr(DeclRefExpr *E) {
3849 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003850 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003851 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3852 return false;
3853 if (DVar.CKind != OMPC_unknown)
3854 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003855 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003856 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003857 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003858 return true;
3859 return false;
3860 }
3861 return false;
3862 }
3863 bool VisitStmt(Stmt *S) {
3864 for (auto Child : S->children()) {
3865 if (Child && Visit(Child))
3866 return true;
3867 }
3868 return false;
3869 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003870 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003871};
Alexey Bataev23b69422014-06-18 07:08:49 +00003872} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003873
3874OMPClause *Sema::ActOnOpenMPReductionClause(
3875 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3876 SourceLocation ColonLoc, SourceLocation EndLoc,
3877 CXXScopeSpec &ReductionIdScopeSpec,
3878 const DeclarationNameInfo &ReductionId) {
3879 // TODO: Allow scope specification search when 'declare reduction' is
3880 // supported.
3881 assert(ReductionIdScopeSpec.isEmpty() &&
3882 "No support for scoped reduction identifiers yet.");
3883
3884 auto DN = ReductionId.getName();
3885 auto OOK = DN.getCXXOverloadedOperator();
3886 BinaryOperatorKind BOK = BO_Comma;
3887
3888 // OpenMP [2.14.3.6, reduction clause]
3889 // C
3890 // reduction-identifier is either an identifier or one of the following
3891 // operators: +, -, *, &, |, ^, && and ||
3892 // C++
3893 // reduction-identifier is either an id-expression or one of the following
3894 // operators: +, -, *, &, |, ^, && and ||
3895 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3896 switch (OOK) {
3897 case OO_Plus:
3898 case OO_Minus:
3899 BOK = BO_AddAssign;
3900 break;
3901 case OO_Star:
3902 BOK = BO_MulAssign;
3903 break;
3904 case OO_Amp:
3905 BOK = BO_AndAssign;
3906 break;
3907 case OO_Pipe:
3908 BOK = BO_OrAssign;
3909 break;
3910 case OO_Caret:
3911 BOK = BO_XorAssign;
3912 break;
3913 case OO_AmpAmp:
3914 BOK = BO_LAnd;
3915 break;
3916 case OO_PipePipe:
3917 BOK = BO_LOr;
3918 break;
3919 default:
3920 if (auto II = DN.getAsIdentifierInfo()) {
3921 if (II->isStr("max"))
3922 BOK = BO_GT;
3923 else if (II->isStr("min"))
3924 BOK = BO_LT;
3925 }
3926 break;
3927 }
3928 SourceRange ReductionIdRange;
3929 if (ReductionIdScopeSpec.isValid()) {
3930 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3931 }
3932 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3933 if (BOK == BO_Comma) {
3934 // Not allowed reduction identifier is found.
3935 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3936 << ReductionIdRange;
3937 return nullptr;
3938 }
3939
3940 SmallVector<Expr *, 8> Vars;
3941 for (auto RefExpr : VarList) {
3942 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3943 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3944 // It will be analyzed later.
3945 Vars.push_back(RefExpr);
3946 continue;
3947 }
3948
3949 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3950 RefExpr->isInstantiationDependent() ||
3951 RefExpr->containsUnexpandedParameterPack()) {
3952 // It will be analyzed later.
3953 Vars.push_back(RefExpr);
3954 continue;
3955 }
3956
3957 auto ELoc = RefExpr->getExprLoc();
3958 auto ERange = RefExpr->getSourceRange();
3959 // OpenMP [2.1, C/C++]
3960 // A list item is a variable or array section, subject to the restrictions
3961 // specified in Section 2.4 on page 42 and in each of the sections
3962 // describing clauses and directives for which a list appears.
3963 // OpenMP [2.14.3.3, Restrictions, p.1]
3964 // A variable that is part of another variable (as an array or
3965 // structure element) cannot appear in a private clause.
3966 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3967 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3968 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3969 continue;
3970 }
3971 auto D = DE->getDecl();
3972 auto VD = cast<VarDecl>(D);
3973 auto Type = VD->getType();
3974 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3975 // A variable that appears in a private clause must not have an incomplete
3976 // type or a reference type.
3977 if (RequireCompleteType(ELoc, Type,
3978 diag::err_omp_reduction_incomplete_type))
3979 continue;
3980 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3981 // Arrays may not appear in a reduction clause.
3982 if (Type.getNonReferenceType()->isArrayType()) {
3983 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3984 bool IsDecl =
3985 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3986 Diag(VD->getLocation(),
3987 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3988 << VD;
3989 continue;
3990 }
3991 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3992 // A list item that appears in a reduction clause must not be
3993 // const-qualified.
3994 if (Type.getNonReferenceType().isConstant(Context)) {
3995 Diag(ELoc, diag::err_omp_const_variable)
3996 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3997 bool IsDecl =
3998 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3999 Diag(VD->getLocation(),
4000 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4001 << VD;
4002 continue;
4003 }
4004 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4005 // If a list-item is a reference type then it must bind to the same object
4006 // for all threads of the team.
4007 VarDecl *VDDef = VD->getDefinition();
4008 if (Type->isReferenceType() && VDDef) {
4009 DSARefChecker Check(DSAStack);
4010 if (Check.Visit(VDDef->getInit())) {
4011 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4012 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4013 continue;
4014 }
4015 }
4016 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4017 // The type of a list item that appears in a reduction clause must be valid
4018 // for the reduction-identifier. For a max or min reduction in C, the type
4019 // of the list item must be an allowed arithmetic data type: char, int,
4020 // float, double, or _Bool, possibly modified with long, short, signed, or
4021 // unsigned. For a max or min reduction in C++, the type of the list item
4022 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4023 // double, or bool, possibly modified with long, short, signed, or unsigned.
4024 if ((BOK == BO_GT || BOK == BO_LT) &&
4025 !(Type->isScalarType() ||
4026 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4027 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4028 << getLangOpts().CPlusPlus;
4029 bool IsDecl =
4030 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4031 Diag(VD->getLocation(),
4032 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4033 << VD;
4034 continue;
4035 }
4036 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4037 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4038 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4039 bool IsDecl =
4040 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4041 Diag(VD->getLocation(),
4042 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4043 << VD;
4044 continue;
4045 }
4046 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4047 getDiagnostics().setSuppressAllDiagnostics(true);
4048 ExprResult ReductionOp =
4049 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4050 RefExpr, RefExpr);
4051 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4052 if (ReductionOp.isInvalid()) {
4053 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004054 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004055 bool IsDecl =
4056 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4057 Diag(VD->getLocation(),
4058 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4059 << VD;
4060 continue;
4061 }
4062
4063 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4064 // in a Construct]
4065 // Variables with the predetermined data-sharing attributes may not be
4066 // listed in data-sharing attributes clauses, except for the cases
4067 // listed below. For these exceptions only, listing a predetermined
4068 // variable in a data-sharing attribute clause is allowed and overrides
4069 // the variable's predetermined data-sharing attributes.
4070 // OpenMP [2.14.3.6, Restrictions, p.3]
4071 // Any number of reduction clauses can be specified on the directive,
4072 // but a list item can appear only once in the reduction clauses for that
4073 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004074 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004075 if (DVar.CKind == OMPC_reduction) {
4076 Diag(ELoc, diag::err_omp_once_referenced)
4077 << getOpenMPClauseName(OMPC_reduction);
4078 if (DVar.RefExpr) {
4079 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4080 }
4081 } else if (DVar.CKind != OMPC_unknown) {
4082 Diag(ELoc, diag::err_omp_wrong_dsa)
4083 << getOpenMPClauseName(DVar.CKind)
4084 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004085 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004086 continue;
4087 }
4088
4089 // OpenMP [2.14.3.6, Restrictions, p.1]
4090 // A list item that appears in a reduction clause of a worksharing
4091 // construct must be shared in the parallel regions to which any of the
4092 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004093 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004094 if (isOpenMPWorksharingDirective(CurrDir) &&
4095 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004096 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004097 if (DVar.CKind != OMPC_shared) {
4098 Diag(ELoc, diag::err_omp_required_access)
4099 << getOpenMPClauseName(OMPC_reduction)
4100 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004101 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004102 continue;
4103 }
4104 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004105
4106 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4107 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4108 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004109 // FIXME This code must be replaced by actual constructing/destructing of
4110 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004111 if (RD) {
4112 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4113 PartialDiagnostic PD =
4114 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004115 if (!CD ||
4116 CheckConstructorAccess(ELoc, CD,
4117 InitializedEntity::InitializeTemporary(Type),
4118 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004119 CD->isDeleted()) {
4120 Diag(ELoc, diag::err_omp_required_method)
4121 << getOpenMPClauseName(OMPC_reduction) << 0;
4122 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4123 VarDecl::DeclarationOnly;
4124 Diag(VD->getLocation(),
4125 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4126 << VD;
4127 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4128 continue;
4129 }
4130 MarkFunctionReferenced(ELoc, CD);
4131 DiagnoseUseOfDecl(CD, ELoc);
4132
4133 CXXDestructorDecl *DD = RD->getDestructor();
4134 if (DD) {
4135 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4136 DD->isDeleted()) {
4137 Diag(ELoc, diag::err_omp_required_method)
4138 << getOpenMPClauseName(OMPC_reduction) << 4;
4139 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4140 VarDecl::DeclarationOnly;
4141 Diag(VD->getLocation(),
4142 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4143 << VD;
4144 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4145 continue;
4146 }
4147 MarkFunctionReferenced(ELoc, DD);
4148 DiagnoseUseOfDecl(DD, ELoc);
4149 }
4150 }
4151
4152 DSAStack->addDSA(VD, DE, OMPC_reduction);
4153 Vars.push_back(DE);
4154 }
4155
4156 if (Vars.empty())
4157 return nullptr;
4158
4159 return OMPReductionClause::Create(
4160 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4161 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4162}
4163
Alexander Musman8dba6642014-04-22 13:09:42 +00004164OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4165 SourceLocation StartLoc,
4166 SourceLocation LParenLoc,
4167 SourceLocation ColonLoc,
4168 SourceLocation EndLoc) {
4169 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004170 for (auto &RefExpr : VarList) {
4171 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4172 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004173 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004174 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004175 continue;
4176 }
4177
4178 // OpenMP [2.14.3.7, linear clause]
4179 // A list item that appears in a linear clause is subject to the private
4180 // clause semantics described in Section 2.14.3.3 on page 159 except as
4181 // noted. In addition, the value of the new list item on each iteration
4182 // of the associated loop(s) corresponds to the value of the original
4183 // list item before entering the construct plus the logical number of
4184 // the iteration times linear-step.
4185
Alexey Bataeved09d242014-05-28 05:53:51 +00004186 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004187 // OpenMP [2.1, C/C++]
4188 // A list item is a variable name.
4189 // OpenMP [2.14.3.3, Restrictions, p.1]
4190 // A variable that is part of another variable (as an array or
4191 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004192 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004193 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004194 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004195 continue;
4196 }
4197
4198 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4199
4200 // OpenMP [2.14.3.7, linear clause]
4201 // A list-item cannot appear in more than one linear clause.
4202 // A list-item that appears in a linear clause cannot appear in any
4203 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004204 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004205 if (DVar.RefExpr) {
4206 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4207 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004208 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004209 continue;
4210 }
4211
4212 QualType QType = VD->getType();
4213 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4214 // It will be analyzed later.
4215 Vars.push_back(DE);
4216 continue;
4217 }
4218
4219 // A variable must not have an incomplete type or a reference type.
4220 if (RequireCompleteType(ELoc, QType,
4221 diag::err_omp_linear_incomplete_type)) {
4222 continue;
4223 }
4224 if (QType->isReferenceType()) {
4225 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4226 << getOpenMPClauseName(OMPC_linear) << QType;
4227 bool IsDecl =
4228 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4229 Diag(VD->getLocation(),
4230 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4231 << VD;
4232 continue;
4233 }
4234
4235 // A list item must not be const-qualified.
4236 if (QType.isConstant(Context)) {
4237 Diag(ELoc, diag::err_omp_const_variable)
4238 << getOpenMPClauseName(OMPC_linear);
4239 bool IsDecl =
4240 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4241 Diag(VD->getLocation(),
4242 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4243 << VD;
4244 continue;
4245 }
4246
4247 // A list item must be of integral or pointer type.
4248 QType = QType.getUnqualifiedType().getCanonicalType();
4249 const Type *Ty = QType.getTypePtrOrNull();
4250 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4251 !Ty->isPointerType())) {
4252 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4253 bool IsDecl =
4254 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4255 Diag(VD->getLocation(),
4256 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4257 << VD;
4258 continue;
4259 }
4260
4261 DSAStack->addDSA(VD, DE, OMPC_linear);
4262 Vars.push_back(DE);
4263 }
4264
4265 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004266 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004267
4268 Expr *StepExpr = Step;
4269 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4270 !Step->isInstantiationDependent() &&
4271 !Step->containsUnexpandedParameterPack()) {
4272 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004273 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004274 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004275 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004276 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004277
4278 // Warn about zero linear step (it would be probably better specified as
4279 // making corresponding variables 'const').
4280 llvm::APSInt Result;
4281 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4282 !Result.isNegative() && !Result.isStrictlyPositive())
4283 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4284 << (Vars.size() > 1);
4285 }
4286
4287 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4288 Vars, StepExpr);
4289}
4290
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004291OMPClause *Sema::ActOnOpenMPAlignedClause(
4292 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4293 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4294
4295 SmallVector<Expr *, 8> Vars;
4296 for (auto &RefExpr : VarList) {
4297 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4298 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4299 // It will be analyzed later.
4300 Vars.push_back(RefExpr);
4301 continue;
4302 }
4303
4304 SourceLocation ELoc = RefExpr->getExprLoc();
4305 // OpenMP [2.1, C/C++]
4306 // A list item is a variable name.
4307 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4308 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4309 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4310 continue;
4311 }
4312
4313 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4314
4315 // OpenMP [2.8.1, simd construct, Restrictions]
4316 // The type of list items appearing in the aligned clause must be
4317 // array, pointer, reference to array, or reference to pointer.
4318 QualType QType = DE->getType()
4319 .getNonReferenceType()
4320 .getUnqualifiedType()
4321 .getCanonicalType();
4322 const Type *Ty = QType.getTypePtrOrNull();
4323 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4324 !Ty->isPointerType())) {
4325 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4326 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4327 bool IsDecl =
4328 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4329 Diag(VD->getLocation(),
4330 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4331 << VD;
4332 continue;
4333 }
4334
4335 // OpenMP [2.8.1, simd construct, Restrictions]
4336 // A list-item cannot appear in more than one aligned clause.
4337 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4338 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4339 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4340 << getOpenMPClauseName(OMPC_aligned);
4341 continue;
4342 }
4343
4344 Vars.push_back(DE);
4345 }
4346
4347 // OpenMP [2.8.1, simd construct, Description]
4348 // The parameter of the aligned clause, alignment, must be a constant
4349 // positive integer expression.
4350 // If no optional parameter is specified, implementation-defined default
4351 // alignments for SIMD instructions on the target platforms are assumed.
4352 if (Alignment != nullptr) {
4353 ExprResult AlignResult =
4354 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4355 if (AlignResult.isInvalid())
4356 return nullptr;
4357 Alignment = AlignResult.get();
4358 }
4359 if (Vars.empty())
4360 return nullptr;
4361
4362 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
4363 EndLoc, Vars, Alignment);
4364}
4365
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004366OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
4367 SourceLocation StartLoc,
4368 SourceLocation LParenLoc,
4369 SourceLocation EndLoc) {
4370 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004371 for (auto &RefExpr : VarList) {
4372 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
4373 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004374 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004375 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004376 continue;
4377 }
4378
Alexey Bataeved09d242014-05-28 05:53:51 +00004379 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004380 // OpenMP [2.1, C/C++]
4381 // A list item is a variable name.
4382 // OpenMP [2.14.4.1, Restrictions, p.1]
4383 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004384 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004385 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004386 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004387 continue;
4388 }
4389
4390 Decl *D = DE->getDecl();
4391 VarDecl *VD = cast<VarDecl>(D);
4392
4393 QualType Type = VD->getType();
4394 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4395 // It will be analyzed later.
4396 Vars.push_back(DE);
4397 continue;
4398 }
4399
4400 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4401 // A list item that appears in a copyin clause must be threadprivate.
4402 if (!DSAStack->isThreadPrivate(VD)) {
4403 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004404 << getOpenMPClauseName(OMPC_copyin)
4405 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004406 continue;
4407 }
4408
4409 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4410 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004411 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004412 // operator for the class type.
4413 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004414 CXXRecordDecl *RD =
4415 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004416 // FIXME This code must be replaced by actual assignment of the
4417 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004418 if (RD) {
4419 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4420 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004421 if (MD) {
4422 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4423 MD->isDeleted()) {
4424 Diag(ELoc, diag::err_omp_required_method)
4425 << getOpenMPClauseName(OMPC_copyin) << 2;
4426 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4427 VarDecl::DeclarationOnly;
4428 Diag(VD->getLocation(),
4429 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4430 << VD;
4431 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4432 continue;
4433 }
4434 MarkFunctionReferenced(ELoc, MD);
4435 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004436 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004437 }
4438
4439 DSAStack->addDSA(VD, DE, OMPC_copyin);
4440 Vars.push_back(DE);
4441 }
4442
Alexey Bataeved09d242014-05-28 05:53:51 +00004443 if (Vars.empty())
4444 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004445
4446 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4447}
4448
Alexey Bataevbae9a792014-06-27 10:37:06 +00004449OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4450 SourceLocation StartLoc,
4451 SourceLocation LParenLoc,
4452 SourceLocation EndLoc) {
4453 SmallVector<Expr *, 8> Vars;
4454 for (auto &RefExpr : VarList) {
4455 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4456 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4457 // It will be analyzed later.
4458 Vars.push_back(RefExpr);
4459 continue;
4460 }
4461
4462 SourceLocation ELoc = RefExpr->getExprLoc();
4463 // OpenMP [2.1, C/C++]
4464 // A list item is a variable name.
4465 // OpenMP [2.14.4.1, Restrictions, p.1]
4466 // A list item that appears in a copyin clause must be threadprivate.
4467 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4468 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4469 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4470 continue;
4471 }
4472
4473 Decl *D = DE->getDecl();
4474 VarDecl *VD = cast<VarDecl>(D);
4475
4476 QualType Type = VD->getType();
4477 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4478 // It will be analyzed later.
4479 Vars.push_back(DE);
4480 continue;
4481 }
4482
4483 // OpenMP [2.14.4.2, Restrictions, p.2]
4484 // A list item that appears in a copyprivate clause may not appear in a
4485 // private or firstprivate clause on the single construct.
4486 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004487 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004488 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4489 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4490 Diag(ELoc, diag::err_omp_wrong_dsa)
4491 << getOpenMPClauseName(DVar.CKind)
4492 << getOpenMPClauseName(OMPC_copyprivate);
4493 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4494 continue;
4495 }
4496
4497 // OpenMP [2.11.4.2, Restrictions, p.1]
4498 // All list items that appear in a copyprivate clause must be either
4499 // threadprivate or private in the enclosing context.
4500 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004501 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004502 if (DVar.CKind == OMPC_shared) {
4503 Diag(ELoc, diag::err_omp_required_access)
4504 << getOpenMPClauseName(OMPC_copyprivate)
4505 << "threadprivate or private in the enclosing context";
4506 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4507 continue;
4508 }
4509 }
4510 }
4511
4512 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4513 // A variable of class type (or array thereof) that appears in a
4514 // copyin clause requires an accessible, unambiguous copy assignment
4515 // operator for the class type.
4516 Type = Context.getBaseElementType(Type);
4517 CXXRecordDecl *RD =
4518 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
4519 // FIXME This code must be replaced by actual assignment of the
4520 // threadprivate variable.
4521 if (RD) {
4522 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4523 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4524 if (MD) {
4525 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4526 MD->isDeleted()) {
4527 Diag(ELoc, diag::err_omp_required_method)
4528 << getOpenMPClauseName(OMPC_copyprivate) << 2;
4529 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4530 VarDecl::DeclarationOnly;
4531 Diag(VD->getLocation(),
4532 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4533 << VD;
4534 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4535 continue;
4536 }
4537 MarkFunctionReferenced(ELoc, MD);
4538 DiagnoseUseOfDecl(MD, ELoc);
4539 }
4540 }
4541
4542 // No need to mark vars as copyprivate, they are already threadprivate or
4543 // implicitly private.
4544 Vars.push_back(DE);
4545 }
4546
4547 if (Vars.empty())
4548 return nullptr;
4549
4550 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4551}
4552
Alexey Bataev6125da92014-07-21 11:26:11 +00004553OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
4554 SourceLocation StartLoc,
4555 SourceLocation LParenLoc,
4556 SourceLocation EndLoc) {
4557 if (VarList.empty())
4558 return nullptr;
4559
4560 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
4561}
Alexey Bataevdea47612014-07-23 07:46:59 +00004562