blob: ef36e7e901e83cc14c3ee37073825f6bdd2fd9b2 [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 Bataeved09d242014-05-28 05:53:51 +000094 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000095 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000096 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000097 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
98 ConstructLoc(Loc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000099 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000100 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000101 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
102 ConstructLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 };
104
105 typedef SmallVector<SharingMapTy, 64> StackTy;
106
107 /// \brief Stack of used declaration and their data-sharing attributes.
108 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000109 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000110
111 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
112
113 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000114
115 /// \brief Checks if the variable is a local for OpenMP region.
116 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000117
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000119 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120
121 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000122 Scope *CurScope, SourceLocation Loc) {
123 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
124 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125 }
126
127 void pop() {
128 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
129 Stack.pop_back();
130 }
131
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000132 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000133 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000134 /// for diagnostics.
135 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
136
Alexey Bataev758e55e2013-09-06 18:03:48 +0000137 /// \brief Adds explicit data sharing attribute to the specified declaration.
138 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 /// \brief Returns data sharing attributes from top of the stack for the
141 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000142 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000143 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000144 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000145 /// \brief Checks if the specified variables has data-sharing attributes which
146 /// match specified \a CPred predicate in any directive which matches \a DPred
147 /// predicate.
148 template <class ClausesPredicate, class DirectivesPredicate>
149 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000150 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000151 /// \brief Checks if the specified variables has data-sharing attributes which
152 /// match specified \a CPred predicate in any innermost directive which
153 /// matches \a DPred predicate.
154 template <class ClausesPredicate, class DirectivesPredicate>
155 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000156 DirectivesPredicate DPred,
157 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000158
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 /// \brief Returns currently analyzed directive.
160 OpenMPDirectiveKind getCurrentDirective() const {
161 return Stack.back().Directive;
162 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000163 /// \brief Returns parent directive.
164 OpenMPDirectiveKind getParentDirective() const {
165 if (Stack.size() > 2)
166 return Stack[Stack.size() - 2].Directive;
167 return OMPD_unknown;
168 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000169
170 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000171 void setDefaultDSANone(SourceLocation Loc) {
172 Stack.back().DefaultAttr = DSA_none;
173 Stack.back().DefaultAttrLoc = Loc;
174 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000176 void setDefaultDSAShared(SourceLocation Loc) {
177 Stack.back().DefaultAttr = DSA_shared;
178 Stack.back().DefaultAttrLoc = Loc;
179 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180
181 DefaultDataSharingAttributes getDefaultDSA() const {
182 return Stack.back().DefaultAttr;
183 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000184 SourceLocation getDefaultDSALocation() const {
185 return Stack.back().DefaultAttrLoc;
186 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000187
Alexey Bataevf29276e2014-06-18 04:14:57 +0000188 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000189 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000190 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000192 }
193
194 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000195 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000196 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000198bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
199 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
200 DKind == OMPD_unknown;
201}
Alexey Bataeved09d242014-05-28 05:53:51 +0000202} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203
204DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
205 VarDecl *D) {
206 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000207 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000208 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
209 // in a region but not in construct]
210 // File-scope or namespace-scope variables referenced in called routines
211 // in the region are shared unless they appear in a threadprivate
212 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000213 if (!D->isFunctionOrMethodVarDecl())
214 DVar.CKind = OMPC_shared;
215
216 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
217 // in a region but not in construct]
218 // Variables with static storage duration that are declared in called
219 // routines in the region are shared.
220 if (D->hasGlobalStorage())
221 DVar.CKind = OMPC_shared;
222
Alexey Bataev758e55e2013-09-06 18:03:48 +0000223 return DVar;
224 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000225
Alexey Bataev758e55e2013-09-06 18:03:48 +0000226 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000227 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
228 // in a Construct, C/C++, predetermined, p.1]
229 // Variables with automatic storage duration that are declared in a scope
230 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000231 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
232 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
233 DVar.CKind = OMPC_private;
234 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000235 }
236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 // Explicitly specified attributes and local variables with predetermined
238 // attributes.
239 if (Iter->SharingMap.count(D)) {
240 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
241 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000242 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000243 return DVar;
244 }
245
246 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
247 // in a Construct, C/C++, implicitly determined, p.1]
248 // In a parallel or task construct, the data-sharing attributes of these
249 // variables are determined by the default clause, if present.
250 switch (Iter->DefaultAttr) {
251 case DSA_shared:
252 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000254 return DVar;
255 case DSA_none:
256 return DVar;
257 case DSA_unspecified:
258 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
259 // in a Construct, implicitly determined, p.2]
260 // In a parallel construct, if no default clause is present, these
261 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000262 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000263 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264 DVar.CKind = OMPC_shared;
265 return DVar;
266 }
267
268 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
269 // in a Construct, implicitly determined, p.4]
270 // In a task construct, if no default clause is present, a variable that in
271 // the enclosing context is determined to be shared by all implicit tasks
272 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273 if (DVar.DKind == OMPD_task) {
274 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000275 for (StackTy::reverse_iterator I = std::next(Iter),
276 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000277 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000278 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
279 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 // in a Construct, implicitly determined, p.6]
281 // In a task construct, if no default clause is present, a variable
282 // whose data-sharing attribute is not determined by the rules above is
283 // firstprivate.
284 DVarTemp = getDSA(I, D);
285 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000286 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000287 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000288 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 return DVar;
290 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000291 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000292 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293 }
294 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000295 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000296 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297 return DVar;
298 }
299 }
300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
301 // in a Construct, implicitly determined, p.3]
302 // For constructs other than task, if no default clause is present, these
303 // variables inherit their data-sharing attributes from the enclosing
304 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000305 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306}
307
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000308DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
309 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
310 auto It = Stack.back().AlignedMap.find(D);
311 if (It == Stack.back().AlignedMap.end()) {
312 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
313 Stack.back().AlignedMap[D] = NewDE;
314 return nullptr;
315 } else {
316 assert(It->second && "Unexpected nullptr expr in the aligned map");
317 return It->second;
318 }
319 return nullptr;
320}
321
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
323 if (A == OMPC_threadprivate) {
324 Stack[0].SharingMap[D].Attributes = A;
325 Stack[0].SharingMap[D].RefExpr = E;
326 } else {
327 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
328 Stack.back().SharingMap[D].Attributes = A;
329 Stack.back().SharingMap[D].RefExpr = E;
330 }
331}
332
Alexey Bataeved09d242014-05-28 05:53:51 +0000333bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000334 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000335 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000336 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000337 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000338 ++I;
339 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000340 if (I == E)
341 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000342 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000343 Scope *CurScope = getCurScope();
344 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000346 }
347 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000348 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000349 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000350}
351
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000352DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 DSAVarData DVar;
354
355 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
356 // in a Construct, C/C++, predetermined, p.1]
357 // Variables appearing in threadprivate directives are threadprivate.
358 if (D->getTLSKind() != VarDecl::TLS_None) {
359 DVar.CKind = OMPC_threadprivate;
360 return DVar;
361 }
362 if (Stack[0].SharingMap.count(D)) {
363 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
364 DVar.CKind = OMPC_threadprivate;
365 return DVar;
366 }
367
368 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
369 // in a Construct, C/C++, predetermined, p.1]
370 // Variables with automatic storage duration that are declared in a scope
371 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000372 OpenMPDirectiveKind Kind =
373 FromParent ? getParentDirective() : getCurrentDirective();
374 auto StartI = std::next(Stack.rbegin());
375 auto EndI = std::prev(Stack.rend());
376 if (FromParent && StartI != EndI) {
377 StartI = std::next(StartI);
378 }
379 if (!isParallelOrTaskRegion(Kind)) {
380 if (isOpenMPLocal(D, StartI) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000381 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000382 DVar.CKind = OMPC_private;
383 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000384 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385 }
386
387 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
388 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000389 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000390 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000391 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000392 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000393 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
394 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000395 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
396 return DVar;
397
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398 DVar.CKind = OMPC_shared;
399 return DVar;
400 }
401
402 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000403 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 while (Type->isArrayType()) {
405 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
406 Type = ElemType.getNonReferenceType().getCanonicalType();
407 }
408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
409 // in a Construct, C/C++, predetermined, p.6]
410 // Variables with const qualified type having no mutable member are
411 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000412 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000413 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000415 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 // Variables with const-qualified type having no mutable member may be
417 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000418 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
419 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000420 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
421 return DVar;
422
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 DVar.CKind = OMPC_shared;
424 return DVar;
425 }
426
427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
428 // in a Construct, C/C++, predetermined, p.7]
429 // Variables with static storage duration that are declared in a scope
430 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000431 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432 DVar.CKind = OMPC_shared;
433 return DVar;
434 }
435
436 // Explicitly specified attributes and local variables with predetermined
437 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000438 auto I = std::prev(StartI);
439 if (I->SharingMap.count(D)) {
440 DVar.RefExpr = I->SharingMap[D].RefExpr;
441 DVar.CKind = I->SharingMap[D].Attributes;
442 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
444
445 return DVar;
446}
447
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000448DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
449 auto StartI = Stack.rbegin();
450 auto EndI = std::prev(Stack.rend());
451 if (FromParent && StartI != EndI) {
452 StartI = std::next(StartI);
453 }
454 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000455}
456
Alexey Bataevf29276e2014-06-18 04:14:57 +0000457template <class ClausesPredicate, class DirectivesPredicate>
458DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000459 DirectivesPredicate DPred,
460 bool FromParent) {
461 auto StartI = std::next(Stack.rbegin());
462 auto EndI = std::prev(Stack.rend());
463 if (FromParent && StartI != EndI) {
464 StartI = std::next(StartI);
465 }
466 for (auto I = StartI, EE = EndI; I != EE; ++I) {
467 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000468 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000469 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000470 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000471 return DVar;
472 }
473 return DSAVarData();
474}
475
Alexey Bataevf29276e2014-06-18 04:14:57 +0000476template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000477DSAStackTy::DSAVarData
478DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
479 DirectivesPredicate DPred, bool FromParent) {
480 auto StartI = std::next(Stack.rbegin());
481 auto EndI = std::prev(Stack.rend());
482 if (FromParent && StartI != EndI) {
483 StartI = std::next(StartI);
484 }
485 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000486 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000487 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000488 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000489 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000490 return DVar;
491 return DSAVarData();
492 }
493 return DSAVarData();
494}
495
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496void Sema::InitDataSharingAttributesStack() {
497 VarDataSharingAttributesStack = new DSAStackTy(*this);
498}
499
500#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
501
Alexey Bataeved09d242014-05-28 05:53:51 +0000502void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503
504void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
505 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000506 Scope *CurScope, SourceLocation Loc) {
507 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000508 PushExpressionEvaluationContext(PotentiallyEvaluated);
509}
510
511void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000512 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
513 // A variable of class type (or array thereof) that appears in a lastprivate
514 // clause requires an accessible, unambiguous default constructor for the
515 // class type, unless the list item is also specified in a firstprivate
516 // clause.
517 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
518 for (auto C : D->clauses()) {
519 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
520 for (auto VarRef : Clause->varlists()) {
521 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
522 continue;
523 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000524 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000525 if (DVar.CKind == OMPC_lastprivate) {
526 SourceLocation ELoc = VarRef->getExprLoc();
527 auto Type = VarRef->getType();
528 if (Type->isArrayType())
529 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
530 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000531 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
532 // FIXME This code must be replaced by actual constructing of the
533 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000534 if (RD) {
535 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
536 PartialDiagnostic PD =
537 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
538 if (!CD ||
539 CheckConstructorAccess(
540 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
541 CD->getAccess(), PD) == AR_inaccessible ||
542 CD->isDeleted()) {
543 Diag(ELoc, diag::err_omp_required_method)
544 << getOpenMPClauseName(OMPC_lastprivate) << 0;
545 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
546 VarDecl::DeclarationOnly;
547 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
548 : diag::note_defined_here)
549 << VD;
550 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
551 continue;
552 }
553 MarkFunctionReferenced(ELoc, CD);
554 DiagnoseUseOfDecl(CD, ELoc);
555 }
556 }
557 }
558 }
559 }
560 }
561
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 DSAStack->pop();
563 DiscardCleanupsInEvaluationContext();
564 PopExpressionEvaluationContext();
565}
566
Alexey Bataeva769e072013-03-22 06:34:35 +0000567namespace {
568
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000569class VarDeclFilterCCC : public CorrectionCandidateCallback {
570private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000571 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000572
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000573public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000574 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000575 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000576 NamedDecl *ND = Candidate.getCorrectionDecl();
577 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
578 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000579 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
580 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000581 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000582 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000583 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000584};
Alexey Bataeved09d242014-05-28 05:53:51 +0000585} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000586
587ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
588 CXXScopeSpec &ScopeSpec,
589 const DeclarationNameInfo &Id) {
590 LookupResult Lookup(*this, Id, LookupOrdinaryName);
591 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
592
593 if (Lookup.isAmbiguous())
594 return ExprError();
595
596 VarDecl *VD;
597 if (!Lookup.isSingleResult()) {
598 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000599 if (TypoCorrection Corrected =
600 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
601 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000602 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000603 PDiag(Lookup.empty()
604 ? diag::err_undeclared_var_use_suggest
605 : diag::err_omp_expected_var_arg_suggest)
606 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000607 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000608 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000609 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
610 : diag::err_omp_expected_var_arg)
611 << Id.getName();
612 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614 } else {
615 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000616 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000617 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
618 return ExprError();
619 }
620 }
621 Lookup.suppressDiagnostics();
622
623 // OpenMP [2.9.2, Syntax, C/C++]
624 // Variables must be file-scope, namespace-scope, or static block-scope.
625 if (!VD->hasGlobalStorage()) {
626 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000627 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
628 bool IsDecl =
629 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000630 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000631 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
632 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000633 return ExprError();
634 }
635
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000636 VarDecl *CanonicalVD = VD->getCanonicalDecl();
637 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000638 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
639 // A threadprivate directive for file-scope variables must appear outside
640 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000641 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
642 !getCurLexicalContext()->isTranslationUnit()) {
643 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000644 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
645 bool IsDecl =
646 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
647 Diag(VD->getLocation(),
648 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
649 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000650 return ExprError();
651 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000652 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
653 // A threadprivate directive for static class member variables must appear
654 // in the class definition, in the same scope in which the member
655 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000656 if (CanonicalVD->isStaticDataMember() &&
657 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
658 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000659 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
660 bool IsDecl =
661 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
662 Diag(VD->getLocation(),
663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
664 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000665 return ExprError();
666 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000667 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
668 // A threadprivate directive for namespace-scope variables must appear
669 // outside any definition or declaration other than the namespace
670 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000671 if (CanonicalVD->getDeclContext()->isNamespace() &&
672 (!getCurLexicalContext()->isFileContext() ||
673 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
674 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000675 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
676 bool IsDecl =
677 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
678 Diag(VD->getLocation(),
679 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
680 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000681 return ExprError();
682 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000683 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
684 // A threadprivate directive for static block-scope variables must appear
685 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000686 if (CanonicalVD->isStaticLocal() && CurScope &&
687 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000688 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000689 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
690 bool IsDecl =
691 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
692 Diag(VD->getLocation(),
693 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
694 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000695 return ExprError();
696 }
697
698 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
699 // A threadprivate directive must lexically precede all references to any
700 // of the variables in its list.
701 if (VD->isUsed()) {
702 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000703 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000704 return ExprError();
705 }
706
707 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000708 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 return DE;
710}
711
Alexey Bataeved09d242014-05-28 05:53:51 +0000712Sema::DeclGroupPtrTy
713Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
714 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000715 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000716 CurContext->addDecl(D);
717 return DeclGroupPtrTy::make(DeclGroupRef(D));
718 }
719 return DeclGroupPtrTy();
720}
721
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000722namespace {
723class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
724 Sema &SemaRef;
725
726public:
727 bool VisitDeclRefExpr(const DeclRefExpr *E) {
728 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
729 if (VD->hasLocalStorage()) {
730 SemaRef.Diag(E->getLocStart(),
731 diag::err_omp_local_var_in_threadprivate_init)
732 << E->getSourceRange();
733 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
734 << VD << VD->getSourceRange();
735 return true;
736 }
737 }
738 return false;
739 }
740 bool VisitStmt(const Stmt *S) {
741 for (auto Child : S->children()) {
742 if (Child && Visit(Child))
743 return true;
744 }
745 return false;
746 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000747 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000748};
749} // namespace
750
Alexey Bataeved09d242014-05-28 05:53:51 +0000751OMPThreadPrivateDecl *
752Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000754 for (auto &RefExpr : VarList) {
755 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756 VarDecl *VD = cast<VarDecl>(DE->getDecl());
757 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000758
759 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
760 // A threadprivate variable must not have an incomplete type.
761 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000762 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000763 continue;
764 }
765
766 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
767 // A threadprivate variable must not have a reference type.
768 if (VD->getType()->isReferenceType()) {
769 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000770 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
771 bool IsDecl =
772 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
773 Diag(VD->getLocation(),
774 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
775 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000776 continue;
777 }
778
Richard Smithfd3834f2013-04-13 02:43:54 +0000779 // Check if this is a TLS variable.
780 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000781 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000782 bool IsDecl =
783 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
784 Diag(VD->getLocation(),
785 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
786 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000787 continue;
788 }
789
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000790 // Check if initial value of threadprivate variable reference variable with
791 // local storage (it is not supported by runtime).
792 if (auto Init = VD->getAnyInitializer()) {
793 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000794 if (Checker.Visit(Init))
795 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000796 }
797
Alexey Bataeved09d242014-05-28 05:53:51 +0000798 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000799 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000800 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000801 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000802 if (!Vars.empty()) {
803 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
804 Vars);
805 D->setAccess(AS_public);
806 }
807 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000808}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000809
Alexey Bataev7ff55242014-06-19 09:13:45 +0000810static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
811 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
812 bool IsLoopIterVar = false) {
813 if (DVar.RefExpr) {
814 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
815 << getOpenMPClauseName(DVar.CKind);
816 return;
817 }
818 enum {
819 PDSA_StaticMemberShared,
820 PDSA_StaticLocalVarShared,
821 PDSA_LoopIterVarPrivate,
822 PDSA_LoopIterVarLinear,
823 PDSA_LoopIterVarLastprivate,
824 PDSA_ConstVarShared,
825 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000826 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000827 PDSA_LocalVarPrivate,
828 PDSA_Implicit
829 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000830 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000831 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000832 if (IsLoopIterVar) {
833 if (DVar.CKind == OMPC_private)
834 Reason = PDSA_LoopIterVarPrivate;
835 else if (DVar.CKind == OMPC_lastprivate)
836 Reason = PDSA_LoopIterVarLastprivate;
837 else
838 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000839 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
840 Reason = PDSA_TaskVarFirstprivate;
841 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000842 } else if (VD->isStaticLocal())
843 Reason = PDSA_StaticLocalVarShared;
844 else if (VD->isStaticDataMember())
845 Reason = PDSA_StaticMemberShared;
846 else if (VD->isFileVarDecl())
847 Reason = PDSA_GlobalVarShared;
848 else if (VD->getType().isConstant(SemaRef.getASTContext()))
849 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000850 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000851 ReportHint = true;
852 Reason = PDSA_LocalVarPrivate;
853 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000854 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000855 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000856 << Reason << ReportHint
857 << getOpenMPDirectiveName(Stack->getCurrentDirective());
858 } else if (DVar.ImplicitDSALoc.isValid()) {
859 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
860 << getOpenMPClauseName(DVar.CKind);
861 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000862}
863
Alexey Bataev758e55e2013-09-06 18:03:48 +0000864namespace {
865class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
866 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000867 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000868 bool ErrorFound;
869 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000870 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000871 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000872
Alexey Bataev758e55e2013-09-06 18:03:48 +0000873public:
874 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000875 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000876 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000877 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
878 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000880 auto DVar = Stack->getTopDSA(VD, false);
881 // Check if the variable has explicit DSA set and stop analysis if it so.
882 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000883
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000884 auto ELoc = E->getExprLoc();
885 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000886 // The default(none) clause requires that each variable that is referenced
887 // in the construct, and does not have a predetermined data-sharing
888 // attribute, must have its data-sharing attribute explicitly determined
889 // by being listed in a data-sharing attribute clause.
890 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000891 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000892 VarsWithInheritedDSA.count(VD) == 0) {
893 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000894 return;
895 }
896
897 // OpenMP [2.9.3.6, Restrictions, p.2]
898 // A list item that appears in a reduction clause of the innermost
899 // enclosing worksharing or parallel construct may not be accessed in an
900 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000901 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000902 [](OpenMPDirectiveKind K) -> bool {
903 return isOpenMPParallelDirective(K) ||
904 isOpenMPWorksharingDirective(K);
905 },
906 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000907 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
908 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000909 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
910 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000911 return;
912 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000913
914 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000915 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000916 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000917 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000918 }
919 }
920 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000921 for (auto *C : S->clauses()) {
922 // Skip analysis of arguments of implicitly defined firstprivate clause
923 // for task directives.
924 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
925 for (auto *CC : C->children()) {
926 if (CC)
927 Visit(CC);
928 }
929 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000930 }
931 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000932 for (auto *C : S->children()) {
933 if (C && !isa<OMPExecutableDirective>(C))
934 Visit(C);
935 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000936 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000937
938 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000939 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000940 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
941 return VarsWithInheritedDSA;
942 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000943
Alexey Bataev7ff55242014-06-19 09:13:45 +0000944 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
945 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000946};
Alexey Bataeved09d242014-05-28 05:53:51 +0000947} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000948
Alexey Bataevbae9a792014-06-27 10:37:06 +0000949void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000950 switch (DKind) {
951 case OMPD_parallel: {
952 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
953 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000954 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000955 std::make_pair(".global_tid.", KmpInt32PtrTy),
956 std::make_pair(".bound_tid.", KmpInt32PtrTy),
957 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000958 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000959 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
960 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000961 break;
962 }
963 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000964 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000965 std::make_pair(StringRef(), QualType()) // __context with shared vars
966 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000967 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
968 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000969 break;
970 }
971 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000972 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000973 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000974 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000975 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
976 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000977 break;
978 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000979 case OMPD_sections: {
980 Sema::CapturedParamNameType Params[] = {
981 std::make_pair(StringRef(), QualType()) // __context with shared vars
982 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000983 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
984 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000985 break;
986 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000987 case OMPD_section: {
988 Sema::CapturedParamNameType Params[] = {
989 std::make_pair(StringRef(), QualType()) // __context with shared vars
990 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000991 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
992 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000993 break;
994 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000995 case OMPD_single: {
996 Sema::CapturedParamNameType Params[] = {
997 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 Bataevd1e40fb2014-06-26 12:05:45 +00001001 break;
1002 }
Alexander Musman80c22892014-07-17 08:54:58 +00001003 case OMPD_master: {
1004 Sema::CapturedParamNameType Params[] = {
1005 std::make_pair(StringRef(), QualType()) // __context with shared vars
1006 };
1007 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1008 Params);
1009 break;
1010 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001011 case OMPD_parallel_for: {
1012 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1013 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1014 Sema::CapturedParamNameType Params[] = {
1015 std::make_pair(".global_tid.", KmpInt32PtrTy),
1016 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1017 std::make_pair(StringRef(), QualType()) // __context with shared vars
1018 };
1019 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1020 Params);
1021 break;
1022 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001023 case OMPD_parallel_sections: {
1024 Sema::CapturedParamNameType Params[] = {
1025 std::make_pair(StringRef(), QualType()) // __context with shared vars
1026 };
1027 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1028 Params);
1029 break;
1030 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001031 case OMPD_task: {
1032 Sema::CapturedParamNameType Params[] = {
1033 std::make_pair(StringRef(), QualType()) // __context with shared vars
1034 };
1035 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1036 Params);
1037 break;
1038 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001039 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001040 llvm_unreachable("OpenMP Directive is not allowed");
1041 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001042 llvm_unreachable("Unknown OpenMP directive");
1043 }
1044}
1045
Alexey Bataev549210e2014-06-24 04:39:47 +00001046bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1047 OpenMPDirectiveKind CurrentRegion,
1048 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001049 // Allowed nesting of constructs
1050 // +------------------+-----------------+------------------------------------+
1051 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1052 // +------------------+-----------------+------------------------------------+
1053 // | parallel | parallel | * |
1054 // | parallel | for | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001055 // | parallel | master | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001056 // | parallel | simd | * |
1057 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001058 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001059 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001060 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001061 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001062 // | parallel | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001063 // +------------------+-----------------+------------------------------------+
1064 // | for | parallel | * |
1065 // | for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001066 // | for | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001067 // | for | simd | * |
1068 // | for | sections | + |
1069 // | for | section | + |
1070 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001071 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001072 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001073 // | for | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001074 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001075 // | master | parallel | * |
1076 // | master | for | + |
1077 // | master | master | * |
1078 // | master | simd | * |
1079 // | master | sections | + |
1080 // | master | section | + |
1081 // | master | single | + |
1082 // | master | parallel for | * |
1083 // | master |parallel sections| * |
1084 // | master | task | * |
1085 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001086 // | simd | parallel | |
1087 // | simd | for | |
Alexander Musman80c22892014-07-17 08:54:58 +00001088 // | simd | master | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001089 // | simd | simd | |
1090 // | simd | sections | |
1091 // | simd | section | |
1092 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001093 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001094 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001095 // | simd | task | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001096 // +------------------+-----------------+------------------------------------+
1097 // | sections | parallel | * |
1098 // | sections | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001099 // | sections | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001100 // | sections | simd | * |
1101 // | sections | sections | + |
1102 // | sections | section | * |
1103 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001104 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001105 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001106 // | sections | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001107 // +------------------+-----------------+------------------------------------+
1108 // | section | parallel | * |
1109 // | section | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001110 // | section | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001111 // | section | simd | * |
1112 // | section | sections | + |
1113 // | section | section | + |
1114 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001115 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001116 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001117 // | section | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001118 // +------------------+-----------------+------------------------------------+
1119 // | single | parallel | * |
1120 // | single | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001121 // | single | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001122 // | single | simd | * |
1123 // | single | sections | + |
1124 // | single | section | + |
1125 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001126 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001127 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001128 // | single | task | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001129 // +------------------+-----------------+------------------------------------+
1130 // | parallel for | parallel | * |
1131 // | parallel for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001132 // | parallel for | master | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001133 // | parallel for | simd | * |
1134 // | parallel for | sections | + |
1135 // | parallel for | section | + |
1136 // | parallel for | single | + |
1137 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001138 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001139 // | parallel for | task | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001140 // +------------------+-----------------+------------------------------------+
1141 // | parallel sections| parallel | * |
1142 // | parallel sections| for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001143 // | parallel sections| master | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001144 // | parallel sections| simd | * |
1145 // | parallel sections| sections | + |
1146 // | parallel sections| section | * |
1147 // | parallel sections| single | + |
1148 // | parallel sections| parallel for | * |
1149 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001150 // | parallel sections| task | * |
1151 // +------------------+-----------------+------------------------------------+
1152 // | task | parallel | * |
1153 // | task | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001154 // | task | master | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001155 // | task | simd | * |
1156 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001157 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001158 // | task | single | + |
1159 // | task | parallel for | * |
1160 // | task |parallel sections| * |
1161 // | task | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001162 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001163 if (Stack->getCurScope()) {
1164 auto ParentRegion = Stack->getParentDirective();
1165 bool NestingProhibited = false;
1166 bool CloseNesting = true;
1167 bool ShouldBeInParallelRegion = false;
1168 if (isOpenMPSimdDirective(ParentRegion)) {
1169 // OpenMP [2.16, Nesting of Regions]
1170 // OpenMP constructs may not be nested inside a simd region.
1171 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1172 return true;
1173 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001174 if (CurrentRegion == OMPD_section) {
1175 // OpenMP [2.7.2, sections Construct, Restrictions]
1176 // Orphaned section directives are prohibited. That is, the section
1177 // directives must appear within the sections construct and must not be
1178 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001179 if (ParentRegion != OMPD_sections &&
1180 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001181 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1182 << (ParentRegion != OMPD_unknown)
1183 << getOpenMPDirectiveName(ParentRegion);
1184 return true;
1185 }
1186 return false;
1187 }
Alexander Musman80c22892014-07-17 08:54:58 +00001188 if (CurrentRegion == OMPD_master) {
1189 // OpenMP [2.16, Nesting of Regions]
1190 // A master region may not be closely nested inside a worksharing,
1191 // atomic (TODO), or explicit task region.
1192 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1193 ParentRegion == OMPD_task;
1194 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1195 !isOpenMPParallelDirective(CurrentRegion) &&
1196 !isOpenMPSimdDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001197 // OpenMP [2.16, Nesting of Regions]
1198 // A worksharing region may not be closely nested inside a worksharing,
1199 // explicit task, critical, ordered, atomic, or master region.
1200 // TODO
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001201 NestingProhibited = (isOpenMPWorksharingDirective(ParentRegion) &&
1202 !isOpenMPSimdDirective(ParentRegion)) ||
Alexander Musman80c22892014-07-17 08:54:58 +00001203 ParentRegion == OMPD_task ||
1204 ParentRegion == OMPD_master;
Alexey Bataev549210e2014-06-24 04:39:47 +00001205 ShouldBeInParallelRegion = true;
1206 }
1207 if (NestingProhibited) {
1208 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev41b97322014-07-02 03:04:53 +00001209 << CloseNesting << getOpenMPDirectiveName(ParentRegion)
1210 << ShouldBeInParallelRegion << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001211 return true;
1212 }
1213 }
1214 return false;
1215}
1216
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001217StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1218 ArrayRef<OMPClause *> Clauses,
1219 Stmt *AStmt,
1220 SourceLocation StartLoc,
1221 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001222 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1223
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001224 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001225 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1226 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001227
1228 // Check default data sharing attributes for referenced variables.
1229 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1230 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1231 if (DSAChecker.isErrorFound())
1232 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001233 // Generate list of implicitly defined firstprivate variables.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001234 auto &VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001235 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1236 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1237
1238 bool ErrorFound = false;
1239 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001240 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1241 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1242 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001243 ClausesWithImplicit.push_back(Implicit);
1244 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001245 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246 } else
1247 ErrorFound = true;
1248 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001249
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001250 switch (Kind) {
1251 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001252 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1253 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001254 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001255 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001256 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1257 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001258 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001259 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001260 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1261 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001262 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001263 case OMPD_sections:
1264 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1265 EndLoc);
1266 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001267 case OMPD_section:
1268 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001269 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001270 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1271 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001272 case OMPD_single:
1273 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1274 EndLoc);
1275 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001276 case OMPD_master:
1277 assert(ClausesWithImplicit.empty() &&
1278 "No clauses are allowed for 'omp master' directive");
1279 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1280 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001281 case OMPD_parallel_for:
1282 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1283 EndLoc, VarsWithInheritedDSA);
1284 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001285 case OMPD_parallel_sections:
1286 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1287 StartLoc, EndLoc);
1288 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001289 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001290 Res =
1291 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1292 break;
1293 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001294 llvm_unreachable("OpenMP Directive is not allowed");
1295 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001296 llvm_unreachable("Unknown OpenMP directive");
1297 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001298
Alexey Bataev4acb8592014-07-07 13:01:15 +00001299 for (auto P : VarsWithInheritedDSA) {
1300 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1301 << P.first << P.second->getSourceRange();
1302 }
1303 if (!VarsWithInheritedDSA.empty())
1304 return StmtError();
1305
Alexey Bataeved09d242014-05-28 05:53:51 +00001306 if (ErrorFound)
1307 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001308 return Res;
1309}
1310
1311StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1312 Stmt *AStmt,
1313 SourceLocation StartLoc,
1314 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001315 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1316 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1317 // 1.2.2 OpenMP Language Terminology
1318 // Structured block - An executable statement with a single entry at the
1319 // top and a single exit at the bottom.
1320 // The point of exit cannot be a branch out of the structured block.
1321 // longjmp() and throw() must not violate the entry/exit criteria.
1322 CS->getCapturedDecl()->setNothrow();
1323
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001324 getCurFunction()->setHasBranchProtectedScope();
1325
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001326 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1327 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001328}
1329
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001330namespace {
1331/// \brief Helper class for checking canonical form of the OpenMP loops and
1332/// extracting iteration space of each loop in the loop nest, that will be used
1333/// for IR generation.
1334class OpenMPIterationSpaceChecker {
1335 /// \brief Reference to Sema.
1336 Sema &SemaRef;
1337 /// \brief A location for diagnostics (when there is no some better location).
1338 SourceLocation DefaultLoc;
1339 /// \brief A location for diagnostics (when increment is not compatible).
1340 SourceLocation ConditionLoc;
1341 /// \brief A source location for referring to condition later.
1342 SourceRange ConditionSrcRange;
1343 /// \brief Loop variable.
1344 VarDecl *Var;
1345 /// \brief Lower bound (initializer for the var).
1346 Expr *LB;
1347 /// \brief Upper bound.
1348 Expr *UB;
1349 /// \brief Loop step (increment).
1350 Expr *Step;
1351 /// \brief This flag is true when condition is one of:
1352 /// Var < UB
1353 /// Var <= UB
1354 /// UB > Var
1355 /// UB >= Var
1356 bool TestIsLessOp;
1357 /// \brief This flag is true when condition is strict ( < or > ).
1358 bool TestIsStrictOp;
1359 /// \brief This flag is true when step is subtracted on each iteration.
1360 bool SubtractStep;
1361
1362public:
1363 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1364 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1365 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1366 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1367 SubtractStep(false) {}
1368 /// \brief Check init-expr for canonical loop form and save loop counter
1369 /// variable - #Var and its initialization value - #LB.
1370 bool CheckInit(Stmt *S);
1371 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1372 /// for less/greater and for strict/non-strict comparison.
1373 bool CheckCond(Expr *S);
1374 /// \brief Check incr-expr for canonical loop form and return true if it
1375 /// does not conform, otherwise save loop step (#Step).
1376 bool CheckInc(Expr *S);
1377 /// \brief Return the loop counter variable.
1378 VarDecl *GetLoopVar() const { return Var; }
1379 /// \brief Return true if any expression is dependent.
1380 bool Dependent() const;
1381
1382private:
1383 /// \brief Check the right-hand side of an assignment in the increment
1384 /// expression.
1385 bool CheckIncRHS(Expr *RHS);
1386 /// \brief Helper to set loop counter variable and its initializer.
1387 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1388 /// \brief Helper to set upper bound.
1389 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1390 const SourceLocation &SL);
1391 /// \brief Helper to set loop increment.
1392 bool SetStep(Expr *NewStep, bool Subtract);
1393};
1394
1395bool OpenMPIterationSpaceChecker::Dependent() const {
1396 if (!Var) {
1397 assert(!LB && !UB && !Step);
1398 return false;
1399 }
1400 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1401 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1402}
1403
1404bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1405 // State consistency checking to ensure correct usage.
1406 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1407 !TestIsLessOp && !TestIsStrictOp);
1408 if (!NewVar || !NewLB)
1409 return true;
1410 Var = NewVar;
1411 LB = NewLB;
1412 return false;
1413}
1414
1415bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1416 const SourceRange &SR,
1417 const SourceLocation &SL) {
1418 // State consistency checking to ensure correct usage.
1419 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1420 !TestIsLessOp && !TestIsStrictOp);
1421 if (!NewUB)
1422 return true;
1423 UB = NewUB;
1424 TestIsLessOp = LessOp;
1425 TestIsStrictOp = StrictOp;
1426 ConditionSrcRange = SR;
1427 ConditionLoc = SL;
1428 return false;
1429}
1430
1431bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1432 // State consistency checking to ensure correct usage.
1433 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1434 if (!NewStep)
1435 return true;
1436 if (!NewStep->isValueDependent()) {
1437 // Check that the step is integer expression.
1438 SourceLocation StepLoc = NewStep->getLocStart();
1439 ExprResult Val =
1440 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1441 if (Val.isInvalid())
1442 return true;
1443 NewStep = Val.get();
1444
1445 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1446 // If test-expr is of form var relational-op b and relational-op is < or
1447 // <= then incr-expr must cause var to increase on each iteration of the
1448 // loop. If test-expr is of form var relational-op b and relational-op is
1449 // > or >= then incr-expr must cause var to decrease on each iteration of
1450 // the loop.
1451 // If test-expr is of form b relational-op var and relational-op is < or
1452 // <= then incr-expr must cause var to decrease on each iteration of the
1453 // loop. If test-expr is of form b relational-op var and relational-op is
1454 // > or >= then incr-expr must cause var to increase on each iteration of
1455 // the loop.
1456 llvm::APSInt Result;
1457 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1458 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1459 bool IsConstNeg =
1460 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1461 bool IsConstZero = IsConstant && !Result.getBoolValue();
1462 if (UB && (IsConstZero ||
1463 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1464 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1465 SemaRef.Diag(NewStep->getExprLoc(),
1466 diag::err_omp_loop_incr_not_compatible)
1467 << Var << TestIsLessOp << NewStep->getSourceRange();
1468 SemaRef.Diag(ConditionLoc,
1469 diag::note_omp_loop_cond_requres_compatible_incr)
1470 << TestIsLessOp << ConditionSrcRange;
1471 return true;
1472 }
1473 }
1474
1475 Step = NewStep;
1476 SubtractStep = Subtract;
1477 return false;
1478}
1479
1480bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1481 // Check init-expr for canonical loop form and save loop counter
1482 // variable - #Var and its initialization value - #LB.
1483 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1484 // var = lb
1485 // integer-type var = lb
1486 // random-access-iterator-type var = lb
1487 // pointer-type var = lb
1488 //
1489 if (!S) {
1490 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1491 return true;
1492 }
1493 if (Expr *E = dyn_cast<Expr>(S))
1494 S = E->IgnoreParens();
1495 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1496 if (BO->getOpcode() == BO_Assign)
1497 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1498 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1499 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1500 if (DS->isSingleDecl()) {
1501 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1502 if (Var->hasInit()) {
1503 // Accept non-canonical init form here but emit ext. warning.
1504 if (Var->getInitStyle() != VarDecl::CInit)
1505 SemaRef.Diag(S->getLocStart(),
1506 diag::ext_omp_loop_not_canonical_init)
1507 << S->getSourceRange();
1508 return SetVarAndLB(Var, Var->getInit());
1509 }
1510 }
1511 }
1512 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1513 if (CE->getOperator() == OO_Equal)
1514 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1515 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1516
1517 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1518 << S->getSourceRange();
1519 return true;
1520}
1521
Alexey Bataev23b69422014-06-18 07:08:49 +00001522/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001523/// variable (which may be the loop variable) if possible.
1524static const VarDecl *GetInitVarDecl(const Expr *E) {
1525 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001526 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001527 E = E->IgnoreParenImpCasts();
1528 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1529 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1530 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1531 CE->getArg(0) != nullptr)
1532 E = CE->getArg(0)->IgnoreParenImpCasts();
1533 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1534 if (!DRE)
1535 return nullptr;
1536 return dyn_cast<VarDecl>(DRE->getDecl());
1537}
1538
1539bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1540 // Check test-expr for canonical form, save upper-bound UB, flags for
1541 // less/greater and for strict/non-strict comparison.
1542 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1543 // var relational-op b
1544 // b relational-op var
1545 //
1546 if (!S) {
1547 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1548 return true;
1549 }
1550 S = S->IgnoreParenImpCasts();
1551 SourceLocation CondLoc = S->getLocStart();
1552 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1553 if (BO->isRelationalOp()) {
1554 if (GetInitVarDecl(BO->getLHS()) == Var)
1555 return SetUB(BO->getRHS(),
1556 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1557 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1558 BO->getSourceRange(), BO->getOperatorLoc());
1559 if (GetInitVarDecl(BO->getRHS()) == Var)
1560 return SetUB(BO->getLHS(),
1561 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1562 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1563 BO->getSourceRange(), BO->getOperatorLoc());
1564 }
1565 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1566 if (CE->getNumArgs() == 2) {
1567 auto Op = CE->getOperator();
1568 switch (Op) {
1569 case OO_Greater:
1570 case OO_GreaterEqual:
1571 case OO_Less:
1572 case OO_LessEqual:
1573 if (GetInitVarDecl(CE->getArg(0)) == Var)
1574 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1575 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1576 CE->getOperatorLoc());
1577 if (GetInitVarDecl(CE->getArg(1)) == Var)
1578 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1579 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1580 CE->getOperatorLoc());
1581 break;
1582 default:
1583 break;
1584 }
1585 }
1586 }
1587 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1588 << S->getSourceRange() << Var;
1589 return true;
1590}
1591
1592bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1593 // RHS of canonical loop form increment can be:
1594 // var + incr
1595 // incr + var
1596 // var - incr
1597 //
1598 RHS = RHS->IgnoreParenImpCasts();
1599 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1600 if (BO->isAdditiveOp()) {
1601 bool IsAdd = BO->getOpcode() == BO_Add;
1602 if (GetInitVarDecl(BO->getLHS()) == Var)
1603 return SetStep(BO->getRHS(), !IsAdd);
1604 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1605 return SetStep(BO->getLHS(), false);
1606 }
1607 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1608 bool IsAdd = CE->getOperator() == OO_Plus;
1609 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1610 if (GetInitVarDecl(CE->getArg(0)) == Var)
1611 return SetStep(CE->getArg(1), !IsAdd);
1612 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1613 return SetStep(CE->getArg(0), false);
1614 }
1615 }
1616 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1617 << RHS->getSourceRange() << Var;
1618 return true;
1619}
1620
1621bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1622 // Check incr-expr for canonical loop form and return true if it
1623 // does not conform.
1624 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1625 // ++var
1626 // var++
1627 // --var
1628 // var--
1629 // var += incr
1630 // var -= incr
1631 // var = var + incr
1632 // var = incr + var
1633 // var = var - incr
1634 //
1635 if (!S) {
1636 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1637 return true;
1638 }
1639 S = S->IgnoreParens();
1640 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1641 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1642 return SetStep(
1643 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1644 (UO->isDecrementOp() ? -1 : 1)).get(),
1645 false);
1646 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1647 switch (BO->getOpcode()) {
1648 case BO_AddAssign:
1649 case BO_SubAssign:
1650 if (GetInitVarDecl(BO->getLHS()) == Var)
1651 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1652 break;
1653 case BO_Assign:
1654 if (GetInitVarDecl(BO->getLHS()) == Var)
1655 return CheckIncRHS(BO->getRHS());
1656 break;
1657 default:
1658 break;
1659 }
1660 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1661 switch (CE->getOperator()) {
1662 case OO_PlusPlus:
1663 case OO_MinusMinus:
1664 if (GetInitVarDecl(CE->getArg(0)) == Var)
1665 return SetStep(
1666 SemaRef.ActOnIntegerConstant(
1667 CE->getLocStart(),
1668 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1669 false);
1670 break;
1671 case OO_PlusEqual:
1672 case OO_MinusEqual:
1673 if (GetInitVarDecl(CE->getArg(0)) == Var)
1674 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1675 break;
1676 case OO_Equal:
1677 if (GetInitVarDecl(CE->getArg(0)) == Var)
1678 return CheckIncRHS(CE->getArg(1));
1679 break;
1680 default:
1681 break;
1682 }
1683 }
1684 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1685 << S->getSourceRange() << Var;
1686 return true;
1687}
Alexey Bataev23b69422014-06-18 07:08:49 +00001688} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001689
1690/// \brief Called on a for stmt to check and extract its iteration space
1691/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001692static bool CheckOpenMPIterationSpace(
1693 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1694 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1695 Expr *NestedLoopCountExpr,
1696 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001697 // OpenMP [2.6, Canonical Loop Form]
1698 // for (init-expr; test-expr; incr-expr) structured-block
1699 auto For = dyn_cast_or_null<ForStmt>(S);
1700 if (!For) {
1701 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001702 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1703 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1704 << CurrentNestedLoopCount;
1705 if (NestedLoopCount > 1)
1706 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1707 diag::note_omp_collapse_expr)
1708 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001709 return true;
1710 }
1711 assert(For->getBody());
1712
1713 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1714
1715 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001716 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001717 if (ISC.CheckInit(Init)) {
1718 return true;
1719 }
1720
1721 bool HasErrors = false;
1722
1723 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001724 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001725
1726 // OpenMP [2.6, Canonical Loop Form]
1727 // Var is one of the following:
1728 // A variable of signed or unsigned integer type.
1729 // For C++, a variable of a random access iterator type.
1730 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001731 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001732 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1733 !VarType->isPointerType() &&
1734 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1735 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1736 << SemaRef.getLangOpts().CPlusPlus;
1737 HasErrors = true;
1738 }
1739
Alexey Bataev4acb8592014-07-07 13:01:15 +00001740 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
1741 // Construct
1742 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1743 // parallel for construct is (are) private.
1744 // The loop iteration variable in the associated for-loop of a simd construct
1745 // with just one associated for-loop is linear with a constant-linear-step
1746 // that is the increment of the associated for-loop.
1747 // Exclude loop var from the list of variables with implicitly defined data
1748 // sharing attributes.
1749 while (VarsWithImplicitDSA.count(Var) > 0)
1750 VarsWithImplicitDSA.erase(Var);
1751
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001752 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1753 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001754 // The loop iteration variable in the associated for-loop of a simd construct
1755 // with just one associated for-loop may be listed in a linear clause with a
1756 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001757 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1758 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001759 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001760 auto PredeterminedCKind =
1761 isOpenMPSimdDirective(DKind)
1762 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
1763 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001764 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001765 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00001766 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1767 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001768 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001769 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00001770 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
1771 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001772 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001773 HasErrors = true;
1774 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001775 // Make the loop iteration variable private (for worksharing constructs),
1776 // linear (for simd directives with the only one associated loop) or
1777 // lastprivate (for simd directives with several collapsed loops).
1778 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001779 }
1780
Alexey Bataev7ff55242014-06-19 09:13:45 +00001781 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001782
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001783 // Check test-expr.
1784 HasErrors |= ISC.CheckCond(For->getCond());
1785
1786 // Check incr-expr.
1787 HasErrors |= ISC.CheckInc(For->getInc());
1788
1789 if (ISC.Dependent())
1790 return HasErrors;
1791
1792 // FIXME: Build loop's iteration space representation.
1793 return HasErrors;
1794}
1795
1796/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1797/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1798/// to get the first for loop.
1799static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1800 if (IgnoreCaptured)
1801 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1802 S = CapS->getCapturedStmt();
1803 // OpenMP [2.8.1, simd construct, Restrictions]
1804 // All loops associated with the construct must be perfectly nested; that is,
1805 // there must be no intervening code nor any OpenMP directive between any two
1806 // loops.
1807 while (true) {
1808 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1809 S = AS->getSubStmt();
1810 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1811 if (CS->size() != 1)
1812 break;
1813 S = CS->body_back();
1814 } else
1815 break;
1816 }
1817 return S;
1818}
1819
1820/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001821/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1822/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001823static unsigned
1824CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
1825 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
1826 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001827 unsigned NestedLoopCount = 1;
1828 if (NestedLoopCountExpr) {
1829 // Found 'collapse' clause - calculate collapse number.
1830 llvm::APSInt Result;
1831 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1832 NestedLoopCount = Result.getLimitedValue();
1833 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001834 // This is helper routine for loop directives (e.g., 'for', 'simd',
1835 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001836 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1837 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001838 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00001839 NestedLoopCount, NestedLoopCountExpr,
1840 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001841 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001842 // Move on to the next nested for loop, or to the loop body.
1843 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1844 }
1845
1846 // FIXME: Build resulting iteration space for IR generation (collapsing
1847 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001848 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001849}
1850
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001851static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001852 auto CollapseFilter = [](const OMPClause *C) -> bool {
1853 return C->getClauseKind() == OMPC_collapse;
1854 };
1855 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1856 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001857 if (I)
1858 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1859 return nullptr;
1860}
1861
Alexey Bataev4acb8592014-07-07 13:01:15 +00001862StmtResult Sema::ActOnOpenMPSimdDirective(
1863 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1864 SourceLocation EndLoc,
1865 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001866 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001867 unsigned NestedLoopCount =
1868 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
1869 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001870 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001871 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001872
1873 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001874 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1875 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001876}
1877
Alexey Bataev4acb8592014-07-07 13:01:15 +00001878StmtResult Sema::ActOnOpenMPForDirective(
1879 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1880 SourceLocation EndLoc,
1881 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001882 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001883 unsigned NestedLoopCount =
1884 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
1885 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001886 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001887 return StmtError();
1888
1889 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001890 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1891 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001892}
1893
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001894StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1895 Stmt *AStmt,
1896 SourceLocation StartLoc,
1897 SourceLocation EndLoc) {
1898 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1899 auto BaseStmt = AStmt;
1900 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1901 BaseStmt = CS->getCapturedStmt();
1902 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1903 auto S = C->children();
1904 if (!S)
1905 return StmtError();
1906 // All associated statements must be '#pragma omp section' except for
1907 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001908 for (++S; S; ++S) {
1909 auto SectionStmt = *S;
1910 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1911 if (SectionStmt)
1912 Diag(SectionStmt->getLocStart(),
1913 diag::err_omp_sections_substmt_not_section);
1914 return StmtError();
1915 }
1916 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001917 } else {
1918 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1919 return StmtError();
1920 }
1921
1922 getCurFunction()->setHasBranchProtectedScope();
1923
1924 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1925 AStmt);
1926}
1927
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001928StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1929 SourceLocation StartLoc,
1930 SourceLocation EndLoc) {
1931 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1932
1933 getCurFunction()->setHasBranchProtectedScope();
1934
1935 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1936}
1937
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001938StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1939 Stmt *AStmt,
1940 SourceLocation StartLoc,
1941 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00001942 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1943
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001944 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00001945
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001946 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1947}
1948
Alexander Musman80c22892014-07-17 08:54:58 +00001949StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
1950 SourceLocation StartLoc,
1951 SourceLocation EndLoc) {
1952 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1953
1954 getCurFunction()->setHasBranchProtectedScope();
1955
1956 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
1957}
1958
Alexey Bataev4acb8592014-07-07 13:01:15 +00001959StmtResult Sema::ActOnOpenMPParallelForDirective(
1960 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1961 SourceLocation EndLoc,
1962 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
1963 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1964 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1965 // 1.2.2 OpenMP Language Terminology
1966 // Structured block - An executable statement with a single entry at the
1967 // top and a single exit at the bottom.
1968 // The point of exit cannot be a branch out of the structured block.
1969 // longjmp() and throw() must not violate the entry/exit criteria.
1970 CS->getCapturedDecl()->setNothrow();
1971
1972 // In presence of clause 'collapse', it will define the nested loops number.
1973 unsigned NestedLoopCount =
1974 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
1975 *this, *DSAStack, VarsWithImplicitDSA);
1976 if (NestedLoopCount == 0)
1977 return StmtError();
1978
1979 getCurFunction()->setHasBranchProtectedScope();
1980 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
1981 NestedLoopCount, Clauses, AStmt);
1982}
1983
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001984StmtResult
1985Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
1986 Stmt *AStmt, SourceLocation StartLoc,
1987 SourceLocation EndLoc) {
1988 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1989 auto BaseStmt = AStmt;
1990 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1991 BaseStmt = CS->getCapturedStmt();
1992 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1993 auto S = C->children();
1994 if (!S)
1995 return StmtError();
1996 // All associated statements must be '#pragma omp section' except for
1997 // the first one.
1998 for (++S; S; ++S) {
1999 auto SectionStmt = *S;
2000 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2001 if (SectionStmt)
2002 Diag(SectionStmt->getLocStart(),
2003 diag::err_omp_parallel_sections_substmt_not_section);
2004 return StmtError();
2005 }
2006 }
2007 } else {
2008 Diag(AStmt->getLocStart(),
2009 diag::err_omp_parallel_sections_not_compound_stmt);
2010 return StmtError();
2011 }
2012
2013 getCurFunction()->setHasBranchProtectedScope();
2014
2015 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2016 Clauses, AStmt);
2017}
2018
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002019StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2020 Stmt *AStmt, SourceLocation StartLoc,
2021 SourceLocation EndLoc) {
2022 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2023 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2024 // 1.2.2 OpenMP Language Terminology
2025 // Structured block - An executable statement with a single entry at the
2026 // top and a single exit at the bottom.
2027 // The point of exit cannot be a branch out of the structured block.
2028 // longjmp() and throw() must not violate the entry/exit criteria.
2029 CS->getCapturedDecl()->setNothrow();
2030
2031 getCurFunction()->setHasBranchProtectedScope();
2032
2033 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2034}
2035
Alexey Bataeved09d242014-05-28 05:53:51 +00002036OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002037 SourceLocation StartLoc,
2038 SourceLocation LParenLoc,
2039 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002040 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002041 switch (Kind) {
2042 case OMPC_if:
2043 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2044 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002045 case OMPC_final:
2046 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2047 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002048 case OMPC_num_threads:
2049 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2050 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002051 case OMPC_safelen:
2052 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2053 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002054 case OMPC_collapse:
2055 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2056 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002057 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002058 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002059 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002060 case OMPC_private:
2061 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002062 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002063 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002064 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002065 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002066 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002067 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002068 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002069 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002070 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002071 case OMPC_threadprivate:
2072 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002073 llvm_unreachable("Clause is not allowed.");
2074 }
2075 return Res;
2076}
2077
Alexey Bataeved09d242014-05-28 05:53:51 +00002078OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002079 SourceLocation LParenLoc,
2080 SourceLocation EndLoc) {
2081 Expr *ValExpr = Condition;
2082 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2083 !Condition->isInstantiationDependent() &&
2084 !Condition->containsUnexpandedParameterPack()) {
2085 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002086 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002087 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002088 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002089
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002090 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002091 }
2092
2093 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2094}
2095
Alexey Bataev3778b602014-07-17 07:32:53 +00002096OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2097 SourceLocation StartLoc,
2098 SourceLocation LParenLoc,
2099 SourceLocation EndLoc) {
2100 Expr *ValExpr = Condition;
2101 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2102 !Condition->isInstantiationDependent() &&
2103 !Condition->containsUnexpandedParameterPack()) {
2104 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2105 Condition->getExprLoc(), Condition);
2106 if (Val.isInvalid())
2107 return nullptr;
2108
2109 ValExpr = Val.get();
2110 }
2111
2112 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2113}
2114
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002115ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2116 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002117 if (!Op)
2118 return ExprError();
2119
2120 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2121 public:
2122 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002123 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002124 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2125 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002126 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2127 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002128 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2129 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002130 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2131 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002132 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2133 QualType T,
2134 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002135 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2136 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002137 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2138 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002139 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002140 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002141 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002142 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2143 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002144 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2145 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002146 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2147 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002148 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002149 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002150 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002151 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2152 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002153 llvm_unreachable("conversion functions are permitted");
2154 }
2155 } ConvertDiagnoser;
2156 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2157}
2158
2159OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2160 SourceLocation StartLoc,
2161 SourceLocation LParenLoc,
2162 SourceLocation EndLoc) {
2163 Expr *ValExpr = NumThreads;
2164 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2165 !NumThreads->isInstantiationDependent() &&
2166 !NumThreads->containsUnexpandedParameterPack()) {
2167 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2168 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002169 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002170 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002171 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002172
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002173 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002174
2175 // OpenMP [2.5, Restrictions]
2176 // The num_threads expression must evaluate to a positive integer value.
2177 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002178 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2179 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002180 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2181 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002182 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002183 }
2184 }
2185
Alexey Bataeved09d242014-05-28 05:53:51 +00002186 return new (Context)
2187 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002188}
2189
Alexey Bataev62c87d22014-03-21 04:51:18 +00002190ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2191 OpenMPClauseKind CKind) {
2192 if (!E)
2193 return ExprError();
2194 if (E->isValueDependent() || E->isTypeDependent() ||
2195 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002196 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002197 llvm::APSInt Result;
2198 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2199 if (ICE.isInvalid())
2200 return ExprError();
2201 if (!Result.isStrictlyPositive()) {
2202 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2203 << getOpenMPClauseName(CKind) << E->getSourceRange();
2204 return ExprError();
2205 }
2206 return ICE;
2207}
2208
2209OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2210 SourceLocation LParenLoc,
2211 SourceLocation EndLoc) {
2212 // OpenMP [2.8.1, simd construct, Description]
2213 // The parameter of the safelen clause must be a constant
2214 // positive integer expression.
2215 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2216 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002217 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002218 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002219 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002220}
2221
Alexander Musman64d33f12014-06-04 07:53:32 +00002222OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2223 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002224 SourceLocation LParenLoc,
2225 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002226 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002227 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002228 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002229 // The parameter of the collapse clause must be a constant
2230 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002231 ExprResult NumForLoopsResult =
2232 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2233 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002234 return nullptr;
2235 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002236 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002237}
2238
Alexey Bataeved09d242014-05-28 05:53:51 +00002239OMPClause *Sema::ActOnOpenMPSimpleClause(
2240 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2241 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002242 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002243 switch (Kind) {
2244 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002245 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002246 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2247 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002248 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002249 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002250 Res = ActOnOpenMPProcBindClause(
2251 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2252 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002253 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002254 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002255 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002256 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002257 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002258 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002259 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002260 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002261 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002262 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002263 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002264 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002265 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002266 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002267 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002268 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002269 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002270 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002271 case OMPC_threadprivate:
2272 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002273 llvm_unreachable("Clause is not allowed.");
2274 }
2275 return Res;
2276}
2277
2278OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2279 SourceLocation KindKwLoc,
2280 SourceLocation StartLoc,
2281 SourceLocation LParenLoc,
2282 SourceLocation EndLoc) {
2283 if (Kind == OMPC_DEFAULT_unknown) {
2284 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002285 static_assert(OMPC_DEFAULT_unknown > 0,
2286 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002287 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002288 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002289 Values += "'";
2290 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2291 Values += "'";
2292 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002293 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002294 Values += " or ";
2295 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002296 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002297 break;
2298 default:
2299 Values += Sep;
2300 break;
2301 }
2302 }
2303 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002304 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002305 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002306 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002307 switch (Kind) {
2308 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002309 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002310 break;
2311 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002312 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002313 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002314 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002315 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002316 break;
2317 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002318 return new (Context)
2319 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002320}
2321
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002322OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2323 SourceLocation KindKwLoc,
2324 SourceLocation StartLoc,
2325 SourceLocation LParenLoc,
2326 SourceLocation EndLoc) {
2327 if (Kind == OMPC_PROC_BIND_unknown) {
2328 std::string Values;
2329 std::string Sep(", ");
2330 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2331 Values += "'";
2332 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2333 Values += "'";
2334 switch (i) {
2335 case OMPC_PROC_BIND_unknown - 2:
2336 Values += " or ";
2337 break;
2338 case OMPC_PROC_BIND_unknown - 1:
2339 break;
2340 default:
2341 Values += Sep;
2342 break;
2343 }
2344 }
2345 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002346 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002347 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002348 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002349 return new (Context)
2350 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002351}
2352
Alexey Bataev56dafe82014-06-20 07:16:17 +00002353OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2354 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2355 SourceLocation StartLoc, SourceLocation LParenLoc,
2356 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2357 SourceLocation EndLoc) {
2358 OMPClause *Res = nullptr;
2359 switch (Kind) {
2360 case OMPC_schedule:
2361 Res = ActOnOpenMPScheduleClause(
2362 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2363 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2364 break;
2365 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002366 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002367 case OMPC_num_threads:
2368 case OMPC_safelen:
2369 case OMPC_collapse:
2370 case OMPC_default:
2371 case OMPC_proc_bind:
2372 case OMPC_private:
2373 case OMPC_firstprivate:
2374 case OMPC_lastprivate:
2375 case OMPC_shared:
2376 case OMPC_reduction:
2377 case OMPC_linear:
2378 case OMPC_aligned:
2379 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002380 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002381 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002382 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002383 case OMPC_threadprivate:
2384 case OMPC_unknown:
2385 llvm_unreachable("Clause is not allowed.");
2386 }
2387 return Res;
2388}
2389
2390OMPClause *Sema::ActOnOpenMPScheduleClause(
2391 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2392 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2393 SourceLocation EndLoc) {
2394 if (Kind == OMPC_SCHEDULE_unknown) {
2395 std::string Values;
2396 std::string Sep(", ");
2397 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2398 Values += "'";
2399 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2400 Values += "'";
2401 switch (i) {
2402 case OMPC_SCHEDULE_unknown - 2:
2403 Values += " or ";
2404 break;
2405 case OMPC_SCHEDULE_unknown - 1:
2406 break;
2407 default:
2408 Values += Sep;
2409 break;
2410 }
2411 }
2412 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2413 << Values << getOpenMPClauseName(OMPC_schedule);
2414 return nullptr;
2415 }
2416 Expr *ValExpr = ChunkSize;
2417 if (ChunkSize) {
2418 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2419 !ChunkSize->isInstantiationDependent() &&
2420 !ChunkSize->containsUnexpandedParameterPack()) {
2421 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2422 ExprResult Val =
2423 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2424 if (Val.isInvalid())
2425 return nullptr;
2426
2427 ValExpr = Val.get();
2428
2429 // OpenMP [2.7.1, Restrictions]
2430 // chunk_size must be a loop invariant integer expression with a positive
2431 // value.
2432 llvm::APSInt Result;
2433 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2434 Result.isSigned() && !Result.isStrictlyPositive()) {
2435 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2436 << "schedule" << ChunkSize->getSourceRange();
2437 return nullptr;
2438 }
2439 }
2440 }
2441
2442 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2443 EndLoc, Kind, ValExpr);
2444}
2445
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002446OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2447 SourceLocation StartLoc,
2448 SourceLocation EndLoc) {
2449 OMPClause *Res = nullptr;
2450 switch (Kind) {
2451 case OMPC_ordered:
2452 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2453 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002454 case OMPC_nowait:
2455 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2456 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002457 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002458 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002459 case OMPC_num_threads:
2460 case OMPC_safelen:
2461 case OMPC_collapse:
2462 case OMPC_schedule:
2463 case OMPC_private:
2464 case OMPC_firstprivate:
2465 case OMPC_lastprivate:
2466 case OMPC_shared:
2467 case OMPC_reduction:
2468 case OMPC_linear:
2469 case OMPC_aligned:
2470 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002471 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002472 case OMPC_default:
2473 case OMPC_proc_bind:
2474 case OMPC_threadprivate:
2475 case OMPC_unknown:
2476 llvm_unreachable("Clause is not allowed.");
2477 }
2478 return Res;
2479}
2480
2481OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2482 SourceLocation EndLoc) {
2483 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2484}
2485
Alexey Bataev236070f2014-06-20 11:19:47 +00002486OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2487 SourceLocation EndLoc) {
2488 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2489}
2490
Alexey Bataevc5e02582014-06-16 07:08:35 +00002491OMPClause *Sema::ActOnOpenMPVarListClause(
2492 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2493 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2494 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2495 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002496 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002497 switch (Kind) {
2498 case OMPC_private:
2499 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2500 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002501 case OMPC_firstprivate:
2502 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2503 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002504 case OMPC_lastprivate:
2505 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2506 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002507 case OMPC_shared:
2508 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2509 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002510 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002511 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2512 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002513 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002514 case OMPC_linear:
2515 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2516 ColonLoc, EndLoc);
2517 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002518 case OMPC_aligned:
2519 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2520 ColonLoc, EndLoc);
2521 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002522 case OMPC_copyin:
2523 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2524 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002525 case OMPC_copyprivate:
2526 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2527 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002528 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002529 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002530 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002531 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002532 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002533 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002534 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002535 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002536 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002537 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002538 case OMPC_threadprivate:
2539 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002540 llvm_unreachable("Clause is not allowed.");
2541 }
2542 return Res;
2543}
2544
2545OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2546 SourceLocation StartLoc,
2547 SourceLocation LParenLoc,
2548 SourceLocation EndLoc) {
2549 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002550 for (auto &RefExpr : VarList) {
2551 assert(RefExpr && "NULL expr in OpenMP private clause.");
2552 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002553 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002554 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002555 continue;
2556 }
2557
Alexey Bataeved09d242014-05-28 05:53:51 +00002558 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002559 // OpenMP [2.1, C/C++]
2560 // A list item is a variable name.
2561 // OpenMP [2.9.3.3, Restrictions, p.1]
2562 // A variable that is part of another variable (as an array or
2563 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002564 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002565 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002566 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002567 continue;
2568 }
2569 Decl *D = DE->getDecl();
2570 VarDecl *VD = cast<VarDecl>(D);
2571
2572 QualType Type = VD->getType();
2573 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2574 // It will be analyzed later.
2575 Vars.push_back(DE);
2576 continue;
2577 }
2578
2579 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2580 // A variable that appears in a private clause must not have an incomplete
2581 // type or a reference type.
2582 if (RequireCompleteType(ELoc, Type,
2583 diag::err_omp_private_incomplete_type)) {
2584 continue;
2585 }
2586 if (Type->isReferenceType()) {
2587 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002588 << getOpenMPClauseName(OMPC_private) << Type;
2589 bool IsDecl =
2590 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2591 Diag(VD->getLocation(),
2592 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2593 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002594 continue;
2595 }
2596
2597 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2598 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002599 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002600 // class type.
2601 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002602 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2603 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002604 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002605 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2606 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2607 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002608 // FIXME This code must be replaced by actual constructing/destructing of
2609 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002610 if (RD) {
2611 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2612 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002613 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002614 if (!CD ||
2615 CheckConstructorAccess(ELoc, CD,
2616 InitializedEntity::InitializeTemporary(Type),
2617 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002618 CD->isDeleted()) {
2619 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002620 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002621 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2622 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002623 Diag(VD->getLocation(),
2624 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2625 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002626 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2627 continue;
2628 }
2629 MarkFunctionReferenced(ELoc, CD);
2630 DiagnoseUseOfDecl(CD, ELoc);
2631
2632 CXXDestructorDecl *DD = RD->getDestructor();
2633 if (DD) {
2634 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2635 DD->isDeleted()) {
2636 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002637 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002638 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2639 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002640 Diag(VD->getLocation(),
2641 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2642 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002643 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2644 continue;
2645 }
2646 MarkFunctionReferenced(ELoc, DD);
2647 DiagnoseUseOfDecl(DD, ELoc);
2648 }
2649 }
2650
Alexey Bataev758e55e2013-09-06 18:03:48 +00002651 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2652 // in a Construct]
2653 // Variables with the predetermined data-sharing attributes may not be
2654 // listed in data-sharing attributes clauses, except for the cases
2655 // listed below. For these exceptions only, listing a predetermined
2656 // variable in a data-sharing attribute clause is allowed and overrides
2657 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002658 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002659 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002660 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2661 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002662 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002663 continue;
2664 }
2665
2666 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002667 Vars.push_back(DE);
2668 }
2669
Alexey Bataeved09d242014-05-28 05:53:51 +00002670 if (Vars.empty())
2671 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002672
2673 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2674}
2675
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002676OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2677 SourceLocation StartLoc,
2678 SourceLocation LParenLoc,
2679 SourceLocation EndLoc) {
2680 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002681 bool IsImplicitClause =
2682 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
2683 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
2684
Alexey Bataeved09d242014-05-28 05:53:51 +00002685 for (auto &RefExpr : VarList) {
2686 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2687 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002688 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002689 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002690 continue;
2691 }
2692
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002693 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
2694 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002695 // OpenMP [2.1, C/C++]
2696 // A list item is a variable name.
2697 // OpenMP [2.9.3.3, Restrictions, p.1]
2698 // A variable that is part of another variable (as an array or
2699 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002700 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002701 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002702 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002703 continue;
2704 }
2705 Decl *D = DE->getDecl();
2706 VarDecl *VD = cast<VarDecl>(D);
2707
2708 QualType Type = VD->getType();
2709 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2710 // It will be analyzed later.
2711 Vars.push_back(DE);
2712 continue;
2713 }
2714
2715 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2716 // A variable that appears in a private clause must not have an incomplete
2717 // type or a reference type.
2718 if (RequireCompleteType(ELoc, Type,
2719 diag::err_omp_firstprivate_incomplete_type)) {
2720 continue;
2721 }
2722 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002723 if (IsImplicitClause) {
2724 Diag(ImplicitClauseLoc,
2725 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
2726 << Type;
2727 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2728 } else {
2729 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2730 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2731 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002732 bool IsDecl =
2733 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2734 Diag(VD->getLocation(),
2735 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2736 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002737 continue;
2738 }
2739
2740 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2741 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002742 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002743 // class type.
2744 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002745 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2746 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2747 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002748 // FIXME This code must be replaced by actual constructing/destructing of
2749 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002750 if (RD) {
2751 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2752 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002753 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002754 if (!CD ||
2755 CheckConstructorAccess(ELoc, CD,
2756 InitializedEntity::InitializeTemporary(Type),
2757 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002758 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002759 if (IsImplicitClause) {
2760 Diag(ImplicitClauseLoc,
2761 diag::err_omp_task_predetermined_firstprivate_required_method)
2762 << 0;
2763 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2764 } else {
2765 Diag(ELoc, diag::err_omp_required_method)
2766 << getOpenMPClauseName(OMPC_firstprivate) << 1;
2767 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002768 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2769 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002770 Diag(VD->getLocation(),
2771 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2772 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002773 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2774 continue;
2775 }
2776 MarkFunctionReferenced(ELoc, CD);
2777 DiagnoseUseOfDecl(CD, ELoc);
2778
2779 CXXDestructorDecl *DD = RD->getDestructor();
2780 if (DD) {
2781 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2782 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002783 if (IsImplicitClause) {
2784 Diag(ImplicitClauseLoc,
2785 diag::err_omp_task_predetermined_firstprivate_required_method)
2786 << 1;
2787 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2788 } else {
2789 Diag(ELoc, diag::err_omp_required_method)
2790 << getOpenMPClauseName(OMPC_firstprivate) << 4;
2791 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002792 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2793 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002794 Diag(VD->getLocation(),
2795 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2796 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002797 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2798 continue;
2799 }
2800 MarkFunctionReferenced(ELoc, DD);
2801 DiagnoseUseOfDecl(DD, ELoc);
2802 }
2803 }
2804
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002805 // If an implicit firstprivate variable found it was checked already.
2806 if (!IsImplicitClause) {
2807 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002808 Type = Type.getNonReferenceType().getCanonicalType();
2809 bool IsConstant = Type.isConstant(Context);
2810 Type = Context.getBaseElementType(Type);
2811 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2812 // A list item that specifies a given variable may not appear in more
2813 // than one clause on the same directive, except that a variable may be
2814 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002815 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002816 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002817 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002818 << getOpenMPClauseName(DVar.CKind)
2819 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002820 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002821 continue;
2822 }
2823
2824 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2825 // in a Construct]
2826 // Variables with the predetermined data-sharing attributes may not be
2827 // listed in data-sharing attributes clauses, except for the cases
2828 // listed below. For these exceptions only, listing a predetermined
2829 // variable in a data-sharing attribute clause is allowed and overrides
2830 // the variable's predetermined data-sharing attributes.
2831 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2832 // in a Construct, C/C++, p.2]
2833 // Variables with const-qualified type having no mutable member may be
2834 // listed in a firstprivate clause, even if they are static data members.
2835 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2836 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2837 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002838 << getOpenMPClauseName(DVar.CKind)
2839 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002840 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002841 continue;
2842 }
2843
Alexey Bataevf29276e2014-06-18 04:14:57 +00002844 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002845 // OpenMP [2.9.3.4, Restrictions, p.2]
2846 // A list item that is private within a parallel region must not appear
2847 // in a firstprivate clause on a worksharing construct if any of the
2848 // worksharing regions arising from the worksharing construct ever bind
2849 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002850 if (isOpenMPWorksharingDirective(CurrDir) &&
2851 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002852 DVar = DSAStack->getImplicitDSA(VD, true);
2853 if (DVar.CKind != OMPC_shared &&
2854 (isOpenMPParallelDirective(DVar.DKind) ||
2855 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002856 Diag(ELoc, diag::err_omp_required_access)
2857 << getOpenMPClauseName(OMPC_firstprivate)
2858 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002859 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002860 continue;
2861 }
2862 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002863 // OpenMP [2.9.3.4, Restrictions, p.3]
2864 // A list item that appears in a reduction clause of a parallel construct
2865 // must not appear in a firstprivate clause on a worksharing or task
2866 // construct if any of the worksharing or task regions arising from the
2867 // worksharing or task construct ever bind to any of the parallel regions
2868 // arising from the parallel construct.
2869 // OpenMP [2.9.3.4, Restrictions, p.4]
2870 // A list item that appears in a reduction clause in worksharing
2871 // construct must not appear in a firstprivate clause in a task construct
2872 // encountered during execution of any of the worksharing regions arising
2873 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002874 if (CurrDir == OMPD_task) {
2875 DVar =
2876 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
2877 [](OpenMPDirectiveKind K) -> bool {
2878 return isOpenMPParallelDirective(K) ||
2879 isOpenMPWorksharingDirective(K);
2880 },
2881 false);
2882 if (DVar.CKind == OMPC_reduction &&
2883 (isOpenMPParallelDirective(DVar.DKind) ||
2884 isOpenMPWorksharingDirective(DVar.DKind))) {
2885 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
2886 << getOpenMPDirectiveName(DVar.DKind);
2887 ReportOriginalDSA(*this, DSAStack, VD, DVar);
2888 continue;
2889 }
2890 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002891 }
2892
2893 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2894 Vars.push_back(DE);
2895 }
2896
Alexey Bataeved09d242014-05-28 05:53:51 +00002897 if (Vars.empty())
2898 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002899
2900 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2901 Vars);
2902}
2903
Alexander Musman1bb328c2014-06-04 13:06:39 +00002904OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2905 SourceLocation StartLoc,
2906 SourceLocation LParenLoc,
2907 SourceLocation EndLoc) {
2908 SmallVector<Expr *, 8> Vars;
2909 for (auto &RefExpr : VarList) {
2910 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2911 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2912 // It will be analyzed later.
2913 Vars.push_back(RefExpr);
2914 continue;
2915 }
2916
2917 SourceLocation ELoc = RefExpr->getExprLoc();
2918 // OpenMP [2.1, C/C++]
2919 // A list item is a variable name.
2920 // OpenMP [2.14.3.5, Restrictions, p.1]
2921 // A variable that is part of another variable (as an array or structure
2922 // element) cannot appear in a lastprivate clause.
2923 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2924 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2925 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2926 continue;
2927 }
2928 Decl *D = DE->getDecl();
2929 VarDecl *VD = cast<VarDecl>(D);
2930
2931 QualType Type = VD->getType();
2932 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2933 // It will be analyzed later.
2934 Vars.push_back(DE);
2935 continue;
2936 }
2937
2938 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2939 // A variable that appears in a lastprivate clause must not have an
2940 // incomplete type or a reference type.
2941 if (RequireCompleteType(ELoc, Type,
2942 diag::err_omp_lastprivate_incomplete_type)) {
2943 continue;
2944 }
2945 if (Type->isReferenceType()) {
2946 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2947 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2948 bool IsDecl =
2949 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2950 Diag(VD->getLocation(),
2951 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2952 << VD;
2953 continue;
2954 }
2955
2956 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2957 // in a Construct]
2958 // Variables with the predetermined data-sharing attributes may not be
2959 // listed in data-sharing attributes clauses, except for the cases
2960 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002961 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002962 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2963 DVar.CKind != OMPC_firstprivate &&
2964 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2965 Diag(ELoc, diag::err_omp_wrong_dsa)
2966 << getOpenMPClauseName(DVar.CKind)
2967 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002968 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002969 continue;
2970 }
2971
Alexey Bataevf29276e2014-06-18 04:14:57 +00002972 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2973 // OpenMP [2.14.3.5, Restrictions, p.2]
2974 // A list item that is private within a parallel region, or that appears in
2975 // the reduction clause of a parallel construct, must not appear in a
2976 // lastprivate clause on a worksharing construct if any of the corresponding
2977 // worksharing regions ever binds to any of the corresponding parallel
2978 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002979 if (isOpenMPWorksharingDirective(CurrDir) &&
2980 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002981 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002982 if (DVar.CKind != OMPC_shared) {
2983 Diag(ELoc, diag::err_omp_required_access)
2984 << getOpenMPClauseName(OMPC_lastprivate)
2985 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002986 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002987 continue;
2988 }
2989 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002990 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002991 // A variable of class type (or array thereof) that appears in a
2992 // lastprivate clause requires an accessible, unambiguous default
2993 // constructor for the class type, unless the list item is also specified
2994 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002995 // A variable of class type (or array thereof) that appears in a
2996 // lastprivate clause requires an accessible, unambiguous copy assignment
2997 // operator for the class type.
2998 while (Type.getNonReferenceType()->isArrayType())
2999 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3000 ->getElementType();
3001 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3002 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3003 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003004 // FIXME This code must be replaced by actual copying and destructing of the
3005 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003006 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003007 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3008 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003009 if (MD) {
3010 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3011 MD->isDeleted()) {
3012 Diag(ELoc, diag::err_omp_required_method)
3013 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3014 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3015 VarDecl::DeclarationOnly;
3016 Diag(VD->getLocation(),
3017 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3018 << VD;
3019 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3020 continue;
3021 }
3022 MarkFunctionReferenced(ELoc, MD);
3023 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003024 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003025
3026 CXXDestructorDecl *DD = RD->getDestructor();
3027 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003028 PartialDiagnostic PD =
3029 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003030 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3031 DD->isDeleted()) {
3032 Diag(ELoc, diag::err_omp_required_method)
3033 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3034 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3035 VarDecl::DeclarationOnly;
3036 Diag(VD->getLocation(),
3037 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3038 << VD;
3039 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3040 continue;
3041 }
3042 MarkFunctionReferenced(ELoc, DD);
3043 DiagnoseUseOfDecl(DD, ELoc);
3044 }
3045 }
3046
Alexey Bataevf29276e2014-06-18 04:14:57 +00003047 if (DVar.CKind != OMPC_firstprivate)
3048 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003049 Vars.push_back(DE);
3050 }
3051
3052 if (Vars.empty())
3053 return nullptr;
3054
3055 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3056 Vars);
3057}
3058
Alexey Bataev758e55e2013-09-06 18:03:48 +00003059OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3060 SourceLocation StartLoc,
3061 SourceLocation LParenLoc,
3062 SourceLocation EndLoc) {
3063 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003064 for (auto &RefExpr : VarList) {
3065 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3066 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003067 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003068 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003069 continue;
3070 }
3071
Alexey Bataeved09d242014-05-28 05:53:51 +00003072 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003073 // OpenMP [2.1, C/C++]
3074 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003075 // OpenMP [2.14.3.2, Restrictions, p.1]
3076 // A variable that is part of another variable (as an array or structure
3077 // element) cannot appear in a shared unless it is a static data member
3078 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003079 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003080 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003081 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003082 continue;
3083 }
3084 Decl *D = DE->getDecl();
3085 VarDecl *VD = cast<VarDecl>(D);
3086
3087 QualType Type = VD->getType();
3088 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3089 // It will be analyzed later.
3090 Vars.push_back(DE);
3091 continue;
3092 }
3093
3094 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3095 // in a Construct]
3096 // Variables with the predetermined data-sharing attributes may not be
3097 // listed in data-sharing attributes clauses, except for the cases
3098 // listed below. For these exceptions only, listing a predetermined
3099 // variable in a data-sharing attribute clause is allowed and overrides
3100 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003101 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003102 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3103 DVar.RefExpr) {
3104 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3105 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003106 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003107 continue;
3108 }
3109
3110 DSAStack->addDSA(VD, DE, OMPC_shared);
3111 Vars.push_back(DE);
3112 }
3113
Alexey Bataeved09d242014-05-28 05:53:51 +00003114 if (Vars.empty())
3115 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003116
3117 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3118}
3119
Alexey Bataevc5e02582014-06-16 07:08:35 +00003120namespace {
3121class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3122 DSAStackTy *Stack;
3123
3124public:
3125 bool VisitDeclRefExpr(DeclRefExpr *E) {
3126 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003127 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003128 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3129 return false;
3130 if (DVar.CKind != OMPC_unknown)
3131 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003132 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003133 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003134 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003135 return true;
3136 return false;
3137 }
3138 return false;
3139 }
3140 bool VisitStmt(Stmt *S) {
3141 for (auto Child : S->children()) {
3142 if (Child && Visit(Child))
3143 return true;
3144 }
3145 return false;
3146 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003147 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003148};
Alexey Bataev23b69422014-06-18 07:08:49 +00003149} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003150
3151OMPClause *Sema::ActOnOpenMPReductionClause(
3152 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3153 SourceLocation ColonLoc, SourceLocation EndLoc,
3154 CXXScopeSpec &ReductionIdScopeSpec,
3155 const DeclarationNameInfo &ReductionId) {
3156 // TODO: Allow scope specification search when 'declare reduction' is
3157 // supported.
3158 assert(ReductionIdScopeSpec.isEmpty() &&
3159 "No support for scoped reduction identifiers yet.");
3160
3161 auto DN = ReductionId.getName();
3162 auto OOK = DN.getCXXOverloadedOperator();
3163 BinaryOperatorKind BOK = BO_Comma;
3164
3165 // OpenMP [2.14.3.6, reduction clause]
3166 // C
3167 // reduction-identifier is either an identifier or one of the following
3168 // operators: +, -, *, &, |, ^, && and ||
3169 // C++
3170 // reduction-identifier is either an id-expression or one of the following
3171 // operators: +, -, *, &, |, ^, && and ||
3172 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3173 switch (OOK) {
3174 case OO_Plus:
3175 case OO_Minus:
3176 BOK = BO_AddAssign;
3177 break;
3178 case OO_Star:
3179 BOK = BO_MulAssign;
3180 break;
3181 case OO_Amp:
3182 BOK = BO_AndAssign;
3183 break;
3184 case OO_Pipe:
3185 BOK = BO_OrAssign;
3186 break;
3187 case OO_Caret:
3188 BOK = BO_XorAssign;
3189 break;
3190 case OO_AmpAmp:
3191 BOK = BO_LAnd;
3192 break;
3193 case OO_PipePipe:
3194 BOK = BO_LOr;
3195 break;
3196 default:
3197 if (auto II = DN.getAsIdentifierInfo()) {
3198 if (II->isStr("max"))
3199 BOK = BO_GT;
3200 else if (II->isStr("min"))
3201 BOK = BO_LT;
3202 }
3203 break;
3204 }
3205 SourceRange ReductionIdRange;
3206 if (ReductionIdScopeSpec.isValid()) {
3207 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3208 }
3209 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3210 if (BOK == BO_Comma) {
3211 // Not allowed reduction identifier is found.
3212 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3213 << ReductionIdRange;
3214 return nullptr;
3215 }
3216
3217 SmallVector<Expr *, 8> Vars;
3218 for (auto RefExpr : VarList) {
3219 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3220 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3221 // It will be analyzed later.
3222 Vars.push_back(RefExpr);
3223 continue;
3224 }
3225
3226 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3227 RefExpr->isInstantiationDependent() ||
3228 RefExpr->containsUnexpandedParameterPack()) {
3229 // It will be analyzed later.
3230 Vars.push_back(RefExpr);
3231 continue;
3232 }
3233
3234 auto ELoc = RefExpr->getExprLoc();
3235 auto ERange = RefExpr->getSourceRange();
3236 // OpenMP [2.1, C/C++]
3237 // A list item is a variable or array section, subject to the restrictions
3238 // specified in Section 2.4 on page 42 and in each of the sections
3239 // describing clauses and directives for which a list appears.
3240 // OpenMP [2.14.3.3, Restrictions, p.1]
3241 // A variable that is part of another variable (as an array or
3242 // structure element) cannot appear in a private clause.
3243 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3244 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3245 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3246 continue;
3247 }
3248 auto D = DE->getDecl();
3249 auto VD = cast<VarDecl>(D);
3250 auto Type = VD->getType();
3251 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3252 // A variable that appears in a private clause must not have an incomplete
3253 // type or a reference type.
3254 if (RequireCompleteType(ELoc, Type,
3255 diag::err_omp_reduction_incomplete_type))
3256 continue;
3257 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3258 // Arrays may not appear in a reduction clause.
3259 if (Type.getNonReferenceType()->isArrayType()) {
3260 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3261 bool IsDecl =
3262 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3263 Diag(VD->getLocation(),
3264 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3265 << VD;
3266 continue;
3267 }
3268 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3269 // A list item that appears in a reduction clause must not be
3270 // const-qualified.
3271 if (Type.getNonReferenceType().isConstant(Context)) {
3272 Diag(ELoc, diag::err_omp_const_variable)
3273 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3274 bool IsDecl =
3275 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3276 Diag(VD->getLocation(),
3277 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3278 << VD;
3279 continue;
3280 }
3281 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3282 // If a list-item is a reference type then it must bind to the same object
3283 // for all threads of the team.
3284 VarDecl *VDDef = VD->getDefinition();
3285 if (Type->isReferenceType() && VDDef) {
3286 DSARefChecker Check(DSAStack);
3287 if (Check.Visit(VDDef->getInit())) {
3288 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3289 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3290 continue;
3291 }
3292 }
3293 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3294 // The type of a list item that appears in a reduction clause must be valid
3295 // for the reduction-identifier. For a max or min reduction in C, the type
3296 // of the list item must be an allowed arithmetic data type: char, int,
3297 // float, double, or _Bool, possibly modified with long, short, signed, or
3298 // unsigned. For a max or min reduction in C++, the type of the list item
3299 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3300 // double, or bool, possibly modified with long, short, signed, or unsigned.
3301 if ((BOK == BO_GT || BOK == BO_LT) &&
3302 !(Type->isScalarType() ||
3303 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3304 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3305 << getLangOpts().CPlusPlus;
3306 bool IsDecl =
3307 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3308 Diag(VD->getLocation(),
3309 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3310 << VD;
3311 continue;
3312 }
3313 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3314 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3315 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3316 bool IsDecl =
3317 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3318 Diag(VD->getLocation(),
3319 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3320 << VD;
3321 continue;
3322 }
3323 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3324 getDiagnostics().setSuppressAllDiagnostics(true);
3325 ExprResult ReductionOp =
3326 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3327 RefExpr, RefExpr);
3328 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3329 if (ReductionOp.isInvalid()) {
3330 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003331 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003332 bool IsDecl =
3333 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3334 Diag(VD->getLocation(),
3335 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3336 << VD;
3337 continue;
3338 }
3339
3340 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3341 // in a Construct]
3342 // Variables with the predetermined data-sharing attributes may not be
3343 // listed in data-sharing attributes clauses, except for the cases
3344 // listed below. For these exceptions only, listing a predetermined
3345 // variable in a data-sharing attribute clause is allowed and overrides
3346 // the variable's predetermined data-sharing attributes.
3347 // OpenMP [2.14.3.6, Restrictions, p.3]
3348 // Any number of reduction clauses can be specified on the directive,
3349 // but a list item can appear only once in the reduction clauses for that
3350 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003351 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003352 if (DVar.CKind == OMPC_reduction) {
3353 Diag(ELoc, diag::err_omp_once_referenced)
3354 << getOpenMPClauseName(OMPC_reduction);
3355 if (DVar.RefExpr) {
3356 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3357 }
3358 } else if (DVar.CKind != OMPC_unknown) {
3359 Diag(ELoc, diag::err_omp_wrong_dsa)
3360 << getOpenMPClauseName(DVar.CKind)
3361 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003362 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003363 continue;
3364 }
3365
3366 // OpenMP [2.14.3.6, Restrictions, p.1]
3367 // A list item that appears in a reduction clause of a worksharing
3368 // construct must be shared in the parallel regions to which any of the
3369 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003370 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003371 if (isOpenMPWorksharingDirective(CurrDir) &&
3372 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003373 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003374 if (DVar.CKind != OMPC_shared) {
3375 Diag(ELoc, diag::err_omp_required_access)
3376 << getOpenMPClauseName(OMPC_reduction)
3377 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003378 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003379 continue;
3380 }
3381 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003382
3383 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3384 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3385 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003386 // FIXME This code must be replaced by actual constructing/destructing of
3387 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003388 if (RD) {
3389 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3390 PartialDiagnostic PD =
3391 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003392 if (!CD ||
3393 CheckConstructorAccess(ELoc, CD,
3394 InitializedEntity::InitializeTemporary(Type),
3395 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003396 CD->isDeleted()) {
3397 Diag(ELoc, diag::err_omp_required_method)
3398 << getOpenMPClauseName(OMPC_reduction) << 0;
3399 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3400 VarDecl::DeclarationOnly;
3401 Diag(VD->getLocation(),
3402 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3403 << VD;
3404 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3405 continue;
3406 }
3407 MarkFunctionReferenced(ELoc, CD);
3408 DiagnoseUseOfDecl(CD, ELoc);
3409
3410 CXXDestructorDecl *DD = RD->getDestructor();
3411 if (DD) {
3412 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3413 DD->isDeleted()) {
3414 Diag(ELoc, diag::err_omp_required_method)
3415 << getOpenMPClauseName(OMPC_reduction) << 4;
3416 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3417 VarDecl::DeclarationOnly;
3418 Diag(VD->getLocation(),
3419 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3420 << VD;
3421 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3422 continue;
3423 }
3424 MarkFunctionReferenced(ELoc, DD);
3425 DiagnoseUseOfDecl(DD, ELoc);
3426 }
3427 }
3428
3429 DSAStack->addDSA(VD, DE, OMPC_reduction);
3430 Vars.push_back(DE);
3431 }
3432
3433 if (Vars.empty())
3434 return nullptr;
3435
3436 return OMPReductionClause::Create(
3437 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3438 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3439}
3440
Alexander Musman8dba6642014-04-22 13:09:42 +00003441OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3442 SourceLocation StartLoc,
3443 SourceLocation LParenLoc,
3444 SourceLocation ColonLoc,
3445 SourceLocation EndLoc) {
3446 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003447 for (auto &RefExpr : VarList) {
3448 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3449 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003450 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003451 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003452 continue;
3453 }
3454
3455 // OpenMP [2.14.3.7, linear clause]
3456 // A list item that appears in a linear clause is subject to the private
3457 // clause semantics described in Section 2.14.3.3 on page 159 except as
3458 // noted. In addition, the value of the new list item on each iteration
3459 // of the associated loop(s) corresponds to the value of the original
3460 // list item before entering the construct plus the logical number of
3461 // the iteration times linear-step.
3462
Alexey Bataeved09d242014-05-28 05:53:51 +00003463 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003464 // OpenMP [2.1, C/C++]
3465 // A list item is a variable name.
3466 // OpenMP [2.14.3.3, Restrictions, p.1]
3467 // A variable that is part of another variable (as an array or
3468 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003469 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003470 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003471 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003472 continue;
3473 }
3474
3475 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3476
3477 // OpenMP [2.14.3.7, linear clause]
3478 // A list-item cannot appear in more than one linear clause.
3479 // A list-item that appears in a linear clause cannot appear in any
3480 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003481 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003482 if (DVar.RefExpr) {
3483 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3484 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003485 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003486 continue;
3487 }
3488
3489 QualType QType = VD->getType();
3490 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3491 // It will be analyzed later.
3492 Vars.push_back(DE);
3493 continue;
3494 }
3495
3496 // A variable must not have an incomplete type or a reference type.
3497 if (RequireCompleteType(ELoc, QType,
3498 diag::err_omp_linear_incomplete_type)) {
3499 continue;
3500 }
3501 if (QType->isReferenceType()) {
3502 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3503 << getOpenMPClauseName(OMPC_linear) << QType;
3504 bool IsDecl =
3505 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3506 Diag(VD->getLocation(),
3507 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3508 << VD;
3509 continue;
3510 }
3511
3512 // A list item must not be const-qualified.
3513 if (QType.isConstant(Context)) {
3514 Diag(ELoc, diag::err_omp_const_variable)
3515 << getOpenMPClauseName(OMPC_linear);
3516 bool IsDecl =
3517 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3518 Diag(VD->getLocation(),
3519 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3520 << VD;
3521 continue;
3522 }
3523
3524 // A list item must be of integral or pointer type.
3525 QType = QType.getUnqualifiedType().getCanonicalType();
3526 const Type *Ty = QType.getTypePtrOrNull();
3527 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3528 !Ty->isPointerType())) {
3529 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3530 bool IsDecl =
3531 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3532 Diag(VD->getLocation(),
3533 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3534 << VD;
3535 continue;
3536 }
3537
3538 DSAStack->addDSA(VD, DE, OMPC_linear);
3539 Vars.push_back(DE);
3540 }
3541
3542 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003543 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003544
3545 Expr *StepExpr = Step;
3546 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3547 !Step->isInstantiationDependent() &&
3548 !Step->containsUnexpandedParameterPack()) {
3549 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003550 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003551 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003552 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003553 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003554
3555 // Warn about zero linear step (it would be probably better specified as
3556 // making corresponding variables 'const').
3557 llvm::APSInt Result;
3558 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3559 !Result.isNegative() && !Result.isStrictlyPositive())
3560 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3561 << (Vars.size() > 1);
3562 }
3563
3564 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3565 Vars, StepExpr);
3566}
3567
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003568OMPClause *Sema::ActOnOpenMPAlignedClause(
3569 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3570 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3571
3572 SmallVector<Expr *, 8> Vars;
3573 for (auto &RefExpr : VarList) {
3574 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3575 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3576 // It will be analyzed later.
3577 Vars.push_back(RefExpr);
3578 continue;
3579 }
3580
3581 SourceLocation ELoc = RefExpr->getExprLoc();
3582 // OpenMP [2.1, C/C++]
3583 // A list item is a variable name.
3584 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3585 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3586 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3587 continue;
3588 }
3589
3590 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3591
3592 // OpenMP [2.8.1, simd construct, Restrictions]
3593 // The type of list items appearing in the aligned clause must be
3594 // array, pointer, reference to array, or reference to pointer.
3595 QualType QType = DE->getType()
3596 .getNonReferenceType()
3597 .getUnqualifiedType()
3598 .getCanonicalType();
3599 const Type *Ty = QType.getTypePtrOrNull();
3600 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3601 !Ty->isPointerType())) {
3602 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3603 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3604 bool IsDecl =
3605 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3606 Diag(VD->getLocation(),
3607 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3608 << VD;
3609 continue;
3610 }
3611
3612 // OpenMP [2.8.1, simd construct, Restrictions]
3613 // A list-item cannot appear in more than one aligned clause.
3614 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3615 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3616 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3617 << getOpenMPClauseName(OMPC_aligned);
3618 continue;
3619 }
3620
3621 Vars.push_back(DE);
3622 }
3623
3624 // OpenMP [2.8.1, simd construct, Description]
3625 // The parameter of the aligned clause, alignment, must be a constant
3626 // positive integer expression.
3627 // If no optional parameter is specified, implementation-defined default
3628 // alignments for SIMD instructions on the target platforms are assumed.
3629 if (Alignment != nullptr) {
3630 ExprResult AlignResult =
3631 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3632 if (AlignResult.isInvalid())
3633 return nullptr;
3634 Alignment = AlignResult.get();
3635 }
3636 if (Vars.empty())
3637 return nullptr;
3638
3639 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3640 EndLoc, Vars, Alignment);
3641}
3642
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003643OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3644 SourceLocation StartLoc,
3645 SourceLocation LParenLoc,
3646 SourceLocation EndLoc) {
3647 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003648 for (auto &RefExpr : VarList) {
3649 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3650 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003651 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003652 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003653 continue;
3654 }
3655
Alexey Bataeved09d242014-05-28 05:53:51 +00003656 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003657 // OpenMP [2.1, C/C++]
3658 // A list item is a variable name.
3659 // OpenMP [2.14.4.1, Restrictions, p.1]
3660 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003661 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003662 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003663 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003664 continue;
3665 }
3666
3667 Decl *D = DE->getDecl();
3668 VarDecl *VD = cast<VarDecl>(D);
3669
3670 QualType Type = VD->getType();
3671 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3672 // It will be analyzed later.
3673 Vars.push_back(DE);
3674 continue;
3675 }
3676
3677 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3678 // A list item that appears in a copyin clause must be threadprivate.
3679 if (!DSAStack->isThreadPrivate(VD)) {
3680 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003681 << getOpenMPClauseName(OMPC_copyin)
3682 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003683 continue;
3684 }
3685
3686 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3687 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003688 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003689 // operator for the class type.
3690 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003691 CXXRecordDecl *RD =
3692 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003693 // FIXME This code must be replaced by actual assignment of the
3694 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003695 if (RD) {
3696 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3697 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003698 if (MD) {
3699 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3700 MD->isDeleted()) {
3701 Diag(ELoc, diag::err_omp_required_method)
3702 << getOpenMPClauseName(OMPC_copyin) << 2;
3703 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3704 VarDecl::DeclarationOnly;
3705 Diag(VD->getLocation(),
3706 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3707 << VD;
3708 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3709 continue;
3710 }
3711 MarkFunctionReferenced(ELoc, MD);
3712 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003713 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003714 }
3715
3716 DSAStack->addDSA(VD, DE, OMPC_copyin);
3717 Vars.push_back(DE);
3718 }
3719
Alexey Bataeved09d242014-05-28 05:53:51 +00003720 if (Vars.empty())
3721 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003722
3723 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3724}
3725
Alexey Bataevbae9a792014-06-27 10:37:06 +00003726OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3727 SourceLocation StartLoc,
3728 SourceLocation LParenLoc,
3729 SourceLocation EndLoc) {
3730 SmallVector<Expr *, 8> Vars;
3731 for (auto &RefExpr : VarList) {
3732 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3733 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3734 // It will be analyzed later.
3735 Vars.push_back(RefExpr);
3736 continue;
3737 }
3738
3739 SourceLocation ELoc = RefExpr->getExprLoc();
3740 // OpenMP [2.1, C/C++]
3741 // A list item is a variable name.
3742 // OpenMP [2.14.4.1, Restrictions, p.1]
3743 // A list item that appears in a copyin clause must be threadprivate.
3744 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3745 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3746 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3747 continue;
3748 }
3749
3750 Decl *D = DE->getDecl();
3751 VarDecl *VD = cast<VarDecl>(D);
3752
3753 QualType Type = VD->getType();
3754 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3755 // It will be analyzed later.
3756 Vars.push_back(DE);
3757 continue;
3758 }
3759
3760 // OpenMP [2.14.4.2, Restrictions, p.2]
3761 // A list item that appears in a copyprivate clause may not appear in a
3762 // private or firstprivate clause on the single construct.
3763 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003764 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003765 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3766 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3767 Diag(ELoc, diag::err_omp_wrong_dsa)
3768 << getOpenMPClauseName(DVar.CKind)
3769 << getOpenMPClauseName(OMPC_copyprivate);
3770 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3771 continue;
3772 }
3773
3774 // OpenMP [2.11.4.2, Restrictions, p.1]
3775 // All list items that appear in a copyprivate clause must be either
3776 // threadprivate or private in the enclosing context.
3777 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003778 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003779 if (DVar.CKind == OMPC_shared) {
3780 Diag(ELoc, diag::err_omp_required_access)
3781 << getOpenMPClauseName(OMPC_copyprivate)
3782 << "threadprivate or private in the enclosing context";
3783 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3784 continue;
3785 }
3786 }
3787 }
3788
3789 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3790 // A variable of class type (or array thereof) that appears in a
3791 // copyin clause requires an accessible, unambiguous copy assignment
3792 // operator for the class type.
3793 Type = Context.getBaseElementType(Type);
3794 CXXRecordDecl *RD =
3795 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3796 // FIXME This code must be replaced by actual assignment of the
3797 // threadprivate variable.
3798 if (RD) {
3799 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3800 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3801 if (MD) {
3802 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3803 MD->isDeleted()) {
3804 Diag(ELoc, diag::err_omp_required_method)
3805 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3806 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3807 VarDecl::DeclarationOnly;
3808 Diag(VD->getLocation(),
3809 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3810 << VD;
3811 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3812 continue;
3813 }
3814 MarkFunctionReferenced(ELoc, MD);
3815 DiagnoseUseOfDecl(MD, ELoc);
3816 }
3817 }
3818
3819 // No need to mark vars as copyprivate, they are already threadprivate or
3820 // implicitly private.
3821 Vars.push_back(DE);
3822 }
3823
3824 if (Vars.empty())
3825 return nullptr;
3826
3827 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3828}
3829
Alexey Bataev758e55e2013-09-06 18:03:48 +00003830#undef DSAStack