blob: 30d873efcc6823c89bdc2d88bd50957ed13f7fe8 [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 Bataev0bd520b2014-09-19 08:19:49 +00001135 case OMPD_target: {
1136 Sema::CapturedParamNameType Params[] = {
1137 std::make_pair(StringRef(), QualType()) // __context with shared vars
1138 };
1139 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1140 Params);
1141 break;
1142 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001143 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001144 llvm_unreachable("OpenMP Directive is not allowed");
1145 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001146 llvm_unreachable("Unknown OpenMP directive");
1147 }
1148}
1149
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001150static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1151 OpenMPDirectiveKind CurrentRegion,
1152 const DeclarationNameInfo &CurrentName,
1153 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001154 // Allowed nesting of constructs
1155 // +------------------+-----------------+------------------------------------+
1156 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1157 // +------------------+-----------------+------------------------------------+
1158 // | parallel | parallel | * |
1159 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001160 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001161 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001162 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001163 // | parallel | simd | * |
1164 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001165 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001166 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001167 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001168 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001169 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001170 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001171 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001172 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001173 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001174 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001175 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001176 // | parallel | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001177 // +------------------+-----------------+------------------------------------+
1178 // | for | parallel | * |
1179 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001180 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001181 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001182 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001183 // | for | simd | * |
1184 // | for | sections | + |
1185 // | for | section | + |
1186 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001187 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001188 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001189 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001190 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001191 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001192 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001193 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001194 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001195 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001196 // | for | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001197 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001198 // | master | parallel | * |
1199 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001200 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001201 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001202 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001203 // | master | simd | * |
1204 // | master | sections | + |
1205 // | master | section | + |
1206 // | master | single | + |
1207 // | master | parallel for | * |
1208 // | master |parallel sections| * |
1209 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001210 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001211 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001212 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001213 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001214 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001215 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001216 // | master | target | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001217 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001218 // | critical | parallel | * |
1219 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001220 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001221 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001222 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001223 // | critical | simd | * |
1224 // | critical | sections | + |
1225 // | critical | section | + |
1226 // | critical | single | + |
1227 // | critical | parallel for | * |
1228 // | critical |parallel sections| * |
1229 // | critical | task | * |
1230 // | critical | taskyield | * |
1231 // | critical | barrier | + |
1232 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001233 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001234 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001235 // | critical | target | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001236 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001237 // | simd | parallel | |
1238 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001239 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001240 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001241 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001242 // | simd | simd | |
1243 // | simd | sections | |
1244 // | simd | section | |
1245 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001246 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001247 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001248 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001249 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001250 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001251 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001252 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001253 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001254 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001255 // | simd | target | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001256 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001257 // | for simd | parallel | |
1258 // | for simd | for | |
1259 // | for simd | for simd | |
1260 // | for simd | master | |
1261 // | for simd | critical | |
1262 // | for simd | simd | |
1263 // | for simd | sections | |
1264 // | for simd | section | |
1265 // | for simd | single | |
1266 // | for simd | parallel for | |
1267 // | for simd |parallel sections| |
1268 // | for simd | task | |
1269 // | for simd | taskyield | |
1270 // | for simd | barrier | |
1271 // | for simd | taskwait | |
1272 // | for simd | flush | |
1273 // | for simd | ordered | |
1274 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001275 // | for simd | target | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001276 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001277 // | sections | parallel | * |
1278 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001279 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001280 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001281 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001282 // | sections | simd | * |
1283 // | sections | sections | + |
1284 // | sections | section | * |
1285 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001286 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001287 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001288 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001289 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001290 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001291 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001292 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001293 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001294 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001295 // | sections | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001296 // +------------------+-----------------+------------------------------------+
1297 // | section | parallel | * |
1298 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001299 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001300 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001301 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001302 // | section | simd | * |
1303 // | section | sections | + |
1304 // | section | section | + |
1305 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001306 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001307 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001308 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001309 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001310 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001311 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001312 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001313 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001314 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001315 // | section | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001316 // +------------------+-----------------+------------------------------------+
1317 // | single | parallel | * |
1318 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001319 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001320 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001321 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001322 // | single | simd | * |
1323 // | single | sections | + |
1324 // | single | section | + |
1325 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001326 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001327 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001328 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001329 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001330 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001331 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001332 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001333 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001334 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001335 // | single | target | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001336 // +------------------+-----------------+------------------------------------+
1337 // | parallel for | parallel | * |
1338 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001339 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001340 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001341 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001342 // | parallel for | simd | * |
1343 // | parallel for | sections | + |
1344 // | parallel for | section | + |
1345 // | parallel for | single | + |
1346 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001347 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001348 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001349 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001350 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001351 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001352 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001353 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001354 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001355 // | parallel for | target | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001356 // +------------------+-----------------+------------------------------------+
1357 // | parallel sections| parallel | * |
1358 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001359 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001360 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001361 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001362 // | parallel sections| simd | * |
1363 // | parallel sections| sections | + |
1364 // | parallel sections| section | * |
1365 // | parallel sections| single | + |
1366 // | parallel sections| parallel for | * |
1367 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001368 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001369 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001370 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001371 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001372 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001373 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001374 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001375 // | parallel sections| target | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001376 // +------------------+-----------------+------------------------------------+
1377 // | task | parallel | * |
1378 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001379 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001380 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001381 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001382 // | task | simd | * |
1383 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001384 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001385 // | task | single | + |
1386 // | task | parallel for | * |
1387 // | task |parallel sections| * |
1388 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001389 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001390 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001391 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001392 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001393 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001394 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001395 // | task | target | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001396 // +------------------+-----------------+------------------------------------+
1397 // | ordered | parallel | * |
1398 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001399 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001400 // | ordered | master | * |
1401 // | ordered | critical | * |
1402 // | ordered | simd | * |
1403 // | ordered | sections | + |
1404 // | ordered | section | + |
1405 // | ordered | single | + |
1406 // | ordered | parallel for | * |
1407 // | ordered |parallel sections| * |
1408 // | ordered | task | * |
1409 // | ordered | taskyield | * |
1410 // | ordered | barrier | + |
1411 // | ordered | taskwait | * |
1412 // | ordered | flush | * |
1413 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001414 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001415 // | ordered | target | * |
1416 // +------------------+-----------------+------------------------------------+
1417 // | atomic | parallel | |
1418 // | atomic | for | |
1419 // | atomic | for simd | |
1420 // | atomic | master | |
1421 // | atomic | critical | |
1422 // | atomic | simd | |
1423 // | atomic | sections | |
1424 // | atomic | section | |
1425 // | atomic | single | |
1426 // | atomic | parallel for | |
1427 // | atomic |parallel sections| |
1428 // | atomic | task | |
1429 // | atomic | taskyield | |
1430 // | atomic | barrier | |
1431 // | atomic | taskwait | |
1432 // | atomic | flush | |
1433 // | atomic | ordered | |
1434 // | atomic | atomic | |
1435 // | atomic | target | |
1436 // +------------------+-----------------+------------------------------------+
1437 // | target | parallel | * |
1438 // | target | for | * |
1439 // | target | for simd | * |
1440 // | target | master | * |
1441 // | target | critical | * |
1442 // | target | simd | * |
1443 // | target | sections | * |
1444 // | target | section | * |
1445 // | target | single | * |
1446 // | target | parallel for | * |
1447 // | target |parallel sections| * |
1448 // | target | task | * |
1449 // | target | taskyield | * |
1450 // | target | barrier | * |
1451 // | target | taskwait | * |
1452 // | target | flush | * |
1453 // | target | ordered | * |
1454 // | target | atomic | * |
1455 // | target | target | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001456 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001457 if (Stack->getCurScope()) {
1458 auto ParentRegion = Stack->getParentDirective();
1459 bool NestingProhibited = false;
1460 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001461 enum {
1462 NoRecommend,
1463 ShouldBeInParallelRegion,
1464 ShouldBeInOrderedRegion
1465 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001466 if (isOpenMPSimdDirective(ParentRegion)) {
1467 // OpenMP [2.16, Nesting of Regions]
1468 // OpenMP constructs may not be nested inside a simd region.
1469 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1470 return true;
1471 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001472 if (ParentRegion == OMPD_atomic) {
1473 // OpenMP [2.16, Nesting of Regions]
1474 // OpenMP constructs may not be nested inside an atomic region.
1475 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1476 return true;
1477 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001478 if (CurrentRegion == OMPD_section) {
1479 // OpenMP [2.7.2, sections Construct, Restrictions]
1480 // Orphaned section directives are prohibited. That is, the section
1481 // directives must appear within the sections construct and must not be
1482 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001483 if (ParentRegion != OMPD_sections &&
1484 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001485 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1486 << (ParentRegion != OMPD_unknown)
1487 << getOpenMPDirectiveName(ParentRegion);
1488 return true;
1489 }
1490 return false;
1491 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001492 // Allow some constructs to be orphaned (they could be used in functions,
1493 // called from OpenMP regions with the required preconditions).
1494 if (ParentRegion == OMPD_unknown)
1495 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001496 if (CurrentRegion == OMPD_master) {
1497 // OpenMP [2.16, Nesting of Regions]
1498 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001499 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001500 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1501 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001502 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1503 // OpenMP [2.16, Nesting of Regions]
1504 // A critical region may not be nested (closely or otherwise) inside a
1505 // critical region with the same name. Note that this restriction is not
1506 // sufficient to prevent deadlock.
1507 SourceLocation PreviousCriticalLoc;
1508 bool DeadLock =
1509 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1510 OpenMPDirectiveKind K,
1511 const DeclarationNameInfo &DNI,
1512 SourceLocation Loc)
1513 ->bool {
1514 if (K == OMPD_critical &&
1515 DNI.getName() == CurrentName.getName()) {
1516 PreviousCriticalLoc = Loc;
1517 return true;
1518 } else
1519 return false;
1520 },
1521 false /* skip top directive */);
1522 if (DeadLock) {
1523 SemaRef.Diag(StartLoc,
1524 diag::err_omp_prohibited_region_critical_same_name)
1525 << CurrentName.getName();
1526 if (PreviousCriticalLoc.isValid())
1527 SemaRef.Diag(PreviousCriticalLoc,
1528 diag::note_omp_previous_critical_region);
1529 return true;
1530 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001531 } else if (CurrentRegion == OMPD_barrier) {
1532 // OpenMP [2.16, Nesting of Regions]
1533 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001534 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001535 NestingProhibited =
1536 isOpenMPWorksharingDirective(ParentRegion) ||
1537 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1538 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001539 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001540 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001541 // OpenMP [2.16, Nesting of Regions]
1542 // A worksharing region may not be closely nested inside a worksharing,
1543 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001544 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001545 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001546 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1547 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1548 Recommend = ShouldBeInParallelRegion;
1549 } else if (CurrentRegion == OMPD_ordered) {
1550 // OpenMP [2.16, Nesting of Regions]
1551 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001552 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001553 // An ordered region must be closely nested inside a loop region (or
1554 // parallel loop region) with an ordered clause.
1555 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001556 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001557 !Stack->isParentOrderedRegion();
1558 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001559 }
1560 if (NestingProhibited) {
1561 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001562 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1563 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001564 return true;
1565 }
1566 }
1567 return false;
1568}
1569
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001570StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001571 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001572 ArrayRef<OMPClause *> Clauses,
1573 Stmt *AStmt,
1574 SourceLocation StartLoc,
1575 SourceLocation EndLoc) {
1576 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001577 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001578 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001579
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001580 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001581 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001582 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001583 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001584 if (AStmt) {
1585 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1586
1587 // Check default data sharing attributes for referenced variables.
1588 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1589 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1590 if (DSAChecker.isErrorFound())
1591 return StmtError();
1592 // Generate list of implicitly defined firstprivate variables.
1593 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001594
1595 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1596 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1597 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1598 SourceLocation(), SourceLocation())) {
1599 ClausesWithImplicit.push_back(Implicit);
1600 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1601 DSAChecker.getImplicitFirstprivate().size();
1602 } else
1603 ErrorFound = true;
1604 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001605 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001606
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001607 switch (Kind) {
1608 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001609 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1610 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001611 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001612 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001613 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1614 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001615 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001616 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001617 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1618 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001619 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001620 case OMPD_for_simd:
1621 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1622 EndLoc, VarsWithInheritedDSA);
1623 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001624 case OMPD_sections:
1625 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1626 EndLoc);
1627 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001628 case OMPD_section:
1629 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001630 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001631 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1632 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001633 case OMPD_single:
1634 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1635 EndLoc);
1636 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001637 case OMPD_master:
1638 assert(ClausesWithImplicit.empty() &&
1639 "No clauses are allowed for 'omp master' directive");
1640 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1641 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001642 case OMPD_critical:
1643 assert(ClausesWithImplicit.empty() &&
1644 "No clauses are allowed for 'omp critical' directive");
1645 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1646 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001647 case OMPD_parallel_for:
1648 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1649 EndLoc, VarsWithInheritedDSA);
1650 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001651 case OMPD_parallel_sections:
1652 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1653 StartLoc, EndLoc);
1654 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001655 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001656 Res =
1657 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1658 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001659 case OMPD_taskyield:
1660 assert(ClausesWithImplicit.empty() &&
1661 "No clauses are allowed for 'omp taskyield' directive");
1662 assert(AStmt == nullptr &&
1663 "No associated statement allowed for 'omp taskyield' directive");
1664 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1665 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001666 case OMPD_barrier:
1667 assert(ClausesWithImplicit.empty() &&
1668 "No clauses are allowed for 'omp barrier' directive");
1669 assert(AStmt == nullptr &&
1670 "No associated statement allowed for 'omp barrier' directive");
1671 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1672 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001673 case OMPD_taskwait:
1674 assert(ClausesWithImplicit.empty() &&
1675 "No clauses are allowed for 'omp taskwait' directive");
1676 assert(AStmt == nullptr &&
1677 "No associated statement allowed for 'omp taskwait' directive");
1678 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1679 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001680 case OMPD_flush:
1681 assert(AStmt == nullptr &&
1682 "No associated statement allowed for 'omp flush' directive");
1683 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1684 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001685 case OMPD_ordered:
1686 assert(ClausesWithImplicit.empty() &&
1687 "No clauses are allowed for 'omp ordered' directive");
1688 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1689 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001690 case OMPD_atomic:
1691 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1692 EndLoc);
1693 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001694 case OMPD_target:
1695 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1696 EndLoc);
1697 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001698 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001699 llvm_unreachable("OpenMP Directive is not allowed");
1700 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001701 llvm_unreachable("Unknown OpenMP directive");
1702 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001703
Alexey Bataev4acb8592014-07-07 13:01:15 +00001704 for (auto P : VarsWithInheritedDSA) {
1705 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1706 << P.first << P.second->getSourceRange();
1707 }
1708 if (!VarsWithInheritedDSA.empty())
1709 return StmtError();
1710
Alexey Bataeved09d242014-05-28 05:53:51 +00001711 if (ErrorFound)
1712 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001713 return Res;
1714}
1715
1716StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1717 Stmt *AStmt,
1718 SourceLocation StartLoc,
1719 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001720 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1721 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1722 // 1.2.2 OpenMP Language Terminology
1723 // Structured block - An executable statement with a single entry at the
1724 // top and a single exit at the bottom.
1725 // The point of exit cannot be a branch out of the structured block.
1726 // longjmp() and throw() must not violate the entry/exit criteria.
1727 CS->getCapturedDecl()->setNothrow();
1728
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001729 getCurFunction()->setHasBranchProtectedScope();
1730
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001731 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1732 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001733}
1734
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001735namespace {
1736/// \brief Helper class for checking canonical form of the OpenMP loops and
1737/// extracting iteration space of each loop in the loop nest, that will be used
1738/// for IR generation.
1739class OpenMPIterationSpaceChecker {
1740 /// \brief Reference to Sema.
1741 Sema &SemaRef;
1742 /// \brief A location for diagnostics (when there is no some better location).
1743 SourceLocation DefaultLoc;
1744 /// \brief A location for diagnostics (when increment is not compatible).
1745 SourceLocation ConditionLoc;
1746 /// \brief A source location for referring to condition later.
1747 SourceRange ConditionSrcRange;
1748 /// \brief Loop variable.
1749 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001750 /// \brief Reference to loop variable.
1751 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001752 /// \brief Lower bound (initializer for the var).
1753 Expr *LB;
1754 /// \brief Upper bound.
1755 Expr *UB;
1756 /// \brief Loop step (increment).
1757 Expr *Step;
1758 /// \brief This flag is true when condition is one of:
1759 /// Var < UB
1760 /// Var <= UB
1761 /// UB > Var
1762 /// UB >= Var
1763 bool TestIsLessOp;
1764 /// \brief This flag is true when condition is strict ( < or > ).
1765 bool TestIsStrictOp;
1766 /// \brief This flag is true when step is subtracted on each iteration.
1767 bool SubtractStep;
1768
1769public:
1770 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1771 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001772 ConditionSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
1773 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1774 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001775 /// \brief Check init-expr for canonical loop form and save loop counter
1776 /// variable - #Var and its initialization value - #LB.
1777 bool CheckInit(Stmt *S);
1778 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1779 /// for less/greater and for strict/non-strict comparison.
1780 bool CheckCond(Expr *S);
1781 /// \brief Check incr-expr for canonical loop form and return true if it
1782 /// does not conform, otherwise save loop step (#Step).
1783 bool CheckInc(Expr *S);
1784 /// \brief Return the loop counter variable.
1785 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001786 /// \brief Return the reference expression to loop counter variable.
1787 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001788 /// \brief Return true if any expression is dependent.
1789 bool Dependent() const;
1790
1791private:
1792 /// \brief Check the right-hand side of an assignment in the increment
1793 /// expression.
1794 bool CheckIncRHS(Expr *RHS);
1795 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001796 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001797 /// \brief Helper to set upper bound.
1798 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1799 const SourceLocation &SL);
1800 /// \brief Helper to set loop increment.
1801 bool SetStep(Expr *NewStep, bool Subtract);
1802};
1803
1804bool OpenMPIterationSpaceChecker::Dependent() const {
1805 if (!Var) {
1806 assert(!LB && !UB && !Step);
1807 return false;
1808 }
1809 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1810 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1811}
1812
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001813bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1814 DeclRefExpr *NewVarRefExpr,
1815 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001816 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001817 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1818 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001819 if (!NewVar || !NewLB)
1820 return true;
1821 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001822 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001823 LB = NewLB;
1824 return false;
1825}
1826
1827bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1828 const SourceRange &SR,
1829 const SourceLocation &SL) {
1830 // State consistency checking to ensure correct usage.
1831 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1832 !TestIsLessOp && !TestIsStrictOp);
1833 if (!NewUB)
1834 return true;
1835 UB = NewUB;
1836 TestIsLessOp = LessOp;
1837 TestIsStrictOp = StrictOp;
1838 ConditionSrcRange = SR;
1839 ConditionLoc = SL;
1840 return false;
1841}
1842
1843bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1844 // State consistency checking to ensure correct usage.
1845 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1846 if (!NewStep)
1847 return true;
1848 if (!NewStep->isValueDependent()) {
1849 // Check that the step is integer expression.
1850 SourceLocation StepLoc = NewStep->getLocStart();
1851 ExprResult Val =
1852 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1853 if (Val.isInvalid())
1854 return true;
1855 NewStep = Val.get();
1856
1857 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1858 // If test-expr is of form var relational-op b and relational-op is < or
1859 // <= then incr-expr must cause var to increase on each iteration of the
1860 // loop. If test-expr is of form var relational-op b and relational-op is
1861 // > or >= then incr-expr must cause var to decrease on each iteration of
1862 // the loop.
1863 // If test-expr is of form b relational-op var and relational-op is < or
1864 // <= then incr-expr must cause var to decrease on each iteration of the
1865 // loop. If test-expr is of form b relational-op var and relational-op is
1866 // > or >= then incr-expr must cause var to increase on each iteration of
1867 // the loop.
1868 llvm::APSInt Result;
1869 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1870 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1871 bool IsConstNeg =
1872 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1873 bool IsConstZero = IsConstant && !Result.getBoolValue();
1874 if (UB && (IsConstZero ||
1875 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1876 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1877 SemaRef.Diag(NewStep->getExprLoc(),
1878 diag::err_omp_loop_incr_not_compatible)
1879 << Var << TestIsLessOp << NewStep->getSourceRange();
1880 SemaRef.Diag(ConditionLoc,
1881 diag::note_omp_loop_cond_requres_compatible_incr)
1882 << TestIsLessOp << ConditionSrcRange;
1883 return true;
1884 }
1885 }
1886
1887 Step = NewStep;
1888 SubtractStep = Subtract;
1889 return false;
1890}
1891
1892bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1893 // Check init-expr for canonical loop form and save loop counter
1894 // variable - #Var and its initialization value - #LB.
1895 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1896 // var = lb
1897 // integer-type var = lb
1898 // random-access-iterator-type var = lb
1899 // pointer-type var = lb
1900 //
1901 if (!S) {
1902 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1903 return true;
1904 }
1905 if (Expr *E = dyn_cast<Expr>(S))
1906 S = E->IgnoreParens();
1907 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1908 if (BO->getOpcode() == BO_Assign)
1909 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001910 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1911 BO->getLHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001912 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1913 if (DS->isSingleDecl()) {
1914 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1915 if (Var->hasInit()) {
1916 // Accept non-canonical init form here but emit ext. warning.
1917 if (Var->getInitStyle() != VarDecl::CInit)
1918 SemaRef.Diag(S->getLocStart(),
1919 diag::ext_omp_loop_not_canonical_init)
1920 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001921 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001922 }
1923 }
1924 }
1925 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1926 if (CE->getOperator() == OO_Equal)
1927 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001928 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
1929 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001930
1931 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1932 << S->getSourceRange();
1933 return true;
1934}
1935
Alexey Bataev23b69422014-06-18 07:08:49 +00001936/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001937/// variable (which may be the loop variable) if possible.
1938static const VarDecl *GetInitVarDecl(const Expr *E) {
1939 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001940 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001941 E = E->IgnoreParenImpCasts();
1942 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1943 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1944 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1945 CE->getArg(0) != nullptr)
1946 E = CE->getArg(0)->IgnoreParenImpCasts();
1947 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1948 if (!DRE)
1949 return nullptr;
1950 return dyn_cast<VarDecl>(DRE->getDecl());
1951}
1952
1953bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1954 // Check test-expr for canonical form, save upper-bound UB, flags for
1955 // less/greater and for strict/non-strict comparison.
1956 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1957 // var relational-op b
1958 // b relational-op var
1959 //
1960 if (!S) {
1961 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1962 return true;
1963 }
1964 S = S->IgnoreParenImpCasts();
1965 SourceLocation CondLoc = S->getLocStart();
1966 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1967 if (BO->isRelationalOp()) {
1968 if (GetInitVarDecl(BO->getLHS()) == Var)
1969 return SetUB(BO->getRHS(),
1970 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1971 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1972 BO->getSourceRange(), BO->getOperatorLoc());
1973 if (GetInitVarDecl(BO->getRHS()) == Var)
1974 return SetUB(BO->getLHS(),
1975 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1976 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1977 BO->getSourceRange(), BO->getOperatorLoc());
1978 }
1979 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1980 if (CE->getNumArgs() == 2) {
1981 auto Op = CE->getOperator();
1982 switch (Op) {
1983 case OO_Greater:
1984 case OO_GreaterEqual:
1985 case OO_Less:
1986 case OO_LessEqual:
1987 if (GetInitVarDecl(CE->getArg(0)) == Var)
1988 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1989 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1990 CE->getOperatorLoc());
1991 if (GetInitVarDecl(CE->getArg(1)) == Var)
1992 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1993 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1994 CE->getOperatorLoc());
1995 break;
1996 default:
1997 break;
1998 }
1999 }
2000 }
2001 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2002 << S->getSourceRange() << Var;
2003 return true;
2004}
2005
2006bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2007 // RHS of canonical loop form increment can be:
2008 // var + incr
2009 // incr + var
2010 // var - incr
2011 //
2012 RHS = RHS->IgnoreParenImpCasts();
2013 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2014 if (BO->isAdditiveOp()) {
2015 bool IsAdd = BO->getOpcode() == BO_Add;
2016 if (GetInitVarDecl(BO->getLHS()) == Var)
2017 return SetStep(BO->getRHS(), !IsAdd);
2018 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2019 return SetStep(BO->getLHS(), false);
2020 }
2021 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2022 bool IsAdd = CE->getOperator() == OO_Plus;
2023 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2024 if (GetInitVarDecl(CE->getArg(0)) == Var)
2025 return SetStep(CE->getArg(1), !IsAdd);
2026 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2027 return SetStep(CE->getArg(0), false);
2028 }
2029 }
2030 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2031 << RHS->getSourceRange() << Var;
2032 return true;
2033}
2034
2035bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2036 // Check incr-expr for canonical loop form and return true if it
2037 // does not conform.
2038 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2039 // ++var
2040 // var++
2041 // --var
2042 // var--
2043 // var += incr
2044 // var -= incr
2045 // var = var + incr
2046 // var = incr + var
2047 // var = var - incr
2048 //
2049 if (!S) {
2050 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2051 return true;
2052 }
2053 S = S->IgnoreParens();
2054 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2055 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2056 return SetStep(
2057 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2058 (UO->isDecrementOp() ? -1 : 1)).get(),
2059 false);
2060 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2061 switch (BO->getOpcode()) {
2062 case BO_AddAssign:
2063 case BO_SubAssign:
2064 if (GetInitVarDecl(BO->getLHS()) == Var)
2065 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2066 break;
2067 case BO_Assign:
2068 if (GetInitVarDecl(BO->getLHS()) == Var)
2069 return CheckIncRHS(BO->getRHS());
2070 break;
2071 default:
2072 break;
2073 }
2074 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2075 switch (CE->getOperator()) {
2076 case OO_PlusPlus:
2077 case OO_MinusMinus:
2078 if (GetInitVarDecl(CE->getArg(0)) == Var)
2079 return SetStep(
2080 SemaRef.ActOnIntegerConstant(
2081 CE->getLocStart(),
2082 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2083 false);
2084 break;
2085 case OO_PlusEqual:
2086 case OO_MinusEqual:
2087 if (GetInitVarDecl(CE->getArg(0)) == Var)
2088 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2089 break;
2090 case OO_Equal:
2091 if (GetInitVarDecl(CE->getArg(0)) == Var)
2092 return CheckIncRHS(CE->getArg(1));
2093 break;
2094 default:
2095 break;
2096 }
2097 }
2098 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2099 << S->getSourceRange() << Var;
2100 return true;
2101}
Alexey Bataev23b69422014-06-18 07:08:49 +00002102} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002103
2104/// \brief Called on a for stmt to check and extract its iteration space
2105/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002106static bool CheckOpenMPIterationSpace(
2107 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2108 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2109 Expr *NestedLoopCountExpr,
2110 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002111 // OpenMP [2.6, Canonical Loop Form]
2112 // for (init-expr; test-expr; incr-expr) structured-block
2113 auto For = dyn_cast_or_null<ForStmt>(S);
2114 if (!For) {
2115 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002116 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2117 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2118 << CurrentNestedLoopCount;
2119 if (NestedLoopCount > 1)
2120 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2121 diag::note_omp_collapse_expr)
2122 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002123 return true;
2124 }
2125 assert(For->getBody());
2126
2127 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2128
2129 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002130 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002131 if (ISC.CheckInit(Init)) {
2132 return true;
2133 }
2134
2135 bool HasErrors = false;
2136
2137 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002138 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002139
2140 // OpenMP [2.6, Canonical Loop Form]
2141 // Var is one of the following:
2142 // A variable of signed or unsigned integer type.
2143 // For C++, a variable of a random access iterator type.
2144 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002145 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002146 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2147 !VarType->isPointerType() &&
2148 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2149 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2150 << SemaRef.getLangOpts().CPlusPlus;
2151 HasErrors = true;
2152 }
2153
Alexey Bataev4acb8592014-07-07 13:01:15 +00002154 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2155 // Construct
2156 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2157 // parallel for construct is (are) private.
2158 // The loop iteration variable in the associated for-loop of a simd construct
2159 // with just one associated for-loop is linear with a constant-linear-step
2160 // that is the increment of the associated for-loop.
2161 // Exclude loop var from the list of variables with implicitly defined data
2162 // sharing attributes.
2163 while (VarsWithImplicitDSA.count(Var) > 0)
2164 VarsWithImplicitDSA.erase(Var);
2165
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002166 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2167 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002168 // The loop iteration variable in the associated for-loop of a simd construct
2169 // with just one associated for-loop may be listed in a linear clause with a
2170 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002171 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2172 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002173 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002174 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2175 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2176 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002177 auto PredeterminedCKind =
2178 isOpenMPSimdDirective(DKind)
2179 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2180 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002181 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002182 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002183 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2184 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2185 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002186 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002187 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002188 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2189 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002190 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002191 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002192 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002193 // Make the loop iteration variable private (for worksharing constructs),
2194 // linear (for simd directives with the only one associated loop) or
2195 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002196 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002197 }
2198
Alexey Bataev7ff55242014-06-19 09:13:45 +00002199 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002200
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002201 // Check test-expr.
2202 HasErrors |= ISC.CheckCond(For->getCond());
2203
2204 // Check incr-expr.
2205 HasErrors |= ISC.CheckInc(For->getInc());
2206
2207 if (ISC.Dependent())
2208 return HasErrors;
2209
2210 // FIXME: Build loop's iteration space representation.
2211 return HasErrors;
2212}
2213
2214/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
2215/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
2216/// to get the first for loop.
2217static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
2218 if (IgnoreCaptured)
2219 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
2220 S = CapS->getCapturedStmt();
2221 // OpenMP [2.8.1, simd construct, Restrictions]
2222 // All loops associated with the construct must be perfectly nested; that is,
2223 // there must be no intervening code nor any OpenMP directive between any two
2224 // loops.
2225 while (true) {
2226 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
2227 S = AS->getSubStmt();
2228 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
2229 if (CS->size() != 1)
2230 break;
2231 S = CS->body_back();
2232 } else
2233 break;
2234 }
2235 return S;
2236}
2237
2238/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002239/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2240/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002241static unsigned
2242CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2243 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
2244 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002245 unsigned NestedLoopCount = 1;
2246 if (NestedLoopCountExpr) {
2247 // Found 'collapse' clause - calculate collapse number.
2248 llvm::APSInt Result;
2249 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2250 NestedLoopCount = Result.getLimitedValue();
2251 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002252 // This is helper routine for loop directives (e.g., 'for', 'simd',
2253 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002254 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
2255 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002256 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002257 NestedLoopCount, NestedLoopCountExpr,
2258 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002259 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002260 // Move on to the next nested for loop, or to the loop body.
2261 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
2262 }
2263
2264 // FIXME: Build resulting iteration space for IR generation (collapsing
2265 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002266 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002267}
2268
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002269static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002270 auto CollapseFilter = [](const OMPClause *C) -> bool {
2271 return C->getClauseKind() == OMPC_collapse;
2272 };
2273 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2274 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002275 if (I)
2276 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2277 return nullptr;
2278}
2279
Alexey Bataev4acb8592014-07-07 13:01:15 +00002280StmtResult Sema::ActOnOpenMPSimdDirective(
2281 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2282 SourceLocation EndLoc,
2283 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002284 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002285 unsigned NestedLoopCount =
2286 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
2287 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002288 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002289 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002290
2291 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002292 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2293 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002294}
2295
Alexey Bataev4acb8592014-07-07 13:01:15 +00002296StmtResult Sema::ActOnOpenMPForDirective(
2297 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2298 SourceLocation EndLoc,
2299 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002300 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002301 unsigned NestedLoopCount =
2302 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
2303 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002304 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002305 return StmtError();
2306
2307 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00002308 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2309 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002310}
2311
Alexander Musmanf82886e2014-09-18 05:12:34 +00002312StmtResult Sema::ActOnOpenMPForSimdDirective(
2313 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2314 SourceLocation EndLoc,
2315 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2316 // In presence of clause 'collapse', it will define the nested loops number.
2317 unsigned NestedLoopCount =
2318 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
2319 *this, *DSAStack, VarsWithImplicitDSA);
2320 if (NestedLoopCount == 0)
2321 return StmtError();
2322
2323 getCurFunction()->setHasBranchProtectedScope();
2324 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2325 Clauses, AStmt);
2326}
2327
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002328StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2329 Stmt *AStmt,
2330 SourceLocation StartLoc,
2331 SourceLocation EndLoc) {
2332 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2333 auto BaseStmt = AStmt;
2334 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2335 BaseStmt = CS->getCapturedStmt();
2336 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2337 auto S = C->children();
2338 if (!S)
2339 return StmtError();
2340 // All associated statements must be '#pragma omp section' except for
2341 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002342 for (++S; S; ++S) {
2343 auto SectionStmt = *S;
2344 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2345 if (SectionStmt)
2346 Diag(SectionStmt->getLocStart(),
2347 diag::err_omp_sections_substmt_not_section);
2348 return StmtError();
2349 }
2350 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002351 } else {
2352 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2353 return StmtError();
2354 }
2355
2356 getCurFunction()->setHasBranchProtectedScope();
2357
2358 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2359 AStmt);
2360}
2361
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002362StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2363 SourceLocation StartLoc,
2364 SourceLocation EndLoc) {
2365 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2366
2367 getCurFunction()->setHasBranchProtectedScope();
2368
2369 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
2370}
2371
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002372StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
2373 Stmt *AStmt,
2374 SourceLocation StartLoc,
2375 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00002376 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2377
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002378 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00002379
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002380 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2381}
2382
Alexander Musman80c22892014-07-17 08:54:58 +00002383StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
2384 SourceLocation StartLoc,
2385 SourceLocation EndLoc) {
2386 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2387
2388 getCurFunction()->setHasBranchProtectedScope();
2389
2390 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
2391}
2392
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002393StmtResult
2394Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
2395 Stmt *AStmt, SourceLocation StartLoc,
2396 SourceLocation EndLoc) {
2397 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2398
2399 getCurFunction()->setHasBranchProtectedScope();
2400
2401 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
2402 AStmt);
2403}
2404
Alexey Bataev4acb8592014-07-07 13:01:15 +00002405StmtResult Sema::ActOnOpenMPParallelForDirective(
2406 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2407 SourceLocation EndLoc,
2408 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
2409 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2410 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2411 // 1.2.2 OpenMP Language Terminology
2412 // Structured block - An executable statement with a single entry at the
2413 // top and a single exit at the bottom.
2414 // The point of exit cannot be a branch out of the structured block.
2415 // longjmp() and throw() must not violate the entry/exit criteria.
2416 CS->getCapturedDecl()->setNothrow();
2417
2418 // In presence of clause 'collapse', it will define the nested loops number.
2419 unsigned NestedLoopCount =
2420 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2421 *this, *DSAStack, VarsWithImplicitDSA);
2422 if (NestedLoopCount == 0)
2423 return StmtError();
2424
2425 getCurFunction()->setHasBranchProtectedScope();
2426 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2427 NestedLoopCount, Clauses, AStmt);
2428}
2429
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002430StmtResult
2431Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2432 Stmt *AStmt, SourceLocation StartLoc,
2433 SourceLocation EndLoc) {
2434 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2435 auto BaseStmt = AStmt;
2436 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2437 BaseStmt = CS->getCapturedStmt();
2438 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2439 auto S = C->children();
2440 if (!S)
2441 return StmtError();
2442 // All associated statements must be '#pragma omp section' except for
2443 // the first one.
2444 for (++S; S; ++S) {
2445 auto SectionStmt = *S;
2446 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2447 if (SectionStmt)
2448 Diag(SectionStmt->getLocStart(),
2449 diag::err_omp_parallel_sections_substmt_not_section);
2450 return StmtError();
2451 }
2452 }
2453 } else {
2454 Diag(AStmt->getLocStart(),
2455 diag::err_omp_parallel_sections_not_compound_stmt);
2456 return StmtError();
2457 }
2458
2459 getCurFunction()->setHasBranchProtectedScope();
2460
2461 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2462 Clauses, AStmt);
2463}
2464
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002465StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2466 Stmt *AStmt, SourceLocation StartLoc,
2467 SourceLocation EndLoc) {
2468 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2469 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2470 // 1.2.2 OpenMP Language Terminology
2471 // Structured block - An executable statement with a single entry at the
2472 // top and a single exit at the bottom.
2473 // The point of exit cannot be a branch out of the structured block.
2474 // longjmp() and throw() must not violate the entry/exit criteria.
2475 CS->getCapturedDecl()->setNothrow();
2476
2477 getCurFunction()->setHasBranchProtectedScope();
2478
2479 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2480}
2481
Alexey Bataev68446b72014-07-18 07:47:19 +00002482StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2483 SourceLocation EndLoc) {
2484 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2485}
2486
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002487StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
2488 SourceLocation EndLoc) {
2489 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
2490}
2491
Alexey Bataev2df347a2014-07-18 10:17:07 +00002492StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
2493 SourceLocation EndLoc) {
2494 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
2495}
2496
Alexey Bataev6125da92014-07-21 11:26:11 +00002497StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
2498 SourceLocation StartLoc,
2499 SourceLocation EndLoc) {
2500 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
2501 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
2502}
2503
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002504StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
2505 SourceLocation StartLoc,
2506 SourceLocation EndLoc) {
2507 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2508
2509 getCurFunction()->setHasBranchProtectedScope();
2510
2511 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
2512}
2513
Alexey Bataev0162e452014-07-22 10:10:35 +00002514StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
2515 Stmt *AStmt,
2516 SourceLocation StartLoc,
2517 SourceLocation EndLoc) {
2518 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002519 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00002520 // 1.2.2 OpenMP Language Terminology
2521 // Structured block - An executable statement with a single entry at the
2522 // top and a single exit at the bottom.
2523 // The point of exit cannot be a branch out of the structured block.
2524 // longjmp() and throw() must not violate the entry/exit criteria.
2525 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00002526 OpenMPClauseKind AtomicKind = OMPC_unknown;
2527 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002528 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00002529 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00002530 C->getClauseKind() == OMPC_update ||
2531 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00002532 if (AtomicKind != OMPC_unknown) {
2533 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
2534 << SourceRange(C->getLocStart(), C->getLocEnd());
2535 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
2536 << getOpenMPClauseName(AtomicKind);
2537 } else {
2538 AtomicKind = C->getClauseKind();
2539 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002540 }
2541 }
2542 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002543 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00002544 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002545 if (!isa<Expr>(Body)) {
2546 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002547 diag::err_omp_atomic_read_not_expression_statement);
2548 return StmtError();
2549 }
2550 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002551 if (!isa<Expr>(Body)) {
2552 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00002553 diag::err_omp_atomic_write_not_expression_statement);
2554 return StmtError();
2555 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00002556 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00002557 if (!isa<Expr>(Body)) {
2558 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00002559 diag::err_omp_atomic_update_not_expression_statement)
2560 << (AtomicKind == OMPC_update);
2561 return StmtError();
2562 }
Alexey Bataev459dec02014-07-24 06:46:57 +00002563 } else if (AtomicKind == OMPC_capture) {
2564 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
2565 Diag(Body->getLocStart(),
2566 diag::err_omp_atomic_capture_not_expression_statement);
2567 return StmtError();
2568 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
2569 Diag(Body->getLocStart(),
2570 diag::err_omp_atomic_capture_not_compound_statement);
2571 return StmtError();
2572 }
Alexey Bataevdea47612014-07-23 07:46:59 +00002573 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002574
2575 getCurFunction()->setHasBranchProtectedScope();
2576
2577 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2578}
2579
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002580StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
2581 Stmt *AStmt,
2582 SourceLocation StartLoc,
2583 SourceLocation EndLoc) {
2584 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2585
2586 getCurFunction()->setHasBranchProtectedScope();
2587
2588 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2589}
2590
Alexey Bataeved09d242014-05-28 05:53:51 +00002591OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002592 SourceLocation StartLoc,
2593 SourceLocation LParenLoc,
2594 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002595 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002596 switch (Kind) {
2597 case OMPC_if:
2598 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2599 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002600 case OMPC_final:
2601 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2602 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002603 case OMPC_num_threads:
2604 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2605 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002606 case OMPC_safelen:
2607 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2608 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002609 case OMPC_collapse:
2610 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2611 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002612 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002613 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002614 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002615 case OMPC_private:
2616 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002617 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002618 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002619 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002620 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002621 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002622 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002623 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002624 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002625 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002626 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002627 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002628 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002629 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002630 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002631 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002632 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002633 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002634 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002635 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002636 llvm_unreachable("Clause is not allowed.");
2637 }
2638 return Res;
2639}
2640
Alexey Bataeved09d242014-05-28 05:53:51 +00002641OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002642 SourceLocation LParenLoc,
2643 SourceLocation EndLoc) {
2644 Expr *ValExpr = Condition;
2645 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2646 !Condition->isInstantiationDependent() &&
2647 !Condition->containsUnexpandedParameterPack()) {
2648 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002649 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002650 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002651 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002652
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002653 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002654 }
2655
2656 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2657}
2658
Alexey Bataev3778b602014-07-17 07:32:53 +00002659OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2660 SourceLocation StartLoc,
2661 SourceLocation LParenLoc,
2662 SourceLocation EndLoc) {
2663 Expr *ValExpr = Condition;
2664 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2665 !Condition->isInstantiationDependent() &&
2666 !Condition->containsUnexpandedParameterPack()) {
2667 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2668 Condition->getExprLoc(), Condition);
2669 if (Val.isInvalid())
2670 return nullptr;
2671
2672 ValExpr = Val.get();
2673 }
2674
2675 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2676}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002677ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2678 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002679 if (!Op)
2680 return ExprError();
2681
2682 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2683 public:
2684 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002685 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002686 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2687 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002688 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2689 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002690 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2691 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002692 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2693 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002694 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2695 QualType T,
2696 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002697 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2698 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002699 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2700 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002701 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002702 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002703 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002704 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2705 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002706 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2707 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002708 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2709 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002710 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002711 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002712 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002713 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2714 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002715 llvm_unreachable("conversion functions are permitted");
2716 }
2717 } ConvertDiagnoser;
2718 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2719}
2720
2721OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2722 SourceLocation StartLoc,
2723 SourceLocation LParenLoc,
2724 SourceLocation EndLoc) {
2725 Expr *ValExpr = NumThreads;
2726 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2727 !NumThreads->isInstantiationDependent() &&
2728 !NumThreads->containsUnexpandedParameterPack()) {
2729 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2730 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002731 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002732 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002733 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002734
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002735 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002736
2737 // OpenMP [2.5, Restrictions]
2738 // The num_threads expression must evaluate to a positive integer value.
2739 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002740 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2741 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002742 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2743 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002744 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002745 }
2746 }
2747
Alexey Bataeved09d242014-05-28 05:53:51 +00002748 return new (Context)
2749 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002750}
2751
Alexey Bataev62c87d22014-03-21 04:51:18 +00002752ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2753 OpenMPClauseKind CKind) {
2754 if (!E)
2755 return ExprError();
2756 if (E->isValueDependent() || E->isTypeDependent() ||
2757 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002758 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002759 llvm::APSInt Result;
2760 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2761 if (ICE.isInvalid())
2762 return ExprError();
2763 if (!Result.isStrictlyPositive()) {
2764 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2765 << getOpenMPClauseName(CKind) << E->getSourceRange();
2766 return ExprError();
2767 }
2768 return ICE;
2769}
2770
2771OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2772 SourceLocation LParenLoc,
2773 SourceLocation EndLoc) {
2774 // OpenMP [2.8.1, simd construct, Description]
2775 // The parameter of the safelen clause must be a constant
2776 // positive integer expression.
2777 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2778 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002779 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002780 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002781 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002782}
2783
Alexander Musman64d33f12014-06-04 07:53:32 +00002784OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2785 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002786 SourceLocation LParenLoc,
2787 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002788 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002789 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002790 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002791 // The parameter of the collapse clause must be a constant
2792 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002793 ExprResult NumForLoopsResult =
2794 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2795 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002796 return nullptr;
2797 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002798 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002799}
2800
Alexey Bataeved09d242014-05-28 05:53:51 +00002801OMPClause *Sema::ActOnOpenMPSimpleClause(
2802 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2803 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002804 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002805 switch (Kind) {
2806 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002807 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002808 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2809 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002810 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002811 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002812 Res = ActOnOpenMPProcBindClause(
2813 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2814 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002815 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002816 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002817 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002818 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002819 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002820 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002821 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002822 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002823 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002824 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002825 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002826 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002827 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002828 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002829 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002830 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002831 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002832 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002833 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002834 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002835 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002836 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002837 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002838 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002839 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002840 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002841 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002842 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002843 llvm_unreachable("Clause is not allowed.");
2844 }
2845 return Res;
2846}
2847
2848OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2849 SourceLocation KindKwLoc,
2850 SourceLocation StartLoc,
2851 SourceLocation LParenLoc,
2852 SourceLocation EndLoc) {
2853 if (Kind == OMPC_DEFAULT_unknown) {
2854 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002855 static_assert(OMPC_DEFAULT_unknown > 0,
2856 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002857 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002858 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002859 Values += "'";
2860 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2861 Values += "'";
2862 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002863 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002864 Values += " or ";
2865 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002866 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002867 break;
2868 default:
2869 Values += Sep;
2870 break;
2871 }
2872 }
2873 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002874 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002875 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002876 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002877 switch (Kind) {
2878 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002879 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002880 break;
2881 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002882 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002883 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002884 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002885 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002886 break;
2887 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002888 return new (Context)
2889 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002890}
2891
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002892OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2893 SourceLocation KindKwLoc,
2894 SourceLocation StartLoc,
2895 SourceLocation LParenLoc,
2896 SourceLocation EndLoc) {
2897 if (Kind == OMPC_PROC_BIND_unknown) {
2898 std::string Values;
2899 std::string Sep(", ");
2900 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2901 Values += "'";
2902 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2903 Values += "'";
2904 switch (i) {
2905 case OMPC_PROC_BIND_unknown - 2:
2906 Values += " or ";
2907 break;
2908 case OMPC_PROC_BIND_unknown - 1:
2909 break;
2910 default:
2911 Values += Sep;
2912 break;
2913 }
2914 }
2915 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002916 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002917 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002918 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002919 return new (Context)
2920 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002921}
2922
Alexey Bataev56dafe82014-06-20 07:16:17 +00002923OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2924 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2925 SourceLocation StartLoc, SourceLocation LParenLoc,
2926 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2927 SourceLocation EndLoc) {
2928 OMPClause *Res = nullptr;
2929 switch (Kind) {
2930 case OMPC_schedule:
2931 Res = ActOnOpenMPScheduleClause(
2932 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2933 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2934 break;
2935 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002936 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002937 case OMPC_num_threads:
2938 case OMPC_safelen:
2939 case OMPC_collapse:
2940 case OMPC_default:
2941 case OMPC_proc_bind:
2942 case OMPC_private:
2943 case OMPC_firstprivate:
2944 case OMPC_lastprivate:
2945 case OMPC_shared:
2946 case OMPC_reduction:
2947 case OMPC_linear:
2948 case OMPC_aligned:
2949 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002950 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002951 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002952 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002953 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002954 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002955 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00002956 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00002957 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00002958 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00002959 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00002960 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00002961 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002962 case OMPC_unknown:
2963 llvm_unreachable("Clause is not allowed.");
2964 }
2965 return Res;
2966}
2967
2968OMPClause *Sema::ActOnOpenMPScheduleClause(
2969 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2970 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2971 SourceLocation EndLoc) {
2972 if (Kind == OMPC_SCHEDULE_unknown) {
2973 std::string Values;
2974 std::string Sep(", ");
2975 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2976 Values += "'";
2977 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2978 Values += "'";
2979 switch (i) {
2980 case OMPC_SCHEDULE_unknown - 2:
2981 Values += " or ";
2982 break;
2983 case OMPC_SCHEDULE_unknown - 1:
2984 break;
2985 default:
2986 Values += Sep;
2987 break;
2988 }
2989 }
2990 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2991 << Values << getOpenMPClauseName(OMPC_schedule);
2992 return nullptr;
2993 }
2994 Expr *ValExpr = ChunkSize;
2995 if (ChunkSize) {
2996 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2997 !ChunkSize->isInstantiationDependent() &&
2998 !ChunkSize->containsUnexpandedParameterPack()) {
2999 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3000 ExprResult Val =
3001 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3002 if (Val.isInvalid())
3003 return nullptr;
3004
3005 ValExpr = Val.get();
3006
3007 // OpenMP [2.7.1, Restrictions]
3008 // chunk_size must be a loop invariant integer expression with a positive
3009 // value.
3010 llvm::APSInt Result;
3011 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3012 Result.isSigned() && !Result.isStrictlyPositive()) {
3013 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3014 << "schedule" << ChunkSize->getSourceRange();
3015 return nullptr;
3016 }
3017 }
3018 }
3019
3020 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3021 EndLoc, Kind, ValExpr);
3022}
3023
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003024OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3025 SourceLocation StartLoc,
3026 SourceLocation EndLoc) {
3027 OMPClause *Res = nullptr;
3028 switch (Kind) {
3029 case OMPC_ordered:
3030 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3031 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003032 case OMPC_nowait:
3033 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3034 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003035 case OMPC_untied:
3036 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3037 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003038 case OMPC_mergeable:
3039 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3040 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003041 case OMPC_read:
3042 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3043 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003044 case OMPC_write:
3045 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3046 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003047 case OMPC_update:
3048 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3049 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003050 case OMPC_capture:
3051 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3052 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003053 case OMPC_seq_cst:
3054 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3055 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003056 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003057 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003058 case OMPC_num_threads:
3059 case OMPC_safelen:
3060 case OMPC_collapse:
3061 case OMPC_schedule:
3062 case OMPC_private:
3063 case OMPC_firstprivate:
3064 case OMPC_lastprivate:
3065 case OMPC_shared:
3066 case OMPC_reduction:
3067 case OMPC_linear:
3068 case OMPC_aligned:
3069 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003070 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003071 case OMPC_default:
3072 case OMPC_proc_bind:
3073 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003074 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003075 case OMPC_unknown:
3076 llvm_unreachable("Clause is not allowed.");
3077 }
3078 return Res;
3079}
3080
3081OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3082 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003083 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003084 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3085}
3086
Alexey Bataev236070f2014-06-20 11:19:47 +00003087OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3088 SourceLocation EndLoc) {
3089 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3090}
3091
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003092OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3093 SourceLocation EndLoc) {
3094 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3095}
3096
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003097OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3098 SourceLocation EndLoc) {
3099 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3100}
3101
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003102OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3103 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003104 return new (Context) OMPReadClause(StartLoc, EndLoc);
3105}
3106
Alexey Bataevdea47612014-07-23 07:46:59 +00003107OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3108 SourceLocation EndLoc) {
3109 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3110}
3111
Alexey Bataev67a4f222014-07-23 10:25:33 +00003112OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3113 SourceLocation EndLoc) {
3114 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3115}
3116
Alexey Bataev459dec02014-07-24 06:46:57 +00003117OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3118 SourceLocation EndLoc) {
3119 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3120}
3121
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003122OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3123 SourceLocation EndLoc) {
3124 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3125}
3126
Alexey Bataevc5e02582014-06-16 07:08:35 +00003127OMPClause *Sema::ActOnOpenMPVarListClause(
3128 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3129 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3130 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3131 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003132 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003133 switch (Kind) {
3134 case OMPC_private:
3135 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3136 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003137 case OMPC_firstprivate:
3138 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3139 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003140 case OMPC_lastprivate:
3141 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3142 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003143 case OMPC_shared:
3144 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3145 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003146 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003147 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3148 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003149 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003150 case OMPC_linear:
3151 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3152 ColonLoc, EndLoc);
3153 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003154 case OMPC_aligned:
3155 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3156 ColonLoc, EndLoc);
3157 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003158 case OMPC_copyin:
3159 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3160 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003161 case OMPC_copyprivate:
3162 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3163 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003164 case OMPC_flush:
3165 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3166 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003167 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003168 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003169 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003170 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003171 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003172 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003173 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003174 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003175 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003176 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003177 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003178 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003179 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003180 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003181 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003182 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003183 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003184 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003185 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003186 llvm_unreachable("Clause is not allowed.");
3187 }
3188 return Res;
3189}
3190
3191OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3192 SourceLocation StartLoc,
3193 SourceLocation LParenLoc,
3194 SourceLocation EndLoc) {
3195 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003196 for (auto &RefExpr : VarList) {
3197 assert(RefExpr && "NULL expr in OpenMP private clause.");
3198 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003199 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003200 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003201 continue;
3202 }
3203
Alexey Bataeved09d242014-05-28 05:53:51 +00003204 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003205 // OpenMP [2.1, C/C++]
3206 // A list item is a variable name.
3207 // OpenMP [2.9.3.3, Restrictions, p.1]
3208 // A variable that is part of another variable (as an array or
3209 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003210 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003211 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003212 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003213 continue;
3214 }
3215 Decl *D = DE->getDecl();
3216 VarDecl *VD = cast<VarDecl>(D);
3217
3218 QualType Type = VD->getType();
3219 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3220 // It will be analyzed later.
3221 Vars.push_back(DE);
3222 continue;
3223 }
3224
3225 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3226 // A variable that appears in a private clause must not have an incomplete
3227 // type or a reference type.
3228 if (RequireCompleteType(ELoc, Type,
3229 diag::err_omp_private_incomplete_type)) {
3230 continue;
3231 }
3232 if (Type->isReferenceType()) {
3233 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003234 << getOpenMPClauseName(OMPC_private) << Type;
3235 bool IsDecl =
3236 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3237 Diag(VD->getLocation(),
3238 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3239 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003240 continue;
3241 }
3242
3243 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3244 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003245 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003246 // class type.
3247 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003248 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3249 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003250 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003251 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3252 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3253 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003254 // FIXME This code must be replaced by actual constructing/destructing of
3255 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003256 if (RD) {
3257 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3258 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003259 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003260 if (!CD ||
3261 CheckConstructorAccess(ELoc, CD,
3262 InitializedEntity::InitializeTemporary(Type),
3263 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003264 CD->isDeleted()) {
3265 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003266 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003267 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3268 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003269 Diag(VD->getLocation(),
3270 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3271 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003272 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3273 continue;
3274 }
3275 MarkFunctionReferenced(ELoc, CD);
3276 DiagnoseUseOfDecl(CD, ELoc);
3277
3278 CXXDestructorDecl *DD = RD->getDestructor();
3279 if (DD) {
3280 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3281 DD->isDeleted()) {
3282 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003283 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003284 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3285 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003286 Diag(VD->getLocation(),
3287 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3288 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003289 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3290 continue;
3291 }
3292 MarkFunctionReferenced(ELoc, DD);
3293 DiagnoseUseOfDecl(DD, ELoc);
3294 }
3295 }
3296
Alexey Bataev758e55e2013-09-06 18:03:48 +00003297 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3298 // in a Construct]
3299 // Variables with the predetermined data-sharing attributes may not be
3300 // listed in data-sharing attributes clauses, except for the cases
3301 // listed below. For these exceptions only, listing a predetermined
3302 // variable in a data-sharing attribute clause is allowed and overrides
3303 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003304 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003305 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003306 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3307 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003308 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003309 continue;
3310 }
3311
3312 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003313 Vars.push_back(DE);
3314 }
3315
Alexey Bataeved09d242014-05-28 05:53:51 +00003316 if (Vars.empty())
3317 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003318
3319 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3320}
3321
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003322OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
3323 SourceLocation StartLoc,
3324 SourceLocation LParenLoc,
3325 SourceLocation EndLoc) {
3326 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003327 bool IsImplicitClause =
3328 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
3329 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
3330
Alexey Bataeved09d242014-05-28 05:53:51 +00003331 for (auto &RefExpr : VarList) {
3332 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
3333 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003334 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003335 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003336 continue;
3337 }
3338
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003339 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
3340 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003341 // OpenMP [2.1, C/C++]
3342 // A list item is a variable name.
3343 // OpenMP [2.9.3.3, Restrictions, p.1]
3344 // A variable that is part of another variable (as an array or
3345 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003346 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003347 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003348 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003349 continue;
3350 }
3351 Decl *D = DE->getDecl();
3352 VarDecl *VD = cast<VarDecl>(D);
3353
3354 QualType Type = VD->getType();
3355 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3356 // It will be analyzed later.
3357 Vars.push_back(DE);
3358 continue;
3359 }
3360
3361 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3362 // A variable that appears in a private clause must not have an incomplete
3363 // type or a reference type.
3364 if (RequireCompleteType(ELoc, Type,
3365 diag::err_omp_firstprivate_incomplete_type)) {
3366 continue;
3367 }
3368 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003369 if (IsImplicitClause) {
3370 Diag(ImplicitClauseLoc,
3371 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
3372 << Type;
3373 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3374 } else {
3375 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3376 << getOpenMPClauseName(OMPC_firstprivate) << Type;
3377 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003378 bool IsDecl =
3379 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3380 Diag(VD->getLocation(),
3381 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3382 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003383 continue;
3384 }
3385
3386 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
3387 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003388 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003389 // class type.
3390 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003391 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3392 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3393 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003394 // FIXME This code must be replaced by actual constructing/destructing of
3395 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003396 if (RD) {
3397 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
3398 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003399 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003400 if (!CD ||
3401 CheckConstructorAccess(ELoc, CD,
3402 InitializedEntity::InitializeTemporary(Type),
3403 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003404 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003405 if (IsImplicitClause) {
3406 Diag(ImplicitClauseLoc,
3407 diag::err_omp_task_predetermined_firstprivate_required_method)
3408 << 0;
3409 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3410 } else {
3411 Diag(ELoc, diag::err_omp_required_method)
3412 << getOpenMPClauseName(OMPC_firstprivate) << 1;
3413 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003414 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3415 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003416 Diag(VD->getLocation(),
3417 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3418 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003419 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3420 continue;
3421 }
3422 MarkFunctionReferenced(ELoc, CD);
3423 DiagnoseUseOfDecl(CD, ELoc);
3424
3425 CXXDestructorDecl *DD = RD->getDestructor();
3426 if (DD) {
3427 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3428 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003429 if (IsImplicitClause) {
3430 Diag(ImplicitClauseLoc,
3431 diag::err_omp_task_predetermined_firstprivate_required_method)
3432 << 1;
3433 Diag(RefExpr->getExprLoc(), diag::note_used_here);
3434 } else {
3435 Diag(ELoc, diag::err_omp_required_method)
3436 << getOpenMPClauseName(OMPC_firstprivate) << 4;
3437 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003438 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3439 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003440 Diag(VD->getLocation(),
3441 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3442 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003443 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3444 continue;
3445 }
3446 MarkFunctionReferenced(ELoc, DD);
3447 DiagnoseUseOfDecl(DD, ELoc);
3448 }
3449 }
3450
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003451 // If an implicit firstprivate variable found it was checked already.
3452 if (!IsImplicitClause) {
3453 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003454 Type = Type.getNonReferenceType().getCanonicalType();
3455 bool IsConstant = Type.isConstant(Context);
3456 Type = Context.getBaseElementType(Type);
3457 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
3458 // A list item that specifies a given variable may not appear in more
3459 // than one clause on the same directive, except that a variable may be
3460 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003461 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00003462 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003463 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003464 << getOpenMPClauseName(DVar.CKind)
3465 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003466 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003467 continue;
3468 }
3469
3470 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3471 // in a Construct]
3472 // Variables with the predetermined data-sharing attributes may not be
3473 // listed in data-sharing attributes clauses, except for the cases
3474 // listed below. For these exceptions only, listing a predetermined
3475 // variable in a data-sharing attribute clause is allowed and overrides
3476 // the variable's predetermined data-sharing attributes.
3477 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3478 // in a Construct, C/C++, p.2]
3479 // Variables with const-qualified type having no mutable member may be
3480 // listed in a firstprivate clause, even if they are static data members.
3481 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
3482 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
3483 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00003484 << getOpenMPClauseName(DVar.CKind)
3485 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003486 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003487 continue;
3488 }
3489
Alexey Bataevf29276e2014-06-18 04:14:57 +00003490 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003491 // OpenMP [2.9.3.4, Restrictions, p.2]
3492 // A list item that is private within a parallel region must not appear
3493 // in a firstprivate clause on a worksharing construct if any of the
3494 // worksharing regions arising from the worksharing construct ever bind
3495 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00003496 if (isOpenMPWorksharingDirective(CurrDir) &&
3497 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003498 DVar = DSAStack->getImplicitDSA(VD, true);
3499 if (DVar.CKind != OMPC_shared &&
3500 (isOpenMPParallelDirective(DVar.DKind) ||
3501 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003502 Diag(ELoc, diag::err_omp_required_access)
3503 << getOpenMPClauseName(OMPC_firstprivate)
3504 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003505 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003506 continue;
3507 }
3508 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003509 // OpenMP [2.9.3.4, Restrictions, p.3]
3510 // A list item that appears in a reduction clause of a parallel construct
3511 // must not appear in a firstprivate clause on a worksharing or task
3512 // construct if any of the worksharing or task regions arising from the
3513 // worksharing or task construct ever bind to any of the parallel regions
3514 // arising from the parallel construct.
3515 // OpenMP [2.9.3.4, Restrictions, p.4]
3516 // A list item that appears in a reduction clause in worksharing
3517 // construct must not appear in a firstprivate clause in a task construct
3518 // encountered during execution of any of the worksharing regions arising
3519 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003520 if (CurrDir == OMPD_task) {
3521 DVar =
3522 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
3523 [](OpenMPDirectiveKind K) -> bool {
3524 return isOpenMPParallelDirective(K) ||
3525 isOpenMPWorksharingDirective(K);
3526 },
3527 false);
3528 if (DVar.CKind == OMPC_reduction &&
3529 (isOpenMPParallelDirective(DVar.DKind) ||
3530 isOpenMPWorksharingDirective(DVar.DKind))) {
3531 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
3532 << getOpenMPDirectiveName(DVar.DKind);
3533 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3534 continue;
3535 }
3536 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003537 }
3538
3539 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
3540 Vars.push_back(DE);
3541 }
3542
Alexey Bataeved09d242014-05-28 05:53:51 +00003543 if (Vars.empty())
3544 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003545
3546 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3547 Vars);
3548}
3549
Alexander Musman1bb328c2014-06-04 13:06:39 +00003550OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
3551 SourceLocation StartLoc,
3552 SourceLocation LParenLoc,
3553 SourceLocation EndLoc) {
3554 SmallVector<Expr *, 8> Vars;
3555 for (auto &RefExpr : VarList) {
3556 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
3557 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3558 // It will be analyzed later.
3559 Vars.push_back(RefExpr);
3560 continue;
3561 }
3562
3563 SourceLocation ELoc = RefExpr->getExprLoc();
3564 // OpenMP [2.1, C/C++]
3565 // A list item is a variable name.
3566 // OpenMP [2.14.3.5, Restrictions, p.1]
3567 // A variable that is part of another variable (as an array or structure
3568 // element) cannot appear in a lastprivate clause.
3569 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
3570 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3571 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3572 continue;
3573 }
3574 Decl *D = DE->getDecl();
3575 VarDecl *VD = cast<VarDecl>(D);
3576
3577 QualType Type = VD->getType();
3578 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3579 // It will be analyzed later.
3580 Vars.push_back(DE);
3581 continue;
3582 }
3583
3584 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
3585 // A variable that appears in a lastprivate clause must not have an
3586 // incomplete type or a reference type.
3587 if (RequireCompleteType(ELoc, Type,
3588 diag::err_omp_lastprivate_incomplete_type)) {
3589 continue;
3590 }
3591 if (Type->isReferenceType()) {
3592 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3593 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3594 bool IsDecl =
3595 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3596 Diag(VD->getLocation(),
3597 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3598 << VD;
3599 continue;
3600 }
3601
3602 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3603 // in a Construct]
3604 // Variables with the predetermined data-sharing attributes may not be
3605 // listed in data-sharing attributes clauses, except for the cases
3606 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003607 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003608 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3609 DVar.CKind != OMPC_firstprivate &&
3610 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3611 Diag(ELoc, diag::err_omp_wrong_dsa)
3612 << getOpenMPClauseName(DVar.CKind)
3613 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003614 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003615 continue;
3616 }
3617
Alexey Bataevf29276e2014-06-18 04:14:57 +00003618 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3619 // OpenMP [2.14.3.5, Restrictions, p.2]
3620 // A list item that is private within a parallel region, or that appears in
3621 // the reduction clause of a parallel construct, must not appear in a
3622 // lastprivate clause on a worksharing construct if any of the corresponding
3623 // worksharing regions ever binds to any of the corresponding parallel
3624 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003625 if (isOpenMPWorksharingDirective(CurrDir) &&
3626 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003627 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003628 if (DVar.CKind != OMPC_shared) {
3629 Diag(ELoc, diag::err_omp_required_access)
3630 << getOpenMPClauseName(OMPC_lastprivate)
3631 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003632 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003633 continue;
3634 }
3635 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003636 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003637 // A variable of class type (or array thereof) that appears in a
3638 // lastprivate clause requires an accessible, unambiguous default
3639 // constructor for the class type, unless the list item is also specified
3640 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003641 // A variable of class type (or array thereof) that appears in a
3642 // lastprivate clause requires an accessible, unambiguous copy assignment
3643 // operator for the class type.
3644 while (Type.getNonReferenceType()->isArrayType())
3645 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3646 ->getElementType();
3647 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3648 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3649 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003650 // FIXME This code must be replaced by actual copying and destructing of the
3651 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003652 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003653 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3654 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003655 if (MD) {
3656 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3657 MD->isDeleted()) {
3658 Diag(ELoc, diag::err_omp_required_method)
3659 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3660 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3661 VarDecl::DeclarationOnly;
3662 Diag(VD->getLocation(),
3663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3664 << VD;
3665 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3666 continue;
3667 }
3668 MarkFunctionReferenced(ELoc, MD);
3669 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003670 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003671
3672 CXXDestructorDecl *DD = RD->getDestructor();
3673 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003674 PartialDiagnostic PD =
3675 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003676 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3677 DD->isDeleted()) {
3678 Diag(ELoc, diag::err_omp_required_method)
3679 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3680 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3681 VarDecl::DeclarationOnly;
3682 Diag(VD->getLocation(),
3683 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3684 << VD;
3685 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3686 continue;
3687 }
3688 MarkFunctionReferenced(ELoc, DD);
3689 DiagnoseUseOfDecl(DD, ELoc);
3690 }
3691 }
3692
Alexey Bataevf29276e2014-06-18 04:14:57 +00003693 if (DVar.CKind != OMPC_firstprivate)
3694 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003695 Vars.push_back(DE);
3696 }
3697
3698 if (Vars.empty())
3699 return nullptr;
3700
3701 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3702 Vars);
3703}
3704
Alexey Bataev758e55e2013-09-06 18:03:48 +00003705OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3706 SourceLocation StartLoc,
3707 SourceLocation LParenLoc,
3708 SourceLocation EndLoc) {
3709 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003710 for (auto &RefExpr : VarList) {
3711 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3712 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003713 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003714 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003715 continue;
3716 }
3717
Alexey Bataeved09d242014-05-28 05:53:51 +00003718 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003719 // OpenMP [2.1, C/C++]
3720 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003721 // OpenMP [2.14.3.2, Restrictions, p.1]
3722 // A variable that is part of another variable (as an array or structure
3723 // element) cannot appear in a shared unless it is a static data member
3724 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003725 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003726 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003727 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003728 continue;
3729 }
3730 Decl *D = DE->getDecl();
3731 VarDecl *VD = cast<VarDecl>(D);
3732
3733 QualType Type = VD->getType();
3734 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3735 // It will be analyzed later.
3736 Vars.push_back(DE);
3737 continue;
3738 }
3739
3740 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3741 // in a Construct]
3742 // Variables with the predetermined data-sharing attributes may not be
3743 // listed in data-sharing attributes clauses, except for the cases
3744 // listed below. For these exceptions only, listing a predetermined
3745 // variable in a data-sharing attribute clause is allowed and overrides
3746 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003747 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003748 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3749 DVar.RefExpr) {
3750 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3751 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003752 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003753 continue;
3754 }
3755
3756 DSAStack->addDSA(VD, DE, OMPC_shared);
3757 Vars.push_back(DE);
3758 }
3759
Alexey Bataeved09d242014-05-28 05:53:51 +00003760 if (Vars.empty())
3761 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003762
3763 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3764}
3765
Alexey Bataevc5e02582014-06-16 07:08:35 +00003766namespace {
3767class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3768 DSAStackTy *Stack;
3769
3770public:
3771 bool VisitDeclRefExpr(DeclRefExpr *E) {
3772 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003773 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003774 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3775 return false;
3776 if (DVar.CKind != OMPC_unknown)
3777 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003778 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003779 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003780 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003781 return true;
3782 return false;
3783 }
3784 return false;
3785 }
3786 bool VisitStmt(Stmt *S) {
3787 for (auto Child : S->children()) {
3788 if (Child && Visit(Child))
3789 return true;
3790 }
3791 return false;
3792 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003793 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003794};
Alexey Bataev23b69422014-06-18 07:08:49 +00003795} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003796
3797OMPClause *Sema::ActOnOpenMPReductionClause(
3798 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3799 SourceLocation ColonLoc, SourceLocation EndLoc,
3800 CXXScopeSpec &ReductionIdScopeSpec,
3801 const DeclarationNameInfo &ReductionId) {
3802 // TODO: Allow scope specification search when 'declare reduction' is
3803 // supported.
3804 assert(ReductionIdScopeSpec.isEmpty() &&
3805 "No support for scoped reduction identifiers yet.");
3806
3807 auto DN = ReductionId.getName();
3808 auto OOK = DN.getCXXOverloadedOperator();
3809 BinaryOperatorKind BOK = BO_Comma;
3810
3811 // OpenMP [2.14.3.6, reduction clause]
3812 // C
3813 // reduction-identifier is either an identifier or one of the following
3814 // operators: +, -, *, &, |, ^, && and ||
3815 // C++
3816 // reduction-identifier is either an id-expression or one of the following
3817 // operators: +, -, *, &, |, ^, && and ||
3818 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3819 switch (OOK) {
3820 case OO_Plus:
3821 case OO_Minus:
3822 BOK = BO_AddAssign;
3823 break;
3824 case OO_Star:
3825 BOK = BO_MulAssign;
3826 break;
3827 case OO_Amp:
3828 BOK = BO_AndAssign;
3829 break;
3830 case OO_Pipe:
3831 BOK = BO_OrAssign;
3832 break;
3833 case OO_Caret:
3834 BOK = BO_XorAssign;
3835 break;
3836 case OO_AmpAmp:
3837 BOK = BO_LAnd;
3838 break;
3839 case OO_PipePipe:
3840 BOK = BO_LOr;
3841 break;
3842 default:
3843 if (auto II = DN.getAsIdentifierInfo()) {
3844 if (II->isStr("max"))
3845 BOK = BO_GT;
3846 else if (II->isStr("min"))
3847 BOK = BO_LT;
3848 }
3849 break;
3850 }
3851 SourceRange ReductionIdRange;
3852 if (ReductionIdScopeSpec.isValid()) {
3853 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3854 }
3855 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3856 if (BOK == BO_Comma) {
3857 // Not allowed reduction identifier is found.
3858 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3859 << ReductionIdRange;
3860 return nullptr;
3861 }
3862
3863 SmallVector<Expr *, 8> Vars;
3864 for (auto RefExpr : VarList) {
3865 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3866 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3867 // It will be analyzed later.
3868 Vars.push_back(RefExpr);
3869 continue;
3870 }
3871
3872 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3873 RefExpr->isInstantiationDependent() ||
3874 RefExpr->containsUnexpandedParameterPack()) {
3875 // It will be analyzed later.
3876 Vars.push_back(RefExpr);
3877 continue;
3878 }
3879
3880 auto ELoc = RefExpr->getExprLoc();
3881 auto ERange = RefExpr->getSourceRange();
3882 // OpenMP [2.1, C/C++]
3883 // A list item is a variable or array section, subject to the restrictions
3884 // specified in Section 2.4 on page 42 and in each of the sections
3885 // describing clauses and directives for which a list appears.
3886 // OpenMP [2.14.3.3, Restrictions, p.1]
3887 // A variable that is part of another variable (as an array or
3888 // structure element) cannot appear in a private clause.
3889 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3890 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3891 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3892 continue;
3893 }
3894 auto D = DE->getDecl();
3895 auto VD = cast<VarDecl>(D);
3896 auto Type = VD->getType();
3897 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3898 // A variable that appears in a private clause must not have an incomplete
3899 // type or a reference type.
3900 if (RequireCompleteType(ELoc, Type,
3901 diag::err_omp_reduction_incomplete_type))
3902 continue;
3903 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3904 // Arrays may not appear in a reduction clause.
3905 if (Type.getNonReferenceType()->isArrayType()) {
3906 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3907 bool IsDecl =
3908 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3909 Diag(VD->getLocation(),
3910 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3911 << VD;
3912 continue;
3913 }
3914 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3915 // A list item that appears in a reduction clause must not be
3916 // const-qualified.
3917 if (Type.getNonReferenceType().isConstant(Context)) {
3918 Diag(ELoc, diag::err_omp_const_variable)
3919 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3920 bool IsDecl =
3921 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3922 Diag(VD->getLocation(),
3923 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3924 << VD;
3925 continue;
3926 }
3927 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3928 // If a list-item is a reference type then it must bind to the same object
3929 // for all threads of the team.
3930 VarDecl *VDDef = VD->getDefinition();
3931 if (Type->isReferenceType() && VDDef) {
3932 DSARefChecker Check(DSAStack);
3933 if (Check.Visit(VDDef->getInit())) {
3934 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3935 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3936 continue;
3937 }
3938 }
3939 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3940 // The type of a list item that appears in a reduction clause must be valid
3941 // for the reduction-identifier. For a max or min reduction in C, the type
3942 // of the list item must be an allowed arithmetic data type: char, int,
3943 // float, double, or _Bool, possibly modified with long, short, signed, or
3944 // unsigned. For a max or min reduction in C++, the type of the list item
3945 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3946 // double, or bool, possibly modified with long, short, signed, or unsigned.
3947 if ((BOK == BO_GT || BOK == BO_LT) &&
3948 !(Type->isScalarType() ||
3949 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3950 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3951 << getLangOpts().CPlusPlus;
3952 bool IsDecl =
3953 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3954 Diag(VD->getLocation(),
3955 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3956 << VD;
3957 continue;
3958 }
3959 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3960 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3961 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3962 bool IsDecl =
3963 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3964 Diag(VD->getLocation(),
3965 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3966 << VD;
3967 continue;
3968 }
3969 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3970 getDiagnostics().setSuppressAllDiagnostics(true);
3971 ExprResult ReductionOp =
3972 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3973 RefExpr, RefExpr);
3974 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3975 if (ReductionOp.isInvalid()) {
3976 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003977 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003978 bool IsDecl =
3979 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3980 Diag(VD->getLocation(),
3981 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3982 << VD;
3983 continue;
3984 }
3985
3986 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3987 // in a Construct]
3988 // Variables with the predetermined data-sharing attributes may not be
3989 // listed in data-sharing attributes clauses, except for the cases
3990 // listed below. For these exceptions only, listing a predetermined
3991 // variable in a data-sharing attribute clause is allowed and overrides
3992 // the variable's predetermined data-sharing attributes.
3993 // OpenMP [2.14.3.6, Restrictions, p.3]
3994 // Any number of reduction clauses can be specified on the directive,
3995 // but a list item can appear only once in the reduction clauses for that
3996 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003997 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003998 if (DVar.CKind == OMPC_reduction) {
3999 Diag(ELoc, diag::err_omp_once_referenced)
4000 << getOpenMPClauseName(OMPC_reduction);
4001 if (DVar.RefExpr) {
4002 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4003 }
4004 } else if (DVar.CKind != OMPC_unknown) {
4005 Diag(ELoc, diag::err_omp_wrong_dsa)
4006 << getOpenMPClauseName(DVar.CKind)
4007 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004008 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004009 continue;
4010 }
4011
4012 // OpenMP [2.14.3.6, Restrictions, p.1]
4013 // A list item that appears in a reduction clause of a worksharing
4014 // construct must be shared in the parallel regions to which any of the
4015 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004016 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004017 if (isOpenMPWorksharingDirective(CurrDir) &&
4018 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004019 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004020 if (DVar.CKind != OMPC_shared) {
4021 Diag(ELoc, diag::err_omp_required_access)
4022 << getOpenMPClauseName(OMPC_reduction)
4023 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004024 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004025 continue;
4026 }
4027 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004028
4029 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4030 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4031 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004032 // FIXME This code must be replaced by actual constructing/destructing of
4033 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004034 if (RD) {
4035 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4036 PartialDiagnostic PD =
4037 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004038 if (!CD ||
4039 CheckConstructorAccess(ELoc, CD,
4040 InitializedEntity::InitializeTemporary(Type),
4041 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004042 CD->isDeleted()) {
4043 Diag(ELoc, diag::err_omp_required_method)
4044 << getOpenMPClauseName(OMPC_reduction) << 0;
4045 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4046 VarDecl::DeclarationOnly;
4047 Diag(VD->getLocation(),
4048 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4049 << VD;
4050 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4051 continue;
4052 }
4053 MarkFunctionReferenced(ELoc, CD);
4054 DiagnoseUseOfDecl(CD, ELoc);
4055
4056 CXXDestructorDecl *DD = RD->getDestructor();
4057 if (DD) {
4058 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4059 DD->isDeleted()) {
4060 Diag(ELoc, diag::err_omp_required_method)
4061 << getOpenMPClauseName(OMPC_reduction) << 4;
4062 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4063 VarDecl::DeclarationOnly;
4064 Diag(VD->getLocation(),
4065 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4066 << VD;
4067 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4068 continue;
4069 }
4070 MarkFunctionReferenced(ELoc, DD);
4071 DiagnoseUseOfDecl(DD, ELoc);
4072 }
4073 }
4074
4075 DSAStack->addDSA(VD, DE, OMPC_reduction);
4076 Vars.push_back(DE);
4077 }
4078
4079 if (Vars.empty())
4080 return nullptr;
4081
4082 return OMPReductionClause::Create(
4083 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4084 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4085}
4086
Alexander Musman8dba6642014-04-22 13:09:42 +00004087OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4088 SourceLocation StartLoc,
4089 SourceLocation LParenLoc,
4090 SourceLocation ColonLoc,
4091 SourceLocation EndLoc) {
4092 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004093 for (auto &RefExpr : VarList) {
4094 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4095 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004096 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004097 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004098 continue;
4099 }
4100
4101 // OpenMP [2.14.3.7, linear clause]
4102 // A list item that appears in a linear clause is subject to the private
4103 // clause semantics described in Section 2.14.3.3 on page 159 except as
4104 // noted. In addition, the value of the new list item on each iteration
4105 // of the associated loop(s) corresponds to the value of the original
4106 // list item before entering the construct plus the logical number of
4107 // the iteration times linear-step.
4108
Alexey Bataeved09d242014-05-28 05:53:51 +00004109 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004110 // OpenMP [2.1, C/C++]
4111 // A list item is a variable name.
4112 // OpenMP [2.14.3.3, Restrictions, p.1]
4113 // A variable that is part of another variable (as an array or
4114 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004115 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004116 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004117 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004118 continue;
4119 }
4120
4121 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4122
4123 // OpenMP [2.14.3.7, linear clause]
4124 // A list-item cannot appear in more than one linear clause.
4125 // A list-item that appears in a linear clause cannot appear in any
4126 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004127 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004128 if (DVar.RefExpr) {
4129 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4130 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004131 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004132 continue;
4133 }
4134
4135 QualType QType = VD->getType();
4136 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4137 // It will be analyzed later.
4138 Vars.push_back(DE);
4139 continue;
4140 }
4141
4142 // A variable must not have an incomplete type or a reference type.
4143 if (RequireCompleteType(ELoc, QType,
4144 diag::err_omp_linear_incomplete_type)) {
4145 continue;
4146 }
4147 if (QType->isReferenceType()) {
4148 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4149 << getOpenMPClauseName(OMPC_linear) << QType;
4150 bool IsDecl =
4151 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4152 Diag(VD->getLocation(),
4153 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4154 << VD;
4155 continue;
4156 }
4157
4158 // A list item must not be const-qualified.
4159 if (QType.isConstant(Context)) {
4160 Diag(ELoc, diag::err_omp_const_variable)
4161 << getOpenMPClauseName(OMPC_linear);
4162 bool IsDecl =
4163 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4164 Diag(VD->getLocation(),
4165 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4166 << VD;
4167 continue;
4168 }
4169
4170 // A list item must be of integral or pointer type.
4171 QType = QType.getUnqualifiedType().getCanonicalType();
4172 const Type *Ty = QType.getTypePtrOrNull();
4173 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4174 !Ty->isPointerType())) {
4175 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4176 bool IsDecl =
4177 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4178 Diag(VD->getLocation(),
4179 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4180 << VD;
4181 continue;
4182 }
4183
4184 DSAStack->addDSA(VD, DE, OMPC_linear);
4185 Vars.push_back(DE);
4186 }
4187
4188 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004189 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004190
4191 Expr *StepExpr = Step;
4192 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4193 !Step->isInstantiationDependent() &&
4194 !Step->containsUnexpandedParameterPack()) {
4195 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004196 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004197 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004198 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004199 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004200
4201 // Warn about zero linear step (it would be probably better specified as
4202 // making corresponding variables 'const').
4203 llvm::APSInt Result;
4204 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4205 !Result.isNegative() && !Result.isStrictlyPositive())
4206 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4207 << (Vars.size() > 1);
4208 }
4209
4210 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4211 Vars, StepExpr);
4212}
4213
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004214OMPClause *Sema::ActOnOpenMPAlignedClause(
4215 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4216 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4217
4218 SmallVector<Expr *, 8> Vars;
4219 for (auto &RefExpr : VarList) {
4220 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4221 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4222 // It will be analyzed later.
4223 Vars.push_back(RefExpr);
4224 continue;
4225 }
4226
4227 SourceLocation ELoc = RefExpr->getExprLoc();
4228 // OpenMP [2.1, C/C++]
4229 // A list item is a variable name.
4230 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4231 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4232 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4233 continue;
4234 }
4235
4236 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4237
4238 // OpenMP [2.8.1, simd construct, Restrictions]
4239 // The type of list items appearing in the aligned clause must be
4240 // array, pointer, reference to array, or reference to pointer.
4241 QualType QType = DE->getType()
4242 .getNonReferenceType()
4243 .getUnqualifiedType()
4244 .getCanonicalType();
4245 const Type *Ty = QType.getTypePtrOrNull();
4246 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4247 !Ty->isPointerType())) {
4248 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4249 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4250 bool IsDecl =
4251 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4252 Diag(VD->getLocation(),
4253 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4254 << VD;
4255 continue;
4256 }
4257
4258 // OpenMP [2.8.1, simd construct, Restrictions]
4259 // A list-item cannot appear in more than one aligned clause.
4260 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4261 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4262 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4263 << getOpenMPClauseName(OMPC_aligned);
4264 continue;
4265 }
4266
4267 Vars.push_back(DE);
4268 }
4269
4270 // OpenMP [2.8.1, simd construct, Description]
4271 // The parameter of the aligned clause, alignment, must be a constant
4272 // positive integer expression.
4273 // If no optional parameter is specified, implementation-defined default
4274 // alignments for SIMD instructions on the target platforms are assumed.
4275 if (Alignment != nullptr) {
4276 ExprResult AlignResult =
4277 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4278 if (AlignResult.isInvalid())
4279 return nullptr;
4280 Alignment = AlignResult.get();
4281 }
4282 if (Vars.empty())
4283 return nullptr;
4284
4285 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
4286 EndLoc, Vars, Alignment);
4287}
4288
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004289OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
4290 SourceLocation StartLoc,
4291 SourceLocation LParenLoc,
4292 SourceLocation EndLoc) {
4293 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004294 for (auto &RefExpr : VarList) {
4295 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
4296 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004297 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004298 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004299 continue;
4300 }
4301
Alexey Bataeved09d242014-05-28 05:53:51 +00004302 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004303 // OpenMP [2.1, C/C++]
4304 // A list item is a variable name.
4305 // OpenMP [2.14.4.1, Restrictions, p.1]
4306 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00004307 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004308 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004309 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004310 continue;
4311 }
4312
4313 Decl *D = DE->getDecl();
4314 VarDecl *VD = cast<VarDecl>(D);
4315
4316 QualType Type = VD->getType();
4317 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4318 // It will be analyzed later.
4319 Vars.push_back(DE);
4320 continue;
4321 }
4322
4323 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
4324 // A list item that appears in a copyin clause must be threadprivate.
4325 if (!DSAStack->isThreadPrivate(VD)) {
4326 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00004327 << getOpenMPClauseName(OMPC_copyin)
4328 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004329 continue;
4330 }
4331
4332 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4333 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00004334 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004335 // operator for the class type.
4336 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004337 CXXRecordDecl *RD =
4338 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004339 // FIXME This code must be replaced by actual assignment of the
4340 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004341 if (RD) {
4342 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4343 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004344 if (MD) {
4345 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4346 MD->isDeleted()) {
4347 Diag(ELoc, diag::err_omp_required_method)
4348 << getOpenMPClauseName(OMPC_copyin) << 2;
4349 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4350 VarDecl::DeclarationOnly;
4351 Diag(VD->getLocation(),
4352 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4353 << VD;
4354 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4355 continue;
4356 }
4357 MarkFunctionReferenced(ELoc, MD);
4358 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004359 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004360 }
4361
4362 DSAStack->addDSA(VD, DE, OMPC_copyin);
4363 Vars.push_back(DE);
4364 }
4365
Alexey Bataeved09d242014-05-28 05:53:51 +00004366 if (Vars.empty())
4367 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004368
4369 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4370}
4371
Alexey Bataevbae9a792014-06-27 10:37:06 +00004372OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
4373 SourceLocation StartLoc,
4374 SourceLocation LParenLoc,
4375 SourceLocation EndLoc) {
4376 SmallVector<Expr *, 8> Vars;
4377 for (auto &RefExpr : VarList) {
4378 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
4379 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4380 // It will be analyzed later.
4381 Vars.push_back(RefExpr);
4382 continue;
4383 }
4384
4385 SourceLocation ELoc = RefExpr->getExprLoc();
4386 // OpenMP [2.1, C/C++]
4387 // A list item is a variable name.
4388 // OpenMP [2.14.4.1, Restrictions, p.1]
4389 // A list item that appears in a copyin clause must be threadprivate.
4390 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4391 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4392 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4393 continue;
4394 }
4395
4396 Decl *D = DE->getDecl();
4397 VarDecl *VD = cast<VarDecl>(D);
4398
4399 QualType Type = VD->getType();
4400 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4401 // It will be analyzed later.
4402 Vars.push_back(DE);
4403 continue;
4404 }
4405
4406 // OpenMP [2.14.4.2, Restrictions, p.2]
4407 // A list item that appears in a copyprivate clause may not appear in a
4408 // private or firstprivate clause on the single construct.
4409 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004410 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004411 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
4412 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
4413 Diag(ELoc, diag::err_omp_wrong_dsa)
4414 << getOpenMPClauseName(DVar.CKind)
4415 << getOpenMPClauseName(OMPC_copyprivate);
4416 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4417 continue;
4418 }
4419
4420 // OpenMP [2.11.4.2, Restrictions, p.1]
4421 // All list items that appear in a copyprivate clause must be either
4422 // threadprivate or private in the enclosing context.
4423 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004424 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00004425 if (DVar.CKind == OMPC_shared) {
4426 Diag(ELoc, diag::err_omp_required_access)
4427 << getOpenMPClauseName(OMPC_copyprivate)
4428 << "threadprivate or private in the enclosing context";
4429 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4430 continue;
4431 }
4432 }
4433 }
4434
4435 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
4436 // A variable of class type (or array thereof) that appears in a
4437 // copyin clause requires an accessible, unambiguous copy assignment
4438 // operator for the class type.
4439 Type = Context.getBaseElementType(Type);
4440 CXXRecordDecl *RD =
4441 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
4442 // FIXME This code must be replaced by actual assignment of the
4443 // threadprivate variable.
4444 if (RD) {
4445 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4446 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
4447 if (MD) {
4448 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4449 MD->isDeleted()) {
4450 Diag(ELoc, diag::err_omp_required_method)
4451 << getOpenMPClauseName(OMPC_copyprivate) << 2;
4452 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4453 VarDecl::DeclarationOnly;
4454 Diag(VD->getLocation(),
4455 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4456 << VD;
4457 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4458 continue;
4459 }
4460 MarkFunctionReferenced(ELoc, MD);
4461 DiagnoseUseOfDecl(MD, ELoc);
4462 }
4463 }
4464
4465 // No need to mark vars as copyprivate, they are already threadprivate or
4466 // implicitly private.
4467 Vars.push_back(DE);
4468 }
4469
4470 if (Vars.empty())
4471 return nullptr;
4472
4473 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4474}
4475
Alexey Bataev6125da92014-07-21 11:26:11 +00004476OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
4477 SourceLocation StartLoc,
4478 SourceLocation LParenLoc,
4479 SourceLocation EndLoc) {
4480 if (VarList.empty())
4481 return nullptr;
4482
4483 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
4484}
Alexey Bataevdea47612014-07-23 07:46:59 +00004485