blob: 23ed63192b6988593b48dd6dd14c9e3cc67cb21d [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 Bataev9fb6e642014-07-22 06:45:04 +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 Bataev9fb6e642014-07-22 06:45:04 +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 Bataev750a58b2014-03-18 12:19:12 +0000229 if (!D->isFunctionOrMethodVarDecl())
230 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)) {
396 if (isOpenMPLocal(D, StartI) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000397 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000398 DVar.CKind = OMPC_private;
399 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000400 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000401 }
402
403 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
404 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000405 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000406 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000407 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000408 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000409 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
410 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000411 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
412 return DVar;
413
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414 DVar.CKind = OMPC_shared;
415 return DVar;
416 }
417
418 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000419 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000420 while (Type->isArrayType()) {
421 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
422 Type = ElemType.getNonReferenceType().getCanonicalType();
423 }
424 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
425 // in a Construct, C/C++, predetermined, p.6]
426 // Variables with const qualified type having no mutable member are
427 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000428 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000429 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000430 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000431 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432 // Variables with const-qualified type having no mutable member may be
433 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000434 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
435 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000436 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
437 return DVar;
438
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 DVar.CKind = OMPC_shared;
440 return DVar;
441 }
442
443 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
444 // in a Construct, C/C++, predetermined, p.7]
445 // Variables with static storage duration that are declared in a scope
446 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000447 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000448 DVar.CKind = OMPC_shared;
449 return DVar;
450 }
451
452 // Explicitly specified attributes and local variables with predetermined
453 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000454 auto I = std::prev(StartI);
455 if (I->SharingMap.count(D)) {
456 DVar.RefExpr = I->SharingMap[D].RefExpr;
457 DVar.CKind = I->SharingMap[D].Attributes;
458 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000459 }
460
461 return DVar;
462}
463
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000464DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
465 auto StartI = Stack.rbegin();
466 auto EndI = std::prev(Stack.rend());
467 if (FromParent && StartI != EndI) {
468 StartI = std::next(StartI);
469 }
470 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000471}
472
Alexey Bataevf29276e2014-06-18 04:14:57 +0000473template <class ClausesPredicate, class DirectivesPredicate>
474DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000475 DirectivesPredicate DPred,
476 bool FromParent) {
477 auto StartI = std::next(Stack.rbegin());
478 auto EndI = std::prev(Stack.rend());
479 if (FromParent && StartI != EndI) {
480 StartI = std::next(StartI);
481 }
482 for (auto I = StartI, EE = EndI; I != EE; ++I) {
483 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000484 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000485 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000486 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000487 return DVar;
488 }
489 return DSAVarData();
490}
491
Alexey Bataevf29276e2014-06-18 04:14:57 +0000492template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000493DSAStackTy::DSAVarData
494DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
495 DirectivesPredicate DPred, bool FromParent) {
496 auto StartI = std::next(Stack.rbegin());
497 auto EndI = std::prev(Stack.rend());
498 if (FromParent && StartI != EndI) {
499 StartI = std::next(StartI);
500 }
501 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000502 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000503 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000504 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000505 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000506 return DVar;
507 return DSAVarData();
508 }
509 return DSAVarData();
510}
511
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000512template <class NamedDirectivesPredicate>
513bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
514 auto StartI = std::next(Stack.rbegin());
515 auto EndI = std::prev(Stack.rend());
516 if (FromParent && StartI != EndI) {
517 StartI = std::next(StartI);
518 }
519 for (auto I = StartI, EE = EndI; I != EE; ++I) {
520 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
521 return true;
522 }
523 return false;
524}
525
Alexey Bataev758e55e2013-09-06 18:03:48 +0000526void Sema::InitDataSharingAttributesStack() {
527 VarDataSharingAttributesStack = new DSAStackTy(*this);
528}
529
530#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
531
Alexey Bataeved09d242014-05-28 05:53:51 +0000532void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000533
534void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
535 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000536 Scope *CurScope, SourceLocation Loc) {
537 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000538 PushExpressionEvaluationContext(PotentiallyEvaluated);
539}
540
541void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000542 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
543 // A variable of class type (or array thereof) that appears in a lastprivate
544 // clause requires an accessible, unambiguous default constructor for the
545 // class type, unless the list item is also specified in a firstprivate
546 // clause.
547 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
548 for (auto C : D->clauses()) {
549 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
550 for (auto VarRef : Clause->varlists()) {
551 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
552 continue;
553 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000554 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000555 if (DVar.CKind == OMPC_lastprivate) {
556 SourceLocation ELoc = VarRef->getExprLoc();
557 auto Type = VarRef->getType();
558 if (Type->isArrayType())
559 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
560 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000561 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
562 // FIXME This code must be replaced by actual constructing of the
563 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000564 if (RD) {
565 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
566 PartialDiagnostic PD =
567 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
568 if (!CD ||
569 CheckConstructorAccess(
570 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
571 CD->getAccess(), PD) == AR_inaccessible ||
572 CD->isDeleted()) {
573 Diag(ELoc, diag::err_omp_required_method)
574 << getOpenMPClauseName(OMPC_lastprivate) << 0;
575 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
576 VarDecl::DeclarationOnly;
577 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
578 : diag::note_defined_here)
579 << VD;
580 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
581 continue;
582 }
583 MarkFunctionReferenced(ELoc, CD);
584 DiagnoseUseOfDecl(CD, ELoc);
585 }
586 }
587 }
588 }
589 }
590 }
591
Alexey Bataev758e55e2013-09-06 18:03:48 +0000592 DSAStack->pop();
593 DiscardCleanupsInEvaluationContext();
594 PopExpressionEvaluationContext();
595}
596
Alexey Bataeva769e072013-03-22 06:34:35 +0000597namespace {
598
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000599class VarDeclFilterCCC : public CorrectionCandidateCallback {
600private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000601 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000602
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000603public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000604 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000605 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000606 NamedDecl *ND = Candidate.getCorrectionDecl();
607 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
608 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000609 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
610 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000611 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000612 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614};
Alexey Bataeved09d242014-05-28 05:53:51 +0000615} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000616
617ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
618 CXXScopeSpec &ScopeSpec,
619 const DeclarationNameInfo &Id) {
620 LookupResult Lookup(*this, Id, LookupOrdinaryName);
621 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
622
623 if (Lookup.isAmbiguous())
624 return ExprError();
625
626 VarDecl *VD;
627 if (!Lookup.isSingleResult()) {
628 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000629 if (TypoCorrection Corrected =
630 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
631 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000632 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000633 PDiag(Lookup.empty()
634 ? diag::err_undeclared_var_use_suggest
635 : diag::err_omp_expected_var_arg_suggest)
636 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000637 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000638 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000639 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
640 : diag::err_omp_expected_var_arg)
641 << Id.getName();
642 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000643 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000644 } else {
645 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000646 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000647 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
648 return ExprError();
649 }
650 }
651 Lookup.suppressDiagnostics();
652
653 // OpenMP [2.9.2, Syntax, C/C++]
654 // Variables must be file-scope, namespace-scope, or static block-scope.
655 if (!VD->hasGlobalStorage()) {
656 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000657 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
658 bool IsDecl =
659 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000660 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000661 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
662 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000663 return ExprError();
664 }
665
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000666 VarDecl *CanonicalVD = VD->getCanonicalDecl();
667 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000668 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
669 // A threadprivate directive for file-scope variables must appear outside
670 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000671 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
672 !getCurLexicalContext()->isTranslationUnit()) {
673 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000674 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
675 bool IsDecl =
676 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
677 Diag(VD->getLocation(),
678 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
679 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000680 return ExprError();
681 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000682 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
683 // A threadprivate directive for static class member variables must appear
684 // in the class definition, in the same scope in which the member
685 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000686 if (CanonicalVD->isStaticDataMember() &&
687 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
688 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000689 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
690 bool IsDecl =
691 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
692 Diag(VD->getLocation(),
693 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
694 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000695 return ExprError();
696 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000697 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
698 // A threadprivate directive for namespace-scope variables must appear
699 // outside any definition or declaration other than the namespace
700 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000701 if (CanonicalVD->getDeclContext()->isNamespace() &&
702 (!getCurLexicalContext()->isFileContext() ||
703 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
704 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000705 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
706 bool IsDecl =
707 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
708 Diag(VD->getLocation(),
709 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
710 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000711 return ExprError();
712 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000713 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
714 // A threadprivate directive for static block-scope variables must appear
715 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000716 if (CanonicalVD->isStaticLocal() && CurScope &&
717 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000718 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000719 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
720 bool IsDecl =
721 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
722 Diag(VD->getLocation(),
723 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
724 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000725 return ExprError();
726 }
727
728 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
729 // A threadprivate directive must lexically precede all references to any
730 // of the variables in its list.
731 if (VD->isUsed()) {
732 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000733 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000734 return ExprError();
735 }
736
737 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000738 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000739 return DE;
740}
741
Alexey Bataeved09d242014-05-28 05:53:51 +0000742Sema::DeclGroupPtrTy
743Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
744 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000745 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000746 CurContext->addDecl(D);
747 return DeclGroupPtrTy::make(DeclGroupRef(D));
748 }
749 return DeclGroupPtrTy();
750}
751
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000752namespace {
753class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
754 Sema &SemaRef;
755
756public:
757 bool VisitDeclRefExpr(const DeclRefExpr *E) {
758 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
759 if (VD->hasLocalStorage()) {
760 SemaRef.Diag(E->getLocStart(),
761 diag::err_omp_local_var_in_threadprivate_init)
762 << E->getSourceRange();
763 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
764 << VD << VD->getSourceRange();
765 return true;
766 }
767 }
768 return false;
769 }
770 bool VisitStmt(const Stmt *S) {
771 for (auto Child : S->children()) {
772 if (Child && Visit(Child))
773 return true;
774 }
775 return false;
776 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000777 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000778};
779} // namespace
780
Alexey Bataeved09d242014-05-28 05:53:51 +0000781OMPThreadPrivateDecl *
782Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000783 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000784 for (auto &RefExpr : VarList) {
785 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000786 VarDecl *VD = cast<VarDecl>(DE->getDecl());
787 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000788
789 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
790 // A threadprivate variable must not have an incomplete type.
791 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000792 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000793 continue;
794 }
795
796 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
797 // A threadprivate variable must not have a reference type.
798 if (VD->getType()->isReferenceType()) {
799 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000800 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
801 bool IsDecl =
802 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
803 Diag(VD->getLocation(),
804 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
805 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000806 continue;
807 }
808
Richard Smithfd3834f2013-04-13 02:43:54 +0000809 // Check if this is a TLS variable.
810 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000811 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000812 bool IsDecl =
813 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
814 Diag(VD->getLocation(),
815 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
816 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000817 continue;
818 }
819
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000820 // Check if initial value of threadprivate variable reference variable with
821 // local storage (it is not supported by runtime).
822 if (auto Init = VD->getAnyInitializer()) {
823 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000824 if (Checker.Visit(Init))
825 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000826 }
827
Alexey Bataeved09d242014-05-28 05:53:51 +0000828 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000829 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000830 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000831 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000832 if (!Vars.empty()) {
833 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
834 Vars);
835 D->setAccess(AS_public);
836 }
837 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000838}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000839
Alexey Bataev7ff55242014-06-19 09:13:45 +0000840static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
841 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
842 bool IsLoopIterVar = false) {
843 if (DVar.RefExpr) {
844 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
845 << getOpenMPClauseName(DVar.CKind);
846 return;
847 }
848 enum {
849 PDSA_StaticMemberShared,
850 PDSA_StaticLocalVarShared,
851 PDSA_LoopIterVarPrivate,
852 PDSA_LoopIterVarLinear,
853 PDSA_LoopIterVarLastprivate,
854 PDSA_ConstVarShared,
855 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000856 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000857 PDSA_LocalVarPrivate,
858 PDSA_Implicit
859 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000860 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000861 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000862 if (IsLoopIterVar) {
863 if (DVar.CKind == OMPC_private)
864 Reason = PDSA_LoopIterVarPrivate;
865 else if (DVar.CKind == OMPC_lastprivate)
866 Reason = PDSA_LoopIterVarLastprivate;
867 else
868 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000869 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
870 Reason = PDSA_TaskVarFirstprivate;
871 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000872 } else if (VD->isStaticLocal())
873 Reason = PDSA_StaticLocalVarShared;
874 else if (VD->isStaticDataMember())
875 Reason = PDSA_StaticMemberShared;
876 else if (VD->isFileVarDecl())
877 Reason = PDSA_GlobalVarShared;
878 else if (VD->getType().isConstant(SemaRef.getASTContext()))
879 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000880 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000881 ReportHint = true;
882 Reason = PDSA_LocalVarPrivate;
883 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000884 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000885 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000886 << Reason << ReportHint
887 << getOpenMPDirectiveName(Stack->getCurrentDirective());
888 } else if (DVar.ImplicitDSALoc.isValid()) {
889 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
890 << getOpenMPClauseName(DVar.CKind);
891 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000892}
893
Alexey Bataev758e55e2013-09-06 18:03:48 +0000894namespace {
895class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
896 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000897 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000898 bool ErrorFound;
899 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000900 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000901 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000902
Alexey Bataev758e55e2013-09-06 18:03:48 +0000903public:
904 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000905 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000906 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000907 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
908 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000909
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000910 auto DVar = Stack->getTopDSA(VD, false);
911 // Check if the variable has explicit DSA set and stop analysis if it so.
912 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000913
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000914 auto ELoc = E->getExprLoc();
915 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000916 // The default(none) clause requires that each variable that is referenced
917 // in the construct, and does not have a predetermined data-sharing
918 // attribute, must have its data-sharing attribute explicitly determined
919 // by being listed in a data-sharing attribute clause.
920 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000921 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000922 VarsWithInheritedDSA.count(VD) == 0) {
923 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000924 return;
925 }
926
927 // OpenMP [2.9.3.6, Restrictions, p.2]
928 // A list item that appears in a reduction clause of the innermost
929 // enclosing worksharing or parallel construct may not be accessed in an
930 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000931 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000932 [](OpenMPDirectiveKind K) -> bool {
933 return isOpenMPParallelDirective(K) ||
934 isOpenMPWorksharingDirective(K);
935 },
936 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000937 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
938 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000939 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
940 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000941 return;
942 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000943
944 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000945 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000946 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000947 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000948 }
949 }
950 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000951 for (auto *C : S->clauses()) {
952 // Skip analysis of arguments of implicitly defined firstprivate clause
953 // for task directives.
954 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
955 for (auto *CC : C->children()) {
956 if (CC)
957 Visit(CC);
958 }
959 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000960 }
961 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000962 for (auto *C : S->children()) {
963 if (C && !isa<OMPExecutableDirective>(C))
964 Visit(C);
965 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000966 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000967
968 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000969 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000970 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
971 return VarsWithInheritedDSA;
972 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000973
Alexey Bataev7ff55242014-06-19 09:13:45 +0000974 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
975 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000976};
Alexey Bataeved09d242014-05-28 05:53:51 +0000977} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978
Alexey Bataevbae9a792014-06-27 10:37:06 +0000979void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000980 switch (DKind) {
981 case OMPD_parallel: {
982 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
983 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000984 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000985 std::make_pair(".global_tid.", KmpInt32PtrTy),
986 std::make_pair(".bound_tid.", KmpInt32PtrTy),
987 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000988 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000989 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
990 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000991 break;
992 }
993 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000994 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000995 std::make_pair(StringRef(), QualType()) // __context with shared vars
996 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000997 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
998 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000999 break;
1000 }
1001 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001002 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001003 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001004 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001005 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1006 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001007 break;
1008 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001009 case OMPD_sections: {
1010 Sema::CapturedParamNameType Params[] = {
1011 std::make_pair(StringRef(), QualType()) // __context with shared vars
1012 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001013 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1014 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001015 break;
1016 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001017 case OMPD_section: {
1018 Sema::CapturedParamNameType Params[] = {
1019 std::make_pair(StringRef(), QualType()) // __context with shared vars
1020 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001021 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1022 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001023 break;
1024 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001025 case OMPD_single: {
1026 Sema::CapturedParamNameType Params[] = {
1027 std::make_pair(StringRef(), QualType()) // __context with shared vars
1028 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001029 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1030 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001031 break;
1032 }
Alexander Musman80c22892014-07-17 08:54:58 +00001033 case OMPD_master: {
1034 Sema::CapturedParamNameType Params[] = {
1035 std::make_pair(StringRef(), QualType()) // __context with shared vars
1036 };
1037 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1038 Params);
1039 break;
1040 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001041 case OMPD_critical: {
1042 Sema::CapturedParamNameType Params[] = {
1043 std::make_pair(StringRef(), QualType()) // __context with shared vars
1044 };
1045 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1046 Params);
1047 break;
1048 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001049 case OMPD_parallel_for: {
1050 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1051 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1052 Sema::CapturedParamNameType Params[] = {
1053 std::make_pair(".global_tid.", KmpInt32PtrTy),
1054 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1055 std::make_pair(StringRef(), QualType()) // __context with shared vars
1056 };
1057 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1058 Params);
1059 break;
1060 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001061 case OMPD_parallel_sections: {
1062 Sema::CapturedParamNameType Params[] = {
1063 std::make_pair(StringRef(), QualType()) // __context with shared vars
1064 };
1065 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1066 Params);
1067 break;
1068 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001069 case OMPD_task: {
1070 Sema::CapturedParamNameType Params[] = {
1071 std::make_pair(StringRef(), QualType()) // __context with shared vars
1072 };
1073 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1074 Params);
1075 break;
1076 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001077 case OMPD_taskyield: {
1078 Sema::CapturedParamNameType Params[] = {
1079 std::make_pair(StringRef(), QualType()) // __context with shared vars
1080 };
1081 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1082 Params);
1083 break;
1084 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001085 case OMPD_barrier: {
1086 Sema::CapturedParamNameType Params[] = {
1087 std::make_pair(StringRef(), QualType()) // __context with shared vars
1088 };
1089 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1090 Params);
1091 break;
1092 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001093 case OMPD_taskwait: {
1094 Sema::CapturedParamNameType Params[] = {
1095 std::make_pair(StringRef(), QualType()) // __context with shared vars
1096 };
1097 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1098 Params);
1099 break;
1100 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001101 case OMPD_flush: {
1102 Sema::CapturedParamNameType Params[] = {
1103 std::make_pair(StringRef(), QualType()) // __context with shared vars
1104 };
1105 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1106 Params);
1107 break;
1108 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001109 case OMPD_ordered: {
1110 Sema::CapturedParamNameType Params[] = {
1111 std::make_pair(StringRef(), QualType()) // __context with shared vars
1112 };
1113 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1114 Params);
1115 break;
1116 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001117 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001118 llvm_unreachable("OpenMP Directive is not allowed");
1119 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001120 llvm_unreachable("Unknown OpenMP directive");
1121 }
1122}
1123
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001124static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1125 OpenMPDirectiveKind CurrentRegion,
1126 const DeclarationNameInfo &CurrentName,
1127 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001128 // Allowed nesting of constructs
1129 // +------------------+-----------------+------------------------------------+
1130 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1131 // +------------------+-----------------+------------------------------------+
1132 // | parallel | parallel | * |
1133 // | parallel | for | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001134 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001135 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001136 // | parallel | simd | * |
1137 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001138 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001139 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001140 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001141 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001142 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001143 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001144 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001145 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001146 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001147 // | parallel | ordered | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001148 // +------------------+-----------------+------------------------------------+
1149 // | for | parallel | * |
1150 // | for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001151 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001152 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001153 // | for | simd | * |
1154 // | for | sections | + |
1155 // | for | section | + |
1156 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001157 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001158 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001159 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001160 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001161 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001162 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001163 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001164 // | for | ordered | * (if construct is ordered) |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001165 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001166 // | master | parallel | * |
1167 // | master | for | + |
1168 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001169 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001170 // | master | simd | * |
1171 // | master | sections | + |
1172 // | master | section | + |
1173 // | master | single | + |
1174 // | master | parallel for | * |
1175 // | master |parallel sections| * |
1176 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001177 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001178 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001179 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001180 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001181 // | master | ordered | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001182 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001183 // | critical | parallel | * |
1184 // | critical | for | + |
1185 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001186 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001187 // | critical | simd | * |
1188 // | critical | sections | + |
1189 // | critical | section | + |
1190 // | critical | single | + |
1191 // | critical | parallel for | * |
1192 // | critical |parallel sections| * |
1193 // | critical | task | * |
1194 // | critical | taskyield | * |
1195 // | critical | barrier | + |
1196 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001197 // | critical | ordered | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001198 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001199 // | simd | parallel | |
1200 // | simd | for | |
Alexander Musman80c22892014-07-17 08:54:58 +00001201 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001202 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001203 // | simd | simd | |
1204 // | simd | sections | |
1205 // | simd | section | |
1206 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001207 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001208 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001209 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001210 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001211 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001212 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001213 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001214 // | simd | ordered | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001215 // +------------------+-----------------+------------------------------------+
1216 // | sections | parallel | * |
1217 // | sections | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001218 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001219 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001220 // | sections | simd | * |
1221 // | sections | sections | + |
1222 // | sections | section | * |
1223 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001224 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001225 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001226 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001227 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001228 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001229 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001230 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001231 // | sections | ordered | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001232 // +------------------+-----------------+------------------------------------+
1233 // | section | parallel | * |
1234 // | section | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001235 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001236 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001237 // | section | simd | * |
1238 // | section | sections | + |
1239 // | section | section | + |
1240 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001241 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001242 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001243 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001244 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001245 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001246 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001247 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001248 // | section | ordered | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001249 // +------------------+-----------------+------------------------------------+
1250 // | single | parallel | * |
1251 // | single | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001252 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001253 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001254 // | single | simd | * |
1255 // | single | sections | + |
1256 // | single | section | + |
1257 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001258 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001259 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001260 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001261 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001262 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001263 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001264 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001265 // | single | ordered | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001266 // +------------------+-----------------+------------------------------------+
1267 // | parallel for | parallel | * |
1268 // | parallel for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001269 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001270 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001271 // | parallel for | simd | * |
1272 // | parallel for | sections | + |
1273 // | parallel for | section | + |
1274 // | parallel for | single | + |
1275 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001276 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001277 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001278 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001279 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001280 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001281 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001282 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001283 // +------------------+-----------------+------------------------------------+
1284 // | parallel sections| parallel | * |
1285 // | parallel sections| for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001286 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001287 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001288 // | parallel sections| simd | * |
1289 // | parallel sections| sections | + |
1290 // | parallel sections| section | * |
1291 // | parallel sections| single | + |
1292 // | parallel sections| parallel for | * |
1293 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001294 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001295 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001296 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001297 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001298 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001299 // | parallel sections| ordered | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001300 // +------------------+-----------------+------------------------------------+
1301 // | task | parallel | * |
1302 // | task | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001303 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001304 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001305 // | task | simd | * |
1306 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001307 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001308 // | task | single | + |
1309 // | task | parallel for | * |
1310 // | task |parallel sections| * |
1311 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001312 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001313 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001314 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001315 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001316 // | task | ordered | + |
1317 // +------------------+-----------------+------------------------------------+
1318 // | ordered | parallel | * |
1319 // | ordered | for | + |
1320 // | ordered | master | * |
1321 // | ordered | critical | * |
1322 // | ordered | simd | * |
1323 // | ordered | sections | + |
1324 // | ordered | section | + |
1325 // | ordered | single | + |
1326 // | ordered | parallel for | * |
1327 // | ordered |parallel sections| * |
1328 // | ordered | task | * |
1329 // | ordered | taskyield | * |
1330 // | ordered | barrier | + |
1331 // | ordered | taskwait | * |
1332 // | ordered | flush | * |
1333 // | ordered | ordered | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001334 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001335 if (Stack->getCurScope()) {
1336 auto ParentRegion = Stack->getParentDirective();
1337 bool NestingProhibited = false;
1338 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001339 enum {
1340 NoRecommend,
1341 ShouldBeInParallelRegion,
1342 ShouldBeInOrderedRegion
1343 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001344 if (isOpenMPSimdDirective(ParentRegion)) {
1345 // OpenMP [2.16, Nesting of Regions]
1346 // OpenMP constructs may not be nested inside a simd region.
1347 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1348 return true;
1349 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001350 if (CurrentRegion == OMPD_section) {
1351 // OpenMP [2.7.2, sections Construct, Restrictions]
1352 // Orphaned section directives are prohibited. That is, the section
1353 // directives must appear within the sections construct and must not be
1354 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001355 if (ParentRegion != OMPD_sections &&
1356 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001357 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1358 << (ParentRegion != OMPD_unknown)
1359 << getOpenMPDirectiveName(ParentRegion);
1360 return true;
1361 }
1362 return false;
1363 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001364 // Allow some constructs to be orphaned (they could be used in functions,
1365 // called from OpenMP regions with the required preconditions).
1366 if (ParentRegion == OMPD_unknown)
1367 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001368 if (CurrentRegion == OMPD_master) {
1369 // OpenMP [2.16, Nesting of Regions]
1370 // A master region may not be closely nested inside a worksharing,
1371 // atomic (TODO), or explicit task region.
1372 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1373 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001374 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1375 // OpenMP [2.16, Nesting of Regions]
1376 // A critical region may not be nested (closely or otherwise) inside a
1377 // critical region with the same name. Note that this restriction is not
1378 // sufficient to prevent deadlock.
1379 SourceLocation PreviousCriticalLoc;
1380 bool DeadLock =
1381 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1382 OpenMPDirectiveKind K,
1383 const DeclarationNameInfo &DNI,
1384 SourceLocation Loc)
1385 ->bool {
1386 if (K == OMPD_critical &&
1387 DNI.getName() == CurrentName.getName()) {
1388 PreviousCriticalLoc = Loc;
1389 return true;
1390 } else
1391 return false;
1392 },
1393 false /* skip top directive */);
1394 if (DeadLock) {
1395 SemaRef.Diag(StartLoc,
1396 diag::err_omp_prohibited_region_critical_same_name)
1397 << CurrentName.getName();
1398 if (PreviousCriticalLoc.isValid())
1399 SemaRef.Diag(PreviousCriticalLoc,
1400 diag::note_omp_previous_critical_region);
1401 return true;
1402 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001403 } else if (CurrentRegion == OMPD_barrier) {
1404 // OpenMP [2.16, Nesting of Regions]
1405 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001406 // explicit task, critical, ordered, atomic(TODO), or master region.
1407 NestingProhibited =
1408 isOpenMPWorksharingDirective(ParentRegion) ||
1409 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1410 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001411 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1412 !isOpenMPParallelDirective(CurrentRegion) &&
1413 !isOpenMPSimdDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001414 // OpenMP [2.16, Nesting of Regions]
1415 // A worksharing region may not be closely nested inside a worksharing,
1416 // explicit task, critical, ordered, atomic, or master region.
1417 // TODO
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001418 NestingProhibited =
1419 (isOpenMPWorksharingDirective(ParentRegion) &&
1420 !isOpenMPSimdDirective(ParentRegion)) ||
1421 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1422 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1423 Recommend = ShouldBeInParallelRegion;
1424 } else if (CurrentRegion == OMPD_ordered) {
1425 // OpenMP [2.16, Nesting of Regions]
1426 // An ordered region may not be closely nested inside a critical,
1427 // atomic(TODO), or explicit task region.
1428 // An ordered region must be closely nested inside a loop region (or
1429 // parallel loop region) with an ordered clause.
1430 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001431 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001432 !Stack->isParentOrderedRegion();
1433 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001434 }
1435 if (NestingProhibited) {
1436 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001437 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1438 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001439 return true;
1440 }
1441 }
1442 return false;
1443}
1444
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001445StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001446 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001447 ArrayRef<OMPClause *> Clauses,
1448 Stmt *AStmt,
1449 SourceLocation StartLoc,
1450 SourceLocation EndLoc) {
1451 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001452 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001453 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001454
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001455 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001456 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001457 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001458 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001459 if (AStmt) {
1460 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1461
1462 // Check default data sharing attributes for referenced variables.
1463 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1464 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1465 if (DSAChecker.isErrorFound())
1466 return StmtError();
1467 // Generate list of implicitly defined firstprivate variables.
1468 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001469
1470 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1471 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1472 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1473 SourceLocation(), SourceLocation())) {
1474 ClausesWithImplicit.push_back(Implicit);
1475 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1476 DSAChecker.getImplicitFirstprivate().size();
1477 } else
1478 ErrorFound = true;
1479 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001480 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001481
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001482 switch (Kind) {
1483 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001484 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1485 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001486 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001487 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001488 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1489 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001490 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001491 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001492 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1493 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001494 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001495 case OMPD_sections:
1496 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1497 EndLoc);
1498 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001499 case OMPD_section:
1500 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001501 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001502 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1503 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001504 case OMPD_single:
1505 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1506 EndLoc);
1507 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001508 case OMPD_master:
1509 assert(ClausesWithImplicit.empty() &&
1510 "No clauses are allowed for 'omp master' directive");
1511 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1512 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001513 case OMPD_critical:
1514 assert(ClausesWithImplicit.empty() &&
1515 "No clauses are allowed for 'omp critical' directive");
1516 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1517 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001518 case OMPD_parallel_for:
1519 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1520 EndLoc, VarsWithInheritedDSA);
1521 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001522 case OMPD_parallel_sections:
1523 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1524 StartLoc, EndLoc);
1525 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001526 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001527 Res =
1528 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1529 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001530 case OMPD_taskyield:
1531 assert(ClausesWithImplicit.empty() &&
1532 "No clauses are allowed for 'omp taskyield' directive");
1533 assert(AStmt == nullptr &&
1534 "No associated statement allowed for 'omp taskyield' directive");
1535 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1536 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001537 case OMPD_barrier:
1538 assert(ClausesWithImplicit.empty() &&
1539 "No clauses are allowed for 'omp barrier' directive");
1540 assert(AStmt == nullptr &&
1541 "No associated statement allowed for 'omp barrier' directive");
1542 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1543 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001544 case OMPD_taskwait:
1545 assert(ClausesWithImplicit.empty() &&
1546 "No clauses are allowed for 'omp taskwait' directive");
1547 assert(AStmt == nullptr &&
1548 "No associated statement allowed for 'omp taskwait' directive");
1549 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1550 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001551 case OMPD_flush:
1552 assert(AStmt == nullptr &&
1553 "No associated statement allowed for 'omp flush' directive");
1554 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1555 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001556 case OMPD_ordered:
1557 assert(ClausesWithImplicit.empty() &&
1558 "No clauses are allowed for 'omp ordered' directive");
1559 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1560 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001561 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001562 llvm_unreachable("OpenMP Directive is not allowed");
1563 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001564 llvm_unreachable("Unknown OpenMP directive");
1565 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001566
Alexey Bataev4acb8592014-07-07 13:01:15 +00001567 for (auto P : VarsWithInheritedDSA) {
1568 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1569 << P.first << P.second->getSourceRange();
1570 }
1571 if (!VarsWithInheritedDSA.empty())
1572 return StmtError();
1573
Alexey Bataeved09d242014-05-28 05:53:51 +00001574 if (ErrorFound)
1575 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001576 return Res;
1577}
1578
1579StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1580 Stmt *AStmt,
1581 SourceLocation StartLoc,
1582 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001583 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1584 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1585 // 1.2.2 OpenMP Language Terminology
1586 // Structured block - An executable statement with a single entry at the
1587 // top and a single exit at the bottom.
1588 // The point of exit cannot be a branch out of the structured block.
1589 // longjmp() and throw() must not violate the entry/exit criteria.
1590 CS->getCapturedDecl()->setNothrow();
1591
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001592 getCurFunction()->setHasBranchProtectedScope();
1593
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001594 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1595 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001596}
1597
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001598namespace {
1599/// \brief Helper class for checking canonical form of the OpenMP loops and
1600/// extracting iteration space of each loop in the loop nest, that will be used
1601/// for IR generation.
1602class OpenMPIterationSpaceChecker {
1603 /// \brief Reference to Sema.
1604 Sema &SemaRef;
1605 /// \brief A location for diagnostics (when there is no some better location).
1606 SourceLocation DefaultLoc;
1607 /// \brief A location for diagnostics (when increment is not compatible).
1608 SourceLocation ConditionLoc;
1609 /// \brief A source location for referring to condition later.
1610 SourceRange ConditionSrcRange;
1611 /// \brief Loop variable.
1612 VarDecl *Var;
1613 /// \brief Lower bound (initializer for the var).
1614 Expr *LB;
1615 /// \brief Upper bound.
1616 Expr *UB;
1617 /// \brief Loop step (increment).
1618 Expr *Step;
1619 /// \brief This flag is true when condition is one of:
1620 /// Var < UB
1621 /// Var <= UB
1622 /// UB > Var
1623 /// UB >= Var
1624 bool TestIsLessOp;
1625 /// \brief This flag is true when condition is strict ( < or > ).
1626 bool TestIsStrictOp;
1627 /// \brief This flag is true when step is subtracted on each iteration.
1628 bool SubtractStep;
1629
1630public:
1631 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1632 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1633 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1634 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1635 SubtractStep(false) {}
1636 /// \brief Check init-expr for canonical loop form and save loop counter
1637 /// variable - #Var and its initialization value - #LB.
1638 bool CheckInit(Stmt *S);
1639 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1640 /// for less/greater and for strict/non-strict comparison.
1641 bool CheckCond(Expr *S);
1642 /// \brief Check incr-expr for canonical loop form and return true if it
1643 /// does not conform, otherwise save loop step (#Step).
1644 bool CheckInc(Expr *S);
1645 /// \brief Return the loop counter variable.
1646 VarDecl *GetLoopVar() const { return Var; }
1647 /// \brief Return true if any expression is dependent.
1648 bool Dependent() const;
1649
1650private:
1651 /// \brief Check the right-hand side of an assignment in the increment
1652 /// expression.
1653 bool CheckIncRHS(Expr *RHS);
1654 /// \brief Helper to set loop counter variable and its initializer.
1655 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1656 /// \brief Helper to set upper bound.
1657 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1658 const SourceLocation &SL);
1659 /// \brief Helper to set loop increment.
1660 bool SetStep(Expr *NewStep, bool Subtract);
1661};
1662
1663bool OpenMPIterationSpaceChecker::Dependent() const {
1664 if (!Var) {
1665 assert(!LB && !UB && !Step);
1666 return false;
1667 }
1668 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1669 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1670}
1671
1672bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1673 // State consistency checking to ensure correct usage.
1674 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1675 !TestIsLessOp && !TestIsStrictOp);
1676 if (!NewVar || !NewLB)
1677 return true;
1678 Var = NewVar;
1679 LB = NewLB;
1680 return false;
1681}
1682
1683bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1684 const SourceRange &SR,
1685 const SourceLocation &SL) {
1686 // State consistency checking to ensure correct usage.
1687 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1688 !TestIsLessOp && !TestIsStrictOp);
1689 if (!NewUB)
1690 return true;
1691 UB = NewUB;
1692 TestIsLessOp = LessOp;
1693 TestIsStrictOp = StrictOp;
1694 ConditionSrcRange = SR;
1695 ConditionLoc = SL;
1696 return false;
1697}
1698
1699bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1700 // State consistency checking to ensure correct usage.
1701 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1702 if (!NewStep)
1703 return true;
1704 if (!NewStep->isValueDependent()) {
1705 // Check that the step is integer expression.
1706 SourceLocation StepLoc = NewStep->getLocStart();
1707 ExprResult Val =
1708 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1709 if (Val.isInvalid())
1710 return true;
1711 NewStep = Val.get();
1712
1713 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1714 // If test-expr is of form var relational-op b and relational-op is < or
1715 // <= then incr-expr must cause var to increase on each iteration of the
1716 // loop. If test-expr is of form var relational-op b and relational-op is
1717 // > or >= then incr-expr must cause var to decrease on each iteration of
1718 // the loop.
1719 // If test-expr is of form b relational-op var and relational-op is < or
1720 // <= then incr-expr must cause var to decrease on each iteration of the
1721 // loop. If test-expr is of form b relational-op var and relational-op is
1722 // > or >= then incr-expr must cause var to increase on each iteration of
1723 // the loop.
1724 llvm::APSInt Result;
1725 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1726 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1727 bool IsConstNeg =
1728 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1729 bool IsConstZero = IsConstant && !Result.getBoolValue();
1730 if (UB && (IsConstZero ||
1731 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1732 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1733 SemaRef.Diag(NewStep->getExprLoc(),
1734 diag::err_omp_loop_incr_not_compatible)
1735 << Var << TestIsLessOp << NewStep->getSourceRange();
1736 SemaRef.Diag(ConditionLoc,
1737 diag::note_omp_loop_cond_requres_compatible_incr)
1738 << TestIsLessOp << ConditionSrcRange;
1739 return true;
1740 }
1741 }
1742
1743 Step = NewStep;
1744 SubtractStep = Subtract;
1745 return false;
1746}
1747
1748bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1749 // Check init-expr for canonical loop form and save loop counter
1750 // variable - #Var and its initialization value - #LB.
1751 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1752 // var = lb
1753 // integer-type var = lb
1754 // random-access-iterator-type var = lb
1755 // pointer-type var = lb
1756 //
1757 if (!S) {
1758 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1759 return true;
1760 }
1761 if (Expr *E = dyn_cast<Expr>(S))
1762 S = E->IgnoreParens();
1763 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1764 if (BO->getOpcode() == BO_Assign)
1765 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1766 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1767 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1768 if (DS->isSingleDecl()) {
1769 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1770 if (Var->hasInit()) {
1771 // Accept non-canonical init form here but emit ext. warning.
1772 if (Var->getInitStyle() != VarDecl::CInit)
1773 SemaRef.Diag(S->getLocStart(),
1774 diag::ext_omp_loop_not_canonical_init)
1775 << S->getSourceRange();
1776 return SetVarAndLB(Var, Var->getInit());
1777 }
1778 }
1779 }
1780 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1781 if (CE->getOperator() == OO_Equal)
1782 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1783 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1784
1785 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1786 << S->getSourceRange();
1787 return true;
1788}
1789
Alexey Bataev23b69422014-06-18 07:08:49 +00001790/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001791/// variable (which may be the loop variable) if possible.
1792static const VarDecl *GetInitVarDecl(const Expr *E) {
1793 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001794 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001795 E = E->IgnoreParenImpCasts();
1796 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1797 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1798 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1799 CE->getArg(0) != nullptr)
1800 E = CE->getArg(0)->IgnoreParenImpCasts();
1801 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1802 if (!DRE)
1803 return nullptr;
1804 return dyn_cast<VarDecl>(DRE->getDecl());
1805}
1806
1807bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1808 // Check test-expr for canonical form, save upper-bound UB, flags for
1809 // less/greater and for strict/non-strict comparison.
1810 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1811 // var relational-op b
1812 // b relational-op var
1813 //
1814 if (!S) {
1815 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1816 return true;
1817 }
1818 S = S->IgnoreParenImpCasts();
1819 SourceLocation CondLoc = S->getLocStart();
1820 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1821 if (BO->isRelationalOp()) {
1822 if (GetInitVarDecl(BO->getLHS()) == Var)
1823 return SetUB(BO->getRHS(),
1824 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1825 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1826 BO->getSourceRange(), BO->getOperatorLoc());
1827 if (GetInitVarDecl(BO->getRHS()) == Var)
1828 return SetUB(BO->getLHS(),
1829 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1830 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1831 BO->getSourceRange(), BO->getOperatorLoc());
1832 }
1833 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1834 if (CE->getNumArgs() == 2) {
1835 auto Op = CE->getOperator();
1836 switch (Op) {
1837 case OO_Greater:
1838 case OO_GreaterEqual:
1839 case OO_Less:
1840 case OO_LessEqual:
1841 if (GetInitVarDecl(CE->getArg(0)) == Var)
1842 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1843 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1844 CE->getOperatorLoc());
1845 if (GetInitVarDecl(CE->getArg(1)) == Var)
1846 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1847 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1848 CE->getOperatorLoc());
1849 break;
1850 default:
1851 break;
1852 }
1853 }
1854 }
1855 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1856 << S->getSourceRange() << Var;
1857 return true;
1858}
1859
1860bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1861 // RHS of canonical loop form increment can be:
1862 // var + incr
1863 // incr + var
1864 // var - incr
1865 //
1866 RHS = RHS->IgnoreParenImpCasts();
1867 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1868 if (BO->isAdditiveOp()) {
1869 bool IsAdd = BO->getOpcode() == BO_Add;
1870 if (GetInitVarDecl(BO->getLHS()) == Var)
1871 return SetStep(BO->getRHS(), !IsAdd);
1872 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1873 return SetStep(BO->getLHS(), false);
1874 }
1875 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1876 bool IsAdd = CE->getOperator() == OO_Plus;
1877 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1878 if (GetInitVarDecl(CE->getArg(0)) == Var)
1879 return SetStep(CE->getArg(1), !IsAdd);
1880 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1881 return SetStep(CE->getArg(0), false);
1882 }
1883 }
1884 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1885 << RHS->getSourceRange() << Var;
1886 return true;
1887}
1888
1889bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1890 // Check incr-expr for canonical loop form and return true if it
1891 // does not conform.
1892 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1893 // ++var
1894 // var++
1895 // --var
1896 // var--
1897 // var += incr
1898 // var -= incr
1899 // var = var + incr
1900 // var = incr + var
1901 // var = var - incr
1902 //
1903 if (!S) {
1904 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1905 return true;
1906 }
1907 S = S->IgnoreParens();
1908 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1909 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1910 return SetStep(
1911 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1912 (UO->isDecrementOp() ? -1 : 1)).get(),
1913 false);
1914 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1915 switch (BO->getOpcode()) {
1916 case BO_AddAssign:
1917 case BO_SubAssign:
1918 if (GetInitVarDecl(BO->getLHS()) == Var)
1919 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1920 break;
1921 case BO_Assign:
1922 if (GetInitVarDecl(BO->getLHS()) == Var)
1923 return CheckIncRHS(BO->getRHS());
1924 break;
1925 default:
1926 break;
1927 }
1928 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1929 switch (CE->getOperator()) {
1930 case OO_PlusPlus:
1931 case OO_MinusMinus:
1932 if (GetInitVarDecl(CE->getArg(0)) == Var)
1933 return SetStep(
1934 SemaRef.ActOnIntegerConstant(
1935 CE->getLocStart(),
1936 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1937 false);
1938 break;
1939 case OO_PlusEqual:
1940 case OO_MinusEqual:
1941 if (GetInitVarDecl(CE->getArg(0)) == Var)
1942 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1943 break;
1944 case OO_Equal:
1945 if (GetInitVarDecl(CE->getArg(0)) == Var)
1946 return CheckIncRHS(CE->getArg(1));
1947 break;
1948 default:
1949 break;
1950 }
1951 }
1952 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1953 << S->getSourceRange() << Var;
1954 return true;
1955}
Alexey Bataev23b69422014-06-18 07:08:49 +00001956} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001957
1958/// \brief Called on a for stmt to check and extract its iteration space
1959/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001960static bool CheckOpenMPIterationSpace(
1961 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1962 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1963 Expr *NestedLoopCountExpr,
1964 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001965 // OpenMP [2.6, Canonical Loop Form]
1966 // for (init-expr; test-expr; incr-expr) structured-block
1967 auto For = dyn_cast_or_null<ForStmt>(S);
1968 if (!For) {
1969 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001970 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1971 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1972 << CurrentNestedLoopCount;
1973 if (NestedLoopCount > 1)
1974 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1975 diag::note_omp_collapse_expr)
1976 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001977 return true;
1978 }
1979 assert(For->getBody());
1980
1981 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1982
1983 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001984 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001985 if (ISC.CheckInit(Init)) {
1986 return true;
1987 }
1988
1989 bool HasErrors = false;
1990
1991 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001992 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001993
1994 // OpenMP [2.6, Canonical Loop Form]
1995 // Var is one of the following:
1996 // A variable of signed or unsigned integer type.
1997 // For C++, a variable of a random access iterator type.
1998 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001999 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002000 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2001 !VarType->isPointerType() &&
2002 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2003 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2004 << SemaRef.getLangOpts().CPlusPlus;
2005 HasErrors = true;
2006 }
2007
Alexey Bataev4acb8592014-07-07 13:01:15 +00002008 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2009 // Construct
2010 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2011 // parallel for construct is (are) private.
2012 // The loop iteration variable in the associated for-loop of a simd construct
2013 // with just one associated for-loop is linear with a constant-linear-step
2014 // that is the increment of the associated for-loop.
2015 // Exclude loop var from the list of variables with implicitly defined data
2016 // sharing attributes.
2017 while (VarsWithImplicitDSA.count(Var) > 0)
2018 VarsWithImplicitDSA.erase(Var);
2019
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002020 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2021 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002022 // The loop iteration variable in the associated for-loop of a simd construct
2023 // with just one associated for-loop may be listed in a linear clause with a
2024 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002025 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2026 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002027 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002028 auto PredeterminedCKind =
2029 isOpenMPSimdDirective(DKind)
2030 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2031 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002032 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002033 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00002034 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
2035 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002036 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002037 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002038 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2039 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002040 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002041 HasErrors = true;
2042 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002043 // Make the loop iteration variable private (for worksharing constructs),
2044 // linear (for simd directives with the only one associated loop) or
2045 // lastprivate (for simd directives with several collapsed loops).
2046 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002047 }
2048
Alexey Bataev7ff55242014-06-19 09:13:45 +00002049 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002050
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002051 // Check test-expr.
2052 HasErrors |= ISC.CheckCond(For->getCond());
2053
2054 // Check incr-expr.
2055 HasErrors |= ISC.CheckInc(For->getInc());
2056
2057 if (ISC.Dependent())
2058 return HasErrors;
2059
2060 // FIXME: Build loop's iteration space representation.
2061 return HasErrors;
2062}
2063
2064/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
2065/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
2066/// to get the first for loop.
2067static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
2068 if (IgnoreCaptured)
2069 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
2070 S = CapS->getCapturedStmt();
2071 // OpenMP [2.8.1, simd construct, Restrictions]
2072 // All loops associated with the construct must be perfectly nested; that is,
2073 // there must be no intervening code nor any OpenMP directive between any two
2074 // loops.
2075 while (true) {
2076 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
2077 S = AS->getSubStmt();
2078 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
2079 if (CS->size() != 1)
2080 break;
2081 S = CS->body_back();
2082 } else
2083 break;
2084 }
2085 return S;
2086}
2087
2088/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002089/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2090/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002091static unsigned
2092CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2093 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2094 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002095 unsigned NestedLoopCount = 1;
2096 if (NestedLoopCountExpr) {
2097 // Found 'collapse' clause - calculate collapse number.
2098 llvm::APSInt Result;
2099 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2100 NestedLoopCount = Result.getLimitedValue();
2101 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002102 // This is helper routine for loop directives (e.g., 'for', 'simd',
2103 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002104 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
2105 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002106 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002107 NestedLoopCount, NestedLoopCountExpr,
2108 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002109 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002110 // Move on to the next nested for loop, or to the loop body.
2111 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
2112 }
2113
2114 // FIXME: Build resulting iteration space for IR generation (collapsing
2115 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002116 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002117}
2118
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002119static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002120 auto CollapseFilter = [](const OMPClause *C) -> bool {
2121 return C->getClauseKind() == OMPC_collapse;
2122 };
2123 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2124 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002125 if (I)
2126 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2127 return nullptr;
2128}
2129
Alexey Bataev4acb8592014-07-07 13:01:15 +00002130StmtResult Sema::ActOnOpenMPSimdDirective(
2131 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2132 SourceLocation EndLoc,
2133 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002134 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002135 unsigned NestedLoopCount =
2136 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2137 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002138 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002139 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002140
2141 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002142 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2143 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002144}
2145
Alexey Bataev4acb8592014-07-07 13:01:15 +00002146StmtResult Sema::ActOnOpenMPForDirective(
2147 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2148 SourceLocation EndLoc,
2149 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002150 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002151 unsigned NestedLoopCount =
2152 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2153 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002154 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002155 return StmtError();
2156
2157 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002158 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2159 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002160}
2161
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002162StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2163 Stmt *AStmt,
2164 SourceLocation StartLoc,
2165 SourceLocation EndLoc) {
2166 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2167 auto BaseStmt = AStmt;
2168 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2169 BaseStmt = CS->getCapturedStmt();
2170 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2171 auto S = C->children();
2172 if (!S)
2173 return StmtError();
2174 // All associated statements must be '#pragma omp section' except for
2175 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002176 for (++S; S; ++S) {
2177 auto SectionStmt = *S;
2178 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2179 if (SectionStmt)
2180 Diag(SectionStmt->getLocStart(),
2181 diag::err_omp_sections_substmt_not_section);
2182 return StmtError();
2183 }
2184 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002185 } else {
2186 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2187 return StmtError();
2188 }
2189
2190 getCurFunction()->setHasBranchProtectedScope();
2191
2192 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2193 AStmt);
2194}
2195
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002196StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2197 SourceLocation StartLoc,
2198 SourceLocation EndLoc) {
2199 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2200
2201 getCurFunction()->setHasBranchProtectedScope();
2202
2203 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2204}
2205
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002206StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2207 Stmt *AStmt,
2208 SourceLocation StartLoc,
2209 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002210 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2211
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002212 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002213
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002214 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2215}
2216
Alexander Musman80c22892014-07-17 08:54:58 +00002217StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2218 SourceLocation StartLoc,
2219 SourceLocation EndLoc) {
2220 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2221
2222 getCurFunction()->setHasBranchProtectedScope();
2223
2224 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2225}
2226
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002227StmtResult
2228Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2229 Stmt *AStmt, SourceLocation StartLoc,
2230 SourceLocation EndLoc) {
2231 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2232
2233 getCurFunction()->setHasBranchProtectedScope();
2234
2235 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2236 AStmt);
2237}
2238
Alexey Bataev4acb8592014-07-07 13:01:15 +00002239StmtResult Sema::ActOnOpenMPParallelForDirective(
2240 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2241 SourceLocation EndLoc,
2242 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2243 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2244 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2245 // 1.2.2 OpenMP Language Terminology
2246 // Structured block - An executable statement with a single entry at the
2247 // top and a single exit at the bottom.
2248 // The point of exit cannot be a branch out of the structured block.
2249 // longjmp() and throw() must not violate the entry/exit criteria.
2250 CS->getCapturedDecl()->setNothrow();
2251
2252 // In presence of clause 'collapse', it will define the nested loops number.
2253 unsigned NestedLoopCount =
2254 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2255 *this, *DSAStack, VarsWithImplicitDSA);
2256 if (NestedLoopCount == 0)
2257 return StmtError();
2258
2259 getCurFunction()->setHasBranchProtectedScope();
2260 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2261 NestedLoopCount, Clauses, AStmt);
2262}
2263
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002264StmtResult
2265Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2266 Stmt *AStmt, SourceLocation StartLoc,
2267 SourceLocation EndLoc) {
2268 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2269 auto BaseStmt = AStmt;
2270 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2271 BaseStmt = CS->getCapturedStmt();
2272 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2273 auto S = C->children();
2274 if (!S)
2275 return StmtError();
2276 // All associated statements must be '#pragma omp section' except for
2277 // the first one.
2278 for (++S; S; ++S) {
2279 auto SectionStmt = *S;
2280 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2281 if (SectionStmt)
2282 Diag(SectionStmt->getLocStart(),
2283 diag::err_omp_parallel_sections_substmt_not_section);
2284 return StmtError();
2285 }
2286 }
2287 } else {
2288 Diag(AStmt->getLocStart(),
2289 diag::err_omp_parallel_sections_not_compound_stmt);
2290 return StmtError();
2291 }
2292
2293 getCurFunction()->setHasBranchProtectedScope();
2294
2295 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2296 Clauses, AStmt);
2297}
2298
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002299StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2300 Stmt *AStmt, SourceLocation StartLoc,
2301 SourceLocation EndLoc) {
2302 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2303 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2304 // 1.2.2 OpenMP Language Terminology
2305 // Structured block - An executable statement with a single entry at the
2306 // top and a single exit at the bottom.
2307 // The point of exit cannot be a branch out of the structured block.
2308 // longjmp() and throw() must not violate the entry/exit criteria.
2309 CS->getCapturedDecl()->setNothrow();
2310
2311 getCurFunction()->setHasBranchProtectedScope();
2312
2313 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2314}
2315
Alexey Bataev68446b72014-07-18 07:47:19 +00002316StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2317 SourceLocation EndLoc) {
2318 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2319}
2320
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002321StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
2322 SourceLocation EndLoc) {
2323 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
2324}
2325
Alexey Bataev2df347a2014-07-18 10:17:07 +00002326StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
2327 SourceLocation EndLoc) {
2328 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
2329}
2330
Alexey Bataev6125da92014-07-21 11:26:11 +00002331StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
2332 SourceLocation StartLoc,
2333 SourceLocation EndLoc) {
2334 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
2335 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
2336}
2337
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002338StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
2339 SourceLocation StartLoc,
2340 SourceLocation EndLoc) {
2341 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2342
2343 getCurFunction()->setHasBranchProtectedScope();
2344
2345 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
2346}
2347
Alexey Bataeved09d242014-05-28 05:53:51 +00002348OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002349 SourceLocation StartLoc,
2350 SourceLocation LParenLoc,
2351 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002352 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002353 switch (Kind) {
2354 case OMPC_if:
2355 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2356 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002357 case OMPC_final:
2358 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2359 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002360 case OMPC_num_threads:
2361 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2362 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002363 case OMPC_safelen:
2364 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2365 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002366 case OMPC_collapse:
2367 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2368 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002369 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002370 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002371 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002372 case OMPC_private:
2373 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002374 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002375 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002376 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002377 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002378 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002379 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002380 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002381 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002382 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002383 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002384 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002385 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002386 case OMPC_flush:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002387 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002388 llvm_unreachable("Clause is not allowed.");
2389 }
2390 return Res;
2391}
2392
Alexey Bataeved09d242014-05-28 05:53:51 +00002393OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002394 SourceLocation LParenLoc,
2395 SourceLocation EndLoc) {
2396 Expr *ValExpr = Condition;
2397 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2398 !Condition->isInstantiationDependent() &&
2399 !Condition->containsUnexpandedParameterPack()) {
2400 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002401 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002402 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002403 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002404
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002405 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002406 }
2407
2408 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2409}
2410
Alexey Bataev3778b602014-07-17 07:32:53 +00002411OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2412 SourceLocation StartLoc,
2413 SourceLocation LParenLoc,
2414 SourceLocation EndLoc) {
2415 Expr *ValExpr = Condition;
2416 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2417 !Condition->isInstantiationDependent() &&
2418 !Condition->containsUnexpandedParameterPack()) {
2419 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2420 Condition->getExprLoc(), Condition);
2421 if (Val.isInvalid())
2422 return nullptr;
2423
2424 ValExpr = Val.get();
2425 }
2426
2427 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2428}
2429
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002430ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2431 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002432 if (!Op)
2433 return ExprError();
2434
2435 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2436 public:
2437 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002438 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002439 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2440 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002441 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2442 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002443 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2444 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002445 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2446 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002447 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2448 QualType T,
2449 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002450 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2451 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002452 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2453 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002454 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002455 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002456 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002457 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2458 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002459 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2460 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002461 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2462 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002463 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002464 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002465 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002466 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2467 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002468 llvm_unreachable("conversion functions are permitted");
2469 }
2470 } ConvertDiagnoser;
2471 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2472}
2473
2474OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2475 SourceLocation StartLoc,
2476 SourceLocation LParenLoc,
2477 SourceLocation EndLoc) {
2478 Expr *ValExpr = NumThreads;
2479 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2480 !NumThreads->isInstantiationDependent() &&
2481 !NumThreads->containsUnexpandedParameterPack()) {
2482 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2483 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002484 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002485 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002486 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002487
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002488 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002489
2490 // OpenMP [2.5, Restrictions]
2491 // The num_threads expression must evaluate to a positive integer value.
2492 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002493 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2494 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002495 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2496 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002497 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002498 }
2499 }
2500
Alexey Bataeved09d242014-05-28 05:53:51 +00002501 return new (Context)
2502 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002503}
2504
Alexey Bataev62c87d22014-03-21 04:51:18 +00002505ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2506 OpenMPClauseKind CKind) {
2507 if (!E)
2508 return ExprError();
2509 if (E->isValueDependent() || E->isTypeDependent() ||
2510 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002511 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002512 llvm::APSInt Result;
2513 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2514 if (ICE.isInvalid())
2515 return ExprError();
2516 if (!Result.isStrictlyPositive()) {
2517 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2518 << getOpenMPClauseName(CKind) << E->getSourceRange();
2519 return ExprError();
2520 }
2521 return ICE;
2522}
2523
2524OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2525 SourceLocation LParenLoc,
2526 SourceLocation EndLoc) {
2527 // OpenMP [2.8.1, simd construct, Description]
2528 // The parameter of the safelen clause must be a constant
2529 // positive integer expression.
2530 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2531 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002532 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002533 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002534 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002535}
2536
Alexander Musman64d33f12014-06-04 07:53:32 +00002537OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2538 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002539 SourceLocation LParenLoc,
2540 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002541 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002542 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002543 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002544 // The parameter of the collapse clause must be a constant
2545 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002546 ExprResult NumForLoopsResult =
2547 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2548 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002549 return nullptr;
2550 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002551 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002552}
2553
Alexey Bataeved09d242014-05-28 05:53:51 +00002554OMPClause *Sema::ActOnOpenMPSimpleClause(
2555 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2556 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002557 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002558 switch (Kind) {
2559 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002560 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002561 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2562 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002563 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002564 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002565 Res = ActOnOpenMPProcBindClause(
2566 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2567 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002568 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002569 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002570 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002571 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002572 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002573 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002574 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002575 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002576 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002577 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002578 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002579 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002580 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002581 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002582 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002583 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002584 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002585 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002586 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002587 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002588 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002589 case OMPC_flush:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002590 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002591 llvm_unreachable("Clause is not allowed.");
2592 }
2593 return Res;
2594}
2595
2596OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2597 SourceLocation KindKwLoc,
2598 SourceLocation StartLoc,
2599 SourceLocation LParenLoc,
2600 SourceLocation EndLoc) {
2601 if (Kind == OMPC_DEFAULT_unknown) {
2602 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002603 static_assert(OMPC_DEFAULT_unknown > 0,
2604 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002605 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002606 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002607 Values += "'";
2608 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2609 Values += "'";
2610 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002611 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002612 Values += " or ";
2613 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002614 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002615 break;
2616 default:
2617 Values += Sep;
2618 break;
2619 }
2620 }
2621 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002622 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002623 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002624 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002625 switch (Kind) {
2626 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002627 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002628 break;
2629 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002630 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002631 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002632 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002633 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002634 break;
2635 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002636 return new (Context)
2637 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002638}
2639
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002640OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2641 SourceLocation KindKwLoc,
2642 SourceLocation StartLoc,
2643 SourceLocation LParenLoc,
2644 SourceLocation EndLoc) {
2645 if (Kind == OMPC_PROC_BIND_unknown) {
2646 std::string Values;
2647 std::string Sep(", ");
2648 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2649 Values += "'";
2650 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2651 Values += "'";
2652 switch (i) {
2653 case OMPC_PROC_BIND_unknown - 2:
2654 Values += " or ";
2655 break;
2656 case OMPC_PROC_BIND_unknown - 1:
2657 break;
2658 default:
2659 Values += Sep;
2660 break;
2661 }
2662 }
2663 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002664 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002665 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002666 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002667 return new (Context)
2668 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002669}
2670
Alexey Bataev56dafe82014-06-20 07:16:17 +00002671OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2672 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2673 SourceLocation StartLoc, SourceLocation LParenLoc,
2674 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2675 SourceLocation EndLoc) {
2676 OMPClause *Res = nullptr;
2677 switch (Kind) {
2678 case OMPC_schedule:
2679 Res = ActOnOpenMPScheduleClause(
2680 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2681 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2682 break;
2683 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002684 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002685 case OMPC_num_threads:
2686 case OMPC_safelen:
2687 case OMPC_collapse:
2688 case OMPC_default:
2689 case OMPC_proc_bind:
2690 case OMPC_private:
2691 case OMPC_firstprivate:
2692 case OMPC_lastprivate:
2693 case OMPC_shared:
2694 case OMPC_reduction:
2695 case OMPC_linear:
2696 case OMPC_aligned:
2697 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002698 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002699 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002700 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002701 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002702 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002703 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002704 case OMPC_flush:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002705 case OMPC_unknown:
2706 llvm_unreachable("Clause is not allowed.");
2707 }
2708 return Res;
2709}
2710
2711OMPClause *Sema::ActOnOpenMPScheduleClause(
2712 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2713 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2714 SourceLocation EndLoc) {
2715 if (Kind == OMPC_SCHEDULE_unknown) {
2716 std::string Values;
2717 std::string Sep(", ");
2718 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2719 Values += "'";
2720 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2721 Values += "'";
2722 switch (i) {
2723 case OMPC_SCHEDULE_unknown - 2:
2724 Values += " or ";
2725 break;
2726 case OMPC_SCHEDULE_unknown - 1:
2727 break;
2728 default:
2729 Values += Sep;
2730 break;
2731 }
2732 }
2733 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2734 << Values << getOpenMPClauseName(OMPC_schedule);
2735 return nullptr;
2736 }
2737 Expr *ValExpr = ChunkSize;
2738 if (ChunkSize) {
2739 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2740 !ChunkSize->isInstantiationDependent() &&
2741 !ChunkSize->containsUnexpandedParameterPack()) {
2742 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2743 ExprResult Val =
2744 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2745 if (Val.isInvalid())
2746 return nullptr;
2747
2748 ValExpr = Val.get();
2749
2750 // OpenMP [2.7.1, Restrictions]
2751 // chunk_size must be a loop invariant integer expression with a positive
2752 // value.
2753 llvm::APSInt Result;
2754 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2755 Result.isSigned() && !Result.isStrictlyPositive()) {
2756 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2757 << "schedule" << ChunkSize->getSourceRange();
2758 return nullptr;
2759 }
2760 }
2761 }
2762
2763 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2764 EndLoc, Kind, ValExpr);
2765}
2766
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002767OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2768 SourceLocation StartLoc,
2769 SourceLocation EndLoc) {
2770 OMPClause *Res = nullptr;
2771 switch (Kind) {
2772 case OMPC_ordered:
2773 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2774 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002775 case OMPC_nowait:
2776 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2777 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002778 case OMPC_untied:
2779 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
2780 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002781 case OMPC_mergeable:
2782 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
2783 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002784 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002785 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002786 case OMPC_num_threads:
2787 case OMPC_safelen:
2788 case OMPC_collapse:
2789 case OMPC_schedule:
2790 case OMPC_private:
2791 case OMPC_firstprivate:
2792 case OMPC_lastprivate:
2793 case OMPC_shared:
2794 case OMPC_reduction:
2795 case OMPC_linear:
2796 case OMPC_aligned:
2797 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002798 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002799 case OMPC_default:
2800 case OMPC_proc_bind:
2801 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002802 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002803 case OMPC_unknown:
2804 llvm_unreachable("Clause is not allowed.");
2805 }
2806 return Res;
2807}
2808
2809OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2810 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002811 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002812 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2813}
2814
Alexey Bataev236070f2014-06-20 11:19:47 +00002815OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2816 SourceLocation EndLoc) {
2817 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2818}
2819
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002820OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
2821 SourceLocation EndLoc) {
2822 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
2823}
2824
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002825OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
2826 SourceLocation EndLoc) {
2827 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
2828}
2829
Alexey Bataevc5e02582014-06-16 07:08:35 +00002830OMPClause *Sema::ActOnOpenMPVarListClause(
2831 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2832 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2833 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2834 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002835 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002836 switch (Kind) {
2837 case OMPC_private:
2838 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2839 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002840 case OMPC_firstprivate:
2841 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2842 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002843 case OMPC_lastprivate:
2844 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2845 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002846 case OMPC_shared:
2847 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2848 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002849 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002850 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2851 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002852 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002853 case OMPC_linear:
2854 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2855 ColonLoc, EndLoc);
2856 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002857 case OMPC_aligned:
2858 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2859 ColonLoc, EndLoc);
2860 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002861 case OMPC_copyin:
2862 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2863 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002864 case OMPC_copyprivate:
2865 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2866 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002867 case OMPC_flush:
2868 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
2869 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002870 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002871 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002872 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002873 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002874 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002875 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002876 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002877 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002878 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002879 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002880 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002881 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002882 case OMPC_threadprivate:
2883 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002884 llvm_unreachable("Clause is not allowed.");
2885 }
2886 return Res;
2887}
2888
2889OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2890 SourceLocation StartLoc,
2891 SourceLocation LParenLoc,
2892 SourceLocation EndLoc) {
2893 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002894 for (auto &RefExpr : VarList) {
2895 assert(RefExpr && "NULL expr in OpenMP private clause.");
2896 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002897 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002898 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002899 continue;
2900 }
2901
Alexey Bataeved09d242014-05-28 05:53:51 +00002902 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002903 // OpenMP [2.1, C/C++]
2904 // A list item is a variable name.
2905 // OpenMP [2.9.3.3, Restrictions, p.1]
2906 // A variable that is part of another variable (as an array or
2907 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002908 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002909 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002910 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002911 continue;
2912 }
2913 Decl *D = DE->getDecl();
2914 VarDecl *VD = cast<VarDecl>(D);
2915
2916 QualType Type = VD->getType();
2917 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2918 // It will be analyzed later.
2919 Vars.push_back(DE);
2920 continue;
2921 }
2922
2923 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2924 // A variable that appears in a private clause must not have an incomplete
2925 // type or a reference type.
2926 if (RequireCompleteType(ELoc, Type,
2927 diag::err_omp_private_incomplete_type)) {
2928 continue;
2929 }
2930 if (Type->isReferenceType()) {
2931 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002932 << getOpenMPClauseName(OMPC_private) << Type;
2933 bool IsDecl =
2934 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2935 Diag(VD->getLocation(),
2936 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2937 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002938 continue;
2939 }
2940
2941 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2942 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002943 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002944 // class type.
2945 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002946 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2947 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002948 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002949 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2950 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2951 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002952 // FIXME This code must be replaced by actual constructing/destructing of
2953 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002954 if (RD) {
2955 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2956 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002957 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002958 if (!CD ||
2959 CheckConstructorAccess(ELoc, CD,
2960 InitializedEntity::InitializeTemporary(Type),
2961 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002962 CD->isDeleted()) {
2963 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002964 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002965 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2966 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002967 Diag(VD->getLocation(),
2968 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2969 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002970 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2971 continue;
2972 }
2973 MarkFunctionReferenced(ELoc, CD);
2974 DiagnoseUseOfDecl(CD, ELoc);
2975
2976 CXXDestructorDecl *DD = RD->getDestructor();
2977 if (DD) {
2978 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2979 DD->isDeleted()) {
2980 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002981 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002982 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2983 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002984 Diag(VD->getLocation(),
2985 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2986 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002987 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2988 continue;
2989 }
2990 MarkFunctionReferenced(ELoc, DD);
2991 DiagnoseUseOfDecl(DD, ELoc);
2992 }
2993 }
2994
Alexey Bataev758e55e2013-09-06 18:03:48 +00002995 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2996 // in a Construct]
2997 // Variables with the predetermined data-sharing attributes may not be
2998 // listed in data-sharing attributes clauses, except for the cases
2999 // listed below. For these exceptions only, listing a predetermined
3000 // variable in a data-sharing attribute clause is allowed and overrides
3001 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003002 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003003 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003004 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3005 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003006 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003007 continue;
3008 }
3009
3010 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003011 Vars.push_back(DE);
3012 }
3013
Alexey Bataeved09d242014-05-28 05:53:51 +00003014 if (Vars.empty())
3015 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003016
3017 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3018}
3019
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003020OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3021 SourceLocation StartLoc,
3022 SourceLocation LParenLoc,
3023 SourceLocation EndLoc) {
3024 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003025 bool IsImplicitClause =
3026 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3027 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3028
Alexey Bataeved09d242014-05-28 05:53:51 +00003029 for (auto &RefExpr : VarList) {
3030 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3031 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003032 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003033 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003034 continue;
3035 }
3036
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003037 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3038 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003039 // OpenMP [2.1, C/C++]
3040 // A list item is a variable name.
3041 // OpenMP [2.9.3.3, Restrictions, p.1]
3042 // A variable that is part of another variable (as an array or
3043 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003044 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003045 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003046 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003047 continue;
3048 }
3049 Decl *D = DE->getDecl();
3050 VarDecl *VD = cast<VarDecl>(D);
3051
3052 QualType Type = VD->getType();
3053 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3054 // It will be analyzed later.
3055 Vars.push_back(DE);
3056 continue;
3057 }
3058
3059 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3060 // A variable that appears in a private clause must not have an incomplete
3061 // type or a reference type.
3062 if (RequireCompleteType(ELoc, Type,
3063 diag::err_omp_firstprivate_incomplete_type)) {
3064 continue;
3065 }
3066 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003067 if (IsImplicitClause) {
3068 Diag(ImplicitClauseLoc,
3069 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3070 << Type;
3071 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3072 } else {
3073 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3074 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3075 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003076 bool IsDecl =
3077 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3078 Diag(VD->getLocation(),
3079 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3080 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003081 continue;
3082 }
3083
3084 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3085 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003086 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003087 // class type.
3088 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003089 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3090 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3091 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003092 // FIXME This code must be replaced by actual constructing/destructing of
3093 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003094 if (RD) {
3095 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3096 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003097 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003098 if (!CD ||
3099 CheckConstructorAccess(ELoc, CD,
3100 InitializedEntity::InitializeTemporary(Type),
3101 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003102 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003103 if (IsImplicitClause) {
3104 Diag(ImplicitClauseLoc,
3105 diag::err_omp_task_predetermined_firstprivate_required_method)
3106 << 0;
3107 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3108 } else {
3109 Diag(ELoc, diag::err_omp_required_method)
3110 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3111 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003112 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3113 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003114 Diag(VD->getLocation(),
3115 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3116 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003117 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3118 continue;
3119 }
3120 MarkFunctionReferenced(ELoc, CD);
3121 DiagnoseUseOfDecl(CD, ELoc);
3122
3123 CXXDestructorDecl *DD = RD->getDestructor();
3124 if (DD) {
3125 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3126 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003127 if (IsImplicitClause) {
3128 Diag(ImplicitClauseLoc,
3129 diag::err_omp_task_predetermined_firstprivate_required_method)
3130 << 1;
3131 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3132 } else {
3133 Diag(ELoc, diag::err_omp_required_method)
3134 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3135 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003136 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3137 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003138 Diag(VD->getLocation(),
3139 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3140 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003141 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3142 continue;
3143 }
3144 MarkFunctionReferenced(ELoc, DD);
3145 DiagnoseUseOfDecl(DD, ELoc);
3146 }
3147 }
3148
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003149 // If an implicit firstprivate variable found it was checked already.
3150 if (!IsImplicitClause) {
3151 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003152 Type = Type.getNonReferenceType().getCanonicalType();
3153 bool IsConstant = Type.isConstant(Context);
3154 Type = Context.getBaseElementType(Type);
3155 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
3156 // A list item that specifies a given variable may not appear in more
3157 // than one clause on the same directive, except that a variable may be
3158 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003159 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00003160 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003161 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003162 << getOpenMPClauseName(DVar.CKind)
3163 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003164 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003165 continue;
3166 }
3167
3168 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3169 // in a Construct]
3170 // Variables with the predetermined data-sharing attributes may not be
3171 // listed in data-sharing attributes clauses, except for the cases
3172 // listed below. For these exceptions only, listing a predetermined
3173 // variable in a data-sharing attribute clause is allowed and overrides
3174 // the variable's predetermined data-sharing attributes.
3175 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3176 // in a Construct, C/C++, p.2]
3177 // Variables with const-qualified type having no mutable member may be
3178 // listed in a firstprivate clause, even if they are static data members.
3179 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
3180 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
3181 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003182 << getOpenMPClauseName(DVar.CKind)
3183 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003184 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003185 continue;
3186 }
3187
Alexey Bataevf29276e2014-06-18 04:14:57 +00003188 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003189 // OpenMP [2.9.3.4, Restrictions, p.2]
3190 // A list item that is private within a parallel region must not appear
3191 // in a firstprivate clause on a worksharing construct if any of the
3192 // worksharing regions arising from the worksharing construct ever bind
3193 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00003194 if (isOpenMPWorksharingDirective(CurrDir) &&
3195 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003196 DVar = DSAStack->getImplicitDSA(VD, true);
3197 if (DVar.CKind != OMPC_shared &&
3198 (isOpenMPParallelDirective(DVar.DKind) ||
3199 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003200 Diag(ELoc, diag::err_omp_required_access)
3201 << getOpenMPClauseName(OMPC_firstprivate)
3202 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003203 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003204 continue;
3205 }
3206 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003207 // OpenMP [2.9.3.4, Restrictions, p.3]
3208 // A list item that appears in a reduction clause of a parallel construct
3209 // must not appear in a firstprivate clause on a worksharing or task
3210 // construct if any of the worksharing or task regions arising from the
3211 // worksharing or task construct ever bind to any of the parallel regions
3212 // arising from the parallel construct.
3213 // OpenMP [2.9.3.4, Restrictions, p.4]
3214 // A list item that appears in a reduction clause in worksharing
3215 // construct must not appear in a firstprivate clause in a task construct
3216 // encountered during execution of any of the worksharing regions arising
3217 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003218 if (CurrDir == OMPD_task) {
3219 DVar =
3220 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
3221 [](OpenMPDirectiveKind K) -> bool {
3222 return isOpenMPParallelDirective(K) ||
3223 isOpenMPWorksharingDirective(K);
3224 },
3225 false);
3226 if (DVar.CKind == OMPC_reduction &&
3227 (isOpenMPParallelDirective(DVar.DKind) ||
3228 isOpenMPWorksharingDirective(DVar.DKind))) {
3229 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
3230 << getOpenMPDirectiveName(DVar.DKind);
3231 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3232 continue;
3233 }
3234 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003235 }
3236
3237 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
3238 Vars.push_back(DE);
3239 }
3240
Alexey Bataeved09d242014-05-28 05:53:51 +00003241 if (Vars.empty())
3242 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003243
3244 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3245 Vars);
3246}
3247
Alexander Musman1bb328c2014-06-04 13:06:39 +00003248OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
3249 SourceLocation StartLoc,
3250 SourceLocation LParenLoc,
3251 SourceLocation EndLoc) {
3252 SmallVector<Expr *, 8> Vars;
3253 for (auto &RefExpr : VarList) {
3254 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
3255 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3256 // It will be analyzed later.
3257 Vars.push_back(RefExpr);
3258 continue;
3259 }
3260
3261 SourceLocation ELoc = RefExpr->getExprLoc();
3262 // OpenMP [2.1, C/C++]
3263 // A list item is a variable name.
3264 // OpenMP [2.14.3.5, Restrictions, p.1]
3265 // A variable that is part of another variable (as an array or structure
3266 // element) cannot appear in a lastprivate clause.
3267 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3268 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3269 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3270 continue;
3271 }
3272 Decl *D = DE->getDecl();
3273 VarDecl *VD = cast<VarDecl>(D);
3274
3275 QualType Type = VD->getType();
3276 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3277 // It will be analyzed later.
3278 Vars.push_back(DE);
3279 continue;
3280 }
3281
3282 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
3283 // A variable that appears in a lastprivate clause must not have an
3284 // incomplete type or a reference type.
3285 if (RequireCompleteType(ELoc, Type,
3286 diag::err_omp_lastprivate_incomplete_type)) {
3287 continue;
3288 }
3289 if (Type->isReferenceType()) {
3290 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3291 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3292 bool IsDecl =
3293 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3294 Diag(VD->getLocation(),
3295 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3296 << VD;
3297 continue;
3298 }
3299
3300 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3301 // in a Construct]
3302 // Variables with the predetermined data-sharing attributes may not be
3303 // listed in data-sharing attributes clauses, except for the cases
3304 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003305 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003306 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3307 DVar.CKind != OMPC_firstprivate &&
3308 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3309 Diag(ELoc, diag::err_omp_wrong_dsa)
3310 << getOpenMPClauseName(DVar.CKind)
3311 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003312 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003313 continue;
3314 }
3315
Alexey Bataevf29276e2014-06-18 04:14:57 +00003316 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3317 // OpenMP [2.14.3.5, Restrictions, p.2]
3318 // A list item that is private within a parallel region, or that appears in
3319 // the reduction clause of a parallel construct, must not appear in a
3320 // lastprivate clause on a worksharing construct if any of the corresponding
3321 // worksharing regions ever binds to any of the corresponding parallel
3322 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003323 if (isOpenMPWorksharingDirective(CurrDir) &&
3324 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003325 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003326 if (DVar.CKind != OMPC_shared) {
3327 Diag(ELoc, diag::err_omp_required_access)
3328 << getOpenMPClauseName(OMPC_lastprivate)
3329 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003330 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003331 continue;
3332 }
3333 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003334 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003335 // A variable of class type (or array thereof) that appears in a
3336 // lastprivate clause requires an accessible, unambiguous default
3337 // constructor for the class type, unless the list item is also specified
3338 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003339 // A variable of class type (or array thereof) that appears in a
3340 // lastprivate clause requires an accessible, unambiguous copy assignment
3341 // operator for the class type.
3342 while (Type.getNonReferenceType()->isArrayType())
3343 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3344 ->getElementType();
3345 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3346 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3347 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003348 // FIXME This code must be replaced by actual copying and destructing of the
3349 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003350 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003351 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3352 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003353 if (MD) {
3354 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3355 MD->isDeleted()) {
3356 Diag(ELoc, diag::err_omp_required_method)
3357 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3358 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3359 VarDecl::DeclarationOnly;
3360 Diag(VD->getLocation(),
3361 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3362 << VD;
3363 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3364 continue;
3365 }
3366 MarkFunctionReferenced(ELoc, MD);
3367 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003368 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003369
3370 CXXDestructorDecl *DD = RD->getDestructor();
3371 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003372 PartialDiagnostic PD =
3373 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003374 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3375 DD->isDeleted()) {
3376 Diag(ELoc, diag::err_omp_required_method)
3377 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3378 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3379 VarDecl::DeclarationOnly;
3380 Diag(VD->getLocation(),
3381 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3382 << VD;
3383 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3384 continue;
3385 }
3386 MarkFunctionReferenced(ELoc, DD);
3387 DiagnoseUseOfDecl(DD, ELoc);
3388 }
3389 }
3390
Alexey Bataevf29276e2014-06-18 04:14:57 +00003391 if (DVar.CKind != OMPC_firstprivate)
3392 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003393 Vars.push_back(DE);
3394 }
3395
3396 if (Vars.empty())
3397 return nullptr;
3398
3399 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3400 Vars);
3401}
3402
Alexey Bataev758e55e2013-09-06 18:03:48 +00003403OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3404 SourceLocation StartLoc,
3405 SourceLocation LParenLoc,
3406 SourceLocation EndLoc) {
3407 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003408 for (auto &RefExpr : VarList) {
3409 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3410 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003411 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003412 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003413 continue;
3414 }
3415
Alexey Bataeved09d242014-05-28 05:53:51 +00003416 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003417 // OpenMP [2.1, C/C++]
3418 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003419 // OpenMP [2.14.3.2, Restrictions, p.1]
3420 // A variable that is part of another variable (as an array or structure
3421 // element) cannot appear in a shared unless it is a static data member
3422 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003423 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003424 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003425 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003426 continue;
3427 }
3428 Decl *D = DE->getDecl();
3429 VarDecl *VD = cast<VarDecl>(D);
3430
3431 QualType Type = VD->getType();
3432 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3433 // It will be analyzed later.
3434 Vars.push_back(DE);
3435 continue;
3436 }
3437
3438 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3439 // in a Construct]
3440 // Variables with the predetermined data-sharing attributes may not be
3441 // listed in data-sharing attributes clauses, except for the cases
3442 // listed below. For these exceptions only, listing a predetermined
3443 // variable in a data-sharing attribute clause is allowed and overrides
3444 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003445 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003446 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3447 DVar.RefExpr) {
3448 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3449 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003450 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003451 continue;
3452 }
3453
3454 DSAStack->addDSA(VD, DE, OMPC_shared);
3455 Vars.push_back(DE);
3456 }
3457
Alexey Bataeved09d242014-05-28 05:53:51 +00003458 if (Vars.empty())
3459 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003460
3461 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3462}
3463
Alexey Bataevc5e02582014-06-16 07:08:35 +00003464namespace {
3465class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3466 DSAStackTy *Stack;
3467
3468public:
3469 bool VisitDeclRefExpr(DeclRefExpr *E) {
3470 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003471 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003472 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3473 return false;
3474 if (DVar.CKind != OMPC_unknown)
3475 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003476 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003477 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003478 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003479 return true;
3480 return false;
3481 }
3482 return false;
3483 }
3484 bool VisitStmt(Stmt *S) {
3485 for (auto Child : S->children()) {
3486 if (Child && Visit(Child))
3487 return true;
3488 }
3489 return false;
3490 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003491 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003492};
Alexey Bataev23b69422014-06-18 07:08:49 +00003493} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003494
3495OMPClause *Sema::ActOnOpenMPReductionClause(
3496 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3497 SourceLocation ColonLoc, SourceLocation EndLoc,
3498 CXXScopeSpec &ReductionIdScopeSpec,
3499 const DeclarationNameInfo &ReductionId) {
3500 // TODO: Allow scope specification search when 'declare reduction' is
3501 // supported.
3502 assert(ReductionIdScopeSpec.isEmpty() &&
3503 "No support for scoped reduction identifiers yet.");
3504
3505 auto DN = ReductionId.getName();
3506 auto OOK = DN.getCXXOverloadedOperator();
3507 BinaryOperatorKind BOK = BO_Comma;
3508
3509 // OpenMP [2.14.3.6, reduction clause]
3510 // C
3511 // reduction-identifier is either an identifier or one of the following
3512 // operators: +, -, *, &, |, ^, && and ||
3513 // C++
3514 // reduction-identifier is either an id-expression or one of the following
3515 // operators: +, -, *, &, |, ^, && and ||
3516 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3517 switch (OOK) {
3518 case OO_Plus:
3519 case OO_Minus:
3520 BOK = BO_AddAssign;
3521 break;
3522 case OO_Star:
3523 BOK = BO_MulAssign;
3524 break;
3525 case OO_Amp:
3526 BOK = BO_AndAssign;
3527 break;
3528 case OO_Pipe:
3529 BOK = BO_OrAssign;
3530 break;
3531 case OO_Caret:
3532 BOK = BO_XorAssign;
3533 break;
3534 case OO_AmpAmp:
3535 BOK = BO_LAnd;
3536 break;
3537 case OO_PipePipe:
3538 BOK = BO_LOr;
3539 break;
3540 default:
3541 if (auto II = DN.getAsIdentifierInfo()) {
3542 if (II->isStr("max"))
3543 BOK = BO_GT;
3544 else if (II->isStr("min"))
3545 BOK = BO_LT;
3546 }
3547 break;
3548 }
3549 SourceRange ReductionIdRange;
3550 if (ReductionIdScopeSpec.isValid()) {
3551 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3552 }
3553 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3554 if (BOK == BO_Comma) {
3555 // Not allowed reduction identifier is found.
3556 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3557 << ReductionIdRange;
3558 return nullptr;
3559 }
3560
3561 SmallVector<Expr *, 8> Vars;
3562 for (auto RefExpr : VarList) {
3563 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3564 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3565 // It will be analyzed later.
3566 Vars.push_back(RefExpr);
3567 continue;
3568 }
3569
3570 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3571 RefExpr->isInstantiationDependent() ||
3572 RefExpr->containsUnexpandedParameterPack()) {
3573 // It will be analyzed later.
3574 Vars.push_back(RefExpr);
3575 continue;
3576 }
3577
3578 auto ELoc = RefExpr->getExprLoc();
3579 auto ERange = RefExpr->getSourceRange();
3580 // OpenMP [2.1, C/C++]
3581 // A list item is a variable or array section, subject to the restrictions
3582 // specified in Section 2.4 on page 42 and in each of the sections
3583 // describing clauses and directives for which a list appears.
3584 // OpenMP [2.14.3.3, Restrictions, p.1]
3585 // A variable that is part of another variable (as an array or
3586 // structure element) cannot appear in a private clause.
3587 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3588 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3589 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3590 continue;
3591 }
3592 auto D = DE->getDecl();
3593 auto VD = cast<VarDecl>(D);
3594 auto Type = VD->getType();
3595 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3596 // A variable that appears in a private clause must not have an incomplete
3597 // type or a reference type.
3598 if (RequireCompleteType(ELoc, Type,
3599 diag::err_omp_reduction_incomplete_type))
3600 continue;
3601 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3602 // Arrays may not appear in a reduction clause.
3603 if (Type.getNonReferenceType()->isArrayType()) {
3604 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3605 bool IsDecl =
3606 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3607 Diag(VD->getLocation(),
3608 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3609 << VD;
3610 continue;
3611 }
3612 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3613 // A list item that appears in a reduction clause must not be
3614 // const-qualified.
3615 if (Type.getNonReferenceType().isConstant(Context)) {
3616 Diag(ELoc, diag::err_omp_const_variable)
3617 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3618 bool IsDecl =
3619 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3620 Diag(VD->getLocation(),
3621 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3622 << VD;
3623 continue;
3624 }
3625 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3626 // If a list-item is a reference type then it must bind to the same object
3627 // for all threads of the team.
3628 VarDecl *VDDef = VD->getDefinition();
3629 if (Type->isReferenceType() && VDDef) {
3630 DSARefChecker Check(DSAStack);
3631 if (Check.Visit(VDDef->getInit())) {
3632 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3633 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3634 continue;
3635 }
3636 }
3637 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3638 // The type of a list item that appears in a reduction clause must be valid
3639 // for the reduction-identifier. For a max or min reduction in C, the type
3640 // of the list item must be an allowed arithmetic data type: char, int,
3641 // float, double, or _Bool, possibly modified with long, short, signed, or
3642 // unsigned. For a max or min reduction in C++, the type of the list item
3643 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3644 // double, or bool, possibly modified with long, short, signed, or unsigned.
3645 if ((BOK == BO_GT || BOK == BO_LT) &&
3646 !(Type->isScalarType() ||
3647 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3648 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3649 << getLangOpts().CPlusPlus;
3650 bool IsDecl =
3651 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3652 Diag(VD->getLocation(),
3653 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3654 << VD;
3655 continue;
3656 }
3657 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3658 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3659 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3660 bool IsDecl =
3661 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3662 Diag(VD->getLocation(),
3663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3664 << VD;
3665 continue;
3666 }
3667 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3668 getDiagnostics().setSuppressAllDiagnostics(true);
3669 ExprResult ReductionOp =
3670 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3671 RefExpr, RefExpr);
3672 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3673 if (ReductionOp.isInvalid()) {
3674 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003675 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003676 bool IsDecl =
3677 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3678 Diag(VD->getLocation(),
3679 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3680 << VD;
3681 continue;
3682 }
3683
3684 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3685 // in a Construct]
3686 // Variables with the predetermined data-sharing attributes may not be
3687 // listed in data-sharing attributes clauses, except for the cases
3688 // listed below. For these exceptions only, listing a predetermined
3689 // variable in a data-sharing attribute clause is allowed and overrides
3690 // the variable's predetermined data-sharing attributes.
3691 // OpenMP [2.14.3.6, Restrictions, p.3]
3692 // Any number of reduction clauses can be specified on the directive,
3693 // but a list item can appear only once in the reduction clauses for that
3694 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003695 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003696 if (DVar.CKind == OMPC_reduction) {
3697 Diag(ELoc, diag::err_omp_once_referenced)
3698 << getOpenMPClauseName(OMPC_reduction);
3699 if (DVar.RefExpr) {
3700 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3701 }
3702 } else if (DVar.CKind != OMPC_unknown) {
3703 Diag(ELoc, diag::err_omp_wrong_dsa)
3704 << getOpenMPClauseName(DVar.CKind)
3705 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003706 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003707 continue;
3708 }
3709
3710 // OpenMP [2.14.3.6, Restrictions, p.1]
3711 // A list item that appears in a reduction clause of a worksharing
3712 // construct must be shared in the parallel regions to which any of the
3713 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003714 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003715 if (isOpenMPWorksharingDirective(CurrDir) &&
3716 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003717 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003718 if (DVar.CKind != OMPC_shared) {
3719 Diag(ELoc, diag::err_omp_required_access)
3720 << getOpenMPClauseName(OMPC_reduction)
3721 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003722 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003723 continue;
3724 }
3725 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003726
3727 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3728 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3729 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003730 // FIXME This code must be replaced by actual constructing/destructing of
3731 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003732 if (RD) {
3733 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3734 PartialDiagnostic PD =
3735 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003736 if (!CD ||
3737 CheckConstructorAccess(ELoc, CD,
3738 InitializedEntity::InitializeTemporary(Type),
3739 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003740 CD->isDeleted()) {
3741 Diag(ELoc, diag::err_omp_required_method)
3742 << getOpenMPClauseName(OMPC_reduction) << 0;
3743 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3744 VarDecl::DeclarationOnly;
3745 Diag(VD->getLocation(),
3746 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3747 << VD;
3748 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3749 continue;
3750 }
3751 MarkFunctionReferenced(ELoc, CD);
3752 DiagnoseUseOfDecl(CD, ELoc);
3753
3754 CXXDestructorDecl *DD = RD->getDestructor();
3755 if (DD) {
3756 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3757 DD->isDeleted()) {
3758 Diag(ELoc, diag::err_omp_required_method)
3759 << getOpenMPClauseName(OMPC_reduction) << 4;
3760 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3761 VarDecl::DeclarationOnly;
3762 Diag(VD->getLocation(),
3763 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3764 << VD;
3765 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3766 continue;
3767 }
3768 MarkFunctionReferenced(ELoc, DD);
3769 DiagnoseUseOfDecl(DD, ELoc);
3770 }
3771 }
3772
3773 DSAStack->addDSA(VD, DE, OMPC_reduction);
3774 Vars.push_back(DE);
3775 }
3776
3777 if (Vars.empty())
3778 return nullptr;
3779
3780 return OMPReductionClause::Create(
3781 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3782 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3783}
3784
Alexander Musman8dba6642014-04-22 13:09:42 +00003785OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3786 SourceLocation StartLoc,
3787 SourceLocation LParenLoc,
3788 SourceLocation ColonLoc,
3789 SourceLocation EndLoc) {
3790 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003791 for (auto &RefExpr : VarList) {
3792 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3793 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003794 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003795 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003796 continue;
3797 }
3798
3799 // OpenMP [2.14.3.7, linear clause]
3800 // A list item that appears in a linear clause is subject to the private
3801 // clause semantics described in Section 2.14.3.3 on page 159 except as
3802 // noted. In addition, the value of the new list item on each iteration
3803 // of the associated loop(s) corresponds to the value of the original
3804 // list item before entering the construct plus the logical number of
3805 // the iteration times linear-step.
3806
Alexey Bataeved09d242014-05-28 05:53:51 +00003807 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003808 // OpenMP [2.1, C/C++]
3809 // A list item is a variable name.
3810 // OpenMP [2.14.3.3, Restrictions, p.1]
3811 // A variable that is part of another variable (as an array or
3812 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003813 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003814 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003815 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003816 continue;
3817 }
3818
3819 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3820
3821 // OpenMP [2.14.3.7, linear clause]
3822 // A list-item cannot appear in more than one linear clause.
3823 // A list-item that appears in a linear clause cannot appear in any
3824 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003825 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003826 if (DVar.RefExpr) {
3827 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3828 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003829 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003830 continue;
3831 }
3832
3833 QualType QType = VD->getType();
3834 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3835 // It will be analyzed later.
3836 Vars.push_back(DE);
3837 continue;
3838 }
3839
3840 // A variable must not have an incomplete type or a reference type.
3841 if (RequireCompleteType(ELoc, QType,
3842 diag::err_omp_linear_incomplete_type)) {
3843 continue;
3844 }
3845 if (QType->isReferenceType()) {
3846 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3847 << getOpenMPClauseName(OMPC_linear) << QType;
3848 bool IsDecl =
3849 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3850 Diag(VD->getLocation(),
3851 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3852 << VD;
3853 continue;
3854 }
3855
3856 // A list item must not be const-qualified.
3857 if (QType.isConstant(Context)) {
3858 Diag(ELoc, diag::err_omp_const_variable)
3859 << getOpenMPClauseName(OMPC_linear);
3860 bool IsDecl =
3861 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3862 Diag(VD->getLocation(),
3863 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3864 << VD;
3865 continue;
3866 }
3867
3868 // A list item must be of integral or pointer type.
3869 QType = QType.getUnqualifiedType().getCanonicalType();
3870 const Type *Ty = QType.getTypePtrOrNull();
3871 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3872 !Ty->isPointerType())) {
3873 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3874 bool IsDecl =
3875 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3876 Diag(VD->getLocation(),
3877 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3878 << VD;
3879 continue;
3880 }
3881
3882 DSAStack->addDSA(VD, DE, OMPC_linear);
3883 Vars.push_back(DE);
3884 }
3885
3886 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003887 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003888
3889 Expr *StepExpr = Step;
3890 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3891 !Step->isInstantiationDependent() &&
3892 !Step->containsUnexpandedParameterPack()) {
3893 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003894 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003895 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003896 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003897 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003898
3899 // Warn about zero linear step (it would be probably better specified as
3900 // making corresponding variables 'const').
3901 llvm::APSInt Result;
3902 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3903 !Result.isNegative() && !Result.isStrictlyPositive())
3904 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3905 << (Vars.size() > 1);
3906 }
3907
3908 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3909 Vars, StepExpr);
3910}
3911
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003912OMPClause *Sema::ActOnOpenMPAlignedClause(
3913 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3914 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3915
3916 SmallVector<Expr *, 8> Vars;
3917 for (auto &RefExpr : VarList) {
3918 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3919 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3920 // It will be analyzed later.
3921 Vars.push_back(RefExpr);
3922 continue;
3923 }
3924
3925 SourceLocation ELoc = RefExpr->getExprLoc();
3926 // OpenMP [2.1, C/C++]
3927 // A list item is a variable name.
3928 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3929 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3930 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3931 continue;
3932 }
3933
3934 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3935
3936 // OpenMP [2.8.1, simd construct, Restrictions]
3937 // The type of list items appearing in the aligned clause must be
3938 // array, pointer, reference to array, or reference to pointer.
3939 QualType QType = DE->getType()
3940 .getNonReferenceType()
3941 .getUnqualifiedType()
3942 .getCanonicalType();
3943 const Type *Ty = QType.getTypePtrOrNull();
3944 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3945 !Ty->isPointerType())) {
3946 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3947 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3948 bool IsDecl =
3949 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3950 Diag(VD->getLocation(),
3951 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3952 << VD;
3953 continue;
3954 }
3955
3956 // OpenMP [2.8.1, simd construct, Restrictions]
3957 // A list-item cannot appear in more than one aligned clause.
3958 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3959 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3960 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3961 << getOpenMPClauseName(OMPC_aligned);
3962 continue;
3963 }
3964
3965 Vars.push_back(DE);
3966 }
3967
3968 // OpenMP [2.8.1, simd construct, Description]
3969 // The parameter of the aligned clause, alignment, must be a constant
3970 // positive integer expression.
3971 // If no optional parameter is specified, implementation-defined default
3972 // alignments for SIMD instructions on the target platforms are assumed.
3973 if (Alignment != nullptr) {
3974 ExprResult AlignResult =
3975 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3976 if (AlignResult.isInvalid())
3977 return nullptr;
3978 Alignment = AlignResult.get();
3979 }
3980 if (Vars.empty())
3981 return nullptr;
3982
3983 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3984 EndLoc, Vars, Alignment);
3985}
3986
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003987OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3988 SourceLocation StartLoc,
3989 SourceLocation LParenLoc,
3990 SourceLocation EndLoc) {
3991 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003992 for (auto &RefExpr : VarList) {
3993 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3994 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003995 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003996 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003997 continue;
3998 }
3999
Alexey Bataeved09d242014-05-28 05:53:51 +00004000 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004001 // OpenMP [2.1, C/C++]
4002 // A list item is a variable name.
4003 // OpenMP [2.14.4.1, Restrictions, p.1]
4004 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004005 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004006 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004007 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004008 continue;
4009 }
4010
4011 Decl *D = DE->getDecl();
4012 VarDecl *VD = cast<VarDecl>(D);
4013
4014 QualType Type = VD->getType();
4015 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4016 // It will be analyzed later.
4017 Vars.push_back(DE);
4018 continue;
4019 }
4020
4021 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4022 // A list item that appears in a copyin clause must be threadprivate.
4023 if (!DSAStack->isThreadPrivate(VD)) {
4024 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004025 << getOpenMPClauseName(OMPC_copyin)
4026 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004027 continue;
4028 }
4029
4030 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4031 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004032 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004033 // operator for the class type.
4034 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004035 CXXRecordDecl *RD =
4036 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004037 // FIXME This code must be replaced by actual assignment of the
4038 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004039 if (RD) {
4040 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4041 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004042 if (MD) {
4043 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4044 MD->isDeleted()) {
4045 Diag(ELoc, diag::err_omp_required_method)
4046 << getOpenMPClauseName(OMPC_copyin) << 2;
4047 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4048 VarDecl::DeclarationOnly;
4049 Diag(VD->getLocation(),
4050 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4051 << VD;
4052 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4053 continue;
4054 }
4055 MarkFunctionReferenced(ELoc, MD);
4056 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004057 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004058 }
4059
4060 DSAStack->addDSA(VD, DE, OMPC_copyin);
4061 Vars.push_back(DE);
4062 }
4063
Alexey Bataeved09d242014-05-28 05:53:51 +00004064 if (Vars.empty())
4065 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004066
4067 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4068}
4069
Alexey Bataevbae9a792014-06-27 10:37:06 +00004070OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4071 SourceLocation StartLoc,
4072 SourceLocation LParenLoc,
4073 SourceLocation EndLoc) {
4074 SmallVector<Expr *, 8> Vars;
4075 for (auto &RefExpr : VarList) {
4076 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4077 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4078 // It will be analyzed later.
4079 Vars.push_back(RefExpr);
4080 continue;
4081 }
4082
4083 SourceLocation ELoc = RefExpr->getExprLoc();
4084 // OpenMP [2.1, C/C++]
4085 // A list item is a variable name.
4086 // OpenMP [2.14.4.1, Restrictions, p.1]
4087 // A list item that appears in a copyin clause must be threadprivate.
4088 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4089 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4090 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4091 continue;
4092 }
4093
4094 Decl *D = DE->getDecl();
4095 VarDecl *VD = cast<VarDecl>(D);
4096
4097 QualType Type = VD->getType();
4098 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4099 // It will be analyzed later.
4100 Vars.push_back(DE);
4101 continue;
4102 }
4103
4104 // OpenMP [2.14.4.2, Restrictions, p.2]
4105 // A list item that appears in a copyprivate clause may not appear in a
4106 // private or firstprivate clause on the single construct.
4107 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004108 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004109 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4110 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4111 Diag(ELoc, diag::err_omp_wrong_dsa)
4112 << getOpenMPClauseName(DVar.CKind)
4113 << getOpenMPClauseName(OMPC_copyprivate);
4114 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4115 continue;
4116 }
4117
4118 // OpenMP [2.11.4.2, Restrictions, p.1]
4119 // All list items that appear in a copyprivate clause must be either
4120 // threadprivate or private in the enclosing context.
4121 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004122 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004123 if (DVar.CKind == OMPC_shared) {
4124 Diag(ELoc, diag::err_omp_required_access)
4125 << getOpenMPClauseName(OMPC_copyprivate)
4126 << "threadprivate or private in the enclosing context";
4127 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4128 continue;
4129 }
4130 }
4131 }
4132
4133 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4134 // A variable of class type (or array thereof) that appears in a
4135 // copyin clause requires an accessible, unambiguous copy assignment
4136 // operator for the class type.
4137 Type = Context.getBaseElementType(Type);
4138 CXXRecordDecl *RD =
4139 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
4140 // FIXME This code must be replaced by actual assignment of the
4141 // threadprivate variable.
4142 if (RD) {
4143 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4144 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4145 if (MD) {
4146 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4147 MD->isDeleted()) {
4148 Diag(ELoc, diag::err_omp_required_method)
4149 << getOpenMPClauseName(OMPC_copyprivate) << 2;
4150 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4151 VarDecl::DeclarationOnly;
4152 Diag(VD->getLocation(),
4153 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4154 << VD;
4155 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4156 continue;
4157 }
4158 MarkFunctionReferenced(ELoc, MD);
4159 DiagnoseUseOfDecl(MD, ELoc);
4160 }
4161 }
4162
4163 // No need to mark vars as copyprivate, they are already threadprivate or
4164 // implicitly private.
4165 Vars.push_back(DE);
4166 }
4167
4168 if (Vars.empty())
4169 return nullptr;
4170
4171 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4172}
4173
Alexey Bataev6125da92014-07-21 11:26:11 +00004174OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
4175 SourceLocation StartLoc,
4176 SourceLocation LParenLoc,
4177 SourceLocation EndLoc) {
4178 if (VarList.empty())
4179 return nullptr;
4180
4181 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
4182}
4183
Alexey Bataev758e55e2013-09-06 18:03:48 +00004184#undef DSAStack