blob: 58f9bbed6f9c10a239b968615c9d2fe0a961e742 [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 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001003 case OMPD_parallel_for: {
1004 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1005 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1006 Sema::CapturedParamNameType Params[] = {
1007 std::make_pair(".global_tid.", KmpInt32PtrTy),
1008 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1009 std::make_pair(StringRef(), QualType()) // __context with shared vars
1010 };
1011 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1012 Params);
1013 break;
1014 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001015 case OMPD_parallel_sections: {
1016 Sema::CapturedParamNameType Params[] = {
1017 std::make_pair(StringRef(), QualType()) // __context with shared vars
1018 };
1019 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1020 Params);
1021 break;
1022 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001023 case OMPD_task: {
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 Bataev9959db52014-05-06 10:08:46 +00001031 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001032 llvm_unreachable("OpenMP Directive is not allowed");
1033 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001034 llvm_unreachable("Unknown OpenMP directive");
1035 }
1036}
1037
Alexey Bataev549210e2014-06-24 04:39:47 +00001038bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1039 OpenMPDirectiveKind CurrentRegion,
1040 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001041 // Allowed nesting of constructs
1042 // +------------------+-----------------+------------------------------------+
1043 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1044 // +------------------+-----------------+------------------------------------+
1045 // | parallel | parallel | * |
1046 // | parallel | for | * |
1047 // | parallel | simd | * |
1048 // | parallel | sections | * |
1049 // | parallel | section | + |
1050 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001051 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001052 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001053 // | parallel | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001054 // +------------------+-----------------+------------------------------------+
1055 // | for | parallel | * |
1056 // | for | for | + |
1057 // | for | simd | * |
1058 // | for | sections | + |
1059 // | for | section | + |
1060 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001061 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001062 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001063 // | for | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001064 // +------------------+-----------------+------------------------------------+
1065 // | simd | parallel | |
1066 // | simd | for | |
1067 // | simd | simd | |
1068 // | simd | sections | |
1069 // | simd | section | |
1070 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001071 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001072 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001073 // | simd | task | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001074 // +------------------+-----------------+------------------------------------+
1075 // | sections | parallel | * |
1076 // | sections | for | + |
1077 // | sections | simd | * |
1078 // | sections | sections | + |
1079 // | sections | section | * |
1080 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001081 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001082 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001083 // | sections | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001084 // +------------------+-----------------+------------------------------------+
1085 // | section | parallel | * |
1086 // | section | for | + |
1087 // | section | simd | * |
1088 // | section | sections | + |
1089 // | section | section | + |
1090 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001091 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001092 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001093 // | section | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001094 // +------------------+-----------------+------------------------------------+
1095 // | single | parallel | * |
1096 // | single | for | + |
1097 // | single | simd | * |
1098 // | single | sections | + |
1099 // | single | section | + |
1100 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001101 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001102 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001103 // | single | task | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001104 // +------------------+-----------------+------------------------------------+
1105 // | parallel for | parallel | * |
1106 // | parallel for | for | + |
1107 // | parallel for | simd | * |
1108 // | parallel for | sections | + |
1109 // | parallel for | section | + |
1110 // | parallel for | single | + |
1111 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001112 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001113 // | parallel for | task | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001114 // +------------------+-----------------+------------------------------------+
1115 // | parallel sections| parallel | * |
1116 // | parallel sections| for | + |
1117 // | parallel sections| simd | * |
1118 // | parallel sections| sections | + |
1119 // | parallel sections| section | * |
1120 // | parallel sections| single | + |
1121 // | parallel sections| parallel for | * |
1122 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001123 // | parallel sections| task | * |
1124 // +------------------+-----------------+------------------------------------+
1125 // | task | parallel | * |
1126 // | task | for | + |
1127 // | task | simd | * |
1128 // | task | sections | + |
1129 // | task | section | + |
1130 // | task | single | + |
1131 // | task | parallel for | * |
1132 // | task |parallel sections| * |
1133 // | task | task | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001134 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001135 if (Stack->getCurScope()) {
1136 auto ParentRegion = Stack->getParentDirective();
1137 bool NestingProhibited = false;
1138 bool CloseNesting = true;
1139 bool ShouldBeInParallelRegion = false;
1140 if (isOpenMPSimdDirective(ParentRegion)) {
1141 // OpenMP [2.16, Nesting of Regions]
1142 // OpenMP constructs may not be nested inside a simd region.
1143 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1144 return true;
1145 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001146 if (CurrentRegion == OMPD_section) {
1147 // OpenMP [2.7.2, sections Construct, Restrictions]
1148 // Orphaned section directives are prohibited. That is, the section
1149 // directives must appear within the sections construct and must not be
1150 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001151 if (ParentRegion != OMPD_sections &&
1152 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001153 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1154 << (ParentRegion != OMPD_unknown)
1155 << getOpenMPDirectiveName(ParentRegion);
1156 return true;
1157 }
1158 return false;
1159 }
Alexey Bataev549210e2014-06-24 04:39:47 +00001160 if (isOpenMPWorksharingDirective(CurrentRegion) &&
1161 !isOpenMPParallelDirective(CurrentRegion) &&
1162 !isOpenMPSimdDirective(CurrentRegion)) {
1163 // OpenMP [2.16, Nesting of Regions]
1164 // A worksharing region may not be closely nested inside a worksharing,
1165 // explicit task, critical, ordered, atomic, or master region.
1166 // TODO
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001167 NestingProhibited = (isOpenMPWorksharingDirective(ParentRegion) &&
1168 !isOpenMPSimdDirective(ParentRegion)) ||
1169 ParentRegion == OMPD_task;
Alexey Bataev549210e2014-06-24 04:39:47 +00001170 ShouldBeInParallelRegion = true;
1171 }
1172 if (NestingProhibited) {
1173 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev41b97322014-07-02 03:04:53 +00001174 << CloseNesting << getOpenMPDirectiveName(ParentRegion)
1175 << ShouldBeInParallelRegion << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001176 return true;
1177 }
1178 }
1179 return false;
1180}
1181
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001182StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1183 ArrayRef<OMPClause *> Clauses,
1184 Stmt *AStmt,
1185 SourceLocation StartLoc,
1186 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001187 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1188
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001189 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001190 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1191 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001192
1193 // Check default data sharing attributes for referenced variables.
1194 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1195 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1196 if (DSAChecker.isErrorFound())
1197 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001198 // Generate list of implicitly defined firstprivate variables.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001199 auto &VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001200 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1201 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1202
1203 bool ErrorFound = false;
1204 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001205 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1206 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1207 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001208 ClausesWithImplicit.push_back(Implicit);
1209 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001210 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001211 } else
1212 ErrorFound = true;
1213 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001214
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001215 switch (Kind) {
1216 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001217 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1218 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001219 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001220 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001221 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1222 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001223 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001224 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001225 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1226 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001227 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001228 case OMPD_sections:
1229 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1230 EndLoc);
1231 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001232 case OMPD_section:
1233 assert(ClausesWithImplicit.empty() &&
1234 "No clauses is allowed for 'omp section' directive");
1235 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1236 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001237 case OMPD_single:
1238 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1239 EndLoc);
1240 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001241 case OMPD_parallel_for:
1242 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1243 EndLoc, VarsWithInheritedDSA);
1244 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001245 case OMPD_parallel_sections:
1246 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1247 StartLoc, EndLoc);
1248 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001249 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001250 Res =
1251 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1252 break;
1253 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001254 llvm_unreachable("OpenMP Directive is not allowed");
1255 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001256 llvm_unreachable("Unknown OpenMP directive");
1257 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001258
Alexey Bataev4acb8592014-07-07 13:01:15 +00001259 for (auto P : VarsWithInheritedDSA) {
1260 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1261 << P.first << P.second->getSourceRange();
1262 }
1263 if (!VarsWithInheritedDSA.empty())
1264 return StmtError();
1265
Alexey Bataeved09d242014-05-28 05:53:51 +00001266 if (ErrorFound)
1267 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001268 return Res;
1269}
1270
1271StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1272 Stmt *AStmt,
1273 SourceLocation StartLoc,
1274 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001275 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1276 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1277 // 1.2.2 OpenMP Language Terminology
1278 // Structured block - An executable statement with a single entry at the
1279 // top and a single exit at the bottom.
1280 // The point of exit cannot be a branch out of the structured block.
1281 // longjmp() and throw() must not violate the entry/exit criteria.
1282 CS->getCapturedDecl()->setNothrow();
1283
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001284 getCurFunction()->setHasBranchProtectedScope();
1285
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001286 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1287 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001288}
1289
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001290namespace {
1291/// \brief Helper class for checking canonical form of the OpenMP loops and
1292/// extracting iteration space of each loop in the loop nest, that will be used
1293/// for IR generation.
1294class OpenMPIterationSpaceChecker {
1295 /// \brief Reference to Sema.
1296 Sema &SemaRef;
1297 /// \brief A location for diagnostics (when there is no some better location).
1298 SourceLocation DefaultLoc;
1299 /// \brief A location for diagnostics (when increment is not compatible).
1300 SourceLocation ConditionLoc;
1301 /// \brief A source location for referring to condition later.
1302 SourceRange ConditionSrcRange;
1303 /// \brief Loop variable.
1304 VarDecl *Var;
1305 /// \brief Lower bound (initializer for the var).
1306 Expr *LB;
1307 /// \brief Upper bound.
1308 Expr *UB;
1309 /// \brief Loop step (increment).
1310 Expr *Step;
1311 /// \brief This flag is true when condition is one of:
1312 /// Var < UB
1313 /// Var <= UB
1314 /// UB > Var
1315 /// UB >= Var
1316 bool TestIsLessOp;
1317 /// \brief This flag is true when condition is strict ( < or > ).
1318 bool TestIsStrictOp;
1319 /// \brief This flag is true when step is subtracted on each iteration.
1320 bool SubtractStep;
1321
1322public:
1323 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1324 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1325 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1326 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1327 SubtractStep(false) {}
1328 /// \brief Check init-expr for canonical loop form and save loop counter
1329 /// variable - #Var and its initialization value - #LB.
1330 bool CheckInit(Stmt *S);
1331 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1332 /// for less/greater and for strict/non-strict comparison.
1333 bool CheckCond(Expr *S);
1334 /// \brief Check incr-expr for canonical loop form and return true if it
1335 /// does not conform, otherwise save loop step (#Step).
1336 bool CheckInc(Expr *S);
1337 /// \brief Return the loop counter variable.
1338 VarDecl *GetLoopVar() const { return Var; }
1339 /// \brief Return true if any expression is dependent.
1340 bool Dependent() const;
1341
1342private:
1343 /// \brief Check the right-hand side of an assignment in the increment
1344 /// expression.
1345 bool CheckIncRHS(Expr *RHS);
1346 /// \brief Helper to set loop counter variable and its initializer.
1347 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1348 /// \brief Helper to set upper bound.
1349 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1350 const SourceLocation &SL);
1351 /// \brief Helper to set loop increment.
1352 bool SetStep(Expr *NewStep, bool Subtract);
1353};
1354
1355bool OpenMPIterationSpaceChecker::Dependent() const {
1356 if (!Var) {
1357 assert(!LB && !UB && !Step);
1358 return false;
1359 }
1360 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1361 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1362}
1363
1364bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1365 // State consistency checking to ensure correct usage.
1366 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1367 !TestIsLessOp && !TestIsStrictOp);
1368 if (!NewVar || !NewLB)
1369 return true;
1370 Var = NewVar;
1371 LB = NewLB;
1372 return false;
1373}
1374
1375bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1376 const SourceRange &SR,
1377 const SourceLocation &SL) {
1378 // State consistency checking to ensure correct usage.
1379 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1380 !TestIsLessOp && !TestIsStrictOp);
1381 if (!NewUB)
1382 return true;
1383 UB = NewUB;
1384 TestIsLessOp = LessOp;
1385 TestIsStrictOp = StrictOp;
1386 ConditionSrcRange = SR;
1387 ConditionLoc = SL;
1388 return false;
1389}
1390
1391bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1392 // State consistency checking to ensure correct usage.
1393 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1394 if (!NewStep)
1395 return true;
1396 if (!NewStep->isValueDependent()) {
1397 // Check that the step is integer expression.
1398 SourceLocation StepLoc = NewStep->getLocStart();
1399 ExprResult Val =
1400 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1401 if (Val.isInvalid())
1402 return true;
1403 NewStep = Val.get();
1404
1405 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1406 // If test-expr is of form var relational-op b and relational-op is < or
1407 // <= then incr-expr must cause var to increase on each iteration of the
1408 // loop. If test-expr is of form var relational-op b and relational-op is
1409 // > or >= then incr-expr must cause var to decrease on each iteration of
1410 // the loop.
1411 // If test-expr is of form b relational-op var and relational-op is < or
1412 // <= then incr-expr must cause var to decrease on each iteration of the
1413 // loop. If test-expr is of form b relational-op var and relational-op is
1414 // > or >= then incr-expr must cause var to increase on each iteration of
1415 // the loop.
1416 llvm::APSInt Result;
1417 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1418 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1419 bool IsConstNeg =
1420 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1421 bool IsConstZero = IsConstant && !Result.getBoolValue();
1422 if (UB && (IsConstZero ||
1423 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1424 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1425 SemaRef.Diag(NewStep->getExprLoc(),
1426 diag::err_omp_loop_incr_not_compatible)
1427 << Var << TestIsLessOp << NewStep->getSourceRange();
1428 SemaRef.Diag(ConditionLoc,
1429 diag::note_omp_loop_cond_requres_compatible_incr)
1430 << TestIsLessOp << ConditionSrcRange;
1431 return true;
1432 }
1433 }
1434
1435 Step = NewStep;
1436 SubtractStep = Subtract;
1437 return false;
1438}
1439
1440bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1441 // Check init-expr for canonical loop form and save loop counter
1442 // variable - #Var and its initialization value - #LB.
1443 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1444 // var = lb
1445 // integer-type var = lb
1446 // random-access-iterator-type var = lb
1447 // pointer-type var = lb
1448 //
1449 if (!S) {
1450 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1451 return true;
1452 }
1453 if (Expr *E = dyn_cast<Expr>(S))
1454 S = E->IgnoreParens();
1455 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1456 if (BO->getOpcode() == BO_Assign)
1457 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1458 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1459 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1460 if (DS->isSingleDecl()) {
1461 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1462 if (Var->hasInit()) {
1463 // Accept non-canonical init form here but emit ext. warning.
1464 if (Var->getInitStyle() != VarDecl::CInit)
1465 SemaRef.Diag(S->getLocStart(),
1466 diag::ext_omp_loop_not_canonical_init)
1467 << S->getSourceRange();
1468 return SetVarAndLB(Var, Var->getInit());
1469 }
1470 }
1471 }
1472 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1473 if (CE->getOperator() == OO_Equal)
1474 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1475 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1476
1477 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1478 << S->getSourceRange();
1479 return true;
1480}
1481
Alexey Bataev23b69422014-06-18 07:08:49 +00001482/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001483/// variable (which may be the loop variable) if possible.
1484static const VarDecl *GetInitVarDecl(const Expr *E) {
1485 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001486 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001487 E = E->IgnoreParenImpCasts();
1488 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1489 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1490 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1491 CE->getArg(0) != nullptr)
1492 E = CE->getArg(0)->IgnoreParenImpCasts();
1493 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1494 if (!DRE)
1495 return nullptr;
1496 return dyn_cast<VarDecl>(DRE->getDecl());
1497}
1498
1499bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1500 // Check test-expr for canonical form, save upper-bound UB, flags for
1501 // less/greater and for strict/non-strict comparison.
1502 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1503 // var relational-op b
1504 // b relational-op var
1505 //
1506 if (!S) {
1507 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1508 return true;
1509 }
1510 S = S->IgnoreParenImpCasts();
1511 SourceLocation CondLoc = S->getLocStart();
1512 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1513 if (BO->isRelationalOp()) {
1514 if (GetInitVarDecl(BO->getLHS()) == Var)
1515 return SetUB(BO->getRHS(),
1516 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1517 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1518 BO->getSourceRange(), BO->getOperatorLoc());
1519 if (GetInitVarDecl(BO->getRHS()) == Var)
1520 return SetUB(BO->getLHS(),
1521 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1522 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1523 BO->getSourceRange(), BO->getOperatorLoc());
1524 }
1525 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1526 if (CE->getNumArgs() == 2) {
1527 auto Op = CE->getOperator();
1528 switch (Op) {
1529 case OO_Greater:
1530 case OO_GreaterEqual:
1531 case OO_Less:
1532 case OO_LessEqual:
1533 if (GetInitVarDecl(CE->getArg(0)) == Var)
1534 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1535 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1536 CE->getOperatorLoc());
1537 if (GetInitVarDecl(CE->getArg(1)) == Var)
1538 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1539 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1540 CE->getOperatorLoc());
1541 break;
1542 default:
1543 break;
1544 }
1545 }
1546 }
1547 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1548 << S->getSourceRange() << Var;
1549 return true;
1550}
1551
1552bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1553 // RHS of canonical loop form increment can be:
1554 // var + incr
1555 // incr + var
1556 // var - incr
1557 //
1558 RHS = RHS->IgnoreParenImpCasts();
1559 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1560 if (BO->isAdditiveOp()) {
1561 bool IsAdd = BO->getOpcode() == BO_Add;
1562 if (GetInitVarDecl(BO->getLHS()) == Var)
1563 return SetStep(BO->getRHS(), !IsAdd);
1564 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1565 return SetStep(BO->getLHS(), false);
1566 }
1567 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1568 bool IsAdd = CE->getOperator() == OO_Plus;
1569 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1570 if (GetInitVarDecl(CE->getArg(0)) == Var)
1571 return SetStep(CE->getArg(1), !IsAdd);
1572 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1573 return SetStep(CE->getArg(0), false);
1574 }
1575 }
1576 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1577 << RHS->getSourceRange() << Var;
1578 return true;
1579}
1580
1581bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1582 // Check incr-expr for canonical loop form and return true if it
1583 // does not conform.
1584 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1585 // ++var
1586 // var++
1587 // --var
1588 // var--
1589 // var += incr
1590 // var -= incr
1591 // var = var + incr
1592 // var = incr + var
1593 // var = var - incr
1594 //
1595 if (!S) {
1596 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1597 return true;
1598 }
1599 S = S->IgnoreParens();
1600 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1601 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1602 return SetStep(
1603 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1604 (UO->isDecrementOp() ? -1 : 1)).get(),
1605 false);
1606 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1607 switch (BO->getOpcode()) {
1608 case BO_AddAssign:
1609 case BO_SubAssign:
1610 if (GetInitVarDecl(BO->getLHS()) == Var)
1611 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1612 break;
1613 case BO_Assign:
1614 if (GetInitVarDecl(BO->getLHS()) == Var)
1615 return CheckIncRHS(BO->getRHS());
1616 break;
1617 default:
1618 break;
1619 }
1620 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1621 switch (CE->getOperator()) {
1622 case OO_PlusPlus:
1623 case OO_MinusMinus:
1624 if (GetInitVarDecl(CE->getArg(0)) == Var)
1625 return SetStep(
1626 SemaRef.ActOnIntegerConstant(
1627 CE->getLocStart(),
1628 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1629 false);
1630 break;
1631 case OO_PlusEqual:
1632 case OO_MinusEqual:
1633 if (GetInitVarDecl(CE->getArg(0)) == Var)
1634 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1635 break;
1636 case OO_Equal:
1637 if (GetInitVarDecl(CE->getArg(0)) == Var)
1638 return CheckIncRHS(CE->getArg(1));
1639 break;
1640 default:
1641 break;
1642 }
1643 }
1644 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1645 << S->getSourceRange() << Var;
1646 return true;
1647}
Alexey Bataev23b69422014-06-18 07:08:49 +00001648} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001649
1650/// \brief Called on a for stmt to check and extract its iteration space
1651/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001652static bool CheckOpenMPIterationSpace(
1653 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1654 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1655 Expr *NestedLoopCountExpr,
1656 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001657 // OpenMP [2.6, Canonical Loop Form]
1658 // for (init-expr; test-expr; incr-expr) structured-block
1659 auto For = dyn_cast_or_null<ForStmt>(S);
1660 if (!For) {
1661 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001662 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1663 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1664 << CurrentNestedLoopCount;
1665 if (NestedLoopCount > 1)
1666 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1667 diag::note_omp_collapse_expr)
1668 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001669 return true;
1670 }
1671 assert(For->getBody());
1672
1673 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1674
1675 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001676 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001677 if (ISC.CheckInit(Init)) {
1678 return true;
1679 }
1680
1681 bool HasErrors = false;
1682
1683 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001684 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001685
1686 // OpenMP [2.6, Canonical Loop Form]
1687 // Var is one of the following:
1688 // A variable of signed or unsigned integer type.
1689 // For C++, a variable of a random access iterator type.
1690 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001691 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001692 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1693 !VarType->isPointerType() &&
1694 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1695 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1696 << SemaRef.getLangOpts().CPlusPlus;
1697 HasErrors = true;
1698 }
1699
Alexey Bataev4acb8592014-07-07 13:01:15 +00001700 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
1701 // Construct
1702 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1703 // parallel for construct is (are) private.
1704 // The loop iteration variable in the associated for-loop of a simd construct
1705 // with just one associated for-loop is linear with a constant-linear-step
1706 // that is the increment of the associated for-loop.
1707 // Exclude loop var from the list of variables with implicitly defined data
1708 // sharing attributes.
1709 while (VarsWithImplicitDSA.count(Var) > 0)
1710 VarsWithImplicitDSA.erase(Var);
1711
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001712 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1713 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001714 // The loop iteration variable in the associated for-loop of a simd construct
1715 // with just one associated for-loop may be listed in a linear clause with a
1716 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001717 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1718 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001719 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001720 auto PredeterminedCKind =
1721 isOpenMPSimdDirective(DKind)
1722 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
1723 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001724 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001725 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00001726 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1727 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001728 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001729 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00001730 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
1731 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001732 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001733 HasErrors = true;
1734 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001735 // Make the loop iteration variable private (for worksharing constructs),
1736 // linear (for simd directives with the only one associated loop) or
1737 // lastprivate (for simd directives with several collapsed loops).
1738 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001739 }
1740
Alexey Bataev7ff55242014-06-19 09:13:45 +00001741 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001742
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001743 // Check test-expr.
1744 HasErrors |= ISC.CheckCond(For->getCond());
1745
1746 // Check incr-expr.
1747 HasErrors |= ISC.CheckInc(For->getInc());
1748
1749 if (ISC.Dependent())
1750 return HasErrors;
1751
1752 // FIXME: Build loop's iteration space representation.
1753 return HasErrors;
1754}
1755
1756/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1757/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1758/// to get the first for loop.
1759static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1760 if (IgnoreCaptured)
1761 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1762 S = CapS->getCapturedStmt();
1763 // OpenMP [2.8.1, simd construct, Restrictions]
1764 // All loops associated with the construct must be perfectly nested; that is,
1765 // there must be no intervening code nor any OpenMP directive between any two
1766 // loops.
1767 while (true) {
1768 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1769 S = AS->getSubStmt();
1770 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1771 if (CS->size() != 1)
1772 break;
1773 S = CS->body_back();
1774 } else
1775 break;
1776 }
1777 return S;
1778}
1779
1780/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001781/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1782/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001783static unsigned
1784CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
1785 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
1786 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001787 unsigned NestedLoopCount = 1;
1788 if (NestedLoopCountExpr) {
1789 // Found 'collapse' clause - calculate collapse number.
1790 llvm::APSInt Result;
1791 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1792 NestedLoopCount = Result.getLimitedValue();
1793 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001794 // This is helper routine for loop directives (e.g., 'for', 'simd',
1795 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001796 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1797 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001798 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00001799 NestedLoopCount, NestedLoopCountExpr,
1800 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001801 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001802 // Move on to the next nested for loop, or to the loop body.
1803 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1804 }
1805
1806 // FIXME: Build resulting iteration space for IR generation (collapsing
1807 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001808 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001809}
1810
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001811static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001812 auto CollapseFilter = [](const OMPClause *C) -> bool {
1813 return C->getClauseKind() == OMPC_collapse;
1814 };
1815 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1816 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001817 if (I)
1818 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1819 return nullptr;
1820}
1821
Alexey Bataev4acb8592014-07-07 13:01:15 +00001822StmtResult Sema::ActOnOpenMPSimdDirective(
1823 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1824 SourceLocation EndLoc,
1825 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001826 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001827 unsigned NestedLoopCount =
1828 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
1829 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001830 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001831 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001832
1833 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001834 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1835 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001836}
1837
Alexey Bataev4acb8592014-07-07 13:01:15 +00001838StmtResult Sema::ActOnOpenMPForDirective(
1839 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1840 SourceLocation EndLoc,
1841 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001842 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001843 unsigned NestedLoopCount =
1844 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
1845 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001846 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001847 return StmtError();
1848
1849 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001850 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1851 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001852}
1853
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001854StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1855 Stmt *AStmt,
1856 SourceLocation StartLoc,
1857 SourceLocation EndLoc) {
1858 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1859 auto BaseStmt = AStmt;
1860 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1861 BaseStmt = CS->getCapturedStmt();
1862 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1863 auto S = C->children();
1864 if (!S)
1865 return StmtError();
1866 // All associated statements must be '#pragma omp section' except for
1867 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001868 for (++S; S; ++S) {
1869 auto SectionStmt = *S;
1870 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1871 if (SectionStmt)
1872 Diag(SectionStmt->getLocStart(),
1873 diag::err_omp_sections_substmt_not_section);
1874 return StmtError();
1875 }
1876 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001877 } else {
1878 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1879 return StmtError();
1880 }
1881
1882 getCurFunction()->setHasBranchProtectedScope();
1883
1884 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1885 AStmt);
1886}
1887
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001888StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1889 SourceLocation StartLoc,
1890 SourceLocation EndLoc) {
1891 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1892
1893 getCurFunction()->setHasBranchProtectedScope();
1894
1895 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1896}
1897
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001898StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1899 Stmt *AStmt,
1900 SourceLocation StartLoc,
1901 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00001902 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1903
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001904 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00001905
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001906 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1907}
1908
Alexey Bataev4acb8592014-07-07 13:01:15 +00001909StmtResult Sema::ActOnOpenMPParallelForDirective(
1910 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1911 SourceLocation EndLoc,
1912 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
1913 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1914 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1915 // 1.2.2 OpenMP Language Terminology
1916 // Structured block - An executable statement with a single entry at the
1917 // top and a single exit at the bottom.
1918 // The point of exit cannot be a branch out of the structured block.
1919 // longjmp() and throw() must not violate the entry/exit criteria.
1920 CS->getCapturedDecl()->setNothrow();
1921
1922 // In presence of clause 'collapse', it will define the nested loops number.
1923 unsigned NestedLoopCount =
1924 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
1925 *this, *DSAStack, VarsWithImplicitDSA);
1926 if (NestedLoopCount == 0)
1927 return StmtError();
1928
1929 getCurFunction()->setHasBranchProtectedScope();
1930 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
1931 NestedLoopCount, Clauses, AStmt);
1932}
1933
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001934StmtResult
1935Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
1936 Stmt *AStmt, SourceLocation StartLoc,
1937 SourceLocation EndLoc) {
1938 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1939 auto BaseStmt = AStmt;
1940 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1941 BaseStmt = CS->getCapturedStmt();
1942 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1943 auto S = C->children();
1944 if (!S)
1945 return StmtError();
1946 // All associated statements must be '#pragma omp section' except for
1947 // the first one.
1948 for (++S; S; ++S) {
1949 auto SectionStmt = *S;
1950 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1951 if (SectionStmt)
1952 Diag(SectionStmt->getLocStart(),
1953 diag::err_omp_parallel_sections_substmt_not_section);
1954 return StmtError();
1955 }
1956 }
1957 } else {
1958 Diag(AStmt->getLocStart(),
1959 diag::err_omp_parallel_sections_not_compound_stmt);
1960 return StmtError();
1961 }
1962
1963 getCurFunction()->setHasBranchProtectedScope();
1964
1965 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
1966 Clauses, AStmt);
1967}
1968
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001969StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
1970 Stmt *AStmt, SourceLocation StartLoc,
1971 SourceLocation EndLoc) {
1972 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1973 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1974 // 1.2.2 OpenMP Language Terminology
1975 // Structured block - An executable statement with a single entry at the
1976 // top and a single exit at the bottom.
1977 // The point of exit cannot be a branch out of the structured block.
1978 // longjmp() and throw() must not violate the entry/exit criteria.
1979 CS->getCapturedDecl()->setNothrow();
1980
1981 getCurFunction()->setHasBranchProtectedScope();
1982
1983 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1984}
1985
Alexey Bataeved09d242014-05-28 05:53:51 +00001986OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001987 SourceLocation StartLoc,
1988 SourceLocation LParenLoc,
1989 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001990 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001991 switch (Kind) {
1992 case OMPC_if:
1993 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1994 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00001995 case OMPC_final:
1996 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
1997 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001998 case OMPC_num_threads:
1999 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2000 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002001 case OMPC_safelen:
2002 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2003 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002004 case OMPC_collapse:
2005 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2006 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002007 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002008 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002009 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002010 case OMPC_private:
2011 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002012 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002013 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002014 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002015 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002016 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002017 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002018 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002019 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002020 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002021 case OMPC_threadprivate:
2022 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002023 llvm_unreachable("Clause is not allowed.");
2024 }
2025 return Res;
2026}
2027
Alexey Bataeved09d242014-05-28 05:53:51 +00002028OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002029 SourceLocation LParenLoc,
2030 SourceLocation EndLoc) {
2031 Expr *ValExpr = Condition;
2032 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2033 !Condition->isInstantiationDependent() &&
2034 !Condition->containsUnexpandedParameterPack()) {
2035 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002036 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002037 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002038 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002039
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002040 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002041 }
2042
2043 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2044}
2045
Alexey Bataev3778b602014-07-17 07:32:53 +00002046OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2047 SourceLocation StartLoc,
2048 SourceLocation LParenLoc,
2049 SourceLocation EndLoc) {
2050 Expr *ValExpr = Condition;
2051 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2052 !Condition->isInstantiationDependent() &&
2053 !Condition->containsUnexpandedParameterPack()) {
2054 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2055 Condition->getExprLoc(), Condition);
2056 if (Val.isInvalid())
2057 return nullptr;
2058
2059 ValExpr = Val.get();
2060 }
2061
2062 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2063}
2064
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002065ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2066 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002067 if (!Op)
2068 return ExprError();
2069
2070 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2071 public:
2072 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002073 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002074 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2075 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002076 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2077 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002078 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2079 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002080 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2081 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002082 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2083 QualType T,
2084 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002085 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2086 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002087 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2088 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002089 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002090 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002091 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002092 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2093 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002094 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2095 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002096 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2097 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002098 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002099 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002100 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002101 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2102 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002103 llvm_unreachable("conversion functions are permitted");
2104 }
2105 } ConvertDiagnoser;
2106 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2107}
2108
2109OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2110 SourceLocation StartLoc,
2111 SourceLocation LParenLoc,
2112 SourceLocation EndLoc) {
2113 Expr *ValExpr = NumThreads;
2114 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2115 !NumThreads->isInstantiationDependent() &&
2116 !NumThreads->containsUnexpandedParameterPack()) {
2117 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2118 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002119 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002120 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002121 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002122
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002123 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002124
2125 // OpenMP [2.5, Restrictions]
2126 // The num_threads expression must evaluate to a positive integer value.
2127 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002128 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2129 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002130 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2131 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002132 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002133 }
2134 }
2135
Alexey Bataeved09d242014-05-28 05:53:51 +00002136 return new (Context)
2137 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002138}
2139
Alexey Bataev62c87d22014-03-21 04:51:18 +00002140ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2141 OpenMPClauseKind CKind) {
2142 if (!E)
2143 return ExprError();
2144 if (E->isValueDependent() || E->isTypeDependent() ||
2145 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002146 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002147 llvm::APSInt Result;
2148 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2149 if (ICE.isInvalid())
2150 return ExprError();
2151 if (!Result.isStrictlyPositive()) {
2152 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2153 << getOpenMPClauseName(CKind) << E->getSourceRange();
2154 return ExprError();
2155 }
2156 return ICE;
2157}
2158
2159OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2160 SourceLocation LParenLoc,
2161 SourceLocation EndLoc) {
2162 // OpenMP [2.8.1, simd construct, Description]
2163 // The parameter of the safelen clause must be a constant
2164 // positive integer expression.
2165 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2166 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002167 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002168 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002169 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002170}
2171
Alexander Musman64d33f12014-06-04 07:53:32 +00002172OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2173 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002174 SourceLocation LParenLoc,
2175 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002176 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002177 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002178 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002179 // The parameter of the collapse clause must be a constant
2180 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002181 ExprResult NumForLoopsResult =
2182 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2183 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002184 return nullptr;
2185 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002186 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002187}
2188
Alexey Bataeved09d242014-05-28 05:53:51 +00002189OMPClause *Sema::ActOnOpenMPSimpleClause(
2190 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2191 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002192 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002193 switch (Kind) {
2194 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002195 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002196 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2197 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002198 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002199 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002200 Res = ActOnOpenMPProcBindClause(
2201 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2202 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002203 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002204 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002205 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002206 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002207 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002208 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002209 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002210 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002211 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002212 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002213 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002214 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002215 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002216 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002217 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002218 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002219 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002220 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002221 case OMPC_threadprivate:
2222 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002223 llvm_unreachable("Clause is not allowed.");
2224 }
2225 return Res;
2226}
2227
2228OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2229 SourceLocation KindKwLoc,
2230 SourceLocation StartLoc,
2231 SourceLocation LParenLoc,
2232 SourceLocation EndLoc) {
2233 if (Kind == OMPC_DEFAULT_unknown) {
2234 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002235 static_assert(OMPC_DEFAULT_unknown > 0,
2236 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002237 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002238 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002239 Values += "'";
2240 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2241 Values += "'";
2242 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002243 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002244 Values += " or ";
2245 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002246 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002247 break;
2248 default:
2249 Values += Sep;
2250 break;
2251 }
2252 }
2253 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002254 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002255 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002256 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002257 switch (Kind) {
2258 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002259 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002260 break;
2261 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002262 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002263 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002264 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002265 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002266 break;
2267 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002268 return new (Context)
2269 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002270}
2271
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002272OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2273 SourceLocation KindKwLoc,
2274 SourceLocation StartLoc,
2275 SourceLocation LParenLoc,
2276 SourceLocation EndLoc) {
2277 if (Kind == OMPC_PROC_BIND_unknown) {
2278 std::string Values;
2279 std::string Sep(", ");
2280 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2281 Values += "'";
2282 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2283 Values += "'";
2284 switch (i) {
2285 case OMPC_PROC_BIND_unknown - 2:
2286 Values += " or ";
2287 break;
2288 case OMPC_PROC_BIND_unknown - 1:
2289 break;
2290 default:
2291 Values += Sep;
2292 break;
2293 }
2294 }
2295 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002296 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002297 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002298 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002299 return new (Context)
2300 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002301}
2302
Alexey Bataev56dafe82014-06-20 07:16:17 +00002303OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2304 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2305 SourceLocation StartLoc, SourceLocation LParenLoc,
2306 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2307 SourceLocation EndLoc) {
2308 OMPClause *Res = nullptr;
2309 switch (Kind) {
2310 case OMPC_schedule:
2311 Res = ActOnOpenMPScheduleClause(
2312 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2313 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2314 break;
2315 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002316 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002317 case OMPC_num_threads:
2318 case OMPC_safelen:
2319 case OMPC_collapse:
2320 case OMPC_default:
2321 case OMPC_proc_bind:
2322 case OMPC_private:
2323 case OMPC_firstprivate:
2324 case OMPC_lastprivate:
2325 case OMPC_shared:
2326 case OMPC_reduction:
2327 case OMPC_linear:
2328 case OMPC_aligned:
2329 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002330 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002331 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002332 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002333 case OMPC_threadprivate:
2334 case OMPC_unknown:
2335 llvm_unreachable("Clause is not allowed.");
2336 }
2337 return Res;
2338}
2339
2340OMPClause *Sema::ActOnOpenMPScheduleClause(
2341 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2342 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2343 SourceLocation EndLoc) {
2344 if (Kind == OMPC_SCHEDULE_unknown) {
2345 std::string Values;
2346 std::string Sep(", ");
2347 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2348 Values += "'";
2349 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2350 Values += "'";
2351 switch (i) {
2352 case OMPC_SCHEDULE_unknown - 2:
2353 Values += " or ";
2354 break;
2355 case OMPC_SCHEDULE_unknown - 1:
2356 break;
2357 default:
2358 Values += Sep;
2359 break;
2360 }
2361 }
2362 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2363 << Values << getOpenMPClauseName(OMPC_schedule);
2364 return nullptr;
2365 }
2366 Expr *ValExpr = ChunkSize;
2367 if (ChunkSize) {
2368 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2369 !ChunkSize->isInstantiationDependent() &&
2370 !ChunkSize->containsUnexpandedParameterPack()) {
2371 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2372 ExprResult Val =
2373 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2374 if (Val.isInvalid())
2375 return nullptr;
2376
2377 ValExpr = Val.get();
2378
2379 // OpenMP [2.7.1, Restrictions]
2380 // chunk_size must be a loop invariant integer expression with a positive
2381 // value.
2382 llvm::APSInt Result;
2383 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2384 Result.isSigned() && !Result.isStrictlyPositive()) {
2385 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2386 << "schedule" << ChunkSize->getSourceRange();
2387 return nullptr;
2388 }
2389 }
2390 }
2391
2392 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2393 EndLoc, Kind, ValExpr);
2394}
2395
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002396OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2397 SourceLocation StartLoc,
2398 SourceLocation EndLoc) {
2399 OMPClause *Res = nullptr;
2400 switch (Kind) {
2401 case OMPC_ordered:
2402 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2403 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002404 case OMPC_nowait:
2405 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2406 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002407 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002408 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002409 case OMPC_num_threads:
2410 case OMPC_safelen:
2411 case OMPC_collapse:
2412 case OMPC_schedule:
2413 case OMPC_private:
2414 case OMPC_firstprivate:
2415 case OMPC_lastprivate:
2416 case OMPC_shared:
2417 case OMPC_reduction:
2418 case OMPC_linear:
2419 case OMPC_aligned:
2420 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002421 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002422 case OMPC_default:
2423 case OMPC_proc_bind:
2424 case OMPC_threadprivate:
2425 case OMPC_unknown:
2426 llvm_unreachable("Clause is not allowed.");
2427 }
2428 return Res;
2429}
2430
2431OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2432 SourceLocation EndLoc) {
2433 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2434}
2435
Alexey Bataev236070f2014-06-20 11:19:47 +00002436OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2437 SourceLocation EndLoc) {
2438 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2439}
2440
Alexey Bataevc5e02582014-06-16 07:08:35 +00002441OMPClause *Sema::ActOnOpenMPVarListClause(
2442 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2443 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2444 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2445 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002446 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002447 switch (Kind) {
2448 case OMPC_private:
2449 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2450 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002451 case OMPC_firstprivate:
2452 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2453 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002454 case OMPC_lastprivate:
2455 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2456 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002457 case OMPC_shared:
2458 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2459 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002460 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002461 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2462 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002463 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002464 case OMPC_linear:
2465 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2466 ColonLoc, EndLoc);
2467 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002468 case OMPC_aligned:
2469 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2470 ColonLoc, EndLoc);
2471 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002472 case OMPC_copyin:
2473 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2474 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002475 case OMPC_copyprivate:
2476 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2477 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002478 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002479 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002480 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002481 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002482 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002483 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002484 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002485 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002486 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002487 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002488 case OMPC_threadprivate:
2489 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002490 llvm_unreachable("Clause is not allowed.");
2491 }
2492 return Res;
2493}
2494
2495OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2496 SourceLocation StartLoc,
2497 SourceLocation LParenLoc,
2498 SourceLocation EndLoc) {
2499 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002500 for (auto &RefExpr : VarList) {
2501 assert(RefExpr && "NULL expr in OpenMP private clause.");
2502 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002503 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002504 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002505 continue;
2506 }
2507
Alexey Bataeved09d242014-05-28 05:53:51 +00002508 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002509 // OpenMP [2.1, C/C++]
2510 // A list item is a variable name.
2511 // OpenMP [2.9.3.3, Restrictions, p.1]
2512 // A variable that is part of another variable (as an array or
2513 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002514 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002515 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002516 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002517 continue;
2518 }
2519 Decl *D = DE->getDecl();
2520 VarDecl *VD = cast<VarDecl>(D);
2521
2522 QualType Type = VD->getType();
2523 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2524 // It will be analyzed later.
2525 Vars.push_back(DE);
2526 continue;
2527 }
2528
2529 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2530 // A variable that appears in a private clause must not have an incomplete
2531 // type or a reference type.
2532 if (RequireCompleteType(ELoc, Type,
2533 diag::err_omp_private_incomplete_type)) {
2534 continue;
2535 }
2536 if (Type->isReferenceType()) {
2537 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002538 << getOpenMPClauseName(OMPC_private) << Type;
2539 bool IsDecl =
2540 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2541 Diag(VD->getLocation(),
2542 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2543 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002544 continue;
2545 }
2546
2547 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2548 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002549 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002550 // class type.
2551 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002552 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2553 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002554 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002555 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2556 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2557 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002558 // FIXME This code must be replaced by actual constructing/destructing of
2559 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002560 if (RD) {
2561 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2562 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002563 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002564 if (!CD ||
2565 CheckConstructorAccess(ELoc, CD,
2566 InitializedEntity::InitializeTemporary(Type),
2567 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002568 CD->isDeleted()) {
2569 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002570 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002571 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2572 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002573 Diag(VD->getLocation(),
2574 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2575 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002576 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2577 continue;
2578 }
2579 MarkFunctionReferenced(ELoc, CD);
2580 DiagnoseUseOfDecl(CD, ELoc);
2581
2582 CXXDestructorDecl *DD = RD->getDestructor();
2583 if (DD) {
2584 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2585 DD->isDeleted()) {
2586 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002587 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002588 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2589 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002590 Diag(VD->getLocation(),
2591 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2592 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002593 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2594 continue;
2595 }
2596 MarkFunctionReferenced(ELoc, DD);
2597 DiagnoseUseOfDecl(DD, ELoc);
2598 }
2599 }
2600
Alexey Bataev758e55e2013-09-06 18:03:48 +00002601 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2602 // in a Construct]
2603 // Variables with the predetermined data-sharing attributes may not be
2604 // listed in data-sharing attributes clauses, except for the cases
2605 // listed below. For these exceptions only, listing a predetermined
2606 // variable in a data-sharing attribute clause is allowed and overrides
2607 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002608 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002609 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002610 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2611 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002612 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002613 continue;
2614 }
2615
2616 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002617 Vars.push_back(DE);
2618 }
2619
Alexey Bataeved09d242014-05-28 05:53:51 +00002620 if (Vars.empty())
2621 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002622
2623 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2624}
2625
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002626OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2627 SourceLocation StartLoc,
2628 SourceLocation LParenLoc,
2629 SourceLocation EndLoc) {
2630 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002631 bool IsImplicitClause =
2632 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
2633 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
2634
Alexey Bataeved09d242014-05-28 05:53:51 +00002635 for (auto &RefExpr : VarList) {
2636 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2637 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002638 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002639 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002640 continue;
2641 }
2642
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002643 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
2644 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002645 // OpenMP [2.1, C/C++]
2646 // A list item is a variable name.
2647 // OpenMP [2.9.3.3, Restrictions, p.1]
2648 // A variable that is part of another variable (as an array or
2649 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002650 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002651 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002652 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002653 continue;
2654 }
2655 Decl *D = DE->getDecl();
2656 VarDecl *VD = cast<VarDecl>(D);
2657
2658 QualType Type = VD->getType();
2659 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2660 // It will be analyzed later.
2661 Vars.push_back(DE);
2662 continue;
2663 }
2664
2665 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2666 // A variable that appears in a private clause must not have an incomplete
2667 // type or a reference type.
2668 if (RequireCompleteType(ELoc, Type,
2669 diag::err_omp_firstprivate_incomplete_type)) {
2670 continue;
2671 }
2672 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002673 if (IsImplicitClause) {
2674 Diag(ImplicitClauseLoc,
2675 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
2676 << Type;
2677 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2678 } else {
2679 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2680 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2681 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002682 bool IsDecl =
2683 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2684 Diag(VD->getLocation(),
2685 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2686 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002687 continue;
2688 }
2689
2690 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2691 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002692 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002693 // class type.
2694 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002695 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2696 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2697 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002698 // FIXME This code must be replaced by actual constructing/destructing of
2699 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002700 if (RD) {
2701 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2702 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002703 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002704 if (!CD ||
2705 CheckConstructorAccess(ELoc, CD,
2706 InitializedEntity::InitializeTemporary(Type),
2707 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002708 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002709 if (IsImplicitClause) {
2710 Diag(ImplicitClauseLoc,
2711 diag::err_omp_task_predetermined_firstprivate_required_method)
2712 << 0;
2713 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2714 } else {
2715 Diag(ELoc, diag::err_omp_required_method)
2716 << getOpenMPClauseName(OMPC_firstprivate) << 1;
2717 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002718 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2719 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002720 Diag(VD->getLocation(),
2721 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2722 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002723 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2724 continue;
2725 }
2726 MarkFunctionReferenced(ELoc, CD);
2727 DiagnoseUseOfDecl(CD, ELoc);
2728
2729 CXXDestructorDecl *DD = RD->getDestructor();
2730 if (DD) {
2731 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2732 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002733 if (IsImplicitClause) {
2734 Diag(ImplicitClauseLoc,
2735 diag::err_omp_task_predetermined_firstprivate_required_method)
2736 << 1;
2737 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2738 } else {
2739 Diag(ELoc, diag::err_omp_required_method)
2740 << getOpenMPClauseName(OMPC_firstprivate) << 4;
2741 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002742 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2743 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002744 Diag(VD->getLocation(),
2745 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2746 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002747 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2748 continue;
2749 }
2750 MarkFunctionReferenced(ELoc, DD);
2751 DiagnoseUseOfDecl(DD, ELoc);
2752 }
2753 }
2754
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002755 // If an implicit firstprivate variable found it was checked already.
2756 if (!IsImplicitClause) {
2757 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002758 Type = Type.getNonReferenceType().getCanonicalType();
2759 bool IsConstant = Type.isConstant(Context);
2760 Type = Context.getBaseElementType(Type);
2761 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2762 // A list item that specifies a given variable may not appear in more
2763 // than one clause on the same directive, except that a variable may be
2764 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002765 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002766 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002767 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002768 << getOpenMPClauseName(DVar.CKind)
2769 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002770 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002771 continue;
2772 }
2773
2774 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2775 // in a Construct]
2776 // Variables with the predetermined data-sharing attributes may not be
2777 // listed in data-sharing attributes clauses, except for the cases
2778 // listed below. For these exceptions only, listing a predetermined
2779 // variable in a data-sharing attribute clause is allowed and overrides
2780 // the variable's predetermined data-sharing attributes.
2781 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2782 // in a Construct, C/C++, p.2]
2783 // Variables with const-qualified type having no mutable member may be
2784 // listed in a firstprivate clause, even if they are static data members.
2785 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2786 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2787 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002788 << getOpenMPClauseName(DVar.CKind)
2789 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002790 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002791 continue;
2792 }
2793
Alexey Bataevf29276e2014-06-18 04:14:57 +00002794 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002795 // OpenMP [2.9.3.4, Restrictions, p.2]
2796 // A list item that is private within a parallel region must not appear
2797 // in a firstprivate clause on a worksharing construct if any of the
2798 // worksharing regions arising from the worksharing construct ever bind
2799 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002800 if (isOpenMPWorksharingDirective(CurrDir) &&
2801 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002802 DVar = DSAStack->getImplicitDSA(VD, true);
2803 if (DVar.CKind != OMPC_shared &&
2804 (isOpenMPParallelDirective(DVar.DKind) ||
2805 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002806 Diag(ELoc, diag::err_omp_required_access)
2807 << getOpenMPClauseName(OMPC_firstprivate)
2808 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002809 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002810 continue;
2811 }
2812 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002813 // OpenMP [2.9.3.4, Restrictions, p.3]
2814 // A list item that appears in a reduction clause of a parallel construct
2815 // must not appear in a firstprivate clause on a worksharing or task
2816 // construct if any of the worksharing or task regions arising from the
2817 // worksharing or task construct ever bind to any of the parallel regions
2818 // arising from the parallel construct.
2819 // OpenMP [2.9.3.4, Restrictions, p.4]
2820 // A list item that appears in a reduction clause in worksharing
2821 // construct must not appear in a firstprivate clause in a task construct
2822 // encountered during execution of any of the worksharing regions arising
2823 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002824 if (CurrDir == OMPD_task) {
2825 DVar =
2826 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
2827 [](OpenMPDirectiveKind K) -> bool {
2828 return isOpenMPParallelDirective(K) ||
2829 isOpenMPWorksharingDirective(K);
2830 },
2831 false);
2832 if (DVar.CKind == OMPC_reduction &&
2833 (isOpenMPParallelDirective(DVar.DKind) ||
2834 isOpenMPWorksharingDirective(DVar.DKind))) {
2835 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
2836 << getOpenMPDirectiveName(DVar.DKind);
2837 ReportOriginalDSA(*this, DSAStack, VD, DVar);
2838 continue;
2839 }
2840 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002841 }
2842
2843 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2844 Vars.push_back(DE);
2845 }
2846
Alexey Bataeved09d242014-05-28 05:53:51 +00002847 if (Vars.empty())
2848 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002849
2850 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2851 Vars);
2852}
2853
Alexander Musman1bb328c2014-06-04 13:06:39 +00002854OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2855 SourceLocation StartLoc,
2856 SourceLocation LParenLoc,
2857 SourceLocation EndLoc) {
2858 SmallVector<Expr *, 8> Vars;
2859 for (auto &RefExpr : VarList) {
2860 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2861 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2862 // It will be analyzed later.
2863 Vars.push_back(RefExpr);
2864 continue;
2865 }
2866
2867 SourceLocation ELoc = RefExpr->getExprLoc();
2868 // OpenMP [2.1, C/C++]
2869 // A list item is a variable name.
2870 // OpenMP [2.14.3.5, Restrictions, p.1]
2871 // A variable that is part of another variable (as an array or structure
2872 // element) cannot appear in a lastprivate clause.
2873 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2874 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2875 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2876 continue;
2877 }
2878 Decl *D = DE->getDecl();
2879 VarDecl *VD = cast<VarDecl>(D);
2880
2881 QualType Type = VD->getType();
2882 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2883 // It will be analyzed later.
2884 Vars.push_back(DE);
2885 continue;
2886 }
2887
2888 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2889 // A variable that appears in a lastprivate clause must not have an
2890 // incomplete type or a reference type.
2891 if (RequireCompleteType(ELoc, Type,
2892 diag::err_omp_lastprivate_incomplete_type)) {
2893 continue;
2894 }
2895 if (Type->isReferenceType()) {
2896 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2897 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2898 bool IsDecl =
2899 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2900 Diag(VD->getLocation(),
2901 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2902 << VD;
2903 continue;
2904 }
2905
2906 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2907 // in a Construct]
2908 // Variables with the predetermined data-sharing attributes may not be
2909 // listed in data-sharing attributes clauses, except for the cases
2910 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002911 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002912 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2913 DVar.CKind != OMPC_firstprivate &&
2914 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2915 Diag(ELoc, diag::err_omp_wrong_dsa)
2916 << getOpenMPClauseName(DVar.CKind)
2917 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002918 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002919 continue;
2920 }
2921
Alexey Bataevf29276e2014-06-18 04:14:57 +00002922 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2923 // OpenMP [2.14.3.5, Restrictions, p.2]
2924 // A list item that is private within a parallel region, or that appears in
2925 // the reduction clause of a parallel construct, must not appear in a
2926 // lastprivate clause on a worksharing construct if any of the corresponding
2927 // worksharing regions ever binds to any of the corresponding parallel
2928 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002929 if (isOpenMPWorksharingDirective(CurrDir) &&
2930 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002931 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002932 if (DVar.CKind != OMPC_shared) {
2933 Diag(ELoc, diag::err_omp_required_access)
2934 << getOpenMPClauseName(OMPC_lastprivate)
2935 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002936 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002937 continue;
2938 }
2939 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002940 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002941 // A variable of class type (or array thereof) that appears in a
2942 // lastprivate clause requires an accessible, unambiguous default
2943 // constructor for the class type, unless the list item is also specified
2944 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002945 // A variable of class type (or array thereof) that appears in a
2946 // lastprivate clause requires an accessible, unambiguous copy assignment
2947 // operator for the class type.
2948 while (Type.getNonReferenceType()->isArrayType())
2949 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2950 ->getElementType();
2951 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2952 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2953 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002954 // FIXME This code must be replaced by actual copying and destructing of the
2955 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002956 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002957 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2958 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002959 if (MD) {
2960 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2961 MD->isDeleted()) {
2962 Diag(ELoc, diag::err_omp_required_method)
2963 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2964 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2965 VarDecl::DeclarationOnly;
2966 Diag(VD->getLocation(),
2967 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2968 << VD;
2969 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2970 continue;
2971 }
2972 MarkFunctionReferenced(ELoc, MD);
2973 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002974 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002975
2976 CXXDestructorDecl *DD = RD->getDestructor();
2977 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002978 PartialDiagnostic PD =
2979 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002980 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2981 DD->isDeleted()) {
2982 Diag(ELoc, diag::err_omp_required_method)
2983 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2984 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2985 VarDecl::DeclarationOnly;
2986 Diag(VD->getLocation(),
2987 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2988 << VD;
2989 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2990 continue;
2991 }
2992 MarkFunctionReferenced(ELoc, DD);
2993 DiagnoseUseOfDecl(DD, ELoc);
2994 }
2995 }
2996
Alexey Bataevf29276e2014-06-18 04:14:57 +00002997 if (DVar.CKind != OMPC_firstprivate)
2998 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002999 Vars.push_back(DE);
3000 }
3001
3002 if (Vars.empty())
3003 return nullptr;
3004
3005 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3006 Vars);
3007}
3008
Alexey Bataev758e55e2013-09-06 18:03:48 +00003009OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3010 SourceLocation StartLoc,
3011 SourceLocation LParenLoc,
3012 SourceLocation EndLoc) {
3013 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003014 for (auto &RefExpr : VarList) {
3015 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3016 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003017 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003018 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003019 continue;
3020 }
3021
Alexey Bataeved09d242014-05-28 05:53:51 +00003022 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003023 // OpenMP [2.1, C/C++]
3024 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003025 // OpenMP [2.14.3.2, Restrictions, p.1]
3026 // A variable that is part of another variable (as an array or structure
3027 // element) cannot appear in a shared unless it is a static data member
3028 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003029 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003030 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003031 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003032 continue;
3033 }
3034 Decl *D = DE->getDecl();
3035 VarDecl *VD = cast<VarDecl>(D);
3036
3037 QualType Type = VD->getType();
3038 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3039 // It will be analyzed later.
3040 Vars.push_back(DE);
3041 continue;
3042 }
3043
3044 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3045 // in a Construct]
3046 // Variables with the predetermined data-sharing attributes may not be
3047 // listed in data-sharing attributes clauses, except for the cases
3048 // listed below. For these exceptions only, listing a predetermined
3049 // variable in a data-sharing attribute clause is allowed and overrides
3050 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003051 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003052 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3053 DVar.RefExpr) {
3054 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3055 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003056 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003057 continue;
3058 }
3059
3060 DSAStack->addDSA(VD, DE, OMPC_shared);
3061 Vars.push_back(DE);
3062 }
3063
Alexey Bataeved09d242014-05-28 05:53:51 +00003064 if (Vars.empty())
3065 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003066
3067 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3068}
3069
Alexey Bataevc5e02582014-06-16 07:08:35 +00003070namespace {
3071class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3072 DSAStackTy *Stack;
3073
3074public:
3075 bool VisitDeclRefExpr(DeclRefExpr *E) {
3076 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003077 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003078 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3079 return false;
3080 if (DVar.CKind != OMPC_unknown)
3081 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003082 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003083 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003084 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003085 return true;
3086 return false;
3087 }
3088 return false;
3089 }
3090 bool VisitStmt(Stmt *S) {
3091 for (auto Child : S->children()) {
3092 if (Child && Visit(Child))
3093 return true;
3094 }
3095 return false;
3096 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003097 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003098};
Alexey Bataev23b69422014-06-18 07:08:49 +00003099} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003100
3101OMPClause *Sema::ActOnOpenMPReductionClause(
3102 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3103 SourceLocation ColonLoc, SourceLocation EndLoc,
3104 CXXScopeSpec &ReductionIdScopeSpec,
3105 const DeclarationNameInfo &ReductionId) {
3106 // TODO: Allow scope specification search when 'declare reduction' is
3107 // supported.
3108 assert(ReductionIdScopeSpec.isEmpty() &&
3109 "No support for scoped reduction identifiers yet.");
3110
3111 auto DN = ReductionId.getName();
3112 auto OOK = DN.getCXXOverloadedOperator();
3113 BinaryOperatorKind BOK = BO_Comma;
3114
3115 // OpenMP [2.14.3.6, reduction clause]
3116 // C
3117 // reduction-identifier is either an identifier or one of the following
3118 // operators: +, -, *, &, |, ^, && and ||
3119 // C++
3120 // reduction-identifier is either an id-expression or one of the following
3121 // operators: +, -, *, &, |, ^, && and ||
3122 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3123 switch (OOK) {
3124 case OO_Plus:
3125 case OO_Minus:
3126 BOK = BO_AddAssign;
3127 break;
3128 case OO_Star:
3129 BOK = BO_MulAssign;
3130 break;
3131 case OO_Amp:
3132 BOK = BO_AndAssign;
3133 break;
3134 case OO_Pipe:
3135 BOK = BO_OrAssign;
3136 break;
3137 case OO_Caret:
3138 BOK = BO_XorAssign;
3139 break;
3140 case OO_AmpAmp:
3141 BOK = BO_LAnd;
3142 break;
3143 case OO_PipePipe:
3144 BOK = BO_LOr;
3145 break;
3146 default:
3147 if (auto II = DN.getAsIdentifierInfo()) {
3148 if (II->isStr("max"))
3149 BOK = BO_GT;
3150 else if (II->isStr("min"))
3151 BOK = BO_LT;
3152 }
3153 break;
3154 }
3155 SourceRange ReductionIdRange;
3156 if (ReductionIdScopeSpec.isValid()) {
3157 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3158 }
3159 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3160 if (BOK == BO_Comma) {
3161 // Not allowed reduction identifier is found.
3162 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3163 << ReductionIdRange;
3164 return nullptr;
3165 }
3166
3167 SmallVector<Expr *, 8> Vars;
3168 for (auto RefExpr : VarList) {
3169 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3170 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3171 // It will be analyzed later.
3172 Vars.push_back(RefExpr);
3173 continue;
3174 }
3175
3176 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3177 RefExpr->isInstantiationDependent() ||
3178 RefExpr->containsUnexpandedParameterPack()) {
3179 // It will be analyzed later.
3180 Vars.push_back(RefExpr);
3181 continue;
3182 }
3183
3184 auto ELoc = RefExpr->getExprLoc();
3185 auto ERange = RefExpr->getSourceRange();
3186 // OpenMP [2.1, C/C++]
3187 // A list item is a variable or array section, subject to the restrictions
3188 // specified in Section 2.4 on page 42 and in each of the sections
3189 // describing clauses and directives for which a list appears.
3190 // OpenMP [2.14.3.3, Restrictions, p.1]
3191 // A variable that is part of another variable (as an array or
3192 // structure element) cannot appear in a private clause.
3193 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3194 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3195 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3196 continue;
3197 }
3198 auto D = DE->getDecl();
3199 auto VD = cast<VarDecl>(D);
3200 auto Type = VD->getType();
3201 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3202 // A variable that appears in a private clause must not have an incomplete
3203 // type or a reference type.
3204 if (RequireCompleteType(ELoc, Type,
3205 diag::err_omp_reduction_incomplete_type))
3206 continue;
3207 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3208 // Arrays may not appear in a reduction clause.
3209 if (Type.getNonReferenceType()->isArrayType()) {
3210 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3211 bool IsDecl =
3212 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3213 Diag(VD->getLocation(),
3214 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3215 << VD;
3216 continue;
3217 }
3218 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3219 // A list item that appears in a reduction clause must not be
3220 // const-qualified.
3221 if (Type.getNonReferenceType().isConstant(Context)) {
3222 Diag(ELoc, diag::err_omp_const_variable)
3223 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3224 bool IsDecl =
3225 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3226 Diag(VD->getLocation(),
3227 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3228 << VD;
3229 continue;
3230 }
3231 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3232 // If a list-item is a reference type then it must bind to the same object
3233 // for all threads of the team.
3234 VarDecl *VDDef = VD->getDefinition();
3235 if (Type->isReferenceType() && VDDef) {
3236 DSARefChecker Check(DSAStack);
3237 if (Check.Visit(VDDef->getInit())) {
3238 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3239 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3240 continue;
3241 }
3242 }
3243 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3244 // The type of a list item that appears in a reduction clause must be valid
3245 // for the reduction-identifier. For a max or min reduction in C, the type
3246 // of the list item must be an allowed arithmetic data type: char, int,
3247 // float, double, or _Bool, possibly modified with long, short, signed, or
3248 // unsigned. For a max or min reduction in C++, the type of the list item
3249 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3250 // double, or bool, possibly modified with long, short, signed, or unsigned.
3251 if ((BOK == BO_GT || BOK == BO_LT) &&
3252 !(Type->isScalarType() ||
3253 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3254 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3255 << getLangOpts().CPlusPlus;
3256 bool IsDecl =
3257 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3258 Diag(VD->getLocation(),
3259 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3260 << VD;
3261 continue;
3262 }
3263 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3264 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3265 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3266 bool IsDecl =
3267 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3268 Diag(VD->getLocation(),
3269 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3270 << VD;
3271 continue;
3272 }
3273 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3274 getDiagnostics().setSuppressAllDiagnostics(true);
3275 ExprResult ReductionOp =
3276 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3277 RefExpr, RefExpr);
3278 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3279 if (ReductionOp.isInvalid()) {
3280 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003281 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003282 bool IsDecl =
3283 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3284 Diag(VD->getLocation(),
3285 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3286 << VD;
3287 continue;
3288 }
3289
3290 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3291 // in a Construct]
3292 // Variables with the predetermined data-sharing attributes may not be
3293 // listed in data-sharing attributes clauses, except for the cases
3294 // listed below. For these exceptions only, listing a predetermined
3295 // variable in a data-sharing attribute clause is allowed and overrides
3296 // the variable's predetermined data-sharing attributes.
3297 // OpenMP [2.14.3.6, Restrictions, p.3]
3298 // Any number of reduction clauses can be specified on the directive,
3299 // but a list item can appear only once in the reduction clauses for that
3300 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003301 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003302 if (DVar.CKind == OMPC_reduction) {
3303 Diag(ELoc, diag::err_omp_once_referenced)
3304 << getOpenMPClauseName(OMPC_reduction);
3305 if (DVar.RefExpr) {
3306 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3307 }
3308 } else if (DVar.CKind != OMPC_unknown) {
3309 Diag(ELoc, diag::err_omp_wrong_dsa)
3310 << getOpenMPClauseName(DVar.CKind)
3311 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003312 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003313 continue;
3314 }
3315
3316 // OpenMP [2.14.3.6, Restrictions, p.1]
3317 // A list item that appears in a reduction clause of a worksharing
3318 // construct must be shared in the parallel regions to which any of the
3319 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003320 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003321 if (isOpenMPWorksharingDirective(CurrDir) &&
3322 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003323 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003324 if (DVar.CKind != OMPC_shared) {
3325 Diag(ELoc, diag::err_omp_required_access)
3326 << getOpenMPClauseName(OMPC_reduction)
3327 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003328 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003329 continue;
3330 }
3331 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003332
3333 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3334 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3335 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003336 // FIXME This code must be replaced by actual constructing/destructing of
3337 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003338 if (RD) {
3339 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3340 PartialDiagnostic PD =
3341 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003342 if (!CD ||
3343 CheckConstructorAccess(ELoc, CD,
3344 InitializedEntity::InitializeTemporary(Type),
3345 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003346 CD->isDeleted()) {
3347 Diag(ELoc, diag::err_omp_required_method)
3348 << getOpenMPClauseName(OMPC_reduction) << 0;
3349 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3350 VarDecl::DeclarationOnly;
3351 Diag(VD->getLocation(),
3352 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3353 << VD;
3354 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3355 continue;
3356 }
3357 MarkFunctionReferenced(ELoc, CD);
3358 DiagnoseUseOfDecl(CD, ELoc);
3359
3360 CXXDestructorDecl *DD = RD->getDestructor();
3361 if (DD) {
3362 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3363 DD->isDeleted()) {
3364 Diag(ELoc, diag::err_omp_required_method)
3365 << getOpenMPClauseName(OMPC_reduction) << 4;
3366 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3367 VarDecl::DeclarationOnly;
3368 Diag(VD->getLocation(),
3369 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3370 << VD;
3371 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3372 continue;
3373 }
3374 MarkFunctionReferenced(ELoc, DD);
3375 DiagnoseUseOfDecl(DD, ELoc);
3376 }
3377 }
3378
3379 DSAStack->addDSA(VD, DE, OMPC_reduction);
3380 Vars.push_back(DE);
3381 }
3382
3383 if (Vars.empty())
3384 return nullptr;
3385
3386 return OMPReductionClause::Create(
3387 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3388 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3389}
3390
Alexander Musman8dba6642014-04-22 13:09:42 +00003391OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3392 SourceLocation StartLoc,
3393 SourceLocation LParenLoc,
3394 SourceLocation ColonLoc,
3395 SourceLocation EndLoc) {
3396 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003397 for (auto &RefExpr : VarList) {
3398 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3399 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003400 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003401 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003402 continue;
3403 }
3404
3405 // OpenMP [2.14.3.7, linear clause]
3406 // A list item that appears in a linear clause is subject to the private
3407 // clause semantics described in Section 2.14.3.3 on page 159 except as
3408 // noted. In addition, the value of the new list item on each iteration
3409 // of the associated loop(s) corresponds to the value of the original
3410 // list item before entering the construct plus the logical number of
3411 // the iteration times linear-step.
3412
Alexey Bataeved09d242014-05-28 05:53:51 +00003413 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003414 // OpenMP [2.1, C/C++]
3415 // A list item is a variable name.
3416 // OpenMP [2.14.3.3, Restrictions, p.1]
3417 // A variable that is part of another variable (as an array or
3418 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003419 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003420 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003421 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003422 continue;
3423 }
3424
3425 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3426
3427 // OpenMP [2.14.3.7, linear clause]
3428 // A list-item cannot appear in more than one linear clause.
3429 // A list-item that appears in a linear clause cannot appear in any
3430 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003431 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003432 if (DVar.RefExpr) {
3433 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3434 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003435 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003436 continue;
3437 }
3438
3439 QualType QType = VD->getType();
3440 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3441 // It will be analyzed later.
3442 Vars.push_back(DE);
3443 continue;
3444 }
3445
3446 // A variable must not have an incomplete type or a reference type.
3447 if (RequireCompleteType(ELoc, QType,
3448 diag::err_omp_linear_incomplete_type)) {
3449 continue;
3450 }
3451 if (QType->isReferenceType()) {
3452 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3453 << getOpenMPClauseName(OMPC_linear) << QType;
3454 bool IsDecl =
3455 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3456 Diag(VD->getLocation(),
3457 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3458 << VD;
3459 continue;
3460 }
3461
3462 // A list item must not be const-qualified.
3463 if (QType.isConstant(Context)) {
3464 Diag(ELoc, diag::err_omp_const_variable)
3465 << getOpenMPClauseName(OMPC_linear);
3466 bool IsDecl =
3467 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3468 Diag(VD->getLocation(),
3469 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3470 << VD;
3471 continue;
3472 }
3473
3474 // A list item must be of integral or pointer type.
3475 QType = QType.getUnqualifiedType().getCanonicalType();
3476 const Type *Ty = QType.getTypePtrOrNull();
3477 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3478 !Ty->isPointerType())) {
3479 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3480 bool IsDecl =
3481 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3482 Diag(VD->getLocation(),
3483 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3484 << VD;
3485 continue;
3486 }
3487
3488 DSAStack->addDSA(VD, DE, OMPC_linear);
3489 Vars.push_back(DE);
3490 }
3491
3492 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003493 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003494
3495 Expr *StepExpr = Step;
3496 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3497 !Step->isInstantiationDependent() &&
3498 !Step->containsUnexpandedParameterPack()) {
3499 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003500 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003501 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003502 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003503 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003504
3505 // Warn about zero linear step (it would be probably better specified as
3506 // making corresponding variables 'const').
3507 llvm::APSInt Result;
3508 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3509 !Result.isNegative() && !Result.isStrictlyPositive())
3510 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3511 << (Vars.size() > 1);
3512 }
3513
3514 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3515 Vars, StepExpr);
3516}
3517
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003518OMPClause *Sema::ActOnOpenMPAlignedClause(
3519 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3520 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3521
3522 SmallVector<Expr *, 8> Vars;
3523 for (auto &RefExpr : VarList) {
3524 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3525 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3526 // It will be analyzed later.
3527 Vars.push_back(RefExpr);
3528 continue;
3529 }
3530
3531 SourceLocation ELoc = RefExpr->getExprLoc();
3532 // OpenMP [2.1, C/C++]
3533 // A list item is a variable name.
3534 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3535 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3536 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3537 continue;
3538 }
3539
3540 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3541
3542 // OpenMP [2.8.1, simd construct, Restrictions]
3543 // The type of list items appearing in the aligned clause must be
3544 // array, pointer, reference to array, or reference to pointer.
3545 QualType QType = DE->getType()
3546 .getNonReferenceType()
3547 .getUnqualifiedType()
3548 .getCanonicalType();
3549 const Type *Ty = QType.getTypePtrOrNull();
3550 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3551 !Ty->isPointerType())) {
3552 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3553 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3554 bool IsDecl =
3555 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3556 Diag(VD->getLocation(),
3557 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3558 << VD;
3559 continue;
3560 }
3561
3562 // OpenMP [2.8.1, simd construct, Restrictions]
3563 // A list-item cannot appear in more than one aligned clause.
3564 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3565 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3566 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3567 << getOpenMPClauseName(OMPC_aligned);
3568 continue;
3569 }
3570
3571 Vars.push_back(DE);
3572 }
3573
3574 // OpenMP [2.8.1, simd construct, Description]
3575 // The parameter of the aligned clause, alignment, must be a constant
3576 // positive integer expression.
3577 // If no optional parameter is specified, implementation-defined default
3578 // alignments for SIMD instructions on the target platforms are assumed.
3579 if (Alignment != nullptr) {
3580 ExprResult AlignResult =
3581 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3582 if (AlignResult.isInvalid())
3583 return nullptr;
3584 Alignment = AlignResult.get();
3585 }
3586 if (Vars.empty())
3587 return nullptr;
3588
3589 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3590 EndLoc, Vars, Alignment);
3591}
3592
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003593OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3594 SourceLocation StartLoc,
3595 SourceLocation LParenLoc,
3596 SourceLocation EndLoc) {
3597 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003598 for (auto &RefExpr : VarList) {
3599 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3600 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003601 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003602 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003603 continue;
3604 }
3605
Alexey Bataeved09d242014-05-28 05:53:51 +00003606 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003607 // OpenMP [2.1, C/C++]
3608 // A list item is a variable name.
3609 // OpenMP [2.14.4.1, Restrictions, p.1]
3610 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003611 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003612 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003613 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003614 continue;
3615 }
3616
3617 Decl *D = DE->getDecl();
3618 VarDecl *VD = cast<VarDecl>(D);
3619
3620 QualType Type = VD->getType();
3621 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3622 // It will be analyzed later.
3623 Vars.push_back(DE);
3624 continue;
3625 }
3626
3627 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3628 // A list item that appears in a copyin clause must be threadprivate.
3629 if (!DSAStack->isThreadPrivate(VD)) {
3630 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003631 << getOpenMPClauseName(OMPC_copyin)
3632 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003633 continue;
3634 }
3635
3636 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3637 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003638 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003639 // operator for the class type.
3640 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003641 CXXRecordDecl *RD =
3642 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003643 // FIXME This code must be replaced by actual assignment of the
3644 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003645 if (RD) {
3646 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3647 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003648 if (MD) {
3649 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3650 MD->isDeleted()) {
3651 Diag(ELoc, diag::err_omp_required_method)
3652 << getOpenMPClauseName(OMPC_copyin) << 2;
3653 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3654 VarDecl::DeclarationOnly;
3655 Diag(VD->getLocation(),
3656 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3657 << VD;
3658 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3659 continue;
3660 }
3661 MarkFunctionReferenced(ELoc, MD);
3662 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003663 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003664 }
3665
3666 DSAStack->addDSA(VD, DE, OMPC_copyin);
3667 Vars.push_back(DE);
3668 }
3669
Alexey Bataeved09d242014-05-28 05:53:51 +00003670 if (Vars.empty())
3671 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003672
3673 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3674}
3675
Alexey Bataevbae9a792014-06-27 10:37:06 +00003676OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3677 SourceLocation StartLoc,
3678 SourceLocation LParenLoc,
3679 SourceLocation EndLoc) {
3680 SmallVector<Expr *, 8> Vars;
3681 for (auto &RefExpr : VarList) {
3682 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3683 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3684 // It will be analyzed later.
3685 Vars.push_back(RefExpr);
3686 continue;
3687 }
3688
3689 SourceLocation ELoc = RefExpr->getExprLoc();
3690 // OpenMP [2.1, C/C++]
3691 // A list item is a variable name.
3692 // OpenMP [2.14.4.1, Restrictions, p.1]
3693 // A list item that appears in a copyin clause must be threadprivate.
3694 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3695 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3696 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3697 continue;
3698 }
3699
3700 Decl *D = DE->getDecl();
3701 VarDecl *VD = cast<VarDecl>(D);
3702
3703 QualType Type = VD->getType();
3704 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3705 // It will be analyzed later.
3706 Vars.push_back(DE);
3707 continue;
3708 }
3709
3710 // OpenMP [2.14.4.2, Restrictions, p.2]
3711 // A list item that appears in a copyprivate clause may not appear in a
3712 // private or firstprivate clause on the single construct.
3713 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003714 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003715 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3716 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3717 Diag(ELoc, diag::err_omp_wrong_dsa)
3718 << getOpenMPClauseName(DVar.CKind)
3719 << getOpenMPClauseName(OMPC_copyprivate);
3720 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3721 continue;
3722 }
3723
3724 // OpenMP [2.11.4.2, Restrictions, p.1]
3725 // All list items that appear in a copyprivate clause must be either
3726 // threadprivate or private in the enclosing context.
3727 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003728 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003729 if (DVar.CKind == OMPC_shared) {
3730 Diag(ELoc, diag::err_omp_required_access)
3731 << getOpenMPClauseName(OMPC_copyprivate)
3732 << "threadprivate or private in the enclosing context";
3733 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3734 continue;
3735 }
3736 }
3737 }
3738
3739 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3740 // A variable of class type (or array thereof) that appears in a
3741 // copyin clause requires an accessible, unambiguous copy assignment
3742 // operator for the class type.
3743 Type = Context.getBaseElementType(Type);
3744 CXXRecordDecl *RD =
3745 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3746 // FIXME This code must be replaced by actual assignment of the
3747 // threadprivate variable.
3748 if (RD) {
3749 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3750 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3751 if (MD) {
3752 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3753 MD->isDeleted()) {
3754 Diag(ELoc, diag::err_omp_required_method)
3755 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3756 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3757 VarDecl::DeclarationOnly;
3758 Diag(VD->getLocation(),
3759 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3760 << VD;
3761 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3762 continue;
3763 }
3764 MarkFunctionReferenced(ELoc, MD);
3765 DiagnoseUseOfDecl(MD, ELoc);
3766 }
3767 }
3768
3769 // No need to mark vars as copyprivate, they are already threadprivate or
3770 // implicitly private.
3771 Vars.push_back(DE);
3772 }
3773
3774 if (Vars.empty())
3775 return nullptr;
3776
3777 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3778}
3779
Alexey Bataev758e55e2013-09-06 18:03:48 +00003780#undef DSAStack