blob: 5c78804239e80ba97272b973d90fb8377e633bca [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "clang/Basic/OpenMPKinds.h"
23#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000025#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000028#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000029using namespace clang;
30
Alexey Bataev758e55e2013-09-06 18:03:48 +000031//===----------------------------------------------------------------------===//
32// Stack of data-sharing attributes for variables
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// \brief Default data sharing attributes, which can be applied to directive.
37enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000038 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
39 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
40 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000041};
Alexey Bataev7ff55242014-06-19 09:13:45 +000042
Alexey Bataevf29276e2014-06-18 04:14:57 +000043template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000044 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000045 bool operator()(T Kind) {
46 for (auto KindEl : Arr)
47 if (KindEl == Kind)
48 return true;
49 return false;
50 }
51
52private:
53 ArrayRef<T> Arr;
54};
Alexey Bataev23b69422014-06-18 07:08:49 +000055struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000056 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000057 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000058};
59
60typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
61typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000062
63/// \brief Stack for tracking declarations used in OpenMP directives and
64/// clauses and their data-sharing attributes.
65class DSAStackTy {
66public:
67 struct DSAVarData {
68 OpenMPDirectiveKind DKind;
69 OpenMPClauseKind CKind;
70 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000071 SourceLocation ImplicitDSALoc;
72 DSAVarData()
73 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
74 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000075 };
Alexey Bataeved09d242014-05-28 05:53:51 +000076
Alexey Bataev758e55e2013-09-06 18:03:48 +000077private:
78 struct DSAInfo {
79 OpenMPClauseKind Attributes;
80 DeclRefExpr *RefExpr;
81 };
82 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000083 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000084
85 struct SharingMapTy {
86 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000087 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 OpenMPDirectiveKind Directive;
91 DeclarationNameInfo DirectiveName;
92 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000094 bool OrderedRegion;
Alexey Bataeved09d242014-05-28 05:53:51 +000095 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000096 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000097 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataevdea47612014-07-23 07:46:59 +000099 ConstructLoc(Loc), OrderedRegion(false) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000100 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000101 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataevdea47612014-07-23 07:46:59 +0000103 ConstructLoc(), OrderedRegion(false) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 };
105
106 typedef SmallVector<SharingMapTy, 64> StackTy;
107
108 /// \brief Stack of used declaration and their data-sharing attributes.
109 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000110 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000111
112 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
113
114 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000115
116 /// \brief Checks if the variable is a local for OpenMP region.
117 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000118
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000120 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121
122 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000123 Scope *CurScope, SourceLocation Loc) {
124 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
125 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000126 }
127
128 void pop() {
129 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
130 Stack.pop_back();
131 }
132
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000133 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000134 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000135 /// for diagnostics.
136 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
137
Alexey Bataev758e55e2013-09-06 18:03:48 +0000138 /// \brief Adds explicit data sharing attribute to the specified declaration.
139 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
140
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141 /// \brief Returns data sharing attributes from top of the stack for the
142 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000143 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000145 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000146 /// \brief Checks if the specified variables has data-sharing attributes which
147 /// match specified \a CPred predicate in any directive which matches \a DPred
148 /// predicate.
149 template <class ClausesPredicate, class DirectivesPredicate>
150 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000151 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000152 /// \brief Checks if the specified variables has data-sharing attributes which
153 /// match specified \a CPred predicate in any innermost directive which
154 /// matches \a DPred predicate.
155 template <class ClausesPredicate, class DirectivesPredicate>
156 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000157 DirectivesPredicate DPred,
158 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000159 /// \brief Finds a directive which matches specified \a DPred predicate.
160 template <class NamedDirectivesPredicate>
161 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000162
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163 /// \brief Returns currently analyzed directive.
164 OpenMPDirectiveKind getCurrentDirective() const {
165 return Stack.back().Directive;
166 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000167 /// \brief Returns parent directive.
168 OpenMPDirectiveKind getParentDirective() const {
169 if (Stack.size() > 2)
170 return Stack[Stack.size() - 2].Directive;
171 return OMPD_unknown;
172 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173
174 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000175 void setDefaultDSANone(SourceLocation Loc) {
176 Stack.back().DefaultAttr = DSA_none;
177 Stack.back().DefaultAttrLoc = Loc;
178 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000179 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000180 void setDefaultDSAShared(SourceLocation Loc) {
181 Stack.back().DefaultAttr = DSA_shared;
182 Stack.back().DefaultAttrLoc = Loc;
183 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000184
185 DefaultDataSharingAttributes getDefaultDSA() const {
186 return Stack.back().DefaultAttr;
187 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000188 SourceLocation getDefaultDSALocation() const {
189 return Stack.back().DefaultAttrLoc;
190 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000191
Alexey Bataevf29276e2014-06-18 04:14:57 +0000192 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000193 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000194 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000195 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000196 }
197
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000198 /// \brief Marks current region as ordered (it has an 'ordered' clause).
199 void setOrderedRegion(bool IsOrdered = true) {
200 Stack.back().OrderedRegion = IsOrdered;
201 }
202 /// \brief Returns true, if parent region is ordered (has associated
203 /// 'ordered' clause), false - otherwise.
204 bool isParentOrderedRegion() const {
205 if (Stack.size() > 2)
206 return Stack[Stack.size() - 2].OrderedRegion;
207 return false;
208 }
209
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000210 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000211 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000212 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000213};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000214bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
215 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
216 DKind == OMPD_unknown;
217}
Alexey Bataeved09d242014-05-28 05:53:51 +0000218} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000219
220DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
221 VarDecl *D) {
222 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000223 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000224 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
225 // in a region but not in construct]
226 // File-scope or namespace-scope variables referenced in called routines
227 // in the region are shared unless they appear in a threadprivate
228 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000229 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000230 DVar.CKind = OMPC_shared;
231
232 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
233 // in a region but not in construct]
234 // Variables with static storage duration that are declared in called
235 // routines in the region are shared.
236 if (D->hasGlobalStorage())
237 DVar.CKind = OMPC_shared;
238
Alexey Bataev758e55e2013-09-06 18:03:48 +0000239 return DVar;
240 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000241
Alexey Bataev758e55e2013-09-06 18:03:48 +0000242 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
244 // in a Construct, C/C++, predetermined, p.1]
245 // Variables with automatic storage duration that are declared in a scope
246 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000247 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
248 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
249 DVar.CKind = OMPC_private;
250 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000251 }
252
Alexey Bataev758e55e2013-09-06 18:03:48 +0000253 // Explicitly specified attributes and local variables with predetermined
254 // attributes.
255 if (Iter->SharingMap.count(D)) {
256 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
257 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000258 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000259 return DVar;
260 }
261
262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263 // in a Construct, C/C++, implicitly determined, p.1]
264 // In a parallel or task construct, the data-sharing attributes of these
265 // variables are determined by the default clause, if present.
266 switch (Iter->DefaultAttr) {
267 case DSA_shared:
268 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000269 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270 return DVar;
271 case DSA_none:
272 return DVar;
273 case DSA_unspecified:
274 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
275 // in a Construct, implicitly determined, p.2]
276 // In a parallel construct, if no default clause is present, these
277 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000278 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000279 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 DVar.CKind = OMPC_shared;
281 return DVar;
282 }
283
284 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
285 // in a Construct, implicitly determined, p.4]
286 // In a task construct, if no default clause is present, a variable that in
287 // the enclosing context is determined to be shared by all implicit tasks
288 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 if (DVar.DKind == OMPD_task) {
290 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000291 for (StackTy::reverse_iterator I = std::next(Iter),
292 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000294 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
295 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000296 // in a Construct, implicitly determined, p.6]
297 // In a task construct, if no default clause is present, a variable
298 // whose data-sharing attribute is not determined by the rules above is
299 // firstprivate.
300 DVarTemp = getDSA(I, D);
301 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000302 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000303 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000304 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305 return DVar;
306 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000307 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000308 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 }
310 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000311 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000312 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000313 return DVar;
314 }
315 }
316 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
317 // in a Construct, implicitly determined, p.3]
318 // For constructs other than task, if no default clause is present, these
319 // variables inherit their data-sharing attributes from the enclosing
320 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000321 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322}
323
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000324DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
325 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
326 auto It = Stack.back().AlignedMap.find(D);
327 if (It == Stack.back().AlignedMap.end()) {
328 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
329 Stack.back().AlignedMap[D] = NewDE;
330 return nullptr;
331 } else {
332 assert(It->second && "Unexpected nullptr expr in the aligned map");
333 return It->second;
334 }
335 return nullptr;
336}
337
Alexey Bataev758e55e2013-09-06 18:03:48 +0000338void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
339 if (A == OMPC_threadprivate) {
340 Stack[0].SharingMap[D].Attributes = A;
341 Stack[0].SharingMap[D].RefExpr = E;
342 } else {
343 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
344 Stack.back().SharingMap[D].Attributes = A;
345 Stack.back().SharingMap[D].RefExpr = E;
346 }
347}
348
Alexey Bataeved09d242014-05-28 05:53:51 +0000349bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000350 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000351 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000352 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000353 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000354 ++I;
355 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000356 if (I == E)
357 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000358 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000359 Scope *CurScope = getCurScope();
360 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000361 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000362 }
363 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000364 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000365 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366}
367
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000368DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000369 DSAVarData DVar;
370
371 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
372 // in a Construct, C/C++, predetermined, p.1]
373 // Variables appearing in threadprivate directives are threadprivate.
374 if (D->getTLSKind() != VarDecl::TLS_None) {
375 DVar.CKind = OMPC_threadprivate;
376 return DVar;
377 }
378 if (Stack[0].SharingMap.count(D)) {
379 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
380 DVar.CKind = OMPC_threadprivate;
381 return DVar;
382 }
383
384 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
385 // in a Construct, C/C++, predetermined, p.1]
386 // Variables with automatic storage duration that are declared in a scope
387 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000388 OpenMPDirectiveKind Kind =
389 FromParent ? getParentDirective() : getCurrentDirective();
390 auto StartI = std::next(Stack.rbegin());
391 auto EndI = std::prev(Stack.rend());
392 if (FromParent && StartI != EndI) {
393 StartI = std::next(StartI);
394 }
395 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000396 if (isOpenMPLocal(D, StartI) &&
397 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
398 D->getStorageClass() == SC_None)) ||
399 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000400 DVar.CKind = OMPC_private;
401 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000402 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403 }
404
405 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
406 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000407 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000408 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000409 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000410 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000411 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
412 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000413 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
414 return DVar;
415
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 DVar.CKind = OMPC_shared;
417 return DVar;
418 }
419
420 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000421 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 while (Type->isArrayType()) {
423 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
424 Type = ElemType.getNonReferenceType().getCanonicalType();
425 }
426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
427 // in a Construct, C/C++, predetermined, p.6]
428 // Variables with const qualified type having no mutable member are
429 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000430 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000431 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000433 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000434 // Variables with const-qualified type having no mutable member may be
435 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000436 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
437 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000438 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
439 return DVar;
440
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 DVar.CKind = OMPC_shared;
442 return DVar;
443 }
444
445 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
446 // in a Construct, C/C++, predetermined, p.7]
447 // Variables with static storage duration that are declared in a scope
448 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000449 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450 DVar.CKind = OMPC_shared;
451 return DVar;
452 }
453
454 // Explicitly specified attributes and local variables with predetermined
455 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000456 auto I = std::prev(StartI);
457 if (I->SharingMap.count(D)) {
458 DVar.RefExpr = I->SharingMap[D].RefExpr;
459 DVar.CKind = I->SharingMap[D].Attributes;
460 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000461 }
462
463 return DVar;
464}
465
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000466DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
467 auto StartI = Stack.rbegin();
468 auto EndI = std::prev(Stack.rend());
469 if (FromParent && StartI != EndI) {
470 StartI = std::next(StartI);
471 }
472 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000473}
474
Alexey Bataevf29276e2014-06-18 04:14:57 +0000475template <class ClausesPredicate, class DirectivesPredicate>
476DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000477 DirectivesPredicate DPred,
478 bool FromParent) {
479 auto StartI = std::next(Stack.rbegin());
480 auto EndI = std::prev(Stack.rend());
481 if (FromParent && StartI != EndI) {
482 StartI = std::next(StartI);
483 }
484 for (auto I = StartI, EE = EndI; I != EE; ++I) {
485 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000486 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000487 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000488 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000489 return DVar;
490 }
491 return DSAVarData();
492}
493
Alexey Bataevf29276e2014-06-18 04:14:57 +0000494template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000495DSAStackTy::DSAVarData
496DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
497 DirectivesPredicate DPred, bool FromParent) {
498 auto StartI = std::next(Stack.rbegin());
499 auto EndI = std::prev(Stack.rend());
500 if (FromParent && StartI != EndI) {
501 StartI = std::next(StartI);
502 }
503 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000504 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000505 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000506 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000507 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000508 return DVar;
509 return DSAVarData();
510 }
511 return DSAVarData();
512}
513
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000514template <class NamedDirectivesPredicate>
515bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
516 auto StartI = std::next(Stack.rbegin());
517 auto EndI = std::prev(Stack.rend());
518 if (FromParent && StartI != EndI) {
519 StartI = std::next(StartI);
520 }
521 for (auto I = StartI, EE = EndI; I != EE; ++I) {
522 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
523 return true;
524 }
525 return false;
526}
527
Alexey Bataev758e55e2013-09-06 18:03:48 +0000528void Sema::InitDataSharingAttributesStack() {
529 VarDataSharingAttributesStack = new DSAStackTy(*this);
530}
531
532#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
533
Alexey Bataeved09d242014-05-28 05:53:51 +0000534void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000535
536void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
537 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000538 Scope *CurScope, SourceLocation Loc) {
539 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000540 PushExpressionEvaluationContext(PotentiallyEvaluated);
541}
542
543void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000544 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
545 // A variable of class type (or array thereof) that appears in a lastprivate
546 // clause requires an accessible, unambiguous default constructor for the
547 // class type, unless the list item is also specified in a firstprivate
548 // clause.
549 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
550 for (auto C : D->clauses()) {
551 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
552 for (auto VarRef : Clause->varlists()) {
553 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
554 continue;
555 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000556 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000557 if (DVar.CKind == OMPC_lastprivate) {
558 SourceLocation ELoc = VarRef->getExprLoc();
559 auto Type = VarRef->getType();
560 if (Type->isArrayType())
561 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
562 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000563 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
564 // FIXME This code must be replaced by actual constructing of the
565 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000566 if (RD) {
567 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
568 PartialDiagnostic PD =
569 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
570 if (!CD ||
571 CheckConstructorAccess(
572 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
573 CD->getAccess(), PD) == AR_inaccessible ||
574 CD->isDeleted()) {
575 Diag(ELoc, diag::err_omp_required_method)
576 << getOpenMPClauseName(OMPC_lastprivate) << 0;
577 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
578 VarDecl::DeclarationOnly;
579 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
580 : diag::note_defined_here)
581 << VD;
582 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
583 continue;
584 }
585 MarkFunctionReferenced(ELoc, CD);
586 DiagnoseUseOfDecl(CD, ELoc);
587 }
588 }
589 }
590 }
591 }
592 }
593
Alexey Bataev758e55e2013-09-06 18:03:48 +0000594 DSAStack->pop();
595 DiscardCleanupsInEvaluationContext();
596 PopExpressionEvaluationContext();
597}
598
Alexey Bataeva769e072013-03-22 06:34:35 +0000599namespace {
600
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000601class VarDeclFilterCCC : public CorrectionCandidateCallback {
602private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000603 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000604
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000605public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000606 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000607 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000608 NamedDecl *ND = Candidate.getCorrectionDecl();
609 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
610 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000611 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
612 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000615 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000616};
Alexey Bataeved09d242014-05-28 05:53:51 +0000617} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000618
619ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
620 CXXScopeSpec &ScopeSpec,
621 const DeclarationNameInfo &Id) {
622 LookupResult Lookup(*this, Id, LookupOrdinaryName);
623 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
624
625 if (Lookup.isAmbiguous())
626 return ExprError();
627
628 VarDecl *VD;
629 if (!Lookup.isSingleResult()) {
630 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000631 if (TypoCorrection Corrected =
632 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
633 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000634 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000635 PDiag(Lookup.empty()
636 ? diag::err_undeclared_var_use_suggest
637 : diag::err_omp_expected_var_arg_suggest)
638 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000639 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000640 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000641 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
642 : diag::err_omp_expected_var_arg)
643 << Id.getName();
644 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000645 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000646 } else {
647 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000648 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000649 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
650 return ExprError();
651 }
652 }
653 Lookup.suppressDiagnostics();
654
655 // OpenMP [2.9.2, Syntax, C/C++]
656 // Variables must be file-scope, namespace-scope, or static block-scope.
657 if (!VD->hasGlobalStorage()) {
658 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000659 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
660 bool IsDecl =
661 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000662 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
664 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000665 return ExprError();
666 }
667
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000668 VarDecl *CanonicalVD = VD->getCanonicalDecl();
669 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000670 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
671 // A threadprivate directive for file-scope variables must appear outside
672 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000673 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
674 !getCurLexicalContext()->isTranslationUnit()) {
675 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000676 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
677 bool IsDecl =
678 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
679 Diag(VD->getLocation(),
680 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
681 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000682 return ExprError();
683 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000684 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
685 // A threadprivate directive for static class member variables must appear
686 // in the class definition, in the same scope in which the member
687 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000688 if (CanonicalVD->isStaticDataMember() &&
689 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
690 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000691 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
692 bool IsDecl =
693 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
694 Diag(VD->getLocation(),
695 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
696 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000697 return ExprError();
698 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000699 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
700 // A threadprivate directive for namespace-scope variables must appear
701 // outside any definition or declaration other than the namespace
702 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000703 if (CanonicalVD->getDeclContext()->isNamespace() &&
704 (!getCurLexicalContext()->isFileContext() ||
705 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
706 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000707 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
708 bool IsDecl =
709 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
710 Diag(VD->getLocation(),
711 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
712 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000713 return ExprError();
714 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000715 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
716 // A threadprivate directive for static block-scope variables must appear
717 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000718 if (CanonicalVD->isStaticLocal() && CurScope &&
719 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000720 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000721 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
722 bool IsDecl =
723 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
724 Diag(VD->getLocation(),
725 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
726 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000727 return ExprError();
728 }
729
730 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
731 // A threadprivate directive must lexically precede all references to any
732 // of the variables in its list.
733 if (VD->isUsed()) {
734 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000735 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000736 return ExprError();
737 }
738
739 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000740 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000741 return DE;
742}
743
Alexey Bataeved09d242014-05-28 05:53:51 +0000744Sema::DeclGroupPtrTy
745Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
746 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000747 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000748 CurContext->addDecl(D);
749 return DeclGroupPtrTy::make(DeclGroupRef(D));
750 }
751 return DeclGroupPtrTy();
752}
753
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000754namespace {
755class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
756 Sema &SemaRef;
757
758public:
759 bool VisitDeclRefExpr(const DeclRefExpr *E) {
760 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
761 if (VD->hasLocalStorage()) {
762 SemaRef.Diag(E->getLocStart(),
763 diag::err_omp_local_var_in_threadprivate_init)
764 << E->getSourceRange();
765 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
766 << VD << VD->getSourceRange();
767 return true;
768 }
769 }
770 return false;
771 }
772 bool VisitStmt(const Stmt *S) {
773 for (auto Child : S->children()) {
774 if (Child && Visit(Child))
775 return true;
776 }
777 return false;
778 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000779 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000780};
781} // namespace
782
Alexey Bataeved09d242014-05-28 05:53:51 +0000783OMPThreadPrivateDecl *
784Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000785 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000786 for (auto &RefExpr : VarList) {
787 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000788 VarDecl *VD = cast<VarDecl>(DE->getDecl());
789 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000790
791 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
792 // A threadprivate variable must not have an incomplete type.
793 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000794 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000795 continue;
796 }
797
798 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
799 // A threadprivate variable must not have a reference type.
800 if (VD->getType()->isReferenceType()) {
801 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000802 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
803 bool IsDecl =
804 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
805 Diag(VD->getLocation(),
806 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
807 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000808 continue;
809 }
810
Richard Smithfd3834f2013-04-13 02:43:54 +0000811 // Check if this is a TLS variable.
812 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000813 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000814 bool IsDecl =
815 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
816 Diag(VD->getLocation(),
817 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
818 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000819 continue;
820 }
821
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000822 // Check if initial value of threadprivate variable reference variable with
823 // local storage (it is not supported by runtime).
824 if (auto Init = VD->getAnyInitializer()) {
825 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000826 if (Checker.Visit(Init))
827 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000828 }
829
Alexey Bataeved09d242014-05-28 05:53:51 +0000830 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000831 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000832 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000833 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000834 if (!Vars.empty()) {
835 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
836 Vars);
837 D->setAccess(AS_public);
838 }
839 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000840}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000841
Alexey Bataev7ff55242014-06-19 09:13:45 +0000842static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
843 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
844 bool IsLoopIterVar = false) {
845 if (DVar.RefExpr) {
846 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
847 << getOpenMPClauseName(DVar.CKind);
848 return;
849 }
850 enum {
851 PDSA_StaticMemberShared,
852 PDSA_StaticLocalVarShared,
853 PDSA_LoopIterVarPrivate,
854 PDSA_LoopIterVarLinear,
855 PDSA_LoopIterVarLastprivate,
856 PDSA_ConstVarShared,
857 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000858 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000859 PDSA_LocalVarPrivate,
860 PDSA_Implicit
861 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000862 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000863 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000864 if (IsLoopIterVar) {
865 if (DVar.CKind == OMPC_private)
866 Reason = PDSA_LoopIterVarPrivate;
867 else if (DVar.CKind == OMPC_lastprivate)
868 Reason = PDSA_LoopIterVarLastprivate;
869 else
870 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000871 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
872 Reason = PDSA_TaskVarFirstprivate;
873 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000874 } else if (VD->isStaticLocal())
875 Reason = PDSA_StaticLocalVarShared;
876 else if (VD->isStaticDataMember())
877 Reason = PDSA_StaticMemberShared;
878 else if (VD->isFileVarDecl())
879 Reason = PDSA_GlobalVarShared;
880 else if (VD->getType().isConstant(SemaRef.getASTContext()))
881 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000882 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000883 ReportHint = true;
884 Reason = PDSA_LocalVarPrivate;
885 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000886 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000887 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000888 << Reason << ReportHint
889 << getOpenMPDirectiveName(Stack->getCurrentDirective());
890 } else if (DVar.ImplicitDSALoc.isValid()) {
891 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
892 << getOpenMPClauseName(DVar.CKind);
893 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000894}
895
Alexey Bataev758e55e2013-09-06 18:03:48 +0000896namespace {
897class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
898 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000899 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900 bool ErrorFound;
901 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000902 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000903 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000904
Alexey Bataev758e55e2013-09-06 18:03:48 +0000905public:
906 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000907 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000908 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000909 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
910 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000911
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000912 auto DVar = Stack->getTopDSA(VD, false);
913 // Check if the variable has explicit DSA set and stop analysis if it so.
914 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000915
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000916 auto ELoc = E->getExprLoc();
917 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000918 // The default(none) clause requires that each variable that is referenced
919 // in the construct, and does not have a predetermined data-sharing
920 // attribute, must have its data-sharing attribute explicitly determined
921 // by being listed in a data-sharing attribute clause.
922 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000923 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000924 VarsWithInheritedDSA.count(VD) == 0) {
925 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000926 return;
927 }
928
929 // OpenMP [2.9.3.6, Restrictions, p.2]
930 // A list item that appears in a reduction clause of the innermost
931 // enclosing worksharing or parallel construct may not be accessed in an
932 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000933 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000934 [](OpenMPDirectiveKind K) -> bool {
935 return isOpenMPParallelDirective(K) ||
936 isOpenMPWorksharingDirective(K);
937 },
938 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000939 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
940 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000941 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
942 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000943 return;
944 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000945
946 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000947 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000948 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000949 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000950 }
951 }
952 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000953 for (auto *C : S->clauses()) {
954 // Skip analysis of arguments of implicitly defined firstprivate clause
955 // for task directives.
956 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
957 for (auto *CC : C->children()) {
958 if (CC)
959 Visit(CC);
960 }
961 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962 }
963 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000964 for (auto *C : S->children()) {
965 if (C && !isa<OMPExecutableDirective>(C))
966 Visit(C);
967 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000968 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969
970 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000971 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000972 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
973 return VarsWithInheritedDSA;
974 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000975
Alexey Bataev7ff55242014-06-19 09:13:45 +0000976 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
977 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978};
Alexey Bataeved09d242014-05-28 05:53:51 +0000979} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000980
Alexey Bataevbae9a792014-06-27 10:37:06 +0000981void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000982 switch (DKind) {
983 case OMPD_parallel: {
984 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
985 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000986 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000987 std::make_pair(".global_tid.", KmpInt32PtrTy),
988 std::make_pair(".bound_tid.", KmpInt32PtrTy),
989 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000990 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000991 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
992 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000993 break;
994 }
995 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000996 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 std::make_pair(StringRef(), QualType()) // __context with shared vars
998 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000999 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1000 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001001 break;
1002 }
1003 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001004 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001005 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001006 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001007 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1008 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001009 break;
1010 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001011 case OMPD_for_simd: {
1012 Sema::CapturedParamNameType Params[] = {
1013 std::make_pair(StringRef(), QualType()) // __context with shared vars
1014 };
1015 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1016 Params);
1017 break;
1018 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001019 case OMPD_sections: {
1020 Sema::CapturedParamNameType Params[] = {
1021 std::make_pair(StringRef(), QualType()) // __context with shared vars
1022 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001023 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1024 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001025 break;
1026 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001027 case OMPD_section: {
1028 Sema::CapturedParamNameType Params[] = {
1029 std::make_pair(StringRef(), QualType()) // __context with shared vars
1030 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001031 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1032 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001033 break;
1034 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001035 case OMPD_single: {
1036 Sema::CapturedParamNameType Params[] = {
1037 std::make_pair(StringRef(), QualType()) // __context with shared vars
1038 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001039 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1040 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001041 break;
1042 }
Alexander Musman80c22892014-07-17 08:54:58 +00001043 case OMPD_master: {
1044 Sema::CapturedParamNameType Params[] = {
1045 std::make_pair(StringRef(), QualType()) // __context with shared vars
1046 };
1047 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1048 Params);
1049 break;
1050 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001051 case OMPD_critical: {
1052 Sema::CapturedParamNameType Params[] = {
1053 std::make_pair(StringRef(), QualType()) // __context with shared vars
1054 };
1055 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1056 Params);
1057 break;
1058 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001059 case OMPD_parallel_for: {
1060 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1061 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1062 Sema::CapturedParamNameType Params[] = {
1063 std::make_pair(".global_tid.", KmpInt32PtrTy),
1064 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1065 std::make_pair(StringRef(), QualType()) // __context with shared vars
1066 };
1067 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1068 Params);
1069 break;
1070 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001071 case OMPD_parallel_sections: {
1072 Sema::CapturedParamNameType Params[] = {
1073 std::make_pair(StringRef(), QualType()) // __context with shared vars
1074 };
1075 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1076 Params);
1077 break;
1078 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001079 case OMPD_task: {
1080 Sema::CapturedParamNameType Params[] = {
1081 std::make_pair(StringRef(), QualType()) // __context with shared vars
1082 };
1083 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1084 Params);
1085 break;
1086 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001087 case OMPD_taskyield: {
1088 Sema::CapturedParamNameType Params[] = {
1089 std::make_pair(StringRef(), QualType()) // __context with shared vars
1090 };
1091 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1092 Params);
1093 break;
1094 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001095 case OMPD_barrier: {
1096 Sema::CapturedParamNameType Params[] = {
1097 std::make_pair(StringRef(), QualType()) // __context with shared vars
1098 };
1099 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1100 Params);
1101 break;
1102 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001103 case OMPD_taskwait: {
1104 Sema::CapturedParamNameType Params[] = {
1105 std::make_pair(StringRef(), QualType()) // __context with shared vars
1106 };
1107 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1108 Params);
1109 break;
1110 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001111 case OMPD_flush: {
1112 Sema::CapturedParamNameType Params[] = {
1113 std::make_pair(StringRef(), QualType()) // __context with shared vars
1114 };
1115 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1116 Params);
1117 break;
1118 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001119 case OMPD_ordered: {
1120 Sema::CapturedParamNameType Params[] = {
1121 std::make_pair(StringRef(), QualType()) // __context with shared vars
1122 };
1123 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1124 Params);
1125 break;
1126 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001127 case OMPD_atomic: {
1128 Sema::CapturedParamNameType Params[] = {
1129 std::make_pair(StringRef(), QualType()) // __context with shared vars
1130 };
1131 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1132 Params);
1133 break;
1134 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001135 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001136 llvm_unreachable("OpenMP Directive is not allowed");
1137 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001138 llvm_unreachable("Unknown OpenMP directive");
1139 }
1140}
1141
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001142static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1143 OpenMPDirectiveKind CurrentRegion,
1144 const DeclarationNameInfo &CurrentName,
1145 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001146 // Allowed nesting of constructs
1147 // +------------------+-----------------+------------------------------------+
1148 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1149 // +------------------+-----------------+------------------------------------+
1150 // | parallel | parallel | * |
1151 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001152 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001153 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001154 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001155 // | parallel | simd | * |
1156 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001157 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001158 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001159 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001160 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001161 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001162 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001163 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001164 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001165 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001166 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001167 // | parallel | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001168 // +------------------+-----------------+------------------------------------+
1169 // | for | parallel | * |
1170 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001171 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001172 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001173 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001174 // | for | simd | * |
1175 // | for | sections | + |
1176 // | for | section | + |
1177 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001178 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001179 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001180 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001181 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001182 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001183 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001184 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001185 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001186 // | for | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001187 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001188 // | master | parallel | * |
1189 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001190 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001191 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001192 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001193 // | master | simd | * |
1194 // | master | sections | + |
1195 // | master | section | + |
1196 // | master | single | + |
1197 // | master | parallel for | * |
1198 // | master |parallel sections| * |
1199 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001200 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001201 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001202 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001203 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001204 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001205 // | master | atomic | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001206 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001207 // | critical | parallel | * |
1208 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001209 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001210 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001211 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001212 // | critical | simd | * |
1213 // | critical | sections | + |
1214 // | critical | section | + |
1215 // | critical | single | + |
1216 // | critical | parallel for | * |
1217 // | critical |parallel sections| * |
1218 // | critical | task | * |
1219 // | critical | taskyield | * |
1220 // | critical | barrier | + |
1221 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001222 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001223 // | critical | atomic | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001224 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001225 // | simd | parallel | |
1226 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001227 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001228 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001229 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001230 // | simd | simd | |
1231 // | simd | sections | |
1232 // | simd | section | |
1233 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001234 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001235 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001236 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001237 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001238 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001239 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001240 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001241 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001242 // | simd | atomic | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001243 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001244 // | for simd | parallel | |
1245 // | for simd | for | |
1246 // | for simd | for simd | |
1247 // | for simd | master | |
1248 // | for simd | critical | |
1249 // | for simd | simd | |
1250 // | for simd | sections | |
1251 // | for simd | section | |
1252 // | for simd | single | |
1253 // | for simd | parallel for | |
1254 // | for simd |parallel sections| |
1255 // | for simd | task | |
1256 // | for simd | taskyield | |
1257 // | for simd | barrier | |
1258 // | for simd | taskwait | |
1259 // | for simd | flush | |
1260 // | for simd | ordered | |
1261 // | for simd | atomic | |
1262 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001263 // | sections | parallel | * |
1264 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001265 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001266 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001267 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001268 // | sections | simd | * |
1269 // | sections | sections | + |
1270 // | sections | section | * |
1271 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001272 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001273 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001274 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001275 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001276 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001277 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001278 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001279 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001280 // | sections | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001281 // +------------------+-----------------+------------------------------------+
1282 // | section | parallel | * |
1283 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001284 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001285 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001286 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001287 // | section | simd | * |
1288 // | section | sections | + |
1289 // | section | section | + |
1290 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001291 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001292 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001293 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001294 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001295 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001296 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001297 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001298 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001299 // | section | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001300 // +------------------+-----------------+------------------------------------+
1301 // | single | parallel | * |
1302 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001303 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001304 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001305 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001306 // | single | simd | * |
1307 // | single | sections | + |
1308 // | single | section | + |
1309 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001310 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001311 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001312 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001313 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001314 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001315 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001316 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001317 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001318 // | single | atomic | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001319 // +------------------+-----------------+------------------------------------+
1320 // | parallel for | parallel | * |
1321 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001322 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001323 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001324 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001325 // | parallel for | simd | * |
1326 // | parallel for | sections | + |
1327 // | parallel for | section | + |
1328 // | parallel for | single | + |
1329 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001330 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001331 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001332 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001333 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001334 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001335 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001336 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001337 // | parallel for | atomic | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001338 // +------------------+-----------------+------------------------------------+
1339 // | parallel sections| parallel | * |
1340 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001341 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001342 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001343 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001344 // | parallel sections| simd | * |
1345 // | parallel sections| sections | + |
1346 // | parallel sections| section | * |
1347 // | parallel sections| single | + |
1348 // | parallel sections| parallel for | * |
1349 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001350 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001351 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001352 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001353 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001354 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001355 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001356 // | parallel sections| atomic | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001357 // +------------------+-----------------+------------------------------------+
1358 // | task | parallel | * |
1359 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001360 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001361 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001362 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001363 // | task | simd | * |
1364 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001365 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001366 // | task | single | + |
1367 // | task | parallel for | * |
1368 // | task |parallel sections| * |
1369 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001370 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001371 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001372 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001373 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001374 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001375 // | task | atomic | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001376 // +------------------+-----------------+------------------------------------+
1377 // | ordered | parallel | * |
1378 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001379 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001380 // | ordered | master | * |
1381 // | ordered | critical | * |
1382 // | ordered | simd | * |
1383 // | ordered | sections | + |
1384 // | ordered | section | + |
1385 // | ordered | single | + |
1386 // | ordered | parallel for | * |
1387 // | ordered |parallel sections| * |
1388 // | ordered | task | * |
1389 // | ordered | taskyield | * |
1390 // | ordered | barrier | + |
1391 // | ordered | taskwait | * |
1392 // | ordered | flush | * |
1393 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001394 // | ordered | atomic | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001395 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001396 if (Stack->getCurScope()) {
1397 auto ParentRegion = Stack->getParentDirective();
1398 bool NestingProhibited = false;
1399 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001400 enum {
1401 NoRecommend,
1402 ShouldBeInParallelRegion,
1403 ShouldBeInOrderedRegion
1404 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001405 if (isOpenMPSimdDirective(ParentRegion)) {
1406 // OpenMP [2.16, Nesting of Regions]
1407 // OpenMP constructs may not be nested inside a simd region.
1408 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1409 return true;
1410 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001411 if (ParentRegion == OMPD_atomic) {
1412 // OpenMP [2.16, Nesting of Regions]
1413 // OpenMP constructs may not be nested inside an atomic region.
1414 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1415 return true;
1416 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001417 if (CurrentRegion == OMPD_section) {
1418 // OpenMP [2.7.2, sections Construct, Restrictions]
1419 // Orphaned section directives are prohibited. That is, the section
1420 // directives must appear within the sections construct and must not be
1421 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001422 if (ParentRegion != OMPD_sections &&
1423 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001424 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1425 << (ParentRegion != OMPD_unknown)
1426 << getOpenMPDirectiveName(ParentRegion);
1427 return true;
1428 }
1429 return false;
1430 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001431 // Allow some constructs to be orphaned (they could be used in functions,
1432 // called from OpenMP regions with the required preconditions).
1433 if (ParentRegion == OMPD_unknown)
1434 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001435 if (CurrentRegion == OMPD_master) {
1436 // OpenMP [2.16, Nesting of Regions]
1437 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001438 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001439 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1440 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001441 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1442 // OpenMP [2.16, Nesting of Regions]
1443 // A critical region may not be nested (closely or otherwise) inside a
1444 // critical region with the same name. Note that this restriction is not
1445 // sufficient to prevent deadlock.
1446 SourceLocation PreviousCriticalLoc;
1447 bool DeadLock =
1448 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1449 OpenMPDirectiveKind K,
1450 const DeclarationNameInfo &DNI,
1451 SourceLocation Loc)
1452 ->bool {
1453 if (K == OMPD_critical &&
1454 DNI.getName() == CurrentName.getName()) {
1455 PreviousCriticalLoc = Loc;
1456 return true;
1457 } else
1458 return false;
1459 },
1460 false /* skip top directive */);
1461 if (DeadLock) {
1462 SemaRef.Diag(StartLoc,
1463 diag::err_omp_prohibited_region_critical_same_name)
1464 << CurrentName.getName();
1465 if (PreviousCriticalLoc.isValid())
1466 SemaRef.Diag(PreviousCriticalLoc,
1467 diag::note_omp_previous_critical_region);
1468 return true;
1469 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001470 } else if (CurrentRegion == OMPD_barrier) {
1471 // OpenMP [2.16, Nesting of Regions]
1472 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001473 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001474 NestingProhibited =
1475 isOpenMPWorksharingDirective(ParentRegion) ||
1476 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1477 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001478 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001479 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001480 // OpenMP [2.16, Nesting of Regions]
1481 // A worksharing region may not be closely nested inside a worksharing,
1482 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001483 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001484 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001485 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1486 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1487 Recommend = ShouldBeInParallelRegion;
1488 } else if (CurrentRegion == OMPD_ordered) {
1489 // OpenMP [2.16, Nesting of Regions]
1490 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001491 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001492 // An ordered region must be closely nested inside a loop region (or
1493 // parallel loop region) with an ordered clause.
1494 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001495 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001496 !Stack->isParentOrderedRegion();
1497 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001498 }
1499 if (NestingProhibited) {
1500 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001501 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1502 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001503 return true;
1504 }
1505 }
1506 return false;
1507}
1508
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001509StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001510 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001511 ArrayRef<OMPClause *> Clauses,
1512 Stmt *AStmt,
1513 SourceLocation StartLoc,
1514 SourceLocation EndLoc) {
1515 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001516 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001517 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001518
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001519 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001520 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001521 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001522 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001523 if (AStmt) {
1524 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1525
1526 // Check default data sharing attributes for referenced variables.
1527 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1528 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1529 if (DSAChecker.isErrorFound())
1530 return StmtError();
1531 // Generate list of implicitly defined firstprivate variables.
1532 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001533
1534 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1535 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1536 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1537 SourceLocation(), SourceLocation())) {
1538 ClausesWithImplicit.push_back(Implicit);
1539 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1540 DSAChecker.getImplicitFirstprivate().size();
1541 } else
1542 ErrorFound = true;
1543 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001544 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001545
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001546 switch (Kind) {
1547 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001548 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1549 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001550 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001551 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001552 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1553 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001554 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001555 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001556 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1557 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001558 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001559 case OMPD_for_simd:
1560 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1561 EndLoc, VarsWithInheritedDSA);
1562 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001563 case OMPD_sections:
1564 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1565 EndLoc);
1566 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001567 case OMPD_section:
1568 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001569 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001570 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1571 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001572 case OMPD_single:
1573 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1574 EndLoc);
1575 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001576 case OMPD_master:
1577 assert(ClausesWithImplicit.empty() &&
1578 "No clauses are allowed for 'omp master' directive");
1579 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1580 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001581 case OMPD_critical:
1582 assert(ClausesWithImplicit.empty() &&
1583 "No clauses are allowed for 'omp critical' directive");
1584 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1585 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001586 case OMPD_parallel_for:
1587 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1588 EndLoc, VarsWithInheritedDSA);
1589 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001590 case OMPD_parallel_sections:
1591 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1592 StartLoc, EndLoc);
1593 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001594 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001595 Res =
1596 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1597 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001598 case OMPD_taskyield:
1599 assert(ClausesWithImplicit.empty() &&
1600 "No clauses are allowed for 'omp taskyield' directive");
1601 assert(AStmt == nullptr &&
1602 "No associated statement allowed for 'omp taskyield' directive");
1603 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1604 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001605 case OMPD_barrier:
1606 assert(ClausesWithImplicit.empty() &&
1607 "No clauses are allowed for 'omp barrier' directive");
1608 assert(AStmt == nullptr &&
1609 "No associated statement allowed for 'omp barrier' directive");
1610 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1611 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001612 case OMPD_taskwait:
1613 assert(ClausesWithImplicit.empty() &&
1614 "No clauses are allowed for 'omp taskwait' directive");
1615 assert(AStmt == nullptr &&
1616 "No associated statement allowed for 'omp taskwait' directive");
1617 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1618 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001619 case OMPD_flush:
1620 assert(AStmt == nullptr &&
1621 "No associated statement allowed for 'omp flush' directive");
1622 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1623 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001624 case OMPD_ordered:
1625 assert(ClausesWithImplicit.empty() &&
1626 "No clauses are allowed for 'omp ordered' directive");
1627 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1628 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001629 case OMPD_atomic:
1630 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1631 EndLoc);
1632 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001633 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001634 llvm_unreachable("OpenMP Directive is not allowed");
1635 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001636 llvm_unreachable("Unknown OpenMP directive");
1637 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001638
Alexey Bataev4acb8592014-07-07 13:01:15 +00001639 for (auto P : VarsWithInheritedDSA) {
1640 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1641 << P.first << P.second->getSourceRange();
1642 }
1643 if (!VarsWithInheritedDSA.empty())
1644 return StmtError();
1645
Alexey Bataeved09d242014-05-28 05:53:51 +00001646 if (ErrorFound)
1647 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001648 return Res;
1649}
1650
1651StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1652 Stmt *AStmt,
1653 SourceLocation StartLoc,
1654 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001655 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1656 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1657 // 1.2.2 OpenMP Language Terminology
1658 // Structured block - An executable statement with a single entry at the
1659 // top and a single exit at the bottom.
1660 // The point of exit cannot be a branch out of the structured block.
1661 // longjmp() and throw() must not violate the entry/exit criteria.
1662 CS->getCapturedDecl()->setNothrow();
1663
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001664 getCurFunction()->setHasBranchProtectedScope();
1665
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001666 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1667 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001668}
1669
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001670namespace {
1671/// \brief Helper class for checking canonical form of the OpenMP loops and
1672/// extracting iteration space of each loop in the loop nest, that will be used
1673/// for IR generation.
1674class OpenMPIterationSpaceChecker {
1675 /// \brief Reference to Sema.
1676 Sema &SemaRef;
1677 /// \brief A location for diagnostics (when there is no some better location).
1678 SourceLocation DefaultLoc;
1679 /// \brief A location for diagnostics (when increment is not compatible).
1680 SourceLocation ConditionLoc;
1681 /// \brief A source location for referring to condition later.
1682 SourceRange ConditionSrcRange;
1683 /// \brief Loop variable.
1684 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001685 /// \brief Reference to loop variable.
1686 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001687 /// \brief Lower bound (initializer for the var).
1688 Expr *LB;
1689 /// \brief Upper bound.
1690 Expr *UB;
1691 /// \brief Loop step (increment).
1692 Expr *Step;
1693 /// \brief This flag is true when condition is one of:
1694 /// Var < UB
1695 /// Var <= UB
1696 /// UB > Var
1697 /// UB >= Var
1698 bool TestIsLessOp;
1699 /// \brief This flag is true when condition is strict ( < or > ).
1700 bool TestIsStrictOp;
1701 /// \brief This flag is true when step is subtracted on each iteration.
1702 bool SubtractStep;
1703
1704public:
1705 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1706 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001707 ConditionSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
1708 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1709 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001710 /// \brief Check init-expr for canonical loop form and save loop counter
1711 /// variable - #Var and its initialization value - #LB.
1712 bool CheckInit(Stmt *S);
1713 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1714 /// for less/greater and for strict/non-strict comparison.
1715 bool CheckCond(Expr *S);
1716 /// \brief Check incr-expr for canonical loop form and return true if it
1717 /// does not conform, otherwise save loop step (#Step).
1718 bool CheckInc(Expr *S);
1719 /// \brief Return the loop counter variable.
1720 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001721 /// \brief Return the reference expression to loop counter variable.
1722 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001723 /// \brief Return true if any expression is dependent.
1724 bool Dependent() const;
1725
1726private:
1727 /// \brief Check the right-hand side of an assignment in the increment
1728 /// expression.
1729 bool CheckIncRHS(Expr *RHS);
1730 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001731 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001732 /// \brief Helper to set upper bound.
1733 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1734 const SourceLocation &SL);
1735 /// \brief Helper to set loop increment.
1736 bool SetStep(Expr *NewStep, bool Subtract);
1737};
1738
1739bool OpenMPIterationSpaceChecker::Dependent() const {
1740 if (!Var) {
1741 assert(!LB && !UB && !Step);
1742 return false;
1743 }
1744 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1745 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1746}
1747
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001748bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1749 DeclRefExpr *NewVarRefExpr,
1750 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001751 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001752 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1753 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001754 if (!NewVar || !NewLB)
1755 return true;
1756 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001757 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001758 LB = NewLB;
1759 return false;
1760}
1761
1762bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1763 const SourceRange &SR,
1764 const SourceLocation &SL) {
1765 // State consistency checking to ensure correct usage.
1766 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1767 !TestIsLessOp && !TestIsStrictOp);
1768 if (!NewUB)
1769 return true;
1770 UB = NewUB;
1771 TestIsLessOp = LessOp;
1772 TestIsStrictOp = StrictOp;
1773 ConditionSrcRange = SR;
1774 ConditionLoc = SL;
1775 return false;
1776}
1777
1778bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1779 // State consistency checking to ensure correct usage.
1780 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1781 if (!NewStep)
1782 return true;
1783 if (!NewStep->isValueDependent()) {
1784 // Check that the step is integer expression.
1785 SourceLocation StepLoc = NewStep->getLocStart();
1786 ExprResult Val =
1787 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1788 if (Val.isInvalid())
1789 return true;
1790 NewStep = Val.get();
1791
1792 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1793 // If test-expr is of form var relational-op b and relational-op is < or
1794 // <= then incr-expr must cause var to increase on each iteration of the
1795 // loop. If test-expr is of form var relational-op b and relational-op is
1796 // > or >= then incr-expr must cause var to decrease on each iteration of
1797 // the loop.
1798 // If test-expr is of form b relational-op var and relational-op is < or
1799 // <= then incr-expr must cause var to decrease on each iteration of the
1800 // loop. If test-expr is of form b relational-op var and relational-op is
1801 // > or >= then incr-expr must cause var to increase on each iteration of
1802 // the loop.
1803 llvm::APSInt Result;
1804 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1805 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1806 bool IsConstNeg =
1807 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1808 bool IsConstZero = IsConstant && !Result.getBoolValue();
1809 if (UB && (IsConstZero ||
1810 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1811 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1812 SemaRef.Diag(NewStep->getExprLoc(),
1813 diag::err_omp_loop_incr_not_compatible)
1814 << Var << TestIsLessOp << NewStep->getSourceRange();
1815 SemaRef.Diag(ConditionLoc,
1816 diag::note_omp_loop_cond_requres_compatible_incr)
1817 << TestIsLessOp << ConditionSrcRange;
1818 return true;
1819 }
1820 }
1821
1822 Step = NewStep;
1823 SubtractStep = Subtract;
1824 return false;
1825}
1826
1827bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1828 // Check init-expr for canonical loop form and save loop counter
1829 // variable - #Var and its initialization value - #LB.
1830 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1831 // var = lb
1832 // integer-type var = lb
1833 // random-access-iterator-type var = lb
1834 // pointer-type var = lb
1835 //
1836 if (!S) {
1837 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1838 return true;
1839 }
1840 if (Expr *E = dyn_cast<Expr>(S))
1841 S = E->IgnoreParens();
1842 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1843 if (BO->getOpcode() == BO_Assign)
1844 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001845 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1846 BO->getLHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001847 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1848 if (DS->isSingleDecl()) {
1849 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1850 if (Var->hasInit()) {
1851 // Accept non-canonical init form here but emit ext. warning.
1852 if (Var->getInitStyle() != VarDecl::CInit)
1853 SemaRef.Diag(S->getLocStart(),
1854 diag::ext_omp_loop_not_canonical_init)
1855 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001856 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001857 }
1858 }
1859 }
1860 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1861 if (CE->getOperator() == OO_Equal)
1862 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001863 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1864 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001865
1866 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1867 << S->getSourceRange();
1868 return true;
1869}
1870
Alexey Bataev23b69422014-06-18 07:08:49 +00001871/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001872/// variable (which may be the loop variable) if possible.
1873static const VarDecl *GetInitVarDecl(const Expr *E) {
1874 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001875 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001876 E = E->IgnoreParenImpCasts();
1877 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1878 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1879 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1880 CE->getArg(0) != nullptr)
1881 E = CE->getArg(0)->IgnoreParenImpCasts();
1882 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1883 if (!DRE)
1884 return nullptr;
1885 return dyn_cast<VarDecl>(DRE->getDecl());
1886}
1887
1888bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1889 // Check test-expr for canonical form, save upper-bound UB, flags for
1890 // less/greater and for strict/non-strict comparison.
1891 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1892 // var relational-op b
1893 // b relational-op var
1894 //
1895 if (!S) {
1896 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1897 return true;
1898 }
1899 S = S->IgnoreParenImpCasts();
1900 SourceLocation CondLoc = S->getLocStart();
1901 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1902 if (BO->isRelationalOp()) {
1903 if (GetInitVarDecl(BO->getLHS()) == Var)
1904 return SetUB(BO->getRHS(),
1905 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1906 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1907 BO->getSourceRange(), BO->getOperatorLoc());
1908 if (GetInitVarDecl(BO->getRHS()) == Var)
1909 return SetUB(BO->getLHS(),
1910 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1911 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1912 BO->getSourceRange(), BO->getOperatorLoc());
1913 }
1914 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1915 if (CE->getNumArgs() == 2) {
1916 auto Op = CE->getOperator();
1917 switch (Op) {
1918 case OO_Greater:
1919 case OO_GreaterEqual:
1920 case OO_Less:
1921 case OO_LessEqual:
1922 if (GetInitVarDecl(CE->getArg(0)) == Var)
1923 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1924 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1925 CE->getOperatorLoc());
1926 if (GetInitVarDecl(CE->getArg(1)) == Var)
1927 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1928 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1929 CE->getOperatorLoc());
1930 break;
1931 default:
1932 break;
1933 }
1934 }
1935 }
1936 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1937 << S->getSourceRange() << Var;
1938 return true;
1939}
1940
1941bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1942 // RHS of canonical loop form increment can be:
1943 // var + incr
1944 // incr + var
1945 // var - incr
1946 //
1947 RHS = RHS->IgnoreParenImpCasts();
1948 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1949 if (BO->isAdditiveOp()) {
1950 bool IsAdd = BO->getOpcode() == BO_Add;
1951 if (GetInitVarDecl(BO->getLHS()) == Var)
1952 return SetStep(BO->getRHS(), !IsAdd);
1953 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1954 return SetStep(BO->getLHS(), false);
1955 }
1956 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1957 bool IsAdd = CE->getOperator() == OO_Plus;
1958 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1959 if (GetInitVarDecl(CE->getArg(0)) == Var)
1960 return SetStep(CE->getArg(1), !IsAdd);
1961 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1962 return SetStep(CE->getArg(0), false);
1963 }
1964 }
1965 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1966 << RHS->getSourceRange() << Var;
1967 return true;
1968}
1969
1970bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1971 // Check incr-expr for canonical loop form and return true if it
1972 // does not conform.
1973 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1974 // ++var
1975 // var++
1976 // --var
1977 // var--
1978 // var += incr
1979 // var -= incr
1980 // var = var + incr
1981 // var = incr + var
1982 // var = var - incr
1983 //
1984 if (!S) {
1985 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1986 return true;
1987 }
1988 S = S->IgnoreParens();
1989 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1990 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1991 return SetStep(
1992 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1993 (UO->isDecrementOp() ? -1 : 1)).get(),
1994 false);
1995 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1996 switch (BO->getOpcode()) {
1997 case BO_AddAssign:
1998 case BO_SubAssign:
1999 if (GetInitVarDecl(BO->getLHS()) == Var)
2000 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2001 break;
2002 case BO_Assign:
2003 if (GetInitVarDecl(BO->getLHS()) == Var)
2004 return CheckIncRHS(BO->getRHS());
2005 break;
2006 default:
2007 break;
2008 }
2009 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2010 switch (CE->getOperator()) {
2011 case OO_PlusPlus:
2012 case OO_MinusMinus:
2013 if (GetInitVarDecl(CE->getArg(0)) == Var)
2014 return SetStep(
2015 SemaRef.ActOnIntegerConstant(
2016 CE->getLocStart(),
2017 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2018 false);
2019 break;
2020 case OO_PlusEqual:
2021 case OO_MinusEqual:
2022 if (GetInitVarDecl(CE->getArg(0)) == Var)
2023 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2024 break;
2025 case OO_Equal:
2026 if (GetInitVarDecl(CE->getArg(0)) == Var)
2027 return CheckIncRHS(CE->getArg(1));
2028 break;
2029 default:
2030 break;
2031 }
2032 }
2033 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2034 << S->getSourceRange() << Var;
2035 return true;
2036}
Alexey Bataev23b69422014-06-18 07:08:49 +00002037} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002038
2039/// \brief Called on a for stmt to check and extract its iteration space
2040/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002041static bool CheckOpenMPIterationSpace(
2042 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2043 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2044 Expr *NestedLoopCountExpr,
2045 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002046 // OpenMP [2.6, Canonical Loop Form]
2047 // for (init-expr; test-expr; incr-expr) structured-block
2048 auto For = dyn_cast_or_null<ForStmt>(S);
2049 if (!For) {
2050 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002051 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2052 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2053 << CurrentNestedLoopCount;
2054 if (NestedLoopCount > 1)
2055 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2056 diag::note_omp_collapse_expr)
2057 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002058 return true;
2059 }
2060 assert(For->getBody());
2061
2062 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2063
2064 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002065 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002066 if (ISC.CheckInit(Init)) {
2067 return true;
2068 }
2069
2070 bool HasErrors = false;
2071
2072 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002073 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002074
2075 // OpenMP [2.6, Canonical Loop Form]
2076 // Var is one of the following:
2077 // A variable of signed or unsigned integer type.
2078 // For C++, a variable of a random access iterator type.
2079 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002080 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002081 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2082 !VarType->isPointerType() &&
2083 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2084 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2085 << SemaRef.getLangOpts().CPlusPlus;
2086 HasErrors = true;
2087 }
2088
Alexey Bataev4acb8592014-07-07 13:01:15 +00002089 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2090 // Construct
2091 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2092 // parallel for construct is (are) private.
2093 // The loop iteration variable in the associated for-loop of a simd construct
2094 // with just one associated for-loop is linear with a constant-linear-step
2095 // that is the increment of the associated for-loop.
2096 // Exclude loop var from the list of variables with implicitly defined data
2097 // sharing attributes.
2098 while (VarsWithImplicitDSA.count(Var) > 0)
2099 VarsWithImplicitDSA.erase(Var);
2100
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002101 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2102 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002103 // The loop iteration variable in the associated for-loop of a simd construct
2104 // with just one associated for-loop may be listed in a linear clause with a
2105 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002106 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2107 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002108 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002109 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2110 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2111 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002112 auto PredeterminedCKind =
2113 isOpenMPSimdDirective(DKind)
2114 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2115 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002116 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002117 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002118 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2119 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2120 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002121 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002122 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002123 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2124 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002125 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002126 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002127 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002128 // Make the loop iteration variable private (for worksharing constructs),
2129 // linear (for simd directives with the only one associated loop) or
2130 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002131 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002132 }
2133
Alexey Bataev7ff55242014-06-19 09:13:45 +00002134 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002135
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002136 // Check test-expr.
2137 HasErrors |= ISC.CheckCond(For->getCond());
2138
2139 // Check incr-expr.
2140 HasErrors |= ISC.CheckInc(For->getInc());
2141
2142 if (ISC.Dependent())
2143 return HasErrors;
2144
2145 // FIXME: Build loop's iteration space representation.
2146 return HasErrors;
2147}
2148
2149/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
2150/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
2151/// to get the first for loop.
2152static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
2153 if (IgnoreCaptured)
2154 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
2155 S = CapS->getCapturedStmt();
2156 // OpenMP [2.8.1, simd construct, Restrictions]
2157 // All loops associated with the construct must be perfectly nested; that is,
2158 // there must be no intervening code nor any OpenMP directive between any two
2159 // loops.
2160 while (true) {
2161 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
2162 S = AS->getSubStmt();
2163 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
2164 if (CS->size() != 1)
2165 break;
2166 S = CS->body_back();
2167 } else
2168 break;
2169 }
2170 return S;
2171}
2172
2173/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002174/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2175/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002176static unsigned
2177CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2178 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2179 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002180 unsigned NestedLoopCount = 1;
2181 if (NestedLoopCountExpr) {
2182 // Found 'collapse' clause - calculate collapse number.
2183 llvm::APSInt Result;
2184 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2185 NestedLoopCount = Result.getLimitedValue();
2186 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002187 // This is helper routine for loop directives (e.g., 'for', 'simd',
2188 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002189 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
2190 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002191 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002192 NestedLoopCount, NestedLoopCountExpr,
2193 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002194 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002195 // Move on to the next nested for loop, or to the loop body.
2196 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
2197 }
2198
2199 // FIXME: Build resulting iteration space for IR generation (collapsing
2200 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002201 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002202}
2203
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002204static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002205 auto CollapseFilter = [](const OMPClause *C) -> bool {
2206 return C->getClauseKind() == OMPC_collapse;
2207 };
2208 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2209 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002210 if (I)
2211 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2212 return nullptr;
2213}
2214
Alexey Bataev4acb8592014-07-07 13:01:15 +00002215StmtResult Sema::ActOnOpenMPSimdDirective(
2216 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2217 SourceLocation EndLoc,
2218 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002219 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002220 unsigned NestedLoopCount =
2221 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2222 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002223 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002224 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002225
2226 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002227 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2228 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002229}
2230
Alexey Bataev4acb8592014-07-07 13:01:15 +00002231StmtResult Sema::ActOnOpenMPForDirective(
2232 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2233 SourceLocation EndLoc,
2234 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002235 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002236 unsigned NestedLoopCount =
2237 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2238 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002239 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002240 return StmtError();
2241
2242 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002243 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2244 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002245}
2246
Alexander Musmanf82886e2014-09-18 05:12:34 +00002247StmtResult Sema::ActOnOpenMPForSimdDirective(
2248 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2249 SourceLocation EndLoc,
2250 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2251 // In presence of clause 'collapse', it will define the nested loops number.
2252 unsigned NestedLoopCount =
2253 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
2254 *this, *DSAStack, VarsWithImplicitDSA);
2255 if (NestedLoopCount == 0)
2256 return StmtError();
2257
2258 getCurFunction()->setHasBranchProtectedScope();
2259 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2260 Clauses, AStmt);
2261}
2262
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002263StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2264 Stmt *AStmt,
2265 SourceLocation StartLoc,
2266 SourceLocation EndLoc) {
2267 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2268 auto BaseStmt = AStmt;
2269 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2270 BaseStmt = CS->getCapturedStmt();
2271 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2272 auto S = C->children();
2273 if (!S)
2274 return StmtError();
2275 // All associated statements must be '#pragma omp section' except for
2276 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002277 for (++S; S; ++S) {
2278 auto SectionStmt = *S;
2279 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2280 if (SectionStmt)
2281 Diag(SectionStmt->getLocStart(),
2282 diag::err_omp_sections_substmt_not_section);
2283 return StmtError();
2284 }
2285 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002286 } else {
2287 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2288 return StmtError();
2289 }
2290
2291 getCurFunction()->setHasBranchProtectedScope();
2292
2293 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2294 AStmt);
2295}
2296
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002297StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2298 SourceLocation StartLoc,
2299 SourceLocation EndLoc) {
2300 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2301
2302 getCurFunction()->setHasBranchProtectedScope();
2303
2304 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2305}
2306
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002307StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2308 Stmt *AStmt,
2309 SourceLocation StartLoc,
2310 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002311 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2312
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002313 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002314
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002315 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2316}
2317
Alexander Musman80c22892014-07-17 08:54:58 +00002318StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2319 SourceLocation StartLoc,
2320 SourceLocation EndLoc) {
2321 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2322
2323 getCurFunction()->setHasBranchProtectedScope();
2324
2325 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2326}
2327
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002328StmtResult
2329Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2330 Stmt *AStmt, SourceLocation StartLoc,
2331 SourceLocation EndLoc) {
2332 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2333
2334 getCurFunction()->setHasBranchProtectedScope();
2335
2336 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2337 AStmt);
2338}
2339
Alexey Bataev4acb8592014-07-07 13:01:15 +00002340StmtResult Sema::ActOnOpenMPParallelForDirective(
2341 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2342 SourceLocation EndLoc,
2343 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2344 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2345 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2346 // 1.2.2 OpenMP Language Terminology
2347 // Structured block - An executable statement with a single entry at the
2348 // top and a single exit at the bottom.
2349 // The point of exit cannot be a branch out of the structured block.
2350 // longjmp() and throw() must not violate the entry/exit criteria.
2351 CS->getCapturedDecl()->setNothrow();
2352
2353 // In presence of clause 'collapse', it will define the nested loops number.
2354 unsigned NestedLoopCount =
2355 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2356 *this, *DSAStack, VarsWithImplicitDSA);
2357 if (NestedLoopCount == 0)
2358 return StmtError();
2359
2360 getCurFunction()->setHasBranchProtectedScope();
2361 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2362 NestedLoopCount, Clauses, AStmt);
2363}
2364
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002365StmtResult
2366Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2367 Stmt *AStmt, SourceLocation StartLoc,
2368 SourceLocation EndLoc) {
2369 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2370 auto BaseStmt = AStmt;
2371 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2372 BaseStmt = CS->getCapturedStmt();
2373 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2374 auto S = C->children();
2375 if (!S)
2376 return StmtError();
2377 // All associated statements must be '#pragma omp section' except for
2378 // the first one.
2379 for (++S; S; ++S) {
2380 auto SectionStmt = *S;
2381 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2382 if (SectionStmt)
2383 Diag(SectionStmt->getLocStart(),
2384 diag::err_omp_parallel_sections_substmt_not_section);
2385 return StmtError();
2386 }
2387 }
2388 } else {
2389 Diag(AStmt->getLocStart(),
2390 diag::err_omp_parallel_sections_not_compound_stmt);
2391 return StmtError();
2392 }
2393
2394 getCurFunction()->setHasBranchProtectedScope();
2395
2396 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2397 Clauses, AStmt);
2398}
2399
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002400StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2401 Stmt *AStmt, SourceLocation StartLoc,
2402 SourceLocation EndLoc) {
2403 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2404 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2405 // 1.2.2 OpenMP Language Terminology
2406 // Structured block - An executable statement with a single entry at the
2407 // top and a single exit at the bottom.
2408 // The point of exit cannot be a branch out of the structured block.
2409 // longjmp() and throw() must not violate the entry/exit criteria.
2410 CS->getCapturedDecl()->setNothrow();
2411
2412 getCurFunction()->setHasBranchProtectedScope();
2413
2414 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2415}
2416
Alexey Bataev68446b72014-07-18 07:47:19 +00002417StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2418 SourceLocation EndLoc) {
2419 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2420}
2421
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002422StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
2423 SourceLocation EndLoc) {
2424 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
2425}
2426
Alexey Bataev2df347a2014-07-18 10:17:07 +00002427StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
2428 SourceLocation EndLoc) {
2429 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
2430}
2431
Alexey Bataev6125da92014-07-21 11:26:11 +00002432StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
2433 SourceLocation StartLoc,
2434 SourceLocation EndLoc) {
2435 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
2436 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
2437}
2438
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002439StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
2440 SourceLocation StartLoc,
2441 SourceLocation EndLoc) {
2442 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2443
2444 getCurFunction()->setHasBranchProtectedScope();
2445
2446 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
2447}
2448
Alexey Bataev0162e452014-07-22 10:10:35 +00002449StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
2450 Stmt *AStmt,
2451 SourceLocation StartLoc,
2452 SourceLocation EndLoc) {
2453 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002454 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00002455 // 1.2.2 OpenMP Language Terminology
2456 // Structured block - An executable statement with a single entry at the
2457 // top and a single exit at the bottom.
2458 // The point of exit cannot be a branch out of the structured block.
2459 // longjmp() and throw() must not violate the entry/exit criteria.
2460 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00002461 OpenMPClauseKind AtomicKind = OMPC_unknown;
2462 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002463 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00002464 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00002465 C->getClauseKind() == OMPC_update ||
2466 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00002467 if (AtomicKind != OMPC_unknown) {
2468 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
2469 << SourceRange(C->getLocStart(), C->getLocEnd());
2470 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
2471 << getOpenMPClauseName(AtomicKind);
2472 } else {
2473 AtomicKind = C->getClauseKind();
2474 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002475 }
2476 }
2477 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002478 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00002479 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002480 if (!isa<Expr>(Body)) {
2481 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002482 diag::err_omp_atomic_read_not_expression_statement);
2483 return StmtError();
2484 }
2485 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002486 if (!isa<Expr>(Body)) {
2487 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002488 diag::err_omp_atomic_write_not_expression_statement);
2489 return StmtError();
2490 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00002491 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002492 if (!isa<Expr>(Body)) {
2493 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00002494 diag::err_omp_atomic_update_not_expression_statement)
2495 << (AtomicKind == OMPC_update);
2496 return StmtError();
2497 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002498 } else if (AtomicKind == OMPC_capture) {
2499 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
2500 Diag(Body->getLocStart(),
2501 diag::err_omp_atomic_capture_not_expression_statement);
2502 return StmtError();
2503 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
2504 Diag(Body->getLocStart(),
2505 diag::err_omp_atomic_capture_not_compound_statement);
2506 return StmtError();
2507 }
Alexey Bataevdea47612014-07-23 07:46:59 +00002508 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002509
2510 getCurFunction()->setHasBranchProtectedScope();
2511
2512 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2513}
2514
Alexey Bataeved09d242014-05-28 05:53:51 +00002515OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002516 SourceLocation StartLoc,
2517 SourceLocation LParenLoc,
2518 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002519 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002520 switch (Kind) {
2521 case OMPC_if:
2522 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2523 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002524 case OMPC_final:
2525 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2526 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002527 case OMPC_num_threads:
2528 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2529 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002530 case OMPC_safelen:
2531 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2532 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002533 case OMPC_collapse:
2534 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2535 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002536 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002537 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002538 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002539 case OMPC_private:
2540 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002541 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002542 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002543 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002544 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002545 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002546 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002547 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002548 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002549 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002550 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002551 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002552 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002553 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002554 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002555 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002556 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002557 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002558 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002559 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002560 llvm_unreachable("Clause is not allowed.");
2561 }
2562 return Res;
2563}
2564
Alexey Bataeved09d242014-05-28 05:53:51 +00002565OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002566 SourceLocation LParenLoc,
2567 SourceLocation EndLoc) {
2568 Expr *ValExpr = Condition;
2569 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2570 !Condition->isInstantiationDependent() &&
2571 !Condition->containsUnexpandedParameterPack()) {
2572 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002573 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002574 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002575 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002576
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002577 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002578 }
2579
2580 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2581}
2582
Alexey Bataev3778b602014-07-17 07:32:53 +00002583OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2584 SourceLocation StartLoc,
2585 SourceLocation LParenLoc,
2586 SourceLocation EndLoc) {
2587 Expr *ValExpr = Condition;
2588 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2589 !Condition->isInstantiationDependent() &&
2590 !Condition->containsUnexpandedParameterPack()) {
2591 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2592 Condition->getExprLoc(), Condition);
2593 if (Val.isInvalid())
2594 return nullptr;
2595
2596 ValExpr = Val.get();
2597 }
2598
2599 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2600}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002601ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2602 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002603 if (!Op)
2604 return ExprError();
2605
2606 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2607 public:
2608 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002609 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002610 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2611 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002612 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2613 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002614 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2615 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002616 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2617 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002618 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2619 QualType T,
2620 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002621 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2622 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002623 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2624 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002625 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002626 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002627 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002628 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2629 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002630 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2631 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002632 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2633 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002634 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002635 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002636 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002637 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2638 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002639 llvm_unreachable("conversion functions are permitted");
2640 }
2641 } ConvertDiagnoser;
2642 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2643}
2644
2645OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2646 SourceLocation StartLoc,
2647 SourceLocation LParenLoc,
2648 SourceLocation EndLoc) {
2649 Expr *ValExpr = NumThreads;
2650 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2651 !NumThreads->isInstantiationDependent() &&
2652 !NumThreads->containsUnexpandedParameterPack()) {
2653 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2654 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002655 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002656 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002657 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002658
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002659 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002660
2661 // OpenMP [2.5, Restrictions]
2662 // The num_threads expression must evaluate to a positive integer value.
2663 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002664 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2665 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002666 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2667 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002668 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002669 }
2670 }
2671
Alexey Bataeved09d242014-05-28 05:53:51 +00002672 return new (Context)
2673 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002674}
2675
Alexey Bataev62c87d22014-03-21 04:51:18 +00002676ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2677 OpenMPClauseKind CKind) {
2678 if (!E)
2679 return ExprError();
2680 if (E->isValueDependent() || E->isTypeDependent() ||
2681 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002682 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002683 llvm::APSInt Result;
2684 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2685 if (ICE.isInvalid())
2686 return ExprError();
2687 if (!Result.isStrictlyPositive()) {
2688 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2689 << getOpenMPClauseName(CKind) << E->getSourceRange();
2690 return ExprError();
2691 }
2692 return ICE;
2693}
2694
2695OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2696 SourceLocation LParenLoc,
2697 SourceLocation EndLoc) {
2698 // OpenMP [2.8.1, simd construct, Description]
2699 // The parameter of the safelen clause must be a constant
2700 // positive integer expression.
2701 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2702 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002703 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002704 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002705 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002706}
2707
Alexander Musman64d33f12014-06-04 07:53:32 +00002708OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2709 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002710 SourceLocation LParenLoc,
2711 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002712 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002713 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002714 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002715 // The parameter of the collapse clause must be a constant
2716 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002717 ExprResult NumForLoopsResult =
2718 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2719 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002720 return nullptr;
2721 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002722 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002723}
2724
Alexey Bataeved09d242014-05-28 05:53:51 +00002725OMPClause *Sema::ActOnOpenMPSimpleClause(
2726 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2727 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002728 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002729 switch (Kind) {
2730 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002731 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002732 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2733 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002734 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002735 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002736 Res = ActOnOpenMPProcBindClause(
2737 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2738 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002739 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002740 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002741 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002742 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002743 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002744 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002745 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002746 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002747 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002748 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002749 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002750 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002751 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002752 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002753 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002754 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002755 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002756 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002757 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002758 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002759 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002760 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002761 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002762 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002763 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002764 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002765 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002766 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002767 llvm_unreachable("Clause is not allowed.");
2768 }
2769 return Res;
2770}
2771
2772OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2773 SourceLocation KindKwLoc,
2774 SourceLocation StartLoc,
2775 SourceLocation LParenLoc,
2776 SourceLocation EndLoc) {
2777 if (Kind == OMPC_DEFAULT_unknown) {
2778 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002779 static_assert(OMPC_DEFAULT_unknown > 0,
2780 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002781 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002782 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002783 Values += "'";
2784 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2785 Values += "'";
2786 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002787 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002788 Values += " or ";
2789 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002790 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002791 break;
2792 default:
2793 Values += Sep;
2794 break;
2795 }
2796 }
2797 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002798 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002799 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002800 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002801 switch (Kind) {
2802 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002803 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002804 break;
2805 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002806 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002807 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002808 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002809 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002810 break;
2811 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002812 return new (Context)
2813 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002814}
2815
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002816OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2817 SourceLocation KindKwLoc,
2818 SourceLocation StartLoc,
2819 SourceLocation LParenLoc,
2820 SourceLocation EndLoc) {
2821 if (Kind == OMPC_PROC_BIND_unknown) {
2822 std::string Values;
2823 std::string Sep(", ");
2824 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2825 Values += "'";
2826 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2827 Values += "'";
2828 switch (i) {
2829 case OMPC_PROC_BIND_unknown - 2:
2830 Values += " or ";
2831 break;
2832 case OMPC_PROC_BIND_unknown - 1:
2833 break;
2834 default:
2835 Values += Sep;
2836 break;
2837 }
2838 }
2839 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002840 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002841 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002842 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002843 return new (Context)
2844 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002845}
2846
Alexey Bataev56dafe82014-06-20 07:16:17 +00002847OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2848 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2849 SourceLocation StartLoc, SourceLocation LParenLoc,
2850 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2851 SourceLocation EndLoc) {
2852 OMPClause *Res = nullptr;
2853 switch (Kind) {
2854 case OMPC_schedule:
2855 Res = ActOnOpenMPScheduleClause(
2856 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2857 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2858 break;
2859 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002860 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002861 case OMPC_num_threads:
2862 case OMPC_safelen:
2863 case OMPC_collapse:
2864 case OMPC_default:
2865 case OMPC_proc_bind:
2866 case OMPC_private:
2867 case OMPC_firstprivate:
2868 case OMPC_lastprivate:
2869 case OMPC_shared:
2870 case OMPC_reduction:
2871 case OMPC_linear:
2872 case OMPC_aligned:
2873 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002874 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002875 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002876 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002877 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002878 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002879 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002880 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002881 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002882 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002883 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002884 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002885 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002886 case OMPC_unknown:
2887 llvm_unreachable("Clause is not allowed.");
2888 }
2889 return Res;
2890}
2891
2892OMPClause *Sema::ActOnOpenMPScheduleClause(
2893 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2894 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2895 SourceLocation EndLoc) {
2896 if (Kind == OMPC_SCHEDULE_unknown) {
2897 std::string Values;
2898 std::string Sep(", ");
2899 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2900 Values += "'";
2901 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2902 Values += "'";
2903 switch (i) {
2904 case OMPC_SCHEDULE_unknown - 2:
2905 Values += " or ";
2906 break;
2907 case OMPC_SCHEDULE_unknown - 1:
2908 break;
2909 default:
2910 Values += Sep;
2911 break;
2912 }
2913 }
2914 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2915 << Values << getOpenMPClauseName(OMPC_schedule);
2916 return nullptr;
2917 }
2918 Expr *ValExpr = ChunkSize;
2919 if (ChunkSize) {
2920 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2921 !ChunkSize->isInstantiationDependent() &&
2922 !ChunkSize->containsUnexpandedParameterPack()) {
2923 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2924 ExprResult Val =
2925 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2926 if (Val.isInvalid())
2927 return nullptr;
2928
2929 ValExpr = Val.get();
2930
2931 // OpenMP [2.7.1, Restrictions]
2932 // chunk_size must be a loop invariant integer expression with a positive
2933 // value.
2934 llvm::APSInt Result;
2935 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2936 Result.isSigned() && !Result.isStrictlyPositive()) {
2937 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2938 << "schedule" << ChunkSize->getSourceRange();
2939 return nullptr;
2940 }
2941 }
2942 }
2943
2944 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2945 EndLoc, Kind, ValExpr);
2946}
2947
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002948OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2949 SourceLocation StartLoc,
2950 SourceLocation EndLoc) {
2951 OMPClause *Res = nullptr;
2952 switch (Kind) {
2953 case OMPC_ordered:
2954 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2955 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002956 case OMPC_nowait:
2957 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2958 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002959 case OMPC_untied:
2960 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
2961 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002962 case OMPC_mergeable:
2963 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
2964 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002965 case OMPC_read:
2966 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
2967 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00002968 case OMPC_write:
2969 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
2970 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00002971 case OMPC_update:
2972 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
2973 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00002974 case OMPC_capture:
2975 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
2976 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002977 case OMPC_seq_cst:
2978 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
2979 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002980 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002981 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002982 case OMPC_num_threads:
2983 case OMPC_safelen:
2984 case OMPC_collapse:
2985 case OMPC_schedule:
2986 case OMPC_private:
2987 case OMPC_firstprivate:
2988 case OMPC_lastprivate:
2989 case OMPC_shared:
2990 case OMPC_reduction:
2991 case OMPC_linear:
2992 case OMPC_aligned:
2993 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002994 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002995 case OMPC_default:
2996 case OMPC_proc_bind:
2997 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002998 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002999 case OMPC_unknown:
3000 llvm_unreachable("Clause is not allowed.");
3001 }
3002 return Res;
3003}
3004
3005OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3006 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003007 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003008 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3009}
3010
Alexey Bataev236070f2014-06-20 11:19:47 +00003011OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3012 SourceLocation EndLoc) {
3013 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3014}
3015
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003016OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3017 SourceLocation EndLoc) {
3018 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3019}
3020
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003021OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3022 SourceLocation EndLoc) {
3023 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3024}
3025
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003026OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3027 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003028 return new (Context) OMPReadClause(StartLoc, EndLoc);
3029}
3030
Alexey Bataevdea47612014-07-23 07:46:59 +00003031OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3032 SourceLocation EndLoc) {
3033 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3034}
3035
Alexey Bataev67a4f222014-07-23 10:25:33 +00003036OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3037 SourceLocation EndLoc) {
3038 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3039}
3040
Alexey Bataev459dec02014-07-24 06:46:57 +00003041OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3042 SourceLocation EndLoc) {
3043 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3044}
3045
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003046OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3047 SourceLocation EndLoc) {
3048 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3049}
3050
Alexey Bataevc5e02582014-06-16 07:08:35 +00003051OMPClause *Sema::ActOnOpenMPVarListClause(
3052 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3053 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3054 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3055 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003056 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003057 switch (Kind) {
3058 case OMPC_private:
3059 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3060 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003061 case OMPC_firstprivate:
3062 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3063 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003064 case OMPC_lastprivate:
3065 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3066 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003067 case OMPC_shared:
3068 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3069 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003070 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003071 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3072 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003073 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003074 case OMPC_linear:
3075 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3076 ColonLoc, EndLoc);
3077 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003078 case OMPC_aligned:
3079 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3080 ColonLoc, EndLoc);
3081 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003082 case OMPC_copyin:
3083 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3084 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003085 case OMPC_copyprivate:
3086 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3087 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003088 case OMPC_flush:
3089 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3090 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003091 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003092 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003093 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003094 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003095 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003096 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003097 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003098 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003099 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003100 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003101 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003102 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003103 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003104 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003105 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003106 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003107 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003108 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003109 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003110 llvm_unreachable("Clause is not allowed.");
3111 }
3112 return Res;
3113}
3114
3115OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3116 SourceLocation StartLoc,
3117 SourceLocation LParenLoc,
3118 SourceLocation EndLoc) {
3119 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003120 for (auto &RefExpr : VarList) {
3121 assert(RefExpr && "NULL expr in OpenMP private clause.");
3122 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003123 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003124 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003125 continue;
3126 }
3127
Alexey Bataeved09d242014-05-28 05:53:51 +00003128 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003129 // OpenMP [2.1, C/C++]
3130 // A list item is a variable name.
3131 // OpenMP [2.9.3.3, Restrictions, p.1]
3132 // A variable that is part of another variable (as an array or
3133 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003134 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003135 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003136 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003137 continue;
3138 }
3139 Decl *D = DE->getDecl();
3140 VarDecl *VD = cast<VarDecl>(D);
3141
3142 QualType Type = VD->getType();
3143 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3144 // It will be analyzed later.
3145 Vars.push_back(DE);
3146 continue;
3147 }
3148
3149 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3150 // A variable that appears in a private clause must not have an incomplete
3151 // type or a reference type.
3152 if (RequireCompleteType(ELoc, Type,
3153 diag::err_omp_private_incomplete_type)) {
3154 continue;
3155 }
3156 if (Type->isReferenceType()) {
3157 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003158 << getOpenMPClauseName(OMPC_private) << Type;
3159 bool IsDecl =
3160 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3161 Diag(VD->getLocation(),
3162 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3163 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003164 continue;
3165 }
3166
3167 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3168 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003169 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003170 // class type.
3171 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003172 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3173 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003174 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003175 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3176 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3177 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003178 // FIXME This code must be replaced by actual constructing/destructing of
3179 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003180 if (RD) {
3181 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3182 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003183 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003184 if (!CD ||
3185 CheckConstructorAccess(ELoc, CD,
3186 InitializedEntity::InitializeTemporary(Type),
3187 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003188 CD->isDeleted()) {
3189 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003190 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003191 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3192 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003193 Diag(VD->getLocation(),
3194 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3195 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003196 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3197 continue;
3198 }
3199 MarkFunctionReferenced(ELoc, CD);
3200 DiagnoseUseOfDecl(CD, ELoc);
3201
3202 CXXDestructorDecl *DD = RD->getDestructor();
3203 if (DD) {
3204 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3205 DD->isDeleted()) {
3206 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003207 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003208 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3209 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003210 Diag(VD->getLocation(),
3211 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3212 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003213 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3214 continue;
3215 }
3216 MarkFunctionReferenced(ELoc, DD);
3217 DiagnoseUseOfDecl(DD, ELoc);
3218 }
3219 }
3220
Alexey Bataev758e55e2013-09-06 18:03:48 +00003221 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3222 // in a Construct]
3223 // Variables with the predetermined data-sharing attributes may not be
3224 // listed in data-sharing attributes clauses, except for the cases
3225 // listed below. For these exceptions only, listing a predetermined
3226 // variable in a data-sharing attribute clause is allowed and overrides
3227 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003228 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003229 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003230 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3231 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003232 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003233 continue;
3234 }
3235
3236 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003237 Vars.push_back(DE);
3238 }
3239
Alexey Bataeved09d242014-05-28 05:53:51 +00003240 if (Vars.empty())
3241 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003242
3243 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3244}
3245
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003246OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3247 SourceLocation StartLoc,
3248 SourceLocation LParenLoc,
3249 SourceLocation EndLoc) {
3250 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003251 bool IsImplicitClause =
3252 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3253 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3254
Alexey Bataeved09d242014-05-28 05:53:51 +00003255 for (auto &RefExpr : VarList) {
3256 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3257 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003258 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003259 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003260 continue;
3261 }
3262
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003263 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3264 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003265 // OpenMP [2.1, C/C++]
3266 // A list item is a variable name.
3267 // OpenMP [2.9.3.3, Restrictions, p.1]
3268 // A variable that is part of another variable (as an array or
3269 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003270 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003271 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003272 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003273 continue;
3274 }
3275 Decl *D = DE->getDecl();
3276 VarDecl *VD = cast<VarDecl>(D);
3277
3278 QualType Type = VD->getType();
3279 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3280 // It will be analyzed later.
3281 Vars.push_back(DE);
3282 continue;
3283 }
3284
3285 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3286 // A variable that appears in a private clause must not have an incomplete
3287 // type or a reference type.
3288 if (RequireCompleteType(ELoc, Type,
3289 diag::err_omp_firstprivate_incomplete_type)) {
3290 continue;
3291 }
3292 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003293 if (IsImplicitClause) {
3294 Diag(ImplicitClauseLoc,
3295 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3296 << Type;
3297 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3298 } else {
3299 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3300 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3301 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003302 bool IsDecl =
3303 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3304 Diag(VD->getLocation(),
3305 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3306 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003307 continue;
3308 }
3309
3310 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3311 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003312 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003313 // class type.
3314 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003315 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3316 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3317 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003318 // FIXME This code must be replaced by actual constructing/destructing of
3319 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003320 if (RD) {
3321 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3322 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003323 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003324 if (!CD ||
3325 CheckConstructorAccess(ELoc, CD,
3326 InitializedEntity::InitializeTemporary(Type),
3327 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003328 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003329 if (IsImplicitClause) {
3330 Diag(ImplicitClauseLoc,
3331 diag::err_omp_task_predetermined_firstprivate_required_method)
3332 << 0;
3333 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3334 } else {
3335 Diag(ELoc, diag::err_omp_required_method)
3336 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3337 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003338 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3339 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003340 Diag(VD->getLocation(),
3341 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3342 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003343 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3344 continue;
3345 }
3346 MarkFunctionReferenced(ELoc, CD);
3347 DiagnoseUseOfDecl(CD, ELoc);
3348
3349 CXXDestructorDecl *DD = RD->getDestructor();
3350 if (DD) {
3351 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3352 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003353 if (IsImplicitClause) {
3354 Diag(ImplicitClauseLoc,
3355 diag::err_omp_task_predetermined_firstprivate_required_method)
3356 << 1;
3357 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3358 } else {
3359 Diag(ELoc, diag::err_omp_required_method)
3360 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3361 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003362 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3363 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003364 Diag(VD->getLocation(),
3365 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3366 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003367 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3368 continue;
3369 }
3370 MarkFunctionReferenced(ELoc, DD);
3371 DiagnoseUseOfDecl(DD, ELoc);
3372 }
3373 }
3374
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003375 // If an implicit firstprivate variable found it was checked already.
3376 if (!IsImplicitClause) {
3377 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003378 Type = Type.getNonReferenceType().getCanonicalType();
3379 bool IsConstant = Type.isConstant(Context);
3380 Type = Context.getBaseElementType(Type);
3381 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
3382 // A list item that specifies a given variable may not appear in more
3383 // than one clause on the same directive, except that a variable may be
3384 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003385 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00003386 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003387 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003388 << getOpenMPClauseName(DVar.CKind)
3389 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003390 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003391 continue;
3392 }
3393
3394 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3395 // in a Construct]
3396 // Variables with the predetermined data-sharing attributes may not be
3397 // listed in data-sharing attributes clauses, except for the cases
3398 // listed below. For these exceptions only, listing a predetermined
3399 // variable in a data-sharing attribute clause is allowed and overrides
3400 // the variable's predetermined data-sharing attributes.
3401 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3402 // in a Construct, C/C++, p.2]
3403 // Variables with const-qualified type having no mutable member may be
3404 // listed in a firstprivate clause, even if they are static data members.
3405 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
3406 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
3407 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003408 << getOpenMPClauseName(DVar.CKind)
3409 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003410 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003411 continue;
3412 }
3413
Alexey Bataevf29276e2014-06-18 04:14:57 +00003414 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003415 // OpenMP [2.9.3.4, Restrictions, p.2]
3416 // A list item that is private within a parallel region must not appear
3417 // in a firstprivate clause on a worksharing construct if any of the
3418 // worksharing regions arising from the worksharing construct ever bind
3419 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00003420 if (isOpenMPWorksharingDirective(CurrDir) &&
3421 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003422 DVar = DSAStack->getImplicitDSA(VD, true);
3423 if (DVar.CKind != OMPC_shared &&
3424 (isOpenMPParallelDirective(DVar.DKind) ||
3425 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003426 Diag(ELoc, diag::err_omp_required_access)
3427 << getOpenMPClauseName(OMPC_firstprivate)
3428 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003429 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003430 continue;
3431 }
3432 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003433 // OpenMP [2.9.3.4, Restrictions, p.3]
3434 // A list item that appears in a reduction clause of a parallel construct
3435 // must not appear in a firstprivate clause on a worksharing or task
3436 // construct if any of the worksharing or task regions arising from the
3437 // worksharing or task construct ever bind to any of the parallel regions
3438 // arising from the parallel construct.
3439 // OpenMP [2.9.3.4, Restrictions, p.4]
3440 // A list item that appears in a reduction clause in worksharing
3441 // construct must not appear in a firstprivate clause in a task construct
3442 // encountered during execution of any of the worksharing regions arising
3443 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003444 if (CurrDir == OMPD_task) {
3445 DVar =
3446 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
3447 [](OpenMPDirectiveKind K) -> bool {
3448 return isOpenMPParallelDirective(K) ||
3449 isOpenMPWorksharingDirective(K);
3450 },
3451 false);
3452 if (DVar.CKind == OMPC_reduction &&
3453 (isOpenMPParallelDirective(DVar.DKind) ||
3454 isOpenMPWorksharingDirective(DVar.DKind))) {
3455 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
3456 << getOpenMPDirectiveName(DVar.DKind);
3457 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3458 continue;
3459 }
3460 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003461 }
3462
3463 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
3464 Vars.push_back(DE);
3465 }
3466
Alexey Bataeved09d242014-05-28 05:53:51 +00003467 if (Vars.empty())
3468 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003469
3470 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3471 Vars);
3472}
3473
Alexander Musman1bb328c2014-06-04 13:06:39 +00003474OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
3475 SourceLocation StartLoc,
3476 SourceLocation LParenLoc,
3477 SourceLocation EndLoc) {
3478 SmallVector<Expr *, 8> Vars;
3479 for (auto &RefExpr : VarList) {
3480 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
3481 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3482 // It will be analyzed later.
3483 Vars.push_back(RefExpr);
3484 continue;
3485 }
3486
3487 SourceLocation ELoc = RefExpr->getExprLoc();
3488 // OpenMP [2.1, C/C++]
3489 // A list item is a variable name.
3490 // OpenMP [2.14.3.5, Restrictions, p.1]
3491 // A variable that is part of another variable (as an array or structure
3492 // element) cannot appear in a lastprivate clause.
3493 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3494 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3495 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3496 continue;
3497 }
3498 Decl *D = DE->getDecl();
3499 VarDecl *VD = cast<VarDecl>(D);
3500
3501 QualType Type = VD->getType();
3502 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3503 // It will be analyzed later.
3504 Vars.push_back(DE);
3505 continue;
3506 }
3507
3508 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
3509 // A variable that appears in a lastprivate clause must not have an
3510 // incomplete type or a reference type.
3511 if (RequireCompleteType(ELoc, Type,
3512 diag::err_omp_lastprivate_incomplete_type)) {
3513 continue;
3514 }
3515 if (Type->isReferenceType()) {
3516 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3517 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3518 bool IsDecl =
3519 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3520 Diag(VD->getLocation(),
3521 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3522 << VD;
3523 continue;
3524 }
3525
3526 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3527 // in a Construct]
3528 // Variables with the predetermined data-sharing attributes may not be
3529 // listed in data-sharing attributes clauses, except for the cases
3530 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003531 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003532 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3533 DVar.CKind != OMPC_firstprivate &&
3534 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3535 Diag(ELoc, diag::err_omp_wrong_dsa)
3536 << getOpenMPClauseName(DVar.CKind)
3537 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003538 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003539 continue;
3540 }
3541
Alexey Bataevf29276e2014-06-18 04:14:57 +00003542 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3543 // OpenMP [2.14.3.5, Restrictions, p.2]
3544 // A list item that is private within a parallel region, or that appears in
3545 // the reduction clause of a parallel construct, must not appear in a
3546 // lastprivate clause on a worksharing construct if any of the corresponding
3547 // worksharing regions ever binds to any of the corresponding parallel
3548 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003549 if (isOpenMPWorksharingDirective(CurrDir) &&
3550 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003551 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003552 if (DVar.CKind != OMPC_shared) {
3553 Diag(ELoc, diag::err_omp_required_access)
3554 << getOpenMPClauseName(OMPC_lastprivate)
3555 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003556 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003557 continue;
3558 }
3559 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003560 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003561 // A variable of class type (or array thereof) that appears in a
3562 // lastprivate clause requires an accessible, unambiguous default
3563 // constructor for the class type, unless the list item is also specified
3564 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003565 // A variable of class type (or array thereof) that appears in a
3566 // lastprivate clause requires an accessible, unambiguous copy assignment
3567 // operator for the class type.
3568 while (Type.getNonReferenceType()->isArrayType())
3569 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3570 ->getElementType();
3571 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3572 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3573 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003574 // FIXME This code must be replaced by actual copying and destructing of the
3575 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003576 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003577 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3578 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003579 if (MD) {
3580 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3581 MD->isDeleted()) {
3582 Diag(ELoc, diag::err_omp_required_method)
3583 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3584 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3585 VarDecl::DeclarationOnly;
3586 Diag(VD->getLocation(),
3587 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3588 << VD;
3589 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3590 continue;
3591 }
3592 MarkFunctionReferenced(ELoc, MD);
3593 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003594 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003595
3596 CXXDestructorDecl *DD = RD->getDestructor();
3597 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003598 PartialDiagnostic PD =
3599 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003600 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3601 DD->isDeleted()) {
3602 Diag(ELoc, diag::err_omp_required_method)
3603 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3604 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3605 VarDecl::DeclarationOnly;
3606 Diag(VD->getLocation(),
3607 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3608 << VD;
3609 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3610 continue;
3611 }
3612 MarkFunctionReferenced(ELoc, DD);
3613 DiagnoseUseOfDecl(DD, ELoc);
3614 }
3615 }
3616
Alexey Bataevf29276e2014-06-18 04:14:57 +00003617 if (DVar.CKind != OMPC_firstprivate)
3618 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003619 Vars.push_back(DE);
3620 }
3621
3622 if (Vars.empty())
3623 return nullptr;
3624
3625 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3626 Vars);
3627}
3628
Alexey Bataev758e55e2013-09-06 18:03:48 +00003629OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3630 SourceLocation StartLoc,
3631 SourceLocation LParenLoc,
3632 SourceLocation EndLoc) {
3633 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003634 for (auto &RefExpr : VarList) {
3635 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3636 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003637 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003638 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003639 continue;
3640 }
3641
Alexey Bataeved09d242014-05-28 05:53:51 +00003642 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003643 // OpenMP [2.1, C/C++]
3644 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003645 // OpenMP [2.14.3.2, Restrictions, p.1]
3646 // A variable that is part of another variable (as an array or structure
3647 // element) cannot appear in a shared unless it is a static data member
3648 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003649 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003650 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003651 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003652 continue;
3653 }
3654 Decl *D = DE->getDecl();
3655 VarDecl *VD = cast<VarDecl>(D);
3656
3657 QualType Type = VD->getType();
3658 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3659 // It will be analyzed later.
3660 Vars.push_back(DE);
3661 continue;
3662 }
3663
3664 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3665 // in a Construct]
3666 // Variables with the predetermined data-sharing attributes may not be
3667 // listed in data-sharing attributes clauses, except for the cases
3668 // listed below. For these exceptions only, listing a predetermined
3669 // variable in a data-sharing attribute clause is allowed and overrides
3670 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003671 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003672 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3673 DVar.RefExpr) {
3674 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3675 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003676 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003677 continue;
3678 }
3679
3680 DSAStack->addDSA(VD, DE, OMPC_shared);
3681 Vars.push_back(DE);
3682 }
3683
Alexey Bataeved09d242014-05-28 05:53:51 +00003684 if (Vars.empty())
3685 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003686
3687 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3688}
3689
Alexey Bataevc5e02582014-06-16 07:08:35 +00003690namespace {
3691class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3692 DSAStackTy *Stack;
3693
3694public:
3695 bool VisitDeclRefExpr(DeclRefExpr *E) {
3696 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003697 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003698 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3699 return false;
3700 if (DVar.CKind != OMPC_unknown)
3701 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003702 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003703 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003704 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003705 return true;
3706 return false;
3707 }
3708 return false;
3709 }
3710 bool VisitStmt(Stmt *S) {
3711 for (auto Child : S->children()) {
3712 if (Child && Visit(Child))
3713 return true;
3714 }
3715 return false;
3716 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003717 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003718};
Alexey Bataev23b69422014-06-18 07:08:49 +00003719} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003720
3721OMPClause *Sema::ActOnOpenMPReductionClause(
3722 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3723 SourceLocation ColonLoc, SourceLocation EndLoc,
3724 CXXScopeSpec &ReductionIdScopeSpec,
3725 const DeclarationNameInfo &ReductionId) {
3726 // TODO: Allow scope specification search when 'declare reduction' is
3727 // supported.
3728 assert(ReductionIdScopeSpec.isEmpty() &&
3729 "No support for scoped reduction identifiers yet.");
3730
3731 auto DN = ReductionId.getName();
3732 auto OOK = DN.getCXXOverloadedOperator();
3733 BinaryOperatorKind BOK = BO_Comma;
3734
3735 // OpenMP [2.14.3.6, reduction clause]
3736 // C
3737 // reduction-identifier is either an identifier or one of the following
3738 // operators: +, -, *, &, |, ^, && and ||
3739 // C++
3740 // reduction-identifier is either an id-expression or one of the following
3741 // operators: +, -, *, &, |, ^, && and ||
3742 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3743 switch (OOK) {
3744 case OO_Plus:
3745 case OO_Minus:
3746 BOK = BO_AddAssign;
3747 break;
3748 case OO_Star:
3749 BOK = BO_MulAssign;
3750 break;
3751 case OO_Amp:
3752 BOK = BO_AndAssign;
3753 break;
3754 case OO_Pipe:
3755 BOK = BO_OrAssign;
3756 break;
3757 case OO_Caret:
3758 BOK = BO_XorAssign;
3759 break;
3760 case OO_AmpAmp:
3761 BOK = BO_LAnd;
3762 break;
3763 case OO_PipePipe:
3764 BOK = BO_LOr;
3765 break;
3766 default:
3767 if (auto II = DN.getAsIdentifierInfo()) {
3768 if (II->isStr("max"))
3769 BOK = BO_GT;
3770 else if (II->isStr("min"))
3771 BOK = BO_LT;
3772 }
3773 break;
3774 }
3775 SourceRange ReductionIdRange;
3776 if (ReductionIdScopeSpec.isValid()) {
3777 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3778 }
3779 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3780 if (BOK == BO_Comma) {
3781 // Not allowed reduction identifier is found.
3782 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3783 << ReductionIdRange;
3784 return nullptr;
3785 }
3786
3787 SmallVector<Expr *, 8> Vars;
3788 for (auto RefExpr : VarList) {
3789 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3790 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3791 // It will be analyzed later.
3792 Vars.push_back(RefExpr);
3793 continue;
3794 }
3795
3796 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3797 RefExpr->isInstantiationDependent() ||
3798 RefExpr->containsUnexpandedParameterPack()) {
3799 // It will be analyzed later.
3800 Vars.push_back(RefExpr);
3801 continue;
3802 }
3803
3804 auto ELoc = RefExpr->getExprLoc();
3805 auto ERange = RefExpr->getSourceRange();
3806 // OpenMP [2.1, C/C++]
3807 // A list item is a variable or array section, subject to the restrictions
3808 // specified in Section 2.4 on page 42 and in each of the sections
3809 // describing clauses and directives for which a list appears.
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.
3813 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3814 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3815 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3816 continue;
3817 }
3818 auto D = DE->getDecl();
3819 auto VD = cast<VarDecl>(D);
3820 auto Type = VD->getType();
3821 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3822 // A variable that appears in a private clause must not have an incomplete
3823 // type or a reference type.
3824 if (RequireCompleteType(ELoc, Type,
3825 diag::err_omp_reduction_incomplete_type))
3826 continue;
3827 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3828 // Arrays may not appear in a reduction clause.
3829 if (Type.getNonReferenceType()->isArrayType()) {
3830 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3831 bool IsDecl =
3832 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3833 Diag(VD->getLocation(),
3834 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3835 << VD;
3836 continue;
3837 }
3838 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3839 // A list item that appears in a reduction clause must not be
3840 // const-qualified.
3841 if (Type.getNonReferenceType().isConstant(Context)) {
3842 Diag(ELoc, diag::err_omp_const_variable)
3843 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3844 bool IsDecl =
3845 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3846 Diag(VD->getLocation(),
3847 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3848 << VD;
3849 continue;
3850 }
3851 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3852 // If a list-item is a reference type then it must bind to the same object
3853 // for all threads of the team.
3854 VarDecl *VDDef = VD->getDefinition();
3855 if (Type->isReferenceType() && VDDef) {
3856 DSARefChecker Check(DSAStack);
3857 if (Check.Visit(VDDef->getInit())) {
3858 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3859 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3860 continue;
3861 }
3862 }
3863 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3864 // The type of a list item that appears in a reduction clause must be valid
3865 // for the reduction-identifier. For a max or min reduction in C, the type
3866 // of the list item must be an allowed arithmetic data type: char, int,
3867 // float, double, or _Bool, possibly modified with long, short, signed, or
3868 // unsigned. For a max or min reduction in C++, the type of the list item
3869 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3870 // double, or bool, possibly modified with long, short, signed, or unsigned.
3871 if ((BOK == BO_GT || BOK == BO_LT) &&
3872 !(Type->isScalarType() ||
3873 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3874 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3875 << getLangOpts().CPlusPlus;
3876 bool IsDecl =
3877 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3878 Diag(VD->getLocation(),
3879 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3880 << VD;
3881 continue;
3882 }
3883 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3884 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3885 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3886 bool IsDecl =
3887 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3888 Diag(VD->getLocation(),
3889 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3890 << VD;
3891 continue;
3892 }
3893 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3894 getDiagnostics().setSuppressAllDiagnostics(true);
3895 ExprResult ReductionOp =
3896 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3897 RefExpr, RefExpr);
3898 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3899 if (ReductionOp.isInvalid()) {
3900 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003901 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003902 bool IsDecl =
3903 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3904 Diag(VD->getLocation(),
3905 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3906 << VD;
3907 continue;
3908 }
3909
3910 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3911 // in a Construct]
3912 // Variables with the predetermined data-sharing attributes may not be
3913 // listed in data-sharing attributes clauses, except for the cases
3914 // listed below. For these exceptions only, listing a predetermined
3915 // variable in a data-sharing attribute clause is allowed and overrides
3916 // the variable's predetermined data-sharing attributes.
3917 // OpenMP [2.14.3.6, Restrictions, p.3]
3918 // Any number of reduction clauses can be specified on the directive,
3919 // but a list item can appear only once in the reduction clauses for that
3920 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003921 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003922 if (DVar.CKind == OMPC_reduction) {
3923 Diag(ELoc, diag::err_omp_once_referenced)
3924 << getOpenMPClauseName(OMPC_reduction);
3925 if (DVar.RefExpr) {
3926 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3927 }
3928 } else if (DVar.CKind != OMPC_unknown) {
3929 Diag(ELoc, diag::err_omp_wrong_dsa)
3930 << getOpenMPClauseName(DVar.CKind)
3931 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003932 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003933 continue;
3934 }
3935
3936 // OpenMP [2.14.3.6, Restrictions, p.1]
3937 // A list item that appears in a reduction clause of a worksharing
3938 // construct must be shared in the parallel regions to which any of the
3939 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003940 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003941 if (isOpenMPWorksharingDirective(CurrDir) &&
3942 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003943 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003944 if (DVar.CKind != OMPC_shared) {
3945 Diag(ELoc, diag::err_omp_required_access)
3946 << getOpenMPClauseName(OMPC_reduction)
3947 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003948 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003949 continue;
3950 }
3951 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003952
3953 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3954 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3955 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003956 // FIXME This code must be replaced by actual constructing/destructing of
3957 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003958 if (RD) {
3959 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3960 PartialDiagnostic PD =
3961 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003962 if (!CD ||
3963 CheckConstructorAccess(ELoc, CD,
3964 InitializedEntity::InitializeTemporary(Type),
3965 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003966 CD->isDeleted()) {
3967 Diag(ELoc, diag::err_omp_required_method)
3968 << getOpenMPClauseName(OMPC_reduction) << 0;
3969 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3970 VarDecl::DeclarationOnly;
3971 Diag(VD->getLocation(),
3972 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3973 << VD;
3974 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3975 continue;
3976 }
3977 MarkFunctionReferenced(ELoc, CD);
3978 DiagnoseUseOfDecl(CD, ELoc);
3979
3980 CXXDestructorDecl *DD = RD->getDestructor();
3981 if (DD) {
3982 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3983 DD->isDeleted()) {
3984 Diag(ELoc, diag::err_omp_required_method)
3985 << getOpenMPClauseName(OMPC_reduction) << 4;
3986 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3987 VarDecl::DeclarationOnly;
3988 Diag(VD->getLocation(),
3989 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3990 << VD;
3991 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3992 continue;
3993 }
3994 MarkFunctionReferenced(ELoc, DD);
3995 DiagnoseUseOfDecl(DD, ELoc);
3996 }
3997 }
3998
3999 DSAStack->addDSA(VD, DE, OMPC_reduction);
4000 Vars.push_back(DE);
4001 }
4002
4003 if (Vars.empty())
4004 return nullptr;
4005
4006 return OMPReductionClause::Create(
4007 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4008 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4009}
4010
Alexander Musman8dba6642014-04-22 13:09:42 +00004011OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4012 SourceLocation StartLoc,
4013 SourceLocation LParenLoc,
4014 SourceLocation ColonLoc,
4015 SourceLocation EndLoc) {
4016 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004017 for (auto &RefExpr : VarList) {
4018 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4019 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004020 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004021 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004022 continue;
4023 }
4024
4025 // OpenMP [2.14.3.7, linear clause]
4026 // A list item that appears in a linear clause is subject to the private
4027 // clause semantics described in Section 2.14.3.3 on page 159 except as
4028 // noted. In addition, the value of the new list item on each iteration
4029 // of the associated loop(s) corresponds to the value of the original
4030 // list item before entering the construct plus the logical number of
4031 // the iteration times linear-step.
4032
Alexey Bataeved09d242014-05-28 05:53:51 +00004033 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004034 // OpenMP [2.1, C/C++]
4035 // A list item is a variable name.
4036 // OpenMP [2.14.3.3, Restrictions, p.1]
4037 // A variable that is part of another variable (as an array or
4038 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004039 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004040 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004041 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004042 continue;
4043 }
4044
4045 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4046
4047 // OpenMP [2.14.3.7, linear clause]
4048 // A list-item cannot appear in more than one linear clause.
4049 // A list-item that appears in a linear clause cannot appear in any
4050 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004051 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004052 if (DVar.RefExpr) {
4053 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4054 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004055 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004056 continue;
4057 }
4058
4059 QualType QType = VD->getType();
4060 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4061 // It will be analyzed later.
4062 Vars.push_back(DE);
4063 continue;
4064 }
4065
4066 // A variable must not have an incomplete type or a reference type.
4067 if (RequireCompleteType(ELoc, QType,
4068 diag::err_omp_linear_incomplete_type)) {
4069 continue;
4070 }
4071 if (QType->isReferenceType()) {
4072 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4073 << getOpenMPClauseName(OMPC_linear) << QType;
4074 bool IsDecl =
4075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4076 Diag(VD->getLocation(),
4077 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4078 << VD;
4079 continue;
4080 }
4081
4082 // A list item must not be const-qualified.
4083 if (QType.isConstant(Context)) {
4084 Diag(ELoc, diag::err_omp_const_variable)
4085 << getOpenMPClauseName(OMPC_linear);
4086 bool IsDecl =
4087 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4088 Diag(VD->getLocation(),
4089 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4090 << VD;
4091 continue;
4092 }
4093
4094 // A list item must be of integral or pointer type.
4095 QType = QType.getUnqualifiedType().getCanonicalType();
4096 const Type *Ty = QType.getTypePtrOrNull();
4097 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4098 !Ty->isPointerType())) {
4099 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4100 bool IsDecl =
4101 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4102 Diag(VD->getLocation(),
4103 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4104 << VD;
4105 continue;
4106 }
4107
4108 DSAStack->addDSA(VD, DE, OMPC_linear);
4109 Vars.push_back(DE);
4110 }
4111
4112 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004113 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004114
4115 Expr *StepExpr = Step;
4116 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4117 !Step->isInstantiationDependent() &&
4118 !Step->containsUnexpandedParameterPack()) {
4119 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004120 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004121 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004122 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004123 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004124
4125 // Warn about zero linear step (it would be probably better specified as
4126 // making corresponding variables 'const').
4127 llvm::APSInt Result;
4128 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4129 !Result.isNegative() && !Result.isStrictlyPositive())
4130 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4131 << (Vars.size() > 1);
4132 }
4133
4134 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4135 Vars, StepExpr);
4136}
4137
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004138OMPClause *Sema::ActOnOpenMPAlignedClause(
4139 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4140 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4141
4142 SmallVector<Expr *, 8> Vars;
4143 for (auto &RefExpr : VarList) {
4144 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4145 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4146 // It will be analyzed later.
4147 Vars.push_back(RefExpr);
4148 continue;
4149 }
4150
4151 SourceLocation ELoc = RefExpr->getExprLoc();
4152 // OpenMP [2.1, C/C++]
4153 // A list item is a variable name.
4154 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4155 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4156 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4157 continue;
4158 }
4159
4160 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4161
4162 // OpenMP [2.8.1, simd construct, Restrictions]
4163 // The type of list items appearing in the aligned clause must be
4164 // array, pointer, reference to array, or reference to pointer.
4165 QualType QType = DE->getType()
4166 .getNonReferenceType()
4167 .getUnqualifiedType()
4168 .getCanonicalType();
4169 const Type *Ty = QType.getTypePtrOrNull();
4170 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4171 !Ty->isPointerType())) {
4172 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4173 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4174 bool IsDecl =
4175 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4176 Diag(VD->getLocation(),
4177 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4178 << VD;
4179 continue;
4180 }
4181
4182 // OpenMP [2.8.1, simd construct, Restrictions]
4183 // A list-item cannot appear in more than one aligned clause.
4184 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4185 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4186 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4187 << getOpenMPClauseName(OMPC_aligned);
4188 continue;
4189 }
4190
4191 Vars.push_back(DE);
4192 }
4193
4194 // OpenMP [2.8.1, simd construct, Description]
4195 // The parameter of the aligned clause, alignment, must be a constant
4196 // positive integer expression.
4197 // If no optional parameter is specified, implementation-defined default
4198 // alignments for SIMD instructions on the target platforms are assumed.
4199 if (Alignment != nullptr) {
4200 ExprResult AlignResult =
4201 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4202 if (AlignResult.isInvalid())
4203 return nullptr;
4204 Alignment = AlignResult.get();
4205 }
4206 if (Vars.empty())
4207 return nullptr;
4208
4209 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
4210 EndLoc, Vars, Alignment);
4211}
4212
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004213OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
4214 SourceLocation StartLoc,
4215 SourceLocation LParenLoc,
4216 SourceLocation EndLoc) {
4217 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004218 for (auto &RefExpr : VarList) {
4219 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
4220 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004221 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004222 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004223 continue;
4224 }
4225
Alexey Bataeved09d242014-05-28 05:53:51 +00004226 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004227 // OpenMP [2.1, C/C++]
4228 // A list item is a variable name.
4229 // OpenMP [2.14.4.1, Restrictions, p.1]
4230 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004231 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004232 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004233 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004234 continue;
4235 }
4236
4237 Decl *D = DE->getDecl();
4238 VarDecl *VD = cast<VarDecl>(D);
4239
4240 QualType Type = VD->getType();
4241 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4242 // It will be analyzed later.
4243 Vars.push_back(DE);
4244 continue;
4245 }
4246
4247 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4248 // A list item that appears in a copyin clause must be threadprivate.
4249 if (!DSAStack->isThreadPrivate(VD)) {
4250 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004251 << getOpenMPClauseName(OMPC_copyin)
4252 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004253 continue;
4254 }
4255
4256 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4257 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004258 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004259 // operator for the class type.
4260 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004261 CXXRecordDecl *RD =
4262 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004263 // FIXME This code must be replaced by actual assignment of the
4264 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004265 if (RD) {
4266 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4267 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004268 if (MD) {
4269 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4270 MD->isDeleted()) {
4271 Diag(ELoc, diag::err_omp_required_method)
4272 << getOpenMPClauseName(OMPC_copyin) << 2;
4273 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4274 VarDecl::DeclarationOnly;
4275 Diag(VD->getLocation(),
4276 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4277 << VD;
4278 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4279 continue;
4280 }
4281 MarkFunctionReferenced(ELoc, MD);
4282 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004283 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004284 }
4285
4286 DSAStack->addDSA(VD, DE, OMPC_copyin);
4287 Vars.push_back(DE);
4288 }
4289
Alexey Bataeved09d242014-05-28 05:53:51 +00004290 if (Vars.empty())
4291 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004292
4293 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4294}
4295
Alexey Bataevbae9a792014-06-27 10:37:06 +00004296OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4297 SourceLocation StartLoc,
4298 SourceLocation LParenLoc,
4299 SourceLocation EndLoc) {
4300 SmallVector<Expr *, 8> Vars;
4301 for (auto &RefExpr : VarList) {
4302 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4303 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4304 // It will be analyzed later.
4305 Vars.push_back(RefExpr);
4306 continue;
4307 }
4308
4309 SourceLocation ELoc = RefExpr->getExprLoc();
4310 // OpenMP [2.1, C/C++]
4311 // A list item is a variable name.
4312 // OpenMP [2.14.4.1, Restrictions, p.1]
4313 // A list item that appears in a copyin clause must be threadprivate.
4314 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4315 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4316 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4317 continue;
4318 }
4319
4320 Decl *D = DE->getDecl();
4321 VarDecl *VD = cast<VarDecl>(D);
4322
4323 QualType Type = VD->getType();
4324 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4325 // It will be analyzed later.
4326 Vars.push_back(DE);
4327 continue;
4328 }
4329
4330 // OpenMP [2.14.4.2, Restrictions, p.2]
4331 // A list item that appears in a copyprivate clause may not appear in a
4332 // private or firstprivate clause on the single construct.
4333 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004334 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004335 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4336 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4337 Diag(ELoc, diag::err_omp_wrong_dsa)
4338 << getOpenMPClauseName(DVar.CKind)
4339 << getOpenMPClauseName(OMPC_copyprivate);
4340 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4341 continue;
4342 }
4343
4344 // OpenMP [2.11.4.2, Restrictions, p.1]
4345 // All list items that appear in a copyprivate clause must be either
4346 // threadprivate or private in the enclosing context.
4347 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004348 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004349 if (DVar.CKind == OMPC_shared) {
4350 Diag(ELoc, diag::err_omp_required_access)
4351 << getOpenMPClauseName(OMPC_copyprivate)
4352 << "threadprivate or private in the enclosing context";
4353 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4354 continue;
4355 }
4356 }
4357 }
4358
4359 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4360 // A variable of class type (or array thereof) that appears in a
4361 // copyin clause requires an accessible, unambiguous copy assignment
4362 // operator for the class type.
4363 Type = Context.getBaseElementType(Type);
4364 CXXRecordDecl *RD =
4365 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
4366 // FIXME This code must be replaced by actual assignment of the
4367 // threadprivate variable.
4368 if (RD) {
4369 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4370 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4371 if (MD) {
4372 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4373 MD->isDeleted()) {
4374 Diag(ELoc, diag::err_omp_required_method)
4375 << getOpenMPClauseName(OMPC_copyprivate) << 2;
4376 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4377 VarDecl::DeclarationOnly;
4378 Diag(VD->getLocation(),
4379 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4380 << VD;
4381 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4382 continue;
4383 }
4384 MarkFunctionReferenced(ELoc, MD);
4385 DiagnoseUseOfDecl(MD, ELoc);
4386 }
4387 }
4388
4389 // No need to mark vars as copyprivate, they are already threadprivate or
4390 // implicitly private.
4391 Vars.push_back(DE);
4392 }
4393
4394 if (Vars.empty())
4395 return nullptr;
4396
4397 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4398}
4399
Alexey Bataev6125da92014-07-21 11:26:11 +00004400OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
4401 SourceLocation StartLoc,
4402 SourceLocation LParenLoc,
4403 SourceLocation EndLoc) {
4404 if (VarList.empty())
4405 return nullptr;
4406
4407 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
4408}
Alexey Bataevdea47612014-07-23 07:46:59 +00004409