blob: e70090d930e7a646bf3c901f3f2bc7cd119ec28f [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;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001798 /// \brief A source location for referring to loop init later.
1799 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001800 /// \brief A source location for referring to condition later.
1801 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001802 /// \brief A source location for referring to increment later.
1803 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001804 /// \brief Loop variable.
1805 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001806 /// \brief Reference to loop variable.
1807 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001808 /// \brief Lower bound (initializer for the var).
1809 Expr *LB;
1810 /// \brief Upper bound.
1811 Expr *UB;
1812 /// \brief Loop step (increment).
1813 Expr *Step;
1814 /// \brief This flag is true when condition is one of:
1815 /// Var < UB
1816 /// Var <= UB
1817 /// UB > Var
1818 /// UB >= Var
1819 bool TestIsLessOp;
1820 /// \brief This flag is true when condition is strict ( < or > ).
1821 bool TestIsStrictOp;
1822 /// \brief This flag is true when step is subtracted on each iteration.
1823 bool SubtractStep;
1824
1825public:
1826 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1827 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001828 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1829 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001830 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1831 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001832 /// \brief Check init-expr for canonical loop form and save loop counter
1833 /// variable - #Var and its initialization value - #LB.
1834 bool CheckInit(Stmt *S);
1835 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1836 /// for less/greater and for strict/non-strict comparison.
1837 bool CheckCond(Expr *S);
1838 /// \brief Check incr-expr for canonical loop form and return true if it
1839 /// does not conform, otherwise save loop step (#Step).
1840 bool CheckInc(Expr *S);
1841 /// \brief Return the loop counter variable.
1842 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001843 /// \brief Return the reference expression to loop counter variable.
1844 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001845 /// \brief Source range of the loop init.
1846 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1847 /// \brief Source range of the loop condition.
1848 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1849 /// \brief Source range of the loop increment.
1850 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1851 /// \brief True if the step should be subtracted.
1852 bool ShouldSubtractStep() const { return SubtractStep; }
1853 /// \brief Build the expression to calculate the number of iterations.
1854 Expr *BuildNumIterations(Scope *S) const;
1855 /// \brief Build reference expression to the counter be used for codegen.
1856 Expr *BuildCounterVar() const;
1857 /// \brief Build initization of the counter be used for codegen.
1858 Expr *BuildCounterInit() const;
1859 /// \brief Build step of the counter be used for codegen.
1860 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001861 /// \brief Return true if any expression is dependent.
1862 bool Dependent() const;
1863
1864private:
1865 /// \brief Check the right-hand side of an assignment in the increment
1866 /// expression.
1867 bool CheckIncRHS(Expr *RHS);
1868 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001869 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001870 /// \brief Helper to set upper bound.
1871 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1872 const SourceLocation &SL);
1873 /// \brief Helper to set loop increment.
1874 bool SetStep(Expr *NewStep, bool Subtract);
1875};
1876
1877bool OpenMPIterationSpaceChecker::Dependent() const {
1878 if (!Var) {
1879 assert(!LB && !UB && !Step);
1880 return false;
1881 }
1882 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1883 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1884}
1885
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001886bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1887 DeclRefExpr *NewVarRefExpr,
1888 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001889 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001890 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1891 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001892 if (!NewVar || !NewLB)
1893 return true;
1894 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001895 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001896 LB = NewLB;
1897 return false;
1898}
1899
1900bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1901 const SourceRange &SR,
1902 const SourceLocation &SL) {
1903 // State consistency checking to ensure correct usage.
1904 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1905 !TestIsLessOp && !TestIsStrictOp);
1906 if (!NewUB)
1907 return true;
1908 UB = NewUB;
1909 TestIsLessOp = LessOp;
1910 TestIsStrictOp = StrictOp;
1911 ConditionSrcRange = SR;
1912 ConditionLoc = SL;
1913 return false;
1914}
1915
1916bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1917 // State consistency checking to ensure correct usage.
1918 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1919 if (!NewStep)
1920 return true;
1921 if (!NewStep->isValueDependent()) {
1922 // Check that the step is integer expression.
1923 SourceLocation StepLoc = NewStep->getLocStart();
1924 ExprResult Val =
1925 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1926 if (Val.isInvalid())
1927 return true;
1928 NewStep = Val.get();
1929
1930 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1931 // If test-expr is of form var relational-op b and relational-op is < or
1932 // <= then incr-expr must cause var to increase on each iteration of the
1933 // loop. If test-expr is of form var relational-op b and relational-op is
1934 // > or >= then incr-expr must cause var to decrease on each iteration of
1935 // the loop.
1936 // If test-expr is of form b relational-op var and relational-op is < or
1937 // <= then incr-expr must cause var to decrease on each iteration of the
1938 // loop. If test-expr is of form b relational-op var and relational-op is
1939 // > or >= then incr-expr must cause var to increase on each iteration of
1940 // the loop.
1941 llvm::APSInt Result;
1942 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1943 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1944 bool IsConstNeg =
1945 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00001946 bool IsConstPos =
1947 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001948 bool IsConstZero = IsConstant && !Result.getBoolValue();
1949 if (UB && (IsConstZero ||
1950 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00001951 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001952 SemaRef.Diag(NewStep->getExprLoc(),
1953 diag::err_omp_loop_incr_not_compatible)
1954 << Var << TestIsLessOp << NewStep->getSourceRange();
1955 SemaRef.Diag(ConditionLoc,
1956 diag::note_omp_loop_cond_requres_compatible_incr)
1957 << TestIsLessOp << ConditionSrcRange;
1958 return true;
1959 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001960 if (TestIsLessOp == Subtract) {
1961 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
1962 NewStep).get();
1963 Subtract = !Subtract;
1964 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001965 }
1966
1967 Step = NewStep;
1968 SubtractStep = Subtract;
1969 return false;
1970}
1971
1972bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1973 // Check init-expr for canonical loop form and save loop counter
1974 // variable - #Var and its initialization value - #LB.
1975 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1976 // var = lb
1977 // integer-type var = lb
1978 // random-access-iterator-type var = lb
1979 // pointer-type var = lb
1980 //
1981 if (!S) {
1982 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1983 return true;
1984 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001985 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001986 if (Expr *E = dyn_cast<Expr>(S))
1987 S = E->IgnoreParens();
1988 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1989 if (BO->getOpcode() == BO_Assign)
1990 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001991 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00001992 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001993 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1994 if (DS->isSingleDecl()) {
1995 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1996 if (Var->hasInit()) {
1997 // Accept non-canonical init form here but emit ext. warning.
1998 if (Var->getInitStyle() != VarDecl::CInit)
1999 SemaRef.Diag(S->getLocStart(),
2000 diag::ext_omp_loop_not_canonical_init)
2001 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002002 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002003 }
2004 }
2005 }
2006 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2007 if (CE->getOperator() == OO_Equal)
2008 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002009 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2010 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002011
2012 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2013 << S->getSourceRange();
2014 return true;
2015}
2016
Alexey Bataev23b69422014-06-18 07:08:49 +00002017/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002018/// variable (which may be the loop variable) if possible.
2019static const VarDecl *GetInitVarDecl(const Expr *E) {
2020 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002021 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002022 E = E->IgnoreParenImpCasts();
2023 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2024 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2025 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2026 CE->getArg(0) != nullptr)
2027 E = CE->getArg(0)->IgnoreParenImpCasts();
2028 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2029 if (!DRE)
2030 return nullptr;
2031 return dyn_cast<VarDecl>(DRE->getDecl());
2032}
2033
2034bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2035 // Check test-expr for canonical form, save upper-bound UB, flags for
2036 // less/greater and for strict/non-strict comparison.
2037 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2038 // var relational-op b
2039 // b relational-op var
2040 //
2041 if (!S) {
2042 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2043 return true;
2044 }
2045 S = S->IgnoreParenImpCasts();
2046 SourceLocation CondLoc = S->getLocStart();
2047 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2048 if (BO->isRelationalOp()) {
2049 if (GetInitVarDecl(BO->getLHS()) == Var)
2050 return SetUB(BO->getRHS(),
2051 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2052 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2053 BO->getSourceRange(), BO->getOperatorLoc());
2054 if (GetInitVarDecl(BO->getRHS()) == Var)
2055 return SetUB(BO->getLHS(),
2056 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2057 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2058 BO->getSourceRange(), BO->getOperatorLoc());
2059 }
2060 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2061 if (CE->getNumArgs() == 2) {
2062 auto Op = CE->getOperator();
2063 switch (Op) {
2064 case OO_Greater:
2065 case OO_GreaterEqual:
2066 case OO_Less:
2067 case OO_LessEqual:
2068 if (GetInitVarDecl(CE->getArg(0)) == Var)
2069 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2070 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2071 CE->getOperatorLoc());
2072 if (GetInitVarDecl(CE->getArg(1)) == Var)
2073 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2074 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2075 CE->getOperatorLoc());
2076 break;
2077 default:
2078 break;
2079 }
2080 }
2081 }
2082 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2083 << S->getSourceRange() << Var;
2084 return true;
2085}
2086
2087bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2088 // RHS of canonical loop form increment can be:
2089 // var + incr
2090 // incr + var
2091 // var - incr
2092 //
2093 RHS = RHS->IgnoreParenImpCasts();
2094 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2095 if (BO->isAdditiveOp()) {
2096 bool IsAdd = BO->getOpcode() == BO_Add;
2097 if (GetInitVarDecl(BO->getLHS()) == Var)
2098 return SetStep(BO->getRHS(), !IsAdd);
2099 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2100 return SetStep(BO->getLHS(), false);
2101 }
2102 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2103 bool IsAdd = CE->getOperator() == OO_Plus;
2104 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2105 if (GetInitVarDecl(CE->getArg(0)) == Var)
2106 return SetStep(CE->getArg(1), !IsAdd);
2107 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2108 return SetStep(CE->getArg(0), false);
2109 }
2110 }
2111 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2112 << RHS->getSourceRange() << Var;
2113 return true;
2114}
2115
2116bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2117 // Check incr-expr for canonical loop form and return true if it
2118 // does not conform.
2119 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2120 // ++var
2121 // var++
2122 // --var
2123 // var--
2124 // var += incr
2125 // var -= incr
2126 // var = var + incr
2127 // var = incr + var
2128 // var = var - incr
2129 //
2130 if (!S) {
2131 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2132 return true;
2133 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002134 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002135 S = S->IgnoreParens();
2136 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2137 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2138 return SetStep(
2139 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2140 (UO->isDecrementOp() ? -1 : 1)).get(),
2141 false);
2142 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2143 switch (BO->getOpcode()) {
2144 case BO_AddAssign:
2145 case BO_SubAssign:
2146 if (GetInitVarDecl(BO->getLHS()) == Var)
2147 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2148 break;
2149 case BO_Assign:
2150 if (GetInitVarDecl(BO->getLHS()) == Var)
2151 return CheckIncRHS(BO->getRHS());
2152 break;
2153 default:
2154 break;
2155 }
2156 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2157 switch (CE->getOperator()) {
2158 case OO_PlusPlus:
2159 case OO_MinusMinus:
2160 if (GetInitVarDecl(CE->getArg(0)) == Var)
2161 return SetStep(
2162 SemaRef.ActOnIntegerConstant(
2163 CE->getLocStart(),
2164 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2165 false);
2166 break;
2167 case OO_PlusEqual:
2168 case OO_MinusEqual:
2169 if (GetInitVarDecl(CE->getArg(0)) == Var)
2170 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2171 break;
2172 case OO_Equal:
2173 if (GetInitVarDecl(CE->getArg(0)) == Var)
2174 return CheckIncRHS(CE->getArg(1));
2175 break;
2176 default:
2177 break;
2178 }
2179 }
2180 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2181 << S->getSourceRange() << Var;
2182 return true;
2183}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002184
2185/// \brief Build the expression to calculate the number of iterations.
2186Expr *OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S) const {
2187 ExprResult Diff;
2188 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2189 SemaRef.getLangOpts().CPlusPlus) {
2190 // Upper - Lower
2191 Expr *Upper = TestIsLessOp ? UB : LB;
2192 Expr *Lower = TestIsLessOp ? LB : UB;
2193
2194 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2195
2196 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2197 // BuildBinOp already emitted error, this one is to point user to upper
2198 // and lower bound, and to tell what is passed to 'operator-'.
2199 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2200 << Upper->getSourceRange() << Lower->getSourceRange();
2201 return nullptr;
2202 }
2203 }
2204
2205 if (!Diff.isUsable())
2206 return nullptr;
2207
2208 // Upper - Lower [- 1]
2209 if (TestIsStrictOp)
2210 Diff = SemaRef.BuildBinOp(
2211 S, DefaultLoc, BO_Sub, Diff.get(),
2212 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2213 if (!Diff.isUsable())
2214 return nullptr;
2215
2216 // Upper - Lower [- 1] + Step
2217 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2218 Step->IgnoreImplicit());
2219 if (!Diff.isUsable())
2220 return nullptr;
2221
2222 // Parentheses (for dumping/debugging purposes only).
2223 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2224 if (!Diff.isUsable())
2225 return nullptr;
2226
2227 // (Upper - Lower [- 1] + Step) / Step
2228 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2229 Step->IgnoreImplicit());
2230 if (!Diff.isUsable())
2231 return nullptr;
2232
2233 return Diff.get();
2234}
2235
2236/// \brief Build reference expression to the counter be used for codegen.
2237Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2238 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2239 GetIncrementSrcRange().getBegin(), Var, false,
2240 DefaultLoc, Var->getType(), VK_LValue);
2241}
2242
2243/// \brief Build initization of the counter be used for codegen.
2244Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2245
2246/// \brief Build step of the counter be used for codegen.
2247Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2248
2249/// \brief Iteration space of a single for loop.
2250struct LoopIterationSpace {
2251 /// \brief This expression calculates the number of iterations in the loop.
2252 /// It is always possible to calculate it before starting the loop.
2253 Expr *NumIterations;
2254 /// \brief The loop counter variable.
2255 Expr *CounterVar;
2256 /// \brief This is initializer for the initial value of #CounterVar.
2257 Expr *CounterInit;
2258 /// \brief This is step for the #CounterVar used to generate its update:
2259 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2260 Expr *CounterStep;
2261 /// \brief Should step be subtracted?
2262 bool Subtract;
2263 /// \brief Source range of the loop init.
2264 SourceRange InitSrcRange;
2265 /// \brief Source range of the loop condition.
2266 SourceRange CondSrcRange;
2267 /// \brief Source range of the loop increment.
2268 SourceRange IncSrcRange;
2269};
2270
2271/// \brief The resulting expressions built for the OpenMP loop CodeGen for the
2272/// whole collapsed loop nest. See class OMPLoopDirective for their description.
2273struct BuiltLoopExprs {
2274 Expr *IterationVarRef;
2275 Expr *LastIteration;
2276 Expr *CalcLastIteration;
2277 Expr *PreCond;
2278 Expr *Cond;
2279 Expr *SeparatedCond;
2280 Expr *Init;
2281 Expr *Inc;
2282 SmallVector<Expr *, 4> Counters;
2283 SmallVector<Expr *, 4> Updates;
2284 SmallVector<Expr *, 4> Finals;
2285
2286 bool builtAll() {
2287 return IterationVarRef != nullptr && LastIteration != nullptr &&
2288 PreCond != nullptr && Cond != nullptr && SeparatedCond != nullptr &&
2289 Init != nullptr && Inc != nullptr;
2290 }
2291 void clear(unsigned size) {
2292 IterationVarRef = nullptr;
2293 LastIteration = nullptr;
2294 CalcLastIteration = nullptr;
2295 PreCond = nullptr;
2296 Cond = nullptr;
2297 SeparatedCond = nullptr;
2298 Init = nullptr;
2299 Inc = nullptr;
2300 Counters.resize(size);
2301 Updates.resize(size);
2302 Finals.resize(size);
2303 for (unsigned i = 0; i < size; ++i) {
2304 Counters[i] = nullptr;
2305 Updates[i] = nullptr;
2306 Finals[i] = nullptr;
2307 }
2308 }
2309};
2310
Alexey Bataev23b69422014-06-18 07:08:49 +00002311} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002312
2313/// \brief Called on a for stmt to check and extract its iteration space
2314/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002315static bool CheckOpenMPIterationSpace(
2316 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2317 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2318 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002319 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2320 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002321 // OpenMP [2.6, Canonical Loop Form]
2322 // for (init-expr; test-expr; incr-expr) structured-block
2323 auto For = dyn_cast_or_null<ForStmt>(S);
2324 if (!For) {
2325 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002326 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2327 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2328 << CurrentNestedLoopCount;
2329 if (NestedLoopCount > 1)
2330 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2331 diag::note_omp_collapse_expr)
2332 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002333 return true;
2334 }
2335 assert(For->getBody());
2336
2337 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2338
2339 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002340 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002341 if (ISC.CheckInit(Init)) {
2342 return true;
2343 }
2344
2345 bool HasErrors = false;
2346
2347 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002348 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002349
2350 // OpenMP [2.6, Canonical Loop Form]
2351 // Var is one of the following:
2352 // A variable of signed or unsigned integer type.
2353 // For C++, a variable of a random access iterator type.
2354 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002355 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002356 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2357 !VarType->isPointerType() &&
2358 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2359 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2360 << SemaRef.getLangOpts().CPlusPlus;
2361 HasErrors = true;
2362 }
2363
Alexey Bataev4acb8592014-07-07 13:01:15 +00002364 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2365 // Construct
2366 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2367 // parallel for construct is (are) private.
2368 // The loop iteration variable in the associated for-loop of a simd construct
2369 // with just one associated for-loop is linear with a constant-linear-step
2370 // that is the increment of the associated for-loop.
2371 // Exclude loop var from the list of variables with implicitly defined data
2372 // sharing attributes.
2373 while (VarsWithImplicitDSA.count(Var) > 0)
2374 VarsWithImplicitDSA.erase(Var);
2375
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002376 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2377 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002378 // The loop iteration variable in the associated for-loop of a simd construct
2379 // with just one associated for-loop may be listed in a linear clause with a
2380 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002381 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2382 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002383 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002384 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2385 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2386 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002387 auto PredeterminedCKind =
2388 isOpenMPSimdDirective(DKind)
2389 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2390 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002391 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002392 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002393 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2394 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2395 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002396 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002397 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002398 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2399 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002400 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002401 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002402 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002403 // Make the loop iteration variable private (for worksharing constructs),
2404 // linear (for simd directives with the only one associated loop) or
2405 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002406 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002407 }
2408
Alexey Bataev7ff55242014-06-19 09:13:45 +00002409 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002410
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002411 // Check test-expr.
2412 HasErrors |= ISC.CheckCond(For->getCond());
2413
2414 // Check incr-expr.
2415 HasErrors |= ISC.CheckInc(For->getInc());
2416
Alexander Musmana5f070a2014-10-01 06:03:56 +00002417 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002418 return HasErrors;
2419
Alexander Musmana5f070a2014-10-01 06:03:56 +00002420 // Build the loop's iteration space representation.
2421 ResultIterSpace.NumIterations = ISC.BuildNumIterations(DSA.getCurScope());
2422 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2423 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2424 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2425 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2426 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2427 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2428 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2429
2430 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2431 ResultIterSpace.CounterVar == nullptr ||
2432 ResultIterSpace.CounterInit == nullptr ||
2433 ResultIterSpace.CounterStep == nullptr);
2434
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002435 return HasErrors;
2436}
2437
Alexander Musmana5f070a2014-10-01 06:03:56 +00002438/// \brief Build a variable declaration for OpenMP loop iteration variable.
2439static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2440 StringRef Name) {
2441 DeclContext *DC = SemaRef.CurContext;
2442 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2443 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2444 VarDecl *Decl =
2445 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2446 Decl->setImplicit();
2447 return Decl;
2448}
2449
2450/// \brief Build 'VarRef = Start + Iter * Step'.
2451static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2452 SourceLocation Loc, ExprResult VarRef,
2453 ExprResult Start, ExprResult Iter,
2454 ExprResult Step, bool Subtract) {
2455 // Add parentheses (for debugging purposes only).
2456 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2457 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2458 !Step.isUsable())
2459 return ExprError();
2460
2461 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2462 Step.get()->IgnoreImplicit());
2463 if (!Update.isUsable())
2464 return ExprError();
2465
2466 // Build 'VarRef = Start + Iter * Step'.
2467 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2468 Start.get()->IgnoreImplicit(), Update.get());
2469 if (!Update.isUsable())
2470 return ExprError();
2471
2472 Update = SemaRef.PerformImplicitConversion(
2473 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2474 if (!Update.isUsable())
2475 return ExprError();
2476
2477 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2478 return Update;
2479}
2480
2481/// \brief Convert integer expression \a E to make it have at least \a Bits
2482/// bits.
2483static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2484 Sema &SemaRef) {
2485 if (E == nullptr)
2486 return ExprError();
2487 auto &C = SemaRef.Context;
2488 QualType OldType = E->getType();
2489 unsigned HasBits = C.getTypeSize(OldType);
2490 if (HasBits >= Bits)
2491 return ExprResult(E);
2492 // OK to convert to signed, because new type has more bits than old.
2493 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2494 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2495 true);
2496}
2497
2498/// \brief Check if the given expression \a E is a constant integer that fits
2499/// into \a Bits bits.
2500static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2501 if (E == nullptr)
2502 return false;
2503 llvm::APSInt Result;
2504 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2505 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2506 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002507}
2508
2509/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002510/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2511/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002512static unsigned
2513CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2514 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002515 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2516 BuiltLoopExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002517 unsigned NestedLoopCount = 1;
2518 if (NestedLoopCountExpr) {
2519 // Found 'collapse' clause - calculate collapse number.
2520 llvm::APSInt Result;
2521 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2522 NestedLoopCount = Result.getLimitedValue();
2523 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002524 // This is helper routine for loop directives (e.g., 'for', 'simd',
2525 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002526 SmallVector<LoopIterationSpace, 4> IterSpaces;
2527 IterSpaces.resize(NestedLoopCount);
2528 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002529 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002530 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002531 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002532 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002533 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002534 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002535 // OpenMP [2.8.1, simd construct, Restrictions]
2536 // All loops associated with the construct must be perfectly nested; that
2537 // is, there must be no intervening code nor any OpenMP directive between
2538 // any two loops.
2539 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002540 }
2541
Alexander Musmana5f070a2014-10-01 06:03:56 +00002542 Built.clear(/* size */ NestedLoopCount);
2543
2544 if (SemaRef.CurContext->isDependentContext())
2545 return NestedLoopCount;
2546
2547 // An example of what is generated for the following code:
2548 //
2549 // #pragma omp simd collapse(2)
2550 // for (i = 0; i < NI; ++i)
2551 // for (j = J0; j < NJ; j+=2) {
2552 // <loop body>
2553 // }
2554 //
2555 // We generate the code below.
2556 // Note: the loop body may be outlined in CodeGen.
2557 // Note: some counters may be C++ classes, operator- is used to find number of
2558 // iterations and operator+= to calculate counter value.
2559 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2560 // or i64 is currently supported).
2561 //
2562 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2563 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2564 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2565 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2566 // // similar updates for vars in clauses (e.g. 'linear')
2567 // <loop body (using local i and j)>
2568 // }
2569 // i = NI; // assign final values of counters
2570 // j = NJ;
2571 //
2572
2573 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2574 // the iteration counts of the collapsed for loops.
2575 auto N0 = IterSpaces[0].NumIterations;
2576 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2577 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2578
2579 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2580 return NestedLoopCount;
2581
2582 auto &C = SemaRef.Context;
2583 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2584
2585 Scope *CurScope = DSA.getCurScope();
2586 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2587 auto N = IterSpaces[Cnt].NumIterations;
2588 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2589 if (LastIteration32.isUsable())
2590 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2591 LastIteration32.get(), N);
2592 if (LastIteration64.isUsable())
2593 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2594 LastIteration64.get(), N);
2595 }
2596
2597 // Choose either the 32-bit or 64-bit version.
2598 ExprResult LastIteration = LastIteration64;
2599 if (LastIteration32.isUsable() &&
2600 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2601 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2602 FitsInto(
2603 32 /* Bits */,
2604 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2605 LastIteration64.get(), SemaRef)))
2606 LastIteration = LastIteration32;
2607
2608 if (!LastIteration.isUsable())
2609 return 0;
2610
2611 // Save the number of iterations.
2612 ExprResult NumIterations = LastIteration;
2613 {
2614 LastIteration = SemaRef.BuildBinOp(
2615 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2616 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2617 if (!LastIteration.isUsable())
2618 return 0;
2619 }
2620
2621 // Calculate the last iteration number beforehand instead of doing this on
2622 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2623 llvm::APSInt Result;
2624 bool IsConstant =
2625 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2626 ExprResult CalcLastIteration;
2627 if (!IsConstant) {
2628 SourceLocation SaveLoc;
2629 VarDecl *SaveVar =
2630 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2631 ".omp.last.iteration");
2632 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2633 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2634 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2635 SaveRef.get(), LastIteration.get());
2636 LastIteration = SaveRef;
2637
2638 // Prepare SaveRef + 1.
2639 NumIterations = SemaRef.BuildBinOp(
2640 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2641 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2642 if (!NumIterations.isUsable())
2643 return 0;
2644 }
2645
2646 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2647
2648 // Precondition tests if there is at least one iteration (LastIteration > 0).
2649 ExprResult PreCond = SemaRef.BuildBinOp(
2650 CurScope, InitLoc, BO_GT, LastIteration.get(),
2651 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2652
2653 // Build the iteration variable and its initialization to zero before loop.
2654 ExprResult IV;
2655 ExprResult Init;
2656 {
2657 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc,
2658 LastIteration.get()->getType(), ".omp.iv");
2659 IV = SemaRef.BuildDeclRefExpr(IVDecl, LastIteration.get()->getType(),
2660 VK_LValue, InitLoc);
2661 Init = SemaRef.BuildBinOp(
2662 CurScope, InitLoc, BO_Assign, IV.get(),
2663 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2664 }
2665
2666 // Loop condition (IV < NumIterations)
2667 SourceLocation CondLoc;
2668 ExprResult Cond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2669 NumIterations.get());
2670 // Loop condition with 1 iteration separated (IV < LastIteration)
2671 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2672 IV.get(), LastIteration.get());
2673
2674 // Loop increment (IV = IV + 1)
2675 SourceLocation IncLoc;
2676 ExprResult Inc =
2677 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2678 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2679 if (!Inc.isUsable())
2680 return 0;
2681 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
2682
2683 // Build updates and final values of the loop counters.
2684 bool HasErrors = false;
2685 Built.Counters.resize(NestedLoopCount);
2686 Built.Updates.resize(NestedLoopCount);
2687 Built.Finals.resize(NestedLoopCount);
2688 {
2689 ExprResult Div;
2690 // Go from inner nested loop to outer.
2691 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2692 LoopIterationSpace &IS = IterSpaces[Cnt];
2693 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2694 // Build: Iter = (IV / Div) % IS.NumIters
2695 // where Div is product of previous iterations' IS.NumIters.
2696 ExprResult Iter;
2697 if (Div.isUsable()) {
2698 Iter =
2699 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2700 } else {
2701 Iter = IV;
2702 assert((Cnt == (int)NestedLoopCount - 1) &&
2703 "unusable div expected on first iteration only");
2704 }
2705
2706 if (Cnt != 0 && Iter.isUsable())
2707 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2708 IS.NumIterations);
2709 if (!Iter.isUsable()) {
2710 HasErrors = true;
2711 break;
2712 }
2713
2714 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2715 ExprResult Update =
2716 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2717 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2718 if (!Update.isUsable()) {
2719 HasErrors = true;
2720 break;
2721 }
2722
2723 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2724 ExprResult Final = BuildCounterUpdate(
2725 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2726 IS.NumIterations, IS.CounterStep, IS.Subtract);
2727 if (!Final.isUsable()) {
2728 HasErrors = true;
2729 break;
2730 }
2731
2732 // Build Div for the next iteration: Div <- Div * IS.NumIters
2733 if (Cnt != 0) {
2734 if (Div.isUnset())
2735 Div = IS.NumIterations;
2736 else
2737 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2738 IS.NumIterations);
2739
2740 // Add parentheses (for debugging purposes only).
2741 if (Div.isUsable())
2742 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2743 if (!Div.isUsable()) {
2744 HasErrors = true;
2745 break;
2746 }
2747 }
2748 if (!Update.isUsable() || !Final.isUsable()) {
2749 HasErrors = true;
2750 break;
2751 }
2752 // Save results
2753 Built.Counters[Cnt] = IS.CounterVar;
2754 Built.Updates[Cnt] = Update.get();
2755 Built.Finals[Cnt] = Final.get();
2756 }
2757 }
2758
2759 if (HasErrors)
2760 return 0;
2761
2762 // Save results
2763 Built.IterationVarRef = IV.get();
2764 Built.LastIteration = LastIteration.get();
2765 Built.CalcLastIteration = CalcLastIteration.get();
2766 Built.PreCond = PreCond.get();
2767 Built.Cond = Cond.get();
2768 Built.SeparatedCond = SeparatedCond.get();
2769 Built.Init = Init.get();
2770 Built.Inc = Inc.get();
2771
Alexey Bataevabfc0692014-06-25 06:52:00 +00002772 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002773}
2774
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002775static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002776 auto CollapseFilter = [](const OMPClause *C) -> bool {
2777 return C->getClauseKind() == OMPC_collapse;
2778 };
2779 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2780 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002781 if (I)
2782 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2783 return nullptr;
2784}
2785
Alexey Bataev4acb8592014-07-07 13:01:15 +00002786StmtResult Sema::ActOnOpenMPSimdDirective(
2787 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2788 SourceLocation EndLoc,
2789 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002790 BuiltLoopExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002791 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002792 unsigned NestedLoopCount =
2793 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002794 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002795 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002796 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002797
Alexander Musmana5f070a2014-10-01 06:03:56 +00002798 assert((CurContext->isDependentContext() || B.builtAll()) &&
2799 "omp simd loop exprs were not built");
2800
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002801 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002802 return OMPSimdDirective::Create(
2803 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2804 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2805 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002806}
2807
Alexey Bataev4acb8592014-07-07 13:01:15 +00002808StmtResult Sema::ActOnOpenMPForDirective(
2809 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2810 SourceLocation EndLoc,
2811 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002812 BuiltLoopExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002813 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002814 unsigned NestedLoopCount =
2815 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002816 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002817 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002818 return StmtError();
2819
Alexander Musmana5f070a2014-10-01 06:03:56 +00002820 assert((CurContext->isDependentContext() || B.builtAll()) &&
2821 "omp for loop exprs were not built");
2822
Alexey Bataevf29276e2014-06-18 04:14:57 +00002823 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002824 return OMPForDirective::Create(
2825 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2826 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2827 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002828}
2829
Alexander Musmanf82886e2014-09-18 05:12:34 +00002830StmtResult Sema::ActOnOpenMPForSimdDirective(
2831 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2832 SourceLocation EndLoc,
2833 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002834 BuiltLoopExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002835 // In presence of clause 'collapse', it will define the nested loops number.
2836 unsigned NestedLoopCount =
2837 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002838 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002839 if (NestedLoopCount == 0)
2840 return StmtError();
2841
2842 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002843 return OMPForSimdDirective::Create(
2844 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2845 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2846 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002847}
2848
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002849StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2850 Stmt *AStmt,
2851 SourceLocation StartLoc,
2852 SourceLocation EndLoc) {
2853 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2854 auto BaseStmt = AStmt;
2855 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2856 BaseStmt = CS->getCapturedStmt();
2857 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2858 auto S = C->children();
2859 if (!S)
2860 return StmtError();
2861 // All associated statements must be '#pragma omp section' except for
2862 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002863 for (++S; S; ++S) {
2864 auto SectionStmt = *S;
2865 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2866 if (SectionStmt)
2867 Diag(SectionStmt->getLocStart(),
2868 diag::err_omp_sections_substmt_not_section);
2869 return StmtError();
2870 }
2871 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002872 } else {
2873 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2874 return StmtError();
2875 }
2876
2877 getCurFunction()->setHasBranchProtectedScope();
2878
2879 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2880 AStmt);
2881}
2882
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002883StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2884 SourceLocation StartLoc,
2885 SourceLocation EndLoc) {
2886 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2887
2888 getCurFunction()->setHasBranchProtectedScope();
2889
2890 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2891}
2892
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002893StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2894 Stmt *AStmt,
2895 SourceLocation StartLoc,
2896 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002897 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2898
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002899 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002900
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002901 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2902}
2903
Alexander Musman80c22892014-07-17 08:54:58 +00002904StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2905 SourceLocation StartLoc,
2906 SourceLocation EndLoc) {
2907 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2908
2909 getCurFunction()->setHasBranchProtectedScope();
2910
2911 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2912}
2913
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002914StmtResult
2915Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2916 Stmt *AStmt, SourceLocation StartLoc,
2917 SourceLocation EndLoc) {
2918 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2919
2920 getCurFunction()->setHasBranchProtectedScope();
2921
2922 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2923 AStmt);
2924}
2925
Alexey Bataev4acb8592014-07-07 13:01:15 +00002926StmtResult Sema::ActOnOpenMPParallelForDirective(
2927 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2928 SourceLocation EndLoc,
2929 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2930 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2931 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2932 // 1.2.2 OpenMP Language Terminology
2933 // Structured block - An executable statement with a single entry at the
2934 // top and a single exit at the bottom.
2935 // The point of exit cannot be a branch out of the structured block.
2936 // longjmp() and throw() must not violate the entry/exit criteria.
2937 CS->getCapturedDecl()->setNothrow();
2938
Alexander Musmana5f070a2014-10-01 06:03:56 +00002939 BuiltLoopExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002940 // In presence of clause 'collapse', it will define the nested loops number.
2941 unsigned NestedLoopCount =
2942 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002943 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002944 if (NestedLoopCount == 0)
2945 return StmtError();
2946
Alexander Musmana5f070a2014-10-01 06:03:56 +00002947 assert((CurContext->isDependentContext() || B.builtAll()) &&
2948 "omp parallel for loop exprs were not built");
2949
Alexey Bataev4acb8592014-07-07 13:01:15 +00002950 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002951 return OMPParallelForDirective::Create(
2952 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2953 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2954 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002955}
2956
Alexander Musmane4e893b2014-09-23 09:33:00 +00002957StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
2958 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2959 SourceLocation EndLoc,
2960 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2961 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2962 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2963 // 1.2.2 OpenMP Language Terminology
2964 // Structured block - An executable statement with a single entry at the
2965 // top and a single exit at the bottom.
2966 // The point of exit cannot be a branch out of the structured block.
2967 // longjmp() and throw() must not violate the entry/exit criteria.
2968 CS->getCapturedDecl()->setNothrow();
2969
Alexander Musmana5f070a2014-10-01 06:03:56 +00002970 BuiltLoopExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002971 // In presence of clause 'collapse', it will define the nested loops number.
2972 unsigned NestedLoopCount =
2973 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002974 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002975 if (NestedLoopCount == 0)
2976 return StmtError();
2977
2978 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002979 return OMPParallelForSimdDirective::Create(
2980 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2981 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2982 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002983}
2984
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002985StmtResult
2986Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2987 Stmt *AStmt, SourceLocation StartLoc,
2988 SourceLocation EndLoc) {
2989 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2990 auto BaseStmt = AStmt;
2991 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2992 BaseStmt = CS->getCapturedStmt();
2993 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2994 auto S = C->children();
2995 if (!S)
2996 return StmtError();
2997 // All associated statements must be '#pragma omp section' except for
2998 // the first one.
2999 for (++S; S; ++S) {
3000 auto SectionStmt = *S;
3001 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3002 if (SectionStmt)
3003 Diag(SectionStmt->getLocStart(),
3004 diag::err_omp_parallel_sections_substmt_not_section);
3005 return StmtError();
3006 }
3007 }
3008 } else {
3009 Diag(AStmt->getLocStart(),
3010 diag::err_omp_parallel_sections_not_compound_stmt);
3011 return StmtError();
3012 }
3013
3014 getCurFunction()->setHasBranchProtectedScope();
3015
3016 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3017 Clauses, AStmt);
3018}
3019
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003020StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3021 Stmt *AStmt, SourceLocation StartLoc,
3022 SourceLocation EndLoc) {
3023 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3024 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3025 // 1.2.2 OpenMP Language Terminology
3026 // Structured block - An executable statement with a single entry at the
3027 // top and a single exit at the bottom.
3028 // The point of exit cannot be a branch out of the structured block.
3029 // longjmp() and throw() must not violate the entry/exit criteria.
3030 CS->getCapturedDecl()->setNothrow();
3031
3032 getCurFunction()->setHasBranchProtectedScope();
3033
3034 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3035}
3036
Alexey Bataev68446b72014-07-18 07:47:19 +00003037StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3038 SourceLocation EndLoc) {
3039 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3040}
3041
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003042StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3043 SourceLocation EndLoc) {
3044 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3045}
3046
Alexey Bataev2df347a2014-07-18 10:17:07 +00003047StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3048 SourceLocation EndLoc) {
3049 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3050}
3051
Alexey Bataev6125da92014-07-21 11:26:11 +00003052StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3053 SourceLocation StartLoc,
3054 SourceLocation EndLoc) {
3055 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3056 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3057}
3058
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003059StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3060 SourceLocation StartLoc,
3061 SourceLocation EndLoc) {
3062 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3063
3064 getCurFunction()->setHasBranchProtectedScope();
3065
3066 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3067}
3068
Alexey Bataev0162e452014-07-22 10:10:35 +00003069StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3070 Stmt *AStmt,
3071 SourceLocation StartLoc,
3072 SourceLocation EndLoc) {
3073 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003074 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003075 // 1.2.2 OpenMP Language Terminology
3076 // Structured block - An executable statement with a single entry at the
3077 // top and a single exit at the bottom.
3078 // The point of exit cannot be a branch out of the structured block.
3079 // longjmp() and throw() must not violate the entry/exit criteria.
3080 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003081 OpenMPClauseKind AtomicKind = OMPC_unknown;
3082 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003083 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003084 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003085 C->getClauseKind() == OMPC_update ||
3086 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003087 if (AtomicKind != OMPC_unknown) {
3088 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3089 << SourceRange(C->getLocStart(), C->getLocEnd());
3090 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3091 << getOpenMPClauseName(AtomicKind);
3092 } else {
3093 AtomicKind = C->getClauseKind();
3094 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003095 }
3096 }
3097 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003098 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00003099 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003100 if (!isa<Expr>(Body)) {
3101 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003102 diag::err_omp_atomic_read_not_expression_statement);
3103 return StmtError();
3104 }
3105 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003106 if (!isa<Expr>(Body)) {
3107 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003108 diag::err_omp_atomic_write_not_expression_statement);
3109 return StmtError();
3110 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00003111 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003112 if (!isa<Expr>(Body)) {
3113 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003114 diag::err_omp_atomic_update_not_expression_statement)
3115 << (AtomicKind == OMPC_update);
3116 return StmtError();
3117 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003118 } else if (AtomicKind == OMPC_capture) {
3119 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3120 Diag(Body->getLocStart(),
3121 diag::err_omp_atomic_capture_not_expression_statement);
3122 return StmtError();
3123 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3124 Diag(Body->getLocStart(),
3125 diag::err_omp_atomic_capture_not_compound_statement);
3126 return StmtError();
3127 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003128 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003129
3130 getCurFunction()->setHasBranchProtectedScope();
3131
3132 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3133}
3134
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003135StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3136 Stmt *AStmt,
3137 SourceLocation StartLoc,
3138 SourceLocation EndLoc) {
3139 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3140
3141 getCurFunction()->setHasBranchProtectedScope();
3142
3143 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3144}
3145
Alexey Bataeved09d242014-05-28 05:53:51 +00003146OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003147 SourceLocation StartLoc,
3148 SourceLocation LParenLoc,
3149 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003150 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003151 switch (Kind) {
3152 case OMPC_if:
3153 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3154 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003155 case OMPC_final:
3156 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3157 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003158 case OMPC_num_threads:
3159 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3160 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003161 case OMPC_safelen:
3162 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3163 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003164 case OMPC_collapse:
3165 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3166 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003167 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003168 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003169 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003170 case OMPC_private:
3171 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003172 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003173 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003174 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003175 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003176 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003177 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003178 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003179 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003180 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003181 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003182 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003183 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003184 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003185 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003186 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003187 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003188 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003189 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003190 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003191 llvm_unreachable("Clause is not allowed.");
3192 }
3193 return Res;
3194}
3195
Alexey Bataeved09d242014-05-28 05:53:51 +00003196OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003197 SourceLocation LParenLoc,
3198 SourceLocation EndLoc) {
3199 Expr *ValExpr = Condition;
3200 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3201 !Condition->isInstantiationDependent() &&
3202 !Condition->containsUnexpandedParameterPack()) {
3203 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003204 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003205 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003206 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003207
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003208 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003209 }
3210
3211 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3212}
3213
Alexey Bataev3778b602014-07-17 07:32:53 +00003214OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3215 SourceLocation StartLoc,
3216 SourceLocation LParenLoc,
3217 SourceLocation EndLoc) {
3218 Expr *ValExpr = Condition;
3219 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3220 !Condition->isInstantiationDependent() &&
3221 !Condition->containsUnexpandedParameterPack()) {
3222 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3223 Condition->getExprLoc(), Condition);
3224 if (Val.isInvalid())
3225 return nullptr;
3226
3227 ValExpr = Val.get();
3228 }
3229
3230 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3231}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003232ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3233 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003234 if (!Op)
3235 return ExprError();
3236
3237 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3238 public:
3239 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003240 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003241 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3242 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003243 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3244 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003245 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3246 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003247 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3248 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003249 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3250 QualType T,
3251 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003252 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3253 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003254 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3255 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003256 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003257 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003258 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003259 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3260 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003261 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3262 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003263 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3264 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003265 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003266 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003267 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003268 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3269 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003270 llvm_unreachable("conversion functions are permitted");
3271 }
3272 } ConvertDiagnoser;
3273 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3274}
3275
3276OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3277 SourceLocation StartLoc,
3278 SourceLocation LParenLoc,
3279 SourceLocation EndLoc) {
3280 Expr *ValExpr = NumThreads;
3281 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
3282 !NumThreads->isInstantiationDependent() &&
3283 !NumThreads->containsUnexpandedParameterPack()) {
3284 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3285 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003286 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003287 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003288 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003289
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003290 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003291
3292 // OpenMP [2.5, Restrictions]
3293 // The num_threads expression must evaluate to a positive integer value.
3294 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003295 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3296 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003297 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3298 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003299 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003300 }
3301 }
3302
Alexey Bataeved09d242014-05-28 05:53:51 +00003303 return new (Context)
3304 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003305}
3306
Alexey Bataev62c87d22014-03-21 04:51:18 +00003307ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3308 OpenMPClauseKind CKind) {
3309 if (!E)
3310 return ExprError();
3311 if (E->isValueDependent() || E->isTypeDependent() ||
3312 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003313 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003314 llvm::APSInt Result;
3315 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3316 if (ICE.isInvalid())
3317 return ExprError();
3318 if (!Result.isStrictlyPositive()) {
3319 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3320 << getOpenMPClauseName(CKind) << E->getSourceRange();
3321 return ExprError();
3322 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003323 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3324 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3325 << E->getSourceRange();
3326 return ExprError();
3327 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003328 return ICE;
3329}
3330
3331OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3332 SourceLocation LParenLoc,
3333 SourceLocation EndLoc) {
3334 // OpenMP [2.8.1, simd construct, Description]
3335 // The parameter of the safelen clause must be a constant
3336 // positive integer expression.
3337 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3338 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003339 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003340 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003341 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003342}
3343
Alexander Musman64d33f12014-06-04 07:53:32 +00003344OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3345 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003346 SourceLocation LParenLoc,
3347 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003348 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003349 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003350 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003351 // The parameter of the collapse clause must be a constant
3352 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003353 ExprResult NumForLoopsResult =
3354 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3355 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003356 return nullptr;
3357 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003358 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003359}
3360
Alexey Bataeved09d242014-05-28 05:53:51 +00003361OMPClause *Sema::ActOnOpenMPSimpleClause(
3362 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3363 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003364 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003365 switch (Kind) {
3366 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003367 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003368 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3369 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003370 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003371 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003372 Res = ActOnOpenMPProcBindClause(
3373 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3374 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003375 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003376 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003377 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003378 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003379 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003380 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003381 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003382 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003383 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003384 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003385 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003386 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003387 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003388 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003389 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003390 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003391 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003392 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003393 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003394 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003395 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003396 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003397 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003398 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003399 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003400 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003401 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003402 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003403 llvm_unreachable("Clause is not allowed.");
3404 }
3405 return Res;
3406}
3407
3408OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3409 SourceLocation KindKwLoc,
3410 SourceLocation StartLoc,
3411 SourceLocation LParenLoc,
3412 SourceLocation EndLoc) {
3413 if (Kind == OMPC_DEFAULT_unknown) {
3414 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003415 static_assert(OMPC_DEFAULT_unknown > 0,
3416 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003417 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003418 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003419 Values += "'";
3420 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3421 Values += "'";
3422 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003423 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003424 Values += " or ";
3425 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003426 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003427 break;
3428 default:
3429 Values += Sep;
3430 break;
3431 }
3432 }
3433 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003434 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003435 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003436 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003437 switch (Kind) {
3438 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003439 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003440 break;
3441 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003442 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003443 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003444 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003445 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003446 break;
3447 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003448 return new (Context)
3449 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003450}
3451
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003452OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3453 SourceLocation KindKwLoc,
3454 SourceLocation StartLoc,
3455 SourceLocation LParenLoc,
3456 SourceLocation EndLoc) {
3457 if (Kind == OMPC_PROC_BIND_unknown) {
3458 std::string Values;
3459 std::string Sep(", ");
3460 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3461 Values += "'";
3462 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3463 Values += "'";
3464 switch (i) {
3465 case OMPC_PROC_BIND_unknown - 2:
3466 Values += " or ";
3467 break;
3468 case OMPC_PROC_BIND_unknown - 1:
3469 break;
3470 default:
3471 Values += Sep;
3472 break;
3473 }
3474 }
3475 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003476 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003477 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003478 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003479 return new (Context)
3480 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003481}
3482
Alexey Bataev56dafe82014-06-20 07:16:17 +00003483OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3484 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3485 SourceLocation StartLoc, SourceLocation LParenLoc,
3486 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3487 SourceLocation EndLoc) {
3488 OMPClause *Res = nullptr;
3489 switch (Kind) {
3490 case OMPC_schedule:
3491 Res = ActOnOpenMPScheduleClause(
3492 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3493 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3494 break;
3495 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003496 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003497 case OMPC_num_threads:
3498 case OMPC_safelen:
3499 case OMPC_collapse:
3500 case OMPC_default:
3501 case OMPC_proc_bind:
3502 case OMPC_private:
3503 case OMPC_firstprivate:
3504 case OMPC_lastprivate:
3505 case OMPC_shared:
3506 case OMPC_reduction:
3507 case OMPC_linear:
3508 case OMPC_aligned:
3509 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003510 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003511 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003512 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003513 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003514 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003515 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003516 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003517 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003518 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003519 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003520 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003521 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003522 case OMPC_unknown:
3523 llvm_unreachable("Clause is not allowed.");
3524 }
3525 return Res;
3526}
3527
3528OMPClause *Sema::ActOnOpenMPScheduleClause(
3529 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3530 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3531 SourceLocation EndLoc) {
3532 if (Kind == OMPC_SCHEDULE_unknown) {
3533 std::string Values;
3534 std::string Sep(", ");
3535 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3536 Values += "'";
3537 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3538 Values += "'";
3539 switch (i) {
3540 case OMPC_SCHEDULE_unknown - 2:
3541 Values += " or ";
3542 break;
3543 case OMPC_SCHEDULE_unknown - 1:
3544 break;
3545 default:
3546 Values += Sep;
3547 break;
3548 }
3549 }
3550 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3551 << Values << getOpenMPClauseName(OMPC_schedule);
3552 return nullptr;
3553 }
3554 Expr *ValExpr = ChunkSize;
3555 if (ChunkSize) {
3556 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3557 !ChunkSize->isInstantiationDependent() &&
3558 !ChunkSize->containsUnexpandedParameterPack()) {
3559 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3560 ExprResult Val =
3561 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3562 if (Val.isInvalid())
3563 return nullptr;
3564
3565 ValExpr = Val.get();
3566
3567 // OpenMP [2.7.1, Restrictions]
3568 // chunk_size must be a loop invariant integer expression with a positive
3569 // value.
3570 llvm::APSInt Result;
3571 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3572 Result.isSigned() && !Result.isStrictlyPositive()) {
3573 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3574 << "schedule" << ChunkSize->getSourceRange();
3575 return nullptr;
3576 }
3577 }
3578 }
3579
3580 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3581 EndLoc, Kind, ValExpr);
3582}
3583
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003584OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3585 SourceLocation StartLoc,
3586 SourceLocation EndLoc) {
3587 OMPClause *Res = nullptr;
3588 switch (Kind) {
3589 case OMPC_ordered:
3590 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3591 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003592 case OMPC_nowait:
3593 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3594 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003595 case OMPC_untied:
3596 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3597 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003598 case OMPC_mergeable:
3599 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3600 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003601 case OMPC_read:
3602 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3603 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003604 case OMPC_write:
3605 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3606 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003607 case OMPC_update:
3608 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3609 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003610 case OMPC_capture:
3611 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3612 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003613 case OMPC_seq_cst:
3614 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3615 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003616 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003617 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003618 case OMPC_num_threads:
3619 case OMPC_safelen:
3620 case OMPC_collapse:
3621 case OMPC_schedule:
3622 case OMPC_private:
3623 case OMPC_firstprivate:
3624 case OMPC_lastprivate:
3625 case OMPC_shared:
3626 case OMPC_reduction:
3627 case OMPC_linear:
3628 case OMPC_aligned:
3629 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003630 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003631 case OMPC_default:
3632 case OMPC_proc_bind:
3633 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003634 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003635 case OMPC_unknown:
3636 llvm_unreachable("Clause is not allowed.");
3637 }
3638 return Res;
3639}
3640
3641OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3642 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003643 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003644 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3645}
3646
Alexey Bataev236070f2014-06-20 11:19:47 +00003647OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3648 SourceLocation EndLoc) {
3649 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3650}
3651
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003652OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3653 SourceLocation EndLoc) {
3654 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3655}
3656
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003657OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3658 SourceLocation EndLoc) {
3659 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3660}
3661
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003662OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3663 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003664 return new (Context) OMPReadClause(StartLoc, EndLoc);
3665}
3666
Alexey Bataevdea47612014-07-23 07:46:59 +00003667OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3668 SourceLocation EndLoc) {
3669 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3670}
3671
Alexey Bataev67a4f222014-07-23 10:25:33 +00003672OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3673 SourceLocation EndLoc) {
3674 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3675}
3676
Alexey Bataev459dec02014-07-24 06:46:57 +00003677OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3678 SourceLocation EndLoc) {
3679 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3680}
3681
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003682OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3683 SourceLocation EndLoc) {
3684 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3685}
3686
Alexey Bataevc5e02582014-06-16 07:08:35 +00003687OMPClause *Sema::ActOnOpenMPVarListClause(
3688 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3689 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3690 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3691 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003692 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003693 switch (Kind) {
3694 case OMPC_private:
3695 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3696 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003697 case OMPC_firstprivate:
3698 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3699 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003700 case OMPC_lastprivate:
3701 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3702 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003703 case OMPC_shared:
3704 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3705 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003706 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003707 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3708 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003709 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003710 case OMPC_linear:
3711 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3712 ColonLoc, EndLoc);
3713 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003714 case OMPC_aligned:
3715 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3716 ColonLoc, EndLoc);
3717 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003718 case OMPC_copyin:
3719 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3720 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003721 case OMPC_copyprivate:
3722 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3723 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003724 case OMPC_flush:
3725 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3726 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003727 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003728 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003729 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003730 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003731 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003732 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003733 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003734 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003735 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003736 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003737 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003738 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003739 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003740 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003741 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003742 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003743 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003744 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003745 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003746 llvm_unreachable("Clause is not allowed.");
3747 }
3748 return Res;
3749}
3750
3751OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3752 SourceLocation StartLoc,
3753 SourceLocation LParenLoc,
3754 SourceLocation EndLoc) {
3755 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003756 for (auto &RefExpr : VarList) {
3757 assert(RefExpr && "NULL expr in OpenMP private clause.");
3758 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003759 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003760 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003761 continue;
3762 }
3763
Alexey Bataeved09d242014-05-28 05:53:51 +00003764 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003765 // OpenMP [2.1, C/C++]
3766 // A list item is a variable name.
3767 // OpenMP [2.9.3.3, Restrictions, p.1]
3768 // A variable that is part of another variable (as an array or
3769 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003770 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003771 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003772 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003773 continue;
3774 }
3775 Decl *D = DE->getDecl();
3776 VarDecl *VD = cast<VarDecl>(D);
3777
3778 QualType Type = VD->getType();
3779 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3780 // It will be analyzed later.
3781 Vars.push_back(DE);
3782 continue;
3783 }
3784
3785 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3786 // A variable that appears in a private clause must not have an incomplete
3787 // type or a reference type.
3788 if (RequireCompleteType(ELoc, Type,
3789 diag::err_omp_private_incomplete_type)) {
3790 continue;
3791 }
3792 if (Type->isReferenceType()) {
3793 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003794 << getOpenMPClauseName(OMPC_private) << Type;
3795 bool IsDecl =
3796 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3797 Diag(VD->getLocation(),
3798 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3799 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003800 continue;
3801 }
3802
3803 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3804 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003805 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003806 // class type.
3807 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003808 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3809 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003810 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003811 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3812 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3813 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003814 // FIXME This code must be replaced by actual constructing/destructing of
3815 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003816 if (RD) {
3817 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3818 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003819 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003820 if (!CD ||
3821 CheckConstructorAccess(ELoc, CD,
3822 InitializedEntity::InitializeTemporary(Type),
3823 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003824 CD->isDeleted()) {
3825 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003826 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003827 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3828 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003829 Diag(VD->getLocation(),
3830 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3831 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003832 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3833 continue;
3834 }
3835 MarkFunctionReferenced(ELoc, CD);
3836 DiagnoseUseOfDecl(CD, ELoc);
3837
3838 CXXDestructorDecl *DD = RD->getDestructor();
3839 if (DD) {
3840 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3841 DD->isDeleted()) {
3842 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003843 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003844 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3845 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003846 Diag(VD->getLocation(),
3847 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3848 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003849 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3850 continue;
3851 }
3852 MarkFunctionReferenced(ELoc, DD);
3853 DiagnoseUseOfDecl(DD, ELoc);
3854 }
3855 }
3856
Alexey Bataev758e55e2013-09-06 18:03:48 +00003857 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3858 // in a Construct]
3859 // Variables with the predetermined data-sharing attributes may not be
3860 // listed in data-sharing attributes clauses, except for the cases
3861 // listed below. For these exceptions only, listing a predetermined
3862 // variable in a data-sharing attribute clause is allowed and overrides
3863 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003864 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003865 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003866 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3867 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003868 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003869 continue;
3870 }
3871
3872 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003873 Vars.push_back(DE);
3874 }
3875
Alexey Bataeved09d242014-05-28 05:53:51 +00003876 if (Vars.empty())
3877 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003878
3879 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3880}
3881
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003882OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3883 SourceLocation StartLoc,
3884 SourceLocation LParenLoc,
3885 SourceLocation EndLoc) {
3886 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003887 bool IsImplicitClause =
3888 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3889 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3890
Alexey Bataeved09d242014-05-28 05:53:51 +00003891 for (auto &RefExpr : VarList) {
3892 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3893 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003894 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003895 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003896 continue;
3897 }
3898
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003899 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3900 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003901 // OpenMP [2.1, C/C++]
3902 // A list item is a variable name.
3903 // OpenMP [2.9.3.3, Restrictions, p.1]
3904 // A variable that is part of another variable (as an array or
3905 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003906 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003907 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003908 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003909 continue;
3910 }
3911 Decl *D = DE->getDecl();
3912 VarDecl *VD = cast<VarDecl>(D);
3913
3914 QualType Type = VD->getType();
3915 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3916 // It will be analyzed later.
3917 Vars.push_back(DE);
3918 continue;
3919 }
3920
3921 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3922 // A variable that appears in a private clause must not have an incomplete
3923 // type or a reference type.
3924 if (RequireCompleteType(ELoc, Type,
3925 diag::err_omp_firstprivate_incomplete_type)) {
3926 continue;
3927 }
3928 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003929 if (IsImplicitClause) {
3930 Diag(ImplicitClauseLoc,
3931 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3932 << Type;
3933 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3934 } else {
3935 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3936 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3937 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003938 bool IsDecl =
3939 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3940 Diag(VD->getLocation(),
3941 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3942 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003943 continue;
3944 }
3945
3946 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3947 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003948 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003949 // class type.
3950 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003951 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3952 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3953 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003954 // FIXME This code must be replaced by actual constructing/destructing of
3955 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003956 if (RD) {
3957 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3958 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003959 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003960 if (!CD ||
3961 CheckConstructorAccess(ELoc, CD,
3962 InitializedEntity::InitializeTemporary(Type),
3963 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003964 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003965 if (IsImplicitClause) {
3966 Diag(ImplicitClauseLoc,
3967 diag::err_omp_task_predetermined_firstprivate_required_method)
3968 << 0;
3969 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3970 } else {
3971 Diag(ELoc, diag::err_omp_required_method)
3972 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3973 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003974 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3975 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003976 Diag(VD->getLocation(),
3977 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3978 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003979 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3980 continue;
3981 }
3982 MarkFunctionReferenced(ELoc, CD);
3983 DiagnoseUseOfDecl(CD, ELoc);
3984
3985 CXXDestructorDecl *DD = RD->getDestructor();
3986 if (DD) {
3987 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3988 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003989 if (IsImplicitClause) {
3990 Diag(ImplicitClauseLoc,
3991 diag::err_omp_task_predetermined_firstprivate_required_method)
3992 << 1;
3993 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3994 } else {
3995 Diag(ELoc, diag::err_omp_required_method)
3996 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3997 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003998 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3999 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00004000 Diag(VD->getLocation(),
4001 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4002 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004003 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4004 continue;
4005 }
4006 MarkFunctionReferenced(ELoc, DD);
4007 DiagnoseUseOfDecl(DD, ELoc);
4008 }
4009 }
4010
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004011 // If an implicit firstprivate variable found it was checked already.
4012 if (!IsImplicitClause) {
4013 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004014 Type = Type.getNonReferenceType().getCanonicalType();
4015 bool IsConstant = Type.isConstant(Context);
4016 Type = Context.getBaseElementType(Type);
4017 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4018 // A list item that specifies a given variable may not appear in more
4019 // than one clause on the same directive, except that a variable may be
4020 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004021 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004022 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004023 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004024 << getOpenMPClauseName(DVar.CKind)
4025 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004026 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004027 continue;
4028 }
4029
4030 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4031 // in a Construct]
4032 // Variables with the predetermined data-sharing attributes may not be
4033 // listed in data-sharing attributes clauses, except for the cases
4034 // listed below. For these exceptions only, listing a predetermined
4035 // variable in a data-sharing attribute clause is allowed and overrides
4036 // the variable's predetermined data-sharing attributes.
4037 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4038 // in a Construct, C/C++, p.2]
4039 // Variables with const-qualified type having no mutable member may be
4040 // listed in a firstprivate clause, even if they are static data members.
4041 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4042 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4043 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004044 << getOpenMPClauseName(DVar.CKind)
4045 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004046 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004047 continue;
4048 }
4049
Alexey Bataevf29276e2014-06-18 04:14:57 +00004050 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004051 // OpenMP [2.9.3.4, Restrictions, p.2]
4052 // A list item that is private within a parallel region must not appear
4053 // in a firstprivate clause on a worksharing construct if any of the
4054 // worksharing regions arising from the worksharing construct ever bind
4055 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004056 if (isOpenMPWorksharingDirective(CurrDir) &&
4057 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004058 DVar = DSAStack->getImplicitDSA(VD, true);
4059 if (DVar.CKind != OMPC_shared &&
4060 (isOpenMPParallelDirective(DVar.DKind) ||
4061 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004062 Diag(ELoc, diag::err_omp_required_access)
4063 << getOpenMPClauseName(OMPC_firstprivate)
4064 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004065 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004066 continue;
4067 }
4068 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004069 // OpenMP [2.9.3.4, Restrictions, p.3]
4070 // A list item that appears in a reduction clause of a parallel construct
4071 // must not appear in a firstprivate clause on a worksharing or task
4072 // construct if any of the worksharing or task regions arising from the
4073 // worksharing or task construct ever bind to any of the parallel regions
4074 // arising from the parallel construct.
4075 // OpenMP [2.9.3.4, Restrictions, p.4]
4076 // A list item that appears in a reduction clause in worksharing
4077 // construct must not appear in a firstprivate clause in a task construct
4078 // encountered during execution of any of the worksharing regions arising
4079 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004080 if (CurrDir == OMPD_task) {
4081 DVar =
4082 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4083 [](OpenMPDirectiveKind K) -> bool {
4084 return isOpenMPParallelDirective(K) ||
4085 isOpenMPWorksharingDirective(K);
4086 },
4087 false);
4088 if (DVar.CKind == OMPC_reduction &&
4089 (isOpenMPParallelDirective(DVar.DKind) ||
4090 isOpenMPWorksharingDirective(DVar.DKind))) {
4091 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4092 << getOpenMPDirectiveName(DVar.DKind);
4093 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4094 continue;
4095 }
4096 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004097 }
4098
4099 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4100 Vars.push_back(DE);
4101 }
4102
Alexey Bataeved09d242014-05-28 05:53:51 +00004103 if (Vars.empty())
4104 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004105
4106 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4107 Vars);
4108}
4109
Alexander Musman1bb328c2014-06-04 13:06:39 +00004110OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4111 SourceLocation StartLoc,
4112 SourceLocation LParenLoc,
4113 SourceLocation EndLoc) {
4114 SmallVector<Expr *, 8> Vars;
4115 for (auto &RefExpr : VarList) {
4116 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4117 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4118 // It will be analyzed later.
4119 Vars.push_back(RefExpr);
4120 continue;
4121 }
4122
4123 SourceLocation ELoc = RefExpr->getExprLoc();
4124 // OpenMP [2.1, C/C++]
4125 // A list item is a variable name.
4126 // OpenMP [2.14.3.5, Restrictions, p.1]
4127 // A variable that is part of another variable (as an array or structure
4128 // element) cannot appear in a lastprivate clause.
4129 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4130 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4131 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4132 continue;
4133 }
4134 Decl *D = DE->getDecl();
4135 VarDecl *VD = cast<VarDecl>(D);
4136
4137 QualType Type = VD->getType();
4138 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4139 // It will be analyzed later.
4140 Vars.push_back(DE);
4141 continue;
4142 }
4143
4144 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4145 // A variable that appears in a lastprivate clause must not have an
4146 // incomplete type or a reference type.
4147 if (RequireCompleteType(ELoc, Type,
4148 diag::err_omp_lastprivate_incomplete_type)) {
4149 continue;
4150 }
4151 if (Type->isReferenceType()) {
4152 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4153 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4154 bool IsDecl =
4155 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4156 Diag(VD->getLocation(),
4157 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4158 << VD;
4159 continue;
4160 }
4161
4162 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4163 // in a Construct]
4164 // Variables with the predetermined data-sharing attributes may not be
4165 // listed in data-sharing attributes clauses, except for the cases
4166 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004167 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004168 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4169 DVar.CKind != OMPC_firstprivate &&
4170 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4171 Diag(ELoc, diag::err_omp_wrong_dsa)
4172 << getOpenMPClauseName(DVar.CKind)
4173 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004174 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004175 continue;
4176 }
4177
Alexey Bataevf29276e2014-06-18 04:14:57 +00004178 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4179 // OpenMP [2.14.3.5, Restrictions, p.2]
4180 // A list item that is private within a parallel region, or that appears in
4181 // the reduction clause of a parallel construct, must not appear in a
4182 // lastprivate clause on a worksharing construct if any of the corresponding
4183 // worksharing regions ever binds to any of the corresponding parallel
4184 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004185 if (isOpenMPWorksharingDirective(CurrDir) &&
4186 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004187 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004188 if (DVar.CKind != OMPC_shared) {
4189 Diag(ELoc, diag::err_omp_required_access)
4190 << getOpenMPClauseName(OMPC_lastprivate)
4191 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004192 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004193 continue;
4194 }
4195 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004196 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004197 // A variable of class type (or array thereof) that appears in a
4198 // lastprivate clause requires an accessible, unambiguous default
4199 // constructor for the class type, unless the list item is also specified
4200 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004201 // A variable of class type (or array thereof) that appears in a
4202 // lastprivate clause requires an accessible, unambiguous copy assignment
4203 // operator for the class type.
4204 while (Type.getNonReferenceType()->isArrayType())
4205 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4206 ->getElementType();
4207 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4208 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4209 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004210 // FIXME This code must be replaced by actual copying and destructing of the
4211 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004212 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004213 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4214 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004215 if (MD) {
4216 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4217 MD->isDeleted()) {
4218 Diag(ELoc, diag::err_omp_required_method)
4219 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4220 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4221 VarDecl::DeclarationOnly;
4222 Diag(VD->getLocation(),
4223 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4224 << VD;
4225 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4226 continue;
4227 }
4228 MarkFunctionReferenced(ELoc, MD);
4229 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004230 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004231
4232 CXXDestructorDecl *DD = RD->getDestructor();
4233 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004234 PartialDiagnostic PD =
4235 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004236 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4237 DD->isDeleted()) {
4238 Diag(ELoc, diag::err_omp_required_method)
4239 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4240 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4241 VarDecl::DeclarationOnly;
4242 Diag(VD->getLocation(),
4243 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4244 << VD;
4245 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4246 continue;
4247 }
4248 MarkFunctionReferenced(ELoc, DD);
4249 DiagnoseUseOfDecl(DD, ELoc);
4250 }
4251 }
4252
Alexey Bataevf29276e2014-06-18 04:14:57 +00004253 if (DVar.CKind != OMPC_firstprivate)
4254 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004255 Vars.push_back(DE);
4256 }
4257
4258 if (Vars.empty())
4259 return nullptr;
4260
4261 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4262 Vars);
4263}
4264
Alexey Bataev758e55e2013-09-06 18:03:48 +00004265OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4266 SourceLocation StartLoc,
4267 SourceLocation LParenLoc,
4268 SourceLocation EndLoc) {
4269 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004270 for (auto &RefExpr : VarList) {
4271 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4272 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004273 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004274 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004275 continue;
4276 }
4277
Alexey Bataeved09d242014-05-28 05:53:51 +00004278 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004279 // OpenMP [2.1, C/C++]
4280 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004281 // OpenMP [2.14.3.2, Restrictions, p.1]
4282 // A variable that is part of another variable (as an array or structure
4283 // element) cannot appear in a shared unless it is a static data member
4284 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004285 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004286 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004287 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004288 continue;
4289 }
4290 Decl *D = DE->getDecl();
4291 VarDecl *VD = cast<VarDecl>(D);
4292
4293 QualType Type = VD->getType();
4294 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4295 // It will be analyzed later.
4296 Vars.push_back(DE);
4297 continue;
4298 }
4299
4300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4301 // in a Construct]
4302 // Variables with the predetermined data-sharing attributes may not be
4303 // listed in data-sharing attributes clauses, except for the cases
4304 // listed below. For these exceptions only, listing a predetermined
4305 // variable in a data-sharing attribute clause is allowed and overrides
4306 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004307 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004308 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4309 DVar.RefExpr) {
4310 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4311 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004312 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004313 continue;
4314 }
4315
4316 DSAStack->addDSA(VD, DE, OMPC_shared);
4317 Vars.push_back(DE);
4318 }
4319
Alexey Bataeved09d242014-05-28 05:53:51 +00004320 if (Vars.empty())
4321 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004322
4323 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4324}
4325
Alexey Bataevc5e02582014-06-16 07:08:35 +00004326namespace {
4327class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4328 DSAStackTy *Stack;
4329
4330public:
4331 bool VisitDeclRefExpr(DeclRefExpr *E) {
4332 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004333 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004334 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4335 return false;
4336 if (DVar.CKind != OMPC_unknown)
4337 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004338 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004339 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004340 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004341 return true;
4342 return false;
4343 }
4344 return false;
4345 }
4346 bool VisitStmt(Stmt *S) {
4347 for (auto Child : S->children()) {
4348 if (Child && Visit(Child))
4349 return true;
4350 }
4351 return false;
4352 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004353 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004354};
Alexey Bataev23b69422014-06-18 07:08:49 +00004355} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004356
4357OMPClause *Sema::ActOnOpenMPReductionClause(
4358 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4359 SourceLocation ColonLoc, SourceLocation EndLoc,
4360 CXXScopeSpec &ReductionIdScopeSpec,
4361 const DeclarationNameInfo &ReductionId) {
4362 // TODO: Allow scope specification search when 'declare reduction' is
4363 // supported.
4364 assert(ReductionIdScopeSpec.isEmpty() &&
4365 "No support for scoped reduction identifiers yet.");
4366
4367 auto DN = ReductionId.getName();
4368 auto OOK = DN.getCXXOverloadedOperator();
4369 BinaryOperatorKind BOK = BO_Comma;
4370
4371 // OpenMP [2.14.3.6, reduction clause]
4372 // C
4373 // reduction-identifier is either an identifier or one of the following
4374 // operators: +, -, *, &, |, ^, && and ||
4375 // C++
4376 // reduction-identifier is either an id-expression or one of the following
4377 // operators: +, -, *, &, |, ^, && and ||
4378 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4379 switch (OOK) {
4380 case OO_Plus:
4381 case OO_Minus:
4382 BOK = BO_AddAssign;
4383 break;
4384 case OO_Star:
4385 BOK = BO_MulAssign;
4386 break;
4387 case OO_Amp:
4388 BOK = BO_AndAssign;
4389 break;
4390 case OO_Pipe:
4391 BOK = BO_OrAssign;
4392 break;
4393 case OO_Caret:
4394 BOK = BO_XorAssign;
4395 break;
4396 case OO_AmpAmp:
4397 BOK = BO_LAnd;
4398 break;
4399 case OO_PipePipe:
4400 BOK = BO_LOr;
4401 break;
4402 default:
4403 if (auto II = DN.getAsIdentifierInfo()) {
4404 if (II->isStr("max"))
4405 BOK = BO_GT;
4406 else if (II->isStr("min"))
4407 BOK = BO_LT;
4408 }
4409 break;
4410 }
4411 SourceRange ReductionIdRange;
4412 if (ReductionIdScopeSpec.isValid()) {
4413 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4414 }
4415 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4416 if (BOK == BO_Comma) {
4417 // Not allowed reduction identifier is found.
4418 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4419 << ReductionIdRange;
4420 return nullptr;
4421 }
4422
4423 SmallVector<Expr *, 8> Vars;
4424 for (auto RefExpr : VarList) {
4425 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4426 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4427 // It will be analyzed later.
4428 Vars.push_back(RefExpr);
4429 continue;
4430 }
4431
4432 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4433 RefExpr->isInstantiationDependent() ||
4434 RefExpr->containsUnexpandedParameterPack()) {
4435 // It will be analyzed later.
4436 Vars.push_back(RefExpr);
4437 continue;
4438 }
4439
4440 auto ELoc = RefExpr->getExprLoc();
4441 auto ERange = RefExpr->getSourceRange();
4442 // OpenMP [2.1, C/C++]
4443 // A list item is a variable or array section, subject to the restrictions
4444 // specified in Section 2.4 on page 42 and in each of the sections
4445 // describing clauses and directives for which a list appears.
4446 // OpenMP [2.14.3.3, Restrictions, p.1]
4447 // A variable that is part of another variable (as an array or
4448 // structure element) cannot appear in a private clause.
4449 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4450 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4451 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4452 continue;
4453 }
4454 auto D = DE->getDecl();
4455 auto VD = cast<VarDecl>(D);
4456 auto Type = VD->getType();
4457 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4458 // A variable that appears in a private clause must not have an incomplete
4459 // type or a reference type.
4460 if (RequireCompleteType(ELoc, Type,
4461 diag::err_omp_reduction_incomplete_type))
4462 continue;
4463 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4464 // Arrays may not appear in a reduction clause.
4465 if (Type.getNonReferenceType()->isArrayType()) {
4466 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4467 bool IsDecl =
4468 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4469 Diag(VD->getLocation(),
4470 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4471 << VD;
4472 continue;
4473 }
4474 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4475 // A list item that appears in a reduction clause must not be
4476 // const-qualified.
4477 if (Type.getNonReferenceType().isConstant(Context)) {
4478 Diag(ELoc, diag::err_omp_const_variable)
4479 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4480 bool IsDecl =
4481 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4482 Diag(VD->getLocation(),
4483 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4484 << VD;
4485 continue;
4486 }
4487 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4488 // If a list-item is a reference type then it must bind to the same object
4489 // for all threads of the team.
4490 VarDecl *VDDef = VD->getDefinition();
4491 if (Type->isReferenceType() && VDDef) {
4492 DSARefChecker Check(DSAStack);
4493 if (Check.Visit(VDDef->getInit())) {
4494 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4495 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4496 continue;
4497 }
4498 }
4499 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4500 // The type of a list item that appears in a reduction clause must be valid
4501 // for the reduction-identifier. For a max or min reduction in C, the type
4502 // of the list item must be an allowed arithmetic data type: char, int,
4503 // float, double, or _Bool, possibly modified with long, short, signed, or
4504 // unsigned. For a max or min reduction in C++, the type of the list item
4505 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4506 // double, or bool, possibly modified with long, short, signed, or unsigned.
4507 if ((BOK == BO_GT || BOK == BO_LT) &&
4508 !(Type->isScalarType() ||
4509 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4510 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4511 << getLangOpts().CPlusPlus;
4512 bool IsDecl =
4513 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4514 Diag(VD->getLocation(),
4515 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4516 << VD;
4517 continue;
4518 }
4519 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4520 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4521 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4522 bool IsDecl =
4523 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4524 Diag(VD->getLocation(),
4525 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4526 << VD;
4527 continue;
4528 }
4529 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4530 getDiagnostics().setSuppressAllDiagnostics(true);
4531 ExprResult ReductionOp =
4532 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4533 RefExpr, RefExpr);
4534 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4535 if (ReductionOp.isInvalid()) {
4536 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004537 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004538 bool IsDecl =
4539 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4540 Diag(VD->getLocation(),
4541 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4542 << VD;
4543 continue;
4544 }
4545
4546 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4547 // in a Construct]
4548 // Variables with the predetermined data-sharing attributes may not be
4549 // listed in data-sharing attributes clauses, except for the cases
4550 // listed below. For these exceptions only, listing a predetermined
4551 // variable in a data-sharing attribute clause is allowed and overrides
4552 // the variable's predetermined data-sharing attributes.
4553 // OpenMP [2.14.3.6, Restrictions, p.3]
4554 // Any number of reduction clauses can be specified on the directive,
4555 // but a list item can appear only once in the reduction clauses for that
4556 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004557 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004558 if (DVar.CKind == OMPC_reduction) {
4559 Diag(ELoc, diag::err_omp_once_referenced)
4560 << getOpenMPClauseName(OMPC_reduction);
4561 if (DVar.RefExpr) {
4562 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4563 }
4564 } else if (DVar.CKind != OMPC_unknown) {
4565 Diag(ELoc, diag::err_omp_wrong_dsa)
4566 << getOpenMPClauseName(DVar.CKind)
4567 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004568 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004569 continue;
4570 }
4571
4572 // OpenMP [2.14.3.6, Restrictions, p.1]
4573 // A list item that appears in a reduction clause of a worksharing
4574 // construct must be shared in the parallel regions to which any of the
4575 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004576 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004577 if (isOpenMPWorksharingDirective(CurrDir) &&
4578 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004579 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004580 if (DVar.CKind != OMPC_shared) {
4581 Diag(ELoc, diag::err_omp_required_access)
4582 << getOpenMPClauseName(OMPC_reduction)
4583 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004584 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004585 continue;
4586 }
4587 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004588
4589 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4590 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4591 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004592 // FIXME This code must be replaced by actual constructing/destructing of
4593 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004594 if (RD) {
4595 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4596 PartialDiagnostic PD =
4597 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004598 if (!CD ||
4599 CheckConstructorAccess(ELoc, CD,
4600 InitializedEntity::InitializeTemporary(Type),
4601 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004602 CD->isDeleted()) {
4603 Diag(ELoc, diag::err_omp_required_method)
4604 << getOpenMPClauseName(OMPC_reduction) << 0;
4605 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4606 VarDecl::DeclarationOnly;
4607 Diag(VD->getLocation(),
4608 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4609 << VD;
4610 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4611 continue;
4612 }
4613 MarkFunctionReferenced(ELoc, CD);
4614 DiagnoseUseOfDecl(CD, ELoc);
4615
4616 CXXDestructorDecl *DD = RD->getDestructor();
4617 if (DD) {
4618 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4619 DD->isDeleted()) {
4620 Diag(ELoc, diag::err_omp_required_method)
4621 << getOpenMPClauseName(OMPC_reduction) << 4;
4622 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4623 VarDecl::DeclarationOnly;
4624 Diag(VD->getLocation(),
4625 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4626 << VD;
4627 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4628 continue;
4629 }
4630 MarkFunctionReferenced(ELoc, DD);
4631 DiagnoseUseOfDecl(DD, ELoc);
4632 }
4633 }
4634
4635 DSAStack->addDSA(VD, DE, OMPC_reduction);
4636 Vars.push_back(DE);
4637 }
4638
4639 if (Vars.empty())
4640 return nullptr;
4641
4642 return OMPReductionClause::Create(
4643 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4644 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4645}
4646
Alexander Musman8dba6642014-04-22 13:09:42 +00004647OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4648 SourceLocation StartLoc,
4649 SourceLocation LParenLoc,
4650 SourceLocation ColonLoc,
4651 SourceLocation EndLoc) {
4652 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004653 for (auto &RefExpr : VarList) {
4654 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4655 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004656 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004657 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004658 continue;
4659 }
4660
4661 // OpenMP [2.14.3.7, linear clause]
4662 // A list item that appears in a linear clause is subject to the private
4663 // clause semantics described in Section 2.14.3.3 on page 159 except as
4664 // noted. In addition, the value of the new list item on each iteration
4665 // of the associated loop(s) corresponds to the value of the original
4666 // list item before entering the construct plus the logical number of
4667 // the iteration times linear-step.
4668
Alexey Bataeved09d242014-05-28 05:53:51 +00004669 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004670 // OpenMP [2.1, C/C++]
4671 // A list item is a variable name.
4672 // OpenMP [2.14.3.3, Restrictions, p.1]
4673 // A variable that is part of another variable (as an array or
4674 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004675 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004676 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004677 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004678 continue;
4679 }
4680
4681 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4682
4683 // OpenMP [2.14.3.7, linear clause]
4684 // A list-item cannot appear in more than one linear clause.
4685 // A list-item that appears in a linear clause cannot appear in any
4686 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004687 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004688 if (DVar.RefExpr) {
4689 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4690 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004691 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004692 continue;
4693 }
4694
4695 QualType QType = VD->getType();
4696 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4697 // It will be analyzed later.
4698 Vars.push_back(DE);
4699 continue;
4700 }
4701
4702 // A variable must not have an incomplete type or a reference type.
4703 if (RequireCompleteType(ELoc, QType,
4704 diag::err_omp_linear_incomplete_type)) {
4705 continue;
4706 }
4707 if (QType->isReferenceType()) {
4708 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4709 << getOpenMPClauseName(OMPC_linear) << QType;
4710 bool IsDecl =
4711 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4712 Diag(VD->getLocation(),
4713 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4714 << VD;
4715 continue;
4716 }
4717
4718 // A list item must not be const-qualified.
4719 if (QType.isConstant(Context)) {
4720 Diag(ELoc, diag::err_omp_const_variable)
4721 << getOpenMPClauseName(OMPC_linear);
4722 bool IsDecl =
4723 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4724 Diag(VD->getLocation(),
4725 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4726 << VD;
4727 continue;
4728 }
4729
4730 // A list item must be of integral or pointer type.
4731 QType = QType.getUnqualifiedType().getCanonicalType();
4732 const Type *Ty = QType.getTypePtrOrNull();
4733 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4734 !Ty->isPointerType())) {
4735 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4736 bool IsDecl =
4737 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4738 Diag(VD->getLocation(),
4739 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4740 << VD;
4741 continue;
4742 }
4743
4744 DSAStack->addDSA(VD, DE, OMPC_linear);
4745 Vars.push_back(DE);
4746 }
4747
4748 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004749 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004750
4751 Expr *StepExpr = Step;
4752 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4753 !Step->isInstantiationDependent() &&
4754 !Step->containsUnexpandedParameterPack()) {
4755 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004756 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004757 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004758 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004759 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004760
4761 // Warn about zero linear step (it would be probably better specified as
4762 // making corresponding variables 'const').
4763 llvm::APSInt Result;
4764 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4765 !Result.isNegative() && !Result.isStrictlyPositive())
4766 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4767 << (Vars.size() > 1);
4768 }
4769
4770 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4771 Vars, StepExpr);
4772}
4773
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004774OMPClause *Sema::ActOnOpenMPAlignedClause(
4775 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4776 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4777
4778 SmallVector<Expr *, 8> Vars;
4779 for (auto &RefExpr : VarList) {
4780 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4781 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4782 // It will be analyzed later.
4783 Vars.push_back(RefExpr);
4784 continue;
4785 }
4786
4787 SourceLocation ELoc = RefExpr->getExprLoc();
4788 // OpenMP [2.1, C/C++]
4789 // A list item is a variable name.
4790 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4791 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4792 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4793 continue;
4794 }
4795
4796 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4797
4798 // OpenMP [2.8.1, simd construct, Restrictions]
4799 // The type of list items appearing in the aligned clause must be
4800 // array, pointer, reference to array, or reference to pointer.
4801 QualType QType = DE->getType()
4802 .getNonReferenceType()
4803 .getUnqualifiedType()
4804 .getCanonicalType();
4805 const Type *Ty = QType.getTypePtrOrNull();
4806 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4807 !Ty->isPointerType())) {
4808 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4809 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4810 bool IsDecl =
4811 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4812 Diag(VD->getLocation(),
4813 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4814 << VD;
4815 continue;
4816 }
4817
4818 // OpenMP [2.8.1, simd construct, Restrictions]
4819 // A list-item cannot appear in more than one aligned clause.
4820 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4821 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4822 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4823 << getOpenMPClauseName(OMPC_aligned);
4824 continue;
4825 }
4826
4827 Vars.push_back(DE);
4828 }
4829
4830 // OpenMP [2.8.1, simd construct, Description]
4831 // The parameter of the aligned clause, alignment, must be a constant
4832 // positive integer expression.
4833 // If no optional parameter is specified, implementation-defined default
4834 // alignments for SIMD instructions on the target platforms are assumed.
4835 if (Alignment != nullptr) {
4836 ExprResult AlignResult =
4837 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4838 if (AlignResult.isInvalid())
4839 return nullptr;
4840 Alignment = AlignResult.get();
4841 }
4842 if (Vars.empty())
4843 return nullptr;
4844
4845 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
4846 EndLoc, Vars, Alignment);
4847}
4848
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004849OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
4850 SourceLocation StartLoc,
4851 SourceLocation LParenLoc,
4852 SourceLocation EndLoc) {
4853 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004854 for (auto &RefExpr : VarList) {
4855 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
4856 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004857 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004858 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004859 continue;
4860 }
4861
Alexey Bataeved09d242014-05-28 05:53:51 +00004862 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004863 // OpenMP [2.1, C/C++]
4864 // A list item is a variable name.
4865 // OpenMP [2.14.4.1, Restrictions, p.1]
4866 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004867 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004868 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004869 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004870 continue;
4871 }
4872
4873 Decl *D = DE->getDecl();
4874 VarDecl *VD = cast<VarDecl>(D);
4875
4876 QualType Type = VD->getType();
4877 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4878 // It will be analyzed later.
4879 Vars.push_back(DE);
4880 continue;
4881 }
4882
4883 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4884 // A list item that appears in a copyin clause must be threadprivate.
4885 if (!DSAStack->isThreadPrivate(VD)) {
4886 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004887 << getOpenMPClauseName(OMPC_copyin)
4888 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004889 continue;
4890 }
4891
4892 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4893 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004894 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004895 // operator for the class type.
4896 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004897 CXXRecordDecl *RD =
4898 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004899 // FIXME This code must be replaced by actual assignment of the
4900 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004901 if (RD) {
4902 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4903 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004904 if (MD) {
4905 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4906 MD->isDeleted()) {
4907 Diag(ELoc, diag::err_omp_required_method)
4908 << getOpenMPClauseName(OMPC_copyin) << 2;
4909 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4910 VarDecl::DeclarationOnly;
4911 Diag(VD->getLocation(),
4912 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4913 << VD;
4914 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4915 continue;
4916 }
4917 MarkFunctionReferenced(ELoc, MD);
4918 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004919 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004920 }
4921
4922 DSAStack->addDSA(VD, DE, OMPC_copyin);
4923 Vars.push_back(DE);
4924 }
4925
Alexey Bataeved09d242014-05-28 05:53:51 +00004926 if (Vars.empty())
4927 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004928
4929 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4930}
4931
Alexey Bataevbae9a792014-06-27 10:37:06 +00004932OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4933 SourceLocation StartLoc,
4934 SourceLocation LParenLoc,
4935 SourceLocation EndLoc) {
4936 SmallVector<Expr *, 8> Vars;
4937 for (auto &RefExpr : VarList) {
4938 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4939 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4940 // It will be analyzed later.
4941 Vars.push_back(RefExpr);
4942 continue;
4943 }
4944
4945 SourceLocation ELoc = RefExpr->getExprLoc();
4946 // OpenMP [2.1, C/C++]
4947 // A list item is a variable name.
4948 // OpenMP [2.14.4.1, Restrictions, p.1]
4949 // A list item that appears in a copyin clause must be threadprivate.
4950 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4951 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4952 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4953 continue;
4954 }
4955
4956 Decl *D = DE->getDecl();
4957 VarDecl *VD = cast<VarDecl>(D);
4958
4959 QualType Type = VD->getType();
4960 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4961 // It will be analyzed later.
4962 Vars.push_back(DE);
4963 continue;
4964 }
4965
4966 // OpenMP [2.14.4.2, Restrictions, p.2]
4967 // A list item that appears in a copyprivate clause may not appear in a
4968 // private or firstprivate clause on the single construct.
4969 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004970 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004971 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4972 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4973 Diag(ELoc, diag::err_omp_wrong_dsa)
4974 << getOpenMPClauseName(DVar.CKind)
4975 << getOpenMPClauseName(OMPC_copyprivate);
4976 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4977 continue;
4978 }
4979
4980 // OpenMP [2.11.4.2, Restrictions, p.1]
4981 // All list items that appear in a copyprivate clause must be either
4982 // threadprivate or private in the enclosing context.
4983 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004984 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004985 if (DVar.CKind == OMPC_shared) {
4986 Diag(ELoc, diag::err_omp_required_access)
4987 << getOpenMPClauseName(OMPC_copyprivate)
4988 << "threadprivate or private in the enclosing context";
4989 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4990 continue;
4991 }
4992 }
4993 }
4994
4995 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4996 // A variable of class type (or array thereof) that appears in a
4997 // copyin clause requires an accessible, unambiguous copy assignment
4998 // operator for the class type.
4999 Type = Context.getBaseElementType(Type);
5000 CXXRecordDecl *RD =
5001 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5002 // FIXME This code must be replaced by actual assignment of the
5003 // threadprivate variable.
5004 if (RD) {
5005 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5006 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5007 if (MD) {
5008 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5009 MD->isDeleted()) {
5010 Diag(ELoc, diag::err_omp_required_method)
5011 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5012 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5013 VarDecl::DeclarationOnly;
5014 Diag(VD->getLocation(),
5015 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5016 << VD;
5017 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5018 continue;
5019 }
5020 MarkFunctionReferenced(ELoc, MD);
5021 DiagnoseUseOfDecl(MD, ELoc);
5022 }
5023 }
5024
5025 // No need to mark vars as copyprivate, they are already threadprivate or
5026 // implicitly private.
5027 Vars.push_back(DE);
5028 }
5029
5030 if (Vars.empty())
5031 return nullptr;
5032
5033 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5034}
5035
Alexey Bataev6125da92014-07-21 11:26:11 +00005036OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5037 SourceLocation StartLoc,
5038 SourceLocation LParenLoc,
5039 SourceLocation EndLoc) {
5040 if (VarList.empty())
5041 return nullptr;
5042
5043 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5044}
Alexey Bataevdea47612014-07-23 07:46:59 +00005045