blob: 67f9eb9fd932611a6557aceeab8877a33f13d7e3 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000016#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "clang/Basic/OpenMPKinds.h"
23#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000025#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000028#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000029using namespace clang;
30
Alexey Bataev758e55e2013-09-06 18:03:48 +000031//===----------------------------------------------------------------------===//
32// Stack of data-sharing attributes for variables
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// \brief Default data sharing attributes, which can be applied to directive.
37enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000038 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
39 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
40 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000041};
Alexey Bataev7ff55242014-06-19 09:13:45 +000042
Alexey Bataevf29276e2014-06-18 04:14:57 +000043template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000044 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000045 bool operator()(T Kind) {
46 for (auto KindEl : Arr)
47 if (KindEl == Kind)
48 return true;
49 return false;
50 }
51
52private:
53 ArrayRef<T> Arr;
54};
Alexey Bataev23b69422014-06-18 07:08:49 +000055struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000056 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000057 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000058};
59
60typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
61typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000062
63/// \brief Stack for tracking declarations used in OpenMP directives and
64/// clauses and their data-sharing attributes.
65class DSAStackTy {
66public:
67 struct DSAVarData {
68 OpenMPDirectiveKind DKind;
69 OpenMPClauseKind CKind;
70 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000071 SourceLocation ImplicitDSALoc;
72 DSAVarData()
73 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
74 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000075 };
Alexey Bataeved09d242014-05-28 05:53:51 +000076
Alexey Bataev758e55e2013-09-06 18:03:48 +000077private:
78 struct DSAInfo {
79 OpenMPClauseKind Attributes;
80 DeclRefExpr *RefExpr;
81 };
82 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000083 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000084
85 struct SharingMapTy {
86 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000087 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 OpenMPDirectiveKind Directive;
91 DeclarationNameInfo DirectiveName;
92 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +000094 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000095 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000096 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000097 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
98 ConstructLoc(Loc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000099 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000100 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000101 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
102 ConstructLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000103 };
104
105 typedef SmallVector<SharingMapTy, 64> StackTy;
106
107 /// \brief Stack of used declaration and their data-sharing attributes.
108 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000109 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000110
111 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
112
113 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000114
115 /// \brief Checks if the variable is a local for OpenMP region.
116 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000117
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000119 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120
121 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000122 Scope *CurScope, SourceLocation Loc) {
123 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
124 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125 }
126
127 void pop() {
128 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
129 Stack.pop_back();
130 }
131
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000132 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000133 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000134 /// for diagnostics.
135 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
136
Alexey Bataev758e55e2013-09-06 18:03:48 +0000137 /// \brief Adds explicit data sharing attribute to the specified declaration.
138 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 /// \brief Returns data sharing attributes from top of the stack for the
141 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000142 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000143 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000144 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000145 /// \brief Checks if the specified variables has data-sharing attributes which
146 /// match specified \a CPred predicate in any directive which matches \a DPred
147 /// predicate.
148 template <class ClausesPredicate, class DirectivesPredicate>
149 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000150 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000151 /// \brief Checks if the specified variables has data-sharing attributes which
152 /// match specified \a CPred predicate in any innermost directive which
153 /// matches \a DPred predicate.
154 template <class ClausesPredicate, class DirectivesPredicate>
155 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000156 DirectivesPredicate DPred,
157 bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000158
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 /// \brief Returns currently analyzed directive.
160 OpenMPDirectiveKind getCurrentDirective() const {
161 return Stack.back().Directive;
162 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000163 /// \brief Returns parent directive.
164 OpenMPDirectiveKind getParentDirective() const {
165 if (Stack.size() > 2)
166 return Stack[Stack.size() - 2].Directive;
167 return OMPD_unknown;
168 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000169
170 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000171 void setDefaultDSANone(SourceLocation Loc) {
172 Stack.back().DefaultAttr = DSA_none;
173 Stack.back().DefaultAttrLoc = Loc;
174 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000176 void setDefaultDSAShared(SourceLocation Loc) {
177 Stack.back().DefaultAttr = DSA_shared;
178 Stack.back().DefaultAttrLoc = Loc;
179 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180
181 DefaultDataSharingAttributes getDefaultDSA() const {
182 return Stack.back().DefaultAttr;
183 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000184 SourceLocation getDefaultDSALocation() const {
185 return Stack.back().DefaultAttrLoc;
186 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000187
Alexey Bataevf29276e2014-06-18 04:14:57 +0000188 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000189 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000190 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000192 }
193
194 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000195 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000196 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000198bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
199 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
200 DKind == OMPD_unknown;
201}
Alexey Bataeved09d242014-05-28 05:53:51 +0000202} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203
204DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
205 VarDecl *D) {
206 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000207 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000208 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
209 // in a region but not in construct]
210 // File-scope or namespace-scope variables referenced in called routines
211 // in the region are shared unless they appear in a threadprivate
212 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000213 if (!D->isFunctionOrMethodVarDecl())
214 DVar.CKind = OMPC_shared;
215
216 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
217 // in a region but not in construct]
218 // Variables with static storage duration that are declared in called
219 // routines in the region are shared.
220 if (D->hasGlobalStorage())
221 DVar.CKind = OMPC_shared;
222
Alexey Bataev758e55e2013-09-06 18:03:48 +0000223 return DVar;
224 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000225
Alexey Bataev758e55e2013-09-06 18:03:48 +0000226 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000227 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
228 // in a Construct, C/C++, predetermined, p.1]
229 // Variables with automatic storage duration that are declared in a scope
230 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000231 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
232 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
233 DVar.CKind = OMPC_private;
234 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000235 }
236
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237 // Explicitly specified attributes and local variables with predetermined
238 // attributes.
239 if (Iter->SharingMap.count(D)) {
240 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
241 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000242 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000243 return DVar;
244 }
245
246 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
247 // in a Construct, C/C++, implicitly determined, p.1]
248 // In a parallel or task construct, the data-sharing attributes of these
249 // variables are determined by the default clause, if present.
250 switch (Iter->DefaultAttr) {
251 case DSA_shared:
252 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000253 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000254 return DVar;
255 case DSA_none:
256 return DVar;
257 case DSA_unspecified:
258 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
259 // in a Construct, implicitly determined, p.2]
260 // In a parallel construct, if no default clause is present, these
261 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000262 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000263 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000264 DVar.CKind = OMPC_shared;
265 return DVar;
266 }
267
268 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
269 // in a Construct, implicitly determined, p.4]
270 // In a task construct, if no default clause is present, a variable that in
271 // the enclosing context is determined to be shared by all implicit tasks
272 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273 if (DVar.DKind == OMPD_task) {
274 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000275 for (StackTy::reverse_iterator I = std::next(Iter),
276 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000277 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000278 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
279 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 // in a Construct, implicitly determined, p.6]
281 // In a task construct, if no default clause is present, a variable
282 // whose data-sharing attribute is not determined by the rules above is
283 // firstprivate.
284 DVarTemp = getDSA(I, D);
285 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000286 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000287 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000288 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 return DVar;
290 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000291 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000292 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000293 }
294 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000295 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000296 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000297 return DVar;
298 }
299 }
300 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
301 // in a Construct, implicitly determined, p.3]
302 // For constructs other than task, if no default clause is present, these
303 // variables inherit their data-sharing attributes from the enclosing
304 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000305 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306}
307
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000308DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
309 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
310 auto It = Stack.back().AlignedMap.find(D);
311 if (It == Stack.back().AlignedMap.end()) {
312 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
313 Stack.back().AlignedMap[D] = NewDE;
314 return nullptr;
315 } else {
316 assert(It->second && "Unexpected nullptr expr in the aligned map");
317 return It->second;
318 }
319 return nullptr;
320}
321
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
323 if (A == OMPC_threadprivate) {
324 Stack[0].SharingMap[D].Attributes = A;
325 Stack[0].SharingMap[D].RefExpr = E;
326 } else {
327 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
328 Stack.back().SharingMap[D].Attributes = A;
329 Stack.back().SharingMap[D].RefExpr = E;
330 }
331}
332
Alexey Bataeved09d242014-05-28 05:53:51 +0000333bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000334 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000335 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000336 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000337 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000338 ++I;
339 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000340 if (I == E)
341 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000342 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000343 Scope *CurScope = getCurScope();
344 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000346 }
347 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000348 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000349 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000350}
351
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000352DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 DSAVarData DVar;
354
355 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
356 // in a Construct, C/C++, predetermined, p.1]
357 // Variables appearing in threadprivate directives are threadprivate.
358 if (D->getTLSKind() != VarDecl::TLS_None) {
359 DVar.CKind = OMPC_threadprivate;
360 return DVar;
361 }
362 if (Stack[0].SharingMap.count(D)) {
363 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
364 DVar.CKind = OMPC_threadprivate;
365 return DVar;
366 }
367
368 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
369 // in a Construct, C/C++, predetermined, p.1]
370 // Variables with automatic storage duration that are declared in a scope
371 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000372 OpenMPDirectiveKind Kind =
373 FromParent ? getParentDirective() : getCurrentDirective();
374 auto StartI = std::next(Stack.rbegin());
375 auto EndI = std::prev(Stack.rend());
376 if (FromParent && StartI != EndI) {
377 StartI = std::next(StartI);
378 }
379 if (!isParallelOrTaskRegion(Kind)) {
380 if (isOpenMPLocal(D, StartI) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000381 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000382 DVar.CKind = OMPC_private;
383 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000384 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385 }
386
387 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
388 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000389 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000390 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000391 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000392 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000393 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
394 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000395 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
396 return DVar;
397
Alexey Bataev758e55e2013-09-06 18:03:48 +0000398 DVar.CKind = OMPC_shared;
399 return DVar;
400 }
401
402 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000403 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 while (Type->isArrayType()) {
405 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
406 Type = ElemType.getNonReferenceType().getCanonicalType();
407 }
408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
409 // in a Construct, C/C++, predetermined, p.6]
410 // Variables with const qualified type having no mutable member are
411 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000412 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000413 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000415 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 // Variables with const-qualified type having no mutable member may be
417 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000418 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
419 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000420 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
421 return DVar;
422
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 DVar.CKind = OMPC_shared;
424 return DVar;
425 }
426
427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
428 // in a Construct, C/C++, predetermined, p.7]
429 // Variables with static storage duration that are declared in a scope
430 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000431 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432 DVar.CKind = OMPC_shared;
433 return DVar;
434 }
435
436 // Explicitly specified attributes and local variables with predetermined
437 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000438 auto I = std::prev(StartI);
439 if (I->SharingMap.count(D)) {
440 DVar.RefExpr = I->SharingMap[D].RefExpr;
441 DVar.CKind = I->SharingMap[D].Attributes;
442 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
444
445 return DVar;
446}
447
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000448DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
449 auto StartI = Stack.rbegin();
450 auto EndI = std::prev(Stack.rend());
451 if (FromParent && StartI != EndI) {
452 StartI = std::next(StartI);
453 }
454 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000455}
456
Alexey Bataevf29276e2014-06-18 04:14:57 +0000457template <class ClausesPredicate, class DirectivesPredicate>
458DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000459 DirectivesPredicate DPred,
460 bool FromParent) {
461 auto StartI = std::next(Stack.rbegin());
462 auto EndI = std::prev(Stack.rend());
463 if (FromParent && StartI != EndI) {
464 StartI = std::next(StartI);
465 }
466 for (auto I = StartI, EE = EndI; I != EE; ++I) {
467 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000468 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000469 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000470 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000471 return DVar;
472 }
473 return DSAVarData();
474}
475
Alexey Bataevf29276e2014-06-18 04:14:57 +0000476template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000477DSAStackTy::DSAVarData
478DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
479 DirectivesPredicate DPred, bool FromParent) {
480 auto StartI = std::next(Stack.rbegin());
481 auto EndI = std::prev(Stack.rend());
482 if (FromParent && StartI != EndI) {
483 StartI = std::next(StartI);
484 }
485 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000486 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000487 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000488 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000489 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000490 return DVar;
491 return DSAVarData();
492 }
493 return DSAVarData();
494}
495
Alexey Bataev758e55e2013-09-06 18:03:48 +0000496void Sema::InitDataSharingAttributesStack() {
497 VarDataSharingAttributesStack = new DSAStackTy(*this);
498}
499
500#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
501
Alexey Bataeved09d242014-05-28 05:53:51 +0000502void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000503
504void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
505 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000506 Scope *CurScope, SourceLocation Loc) {
507 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000508 PushExpressionEvaluationContext(PotentiallyEvaluated);
509}
510
511void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000512 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
513 // A variable of class type (or array thereof) that appears in a lastprivate
514 // clause requires an accessible, unambiguous default constructor for the
515 // class type, unless the list item is also specified in a firstprivate
516 // clause.
517 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
518 for (auto C : D->clauses()) {
519 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
520 for (auto VarRef : Clause->varlists()) {
521 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
522 continue;
523 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000524 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000525 if (DVar.CKind == OMPC_lastprivate) {
526 SourceLocation ELoc = VarRef->getExprLoc();
527 auto Type = VarRef->getType();
528 if (Type->isArrayType())
529 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
530 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000531 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
532 // FIXME This code must be replaced by actual constructing of the
533 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000534 if (RD) {
535 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
536 PartialDiagnostic PD =
537 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
538 if (!CD ||
539 CheckConstructorAccess(
540 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
541 CD->getAccess(), PD) == AR_inaccessible ||
542 CD->isDeleted()) {
543 Diag(ELoc, diag::err_omp_required_method)
544 << getOpenMPClauseName(OMPC_lastprivate) << 0;
545 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
546 VarDecl::DeclarationOnly;
547 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
548 : diag::note_defined_here)
549 << VD;
550 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
551 continue;
552 }
553 MarkFunctionReferenced(ELoc, CD);
554 DiagnoseUseOfDecl(CD, ELoc);
555 }
556 }
557 }
558 }
559 }
560 }
561
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562 DSAStack->pop();
563 DiscardCleanupsInEvaluationContext();
564 PopExpressionEvaluationContext();
565}
566
Alexey Bataeva769e072013-03-22 06:34:35 +0000567namespace {
568
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000569class VarDeclFilterCCC : public CorrectionCandidateCallback {
570private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000571 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000572
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000573public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000574 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000575 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000576 NamedDecl *ND = Candidate.getCorrectionDecl();
577 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
578 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000579 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
580 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000581 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000582 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000583 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000584};
Alexey Bataeved09d242014-05-28 05:53:51 +0000585} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000586
587ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
588 CXXScopeSpec &ScopeSpec,
589 const DeclarationNameInfo &Id) {
590 LookupResult Lookup(*this, Id, LookupOrdinaryName);
591 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
592
593 if (Lookup.isAmbiguous())
594 return ExprError();
595
596 VarDecl *VD;
597 if (!Lookup.isSingleResult()) {
598 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000599 if (TypoCorrection Corrected =
600 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
601 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000602 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000603 PDiag(Lookup.empty()
604 ? diag::err_undeclared_var_use_suggest
605 : diag::err_omp_expected_var_arg_suggest)
606 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000607 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000608 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000609 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
610 : diag::err_omp_expected_var_arg)
611 << Id.getName();
612 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000613 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000614 } else {
615 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000616 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000617 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
618 return ExprError();
619 }
620 }
621 Lookup.suppressDiagnostics();
622
623 // OpenMP [2.9.2, Syntax, C/C++]
624 // Variables must be file-scope, namespace-scope, or static block-scope.
625 if (!VD->hasGlobalStorage()) {
626 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000627 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
628 bool IsDecl =
629 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000630 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000631 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
632 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000633 return ExprError();
634 }
635
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000636 VarDecl *CanonicalVD = VD->getCanonicalDecl();
637 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000638 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
639 // A threadprivate directive for file-scope variables must appear outside
640 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000641 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
642 !getCurLexicalContext()->isTranslationUnit()) {
643 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000644 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
645 bool IsDecl =
646 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
647 Diag(VD->getLocation(),
648 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
649 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000650 return ExprError();
651 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000652 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
653 // A threadprivate directive for static class member variables must appear
654 // in the class definition, in the same scope in which the member
655 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000656 if (CanonicalVD->isStaticDataMember() &&
657 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
658 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000659 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
660 bool IsDecl =
661 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
662 Diag(VD->getLocation(),
663 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
664 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000665 return ExprError();
666 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000667 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
668 // A threadprivate directive for namespace-scope variables must appear
669 // outside any definition or declaration other than the namespace
670 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000671 if (CanonicalVD->getDeclContext()->isNamespace() &&
672 (!getCurLexicalContext()->isFileContext() ||
673 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
674 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000675 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
676 bool IsDecl =
677 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
678 Diag(VD->getLocation(),
679 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
680 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000681 return ExprError();
682 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000683 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
684 // A threadprivate directive for static block-scope variables must appear
685 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000686 if (CanonicalVD->isStaticLocal() && CurScope &&
687 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000688 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000689 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
690 bool IsDecl =
691 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
692 Diag(VD->getLocation(),
693 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
694 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000695 return ExprError();
696 }
697
698 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
699 // A threadprivate directive must lexically precede all references to any
700 // of the variables in its list.
701 if (VD->isUsed()) {
702 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000703 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000704 return ExprError();
705 }
706
707 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000708 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 return DE;
710}
711
Alexey Bataeved09d242014-05-28 05:53:51 +0000712Sema::DeclGroupPtrTy
713Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
714 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000715 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000716 CurContext->addDecl(D);
717 return DeclGroupPtrTy::make(DeclGroupRef(D));
718 }
719 return DeclGroupPtrTy();
720}
721
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000722namespace {
723class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
724 Sema &SemaRef;
725
726public:
727 bool VisitDeclRefExpr(const DeclRefExpr *E) {
728 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
729 if (VD->hasLocalStorage()) {
730 SemaRef.Diag(E->getLocStart(),
731 diag::err_omp_local_var_in_threadprivate_init)
732 << E->getSourceRange();
733 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
734 << VD << VD->getSourceRange();
735 return true;
736 }
737 }
738 return false;
739 }
740 bool VisitStmt(const Stmt *S) {
741 for (auto Child : S->children()) {
742 if (Child && Visit(Child))
743 return true;
744 }
745 return false;
746 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000747 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000748};
749} // namespace
750
Alexey Bataeved09d242014-05-28 05:53:51 +0000751OMPThreadPrivateDecl *
752Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000754 for (auto &RefExpr : VarList) {
755 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756 VarDecl *VD = cast<VarDecl>(DE->getDecl());
757 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000758
759 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
760 // A threadprivate variable must not have an incomplete type.
761 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000762 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000763 continue;
764 }
765
766 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
767 // A threadprivate variable must not have a reference type.
768 if (VD->getType()->isReferenceType()) {
769 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000770 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
771 bool IsDecl =
772 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
773 Diag(VD->getLocation(),
774 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
775 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000776 continue;
777 }
778
Richard Smithfd3834f2013-04-13 02:43:54 +0000779 // Check if this is a TLS variable.
780 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000781 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000782 bool IsDecl =
783 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
784 Diag(VD->getLocation(),
785 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
786 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000787 continue;
788 }
789
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000790 // Check if initial value of threadprivate variable reference variable with
791 // local storage (it is not supported by runtime).
792 if (auto Init = VD->getAnyInitializer()) {
793 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000794 if (Checker.Visit(Init))
795 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000796 }
797
Alexey Bataeved09d242014-05-28 05:53:51 +0000798 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000799 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000800 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000801 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000802 if (!Vars.empty()) {
803 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
804 Vars);
805 D->setAccess(AS_public);
806 }
807 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000808}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000809
Alexey Bataev7ff55242014-06-19 09:13:45 +0000810static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
811 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
812 bool IsLoopIterVar = false) {
813 if (DVar.RefExpr) {
814 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
815 << getOpenMPClauseName(DVar.CKind);
816 return;
817 }
818 enum {
819 PDSA_StaticMemberShared,
820 PDSA_StaticLocalVarShared,
821 PDSA_LoopIterVarPrivate,
822 PDSA_LoopIterVarLinear,
823 PDSA_LoopIterVarLastprivate,
824 PDSA_ConstVarShared,
825 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000826 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000827 PDSA_LocalVarPrivate,
828 PDSA_Implicit
829 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000830 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000831 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000832 if (IsLoopIterVar) {
833 if (DVar.CKind == OMPC_private)
834 Reason = PDSA_LoopIterVarPrivate;
835 else if (DVar.CKind == OMPC_lastprivate)
836 Reason = PDSA_LoopIterVarLastprivate;
837 else
838 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000839 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
840 Reason = PDSA_TaskVarFirstprivate;
841 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000842 } else if (VD->isStaticLocal())
843 Reason = PDSA_StaticLocalVarShared;
844 else if (VD->isStaticDataMember())
845 Reason = PDSA_StaticMemberShared;
846 else if (VD->isFileVarDecl())
847 Reason = PDSA_GlobalVarShared;
848 else if (VD->getType().isConstant(SemaRef.getASTContext()))
849 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000850 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000851 ReportHint = true;
852 Reason = PDSA_LocalVarPrivate;
853 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000854 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000855 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000856 << Reason << ReportHint
857 << getOpenMPDirectiveName(Stack->getCurrentDirective());
858 } else if (DVar.ImplicitDSALoc.isValid()) {
859 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
860 << getOpenMPClauseName(DVar.CKind);
861 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000862}
863
Alexey Bataev758e55e2013-09-06 18:03:48 +0000864namespace {
865class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
866 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000867 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000868 bool ErrorFound;
869 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000870 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000871 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000872
Alexey Bataev758e55e2013-09-06 18:03:48 +0000873public:
874 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000875 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000876 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000877 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
878 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000880 auto DVar = Stack->getTopDSA(VD, false);
881 // Check if the variable has explicit DSA set and stop analysis if it so.
882 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000883
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000884 auto ELoc = E->getExprLoc();
885 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000886 // The default(none) clause requires that each variable that is referenced
887 // in the construct, and does not have a predetermined data-sharing
888 // attribute, must have its data-sharing attribute explicitly determined
889 // by being listed in a data-sharing attribute clause.
890 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000891 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000892 VarsWithInheritedDSA.count(VD) == 0) {
893 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000894 return;
895 }
896
897 // OpenMP [2.9.3.6, Restrictions, p.2]
898 // A list item that appears in a reduction clause of the innermost
899 // enclosing worksharing or parallel construct may not be accessed in an
900 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000901 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000902 [](OpenMPDirectiveKind K) -> bool {
903 return isOpenMPParallelDirective(K) ||
904 isOpenMPWorksharingDirective(K);
905 },
906 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000907 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
908 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000909 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
910 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000911 return;
912 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000913
914 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000915 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000916 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000917 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000918 }
919 }
920 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000921 for (auto *C : S->clauses()) {
922 // Skip analysis of arguments of implicitly defined firstprivate clause
923 // for task directives.
924 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
925 for (auto *CC : C->children()) {
926 if (CC)
927 Visit(CC);
928 }
929 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000930 }
931 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000932 for (auto *C : S->children()) {
933 if (C && !isa<OMPExecutableDirective>(C))
934 Visit(C);
935 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000936 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000937
938 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000939 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000940 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
941 return VarsWithInheritedDSA;
942 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000943
Alexey Bataev7ff55242014-06-19 09:13:45 +0000944 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
945 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000946};
Alexey Bataeved09d242014-05-28 05:53:51 +0000947} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000948
Alexey Bataevbae9a792014-06-27 10:37:06 +0000949void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000950 switch (DKind) {
951 case OMPD_parallel: {
952 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
953 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000954 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000955 std::make_pair(".global_tid.", KmpInt32PtrTy),
956 std::make_pair(".bound_tid.", KmpInt32PtrTy),
957 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000958 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000959 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
960 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000961 break;
962 }
963 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000964 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000965 std::make_pair(StringRef(), QualType()) // __context with shared vars
966 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000967 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
968 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000969 break;
970 }
971 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000972 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000973 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000974 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000975 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
976 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000977 break;
978 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000979 case OMPD_sections: {
980 Sema::CapturedParamNameType Params[] = {
981 std::make_pair(StringRef(), QualType()) // __context with shared vars
982 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000983 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
984 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000985 break;
986 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000987 case OMPD_section: {
988 Sema::CapturedParamNameType Params[] = {
989 std::make_pair(StringRef(), QualType()) // __context with shared vars
990 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000991 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
992 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000993 break;
994 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000995 case OMPD_single: {
996 Sema::CapturedParamNameType Params[] = {
997 std::make_pair(StringRef(), QualType()) // __context with shared vars
998 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000999 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1000 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001001 break;
1002 }
Alexander Musman80c22892014-07-17 08:54:58 +00001003 case OMPD_master: {
1004 Sema::CapturedParamNameType Params[] = {
1005 std::make_pair(StringRef(), QualType()) // __context with shared vars
1006 };
1007 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1008 Params);
1009 break;
1010 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001011 case OMPD_parallel_for: {
1012 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1013 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1014 Sema::CapturedParamNameType Params[] = {
1015 std::make_pair(".global_tid.", KmpInt32PtrTy),
1016 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1017 std::make_pair(StringRef(), QualType()) // __context with shared vars
1018 };
1019 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1020 Params);
1021 break;
1022 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001023 case OMPD_parallel_sections: {
1024 Sema::CapturedParamNameType Params[] = {
1025 std::make_pair(StringRef(), QualType()) // __context with shared vars
1026 };
1027 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1028 Params);
1029 break;
1030 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001031 case OMPD_task: {
1032 Sema::CapturedParamNameType Params[] = {
1033 std::make_pair(StringRef(), QualType()) // __context with shared vars
1034 };
1035 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1036 Params);
1037 break;
1038 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001039 case OMPD_taskyield: {
1040 Sema::CapturedParamNameType Params[] = {
1041 std::make_pair(StringRef(), QualType()) // __context with shared vars
1042 };
1043 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1044 Params);
1045 break;
1046 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001047 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001048 llvm_unreachable("OpenMP Directive is not allowed");
1049 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001050 llvm_unreachable("Unknown OpenMP directive");
1051 }
1052}
1053
Alexey Bataev549210e2014-06-24 04:39:47 +00001054bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1055 OpenMPDirectiveKind CurrentRegion,
1056 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001057 // Allowed nesting of constructs
1058 // +------------------+-----------------+------------------------------------+
1059 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1060 // +------------------+-----------------+------------------------------------+
1061 // | parallel | parallel | * |
1062 // | parallel | for | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001063 // | parallel | master | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001064 // | parallel | simd | * |
1065 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001066 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001067 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001068 // | parallel | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001069 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001070 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001071 // | parallel | taskyield | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001072 // +------------------+-----------------+------------------------------------+
1073 // | for | parallel | * |
1074 // | for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001075 // | for | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001076 // | for | simd | * |
1077 // | for | sections | + |
1078 // | for | section | + |
1079 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001080 // | for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001081 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001082 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001083 // | for | taskyield | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001084 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001085 // | master | parallel | * |
1086 // | master | for | + |
1087 // | master | master | * |
1088 // | master | simd | * |
1089 // | master | sections | + |
1090 // | master | section | + |
1091 // | master | single | + |
1092 // | master | parallel for | * |
1093 // | master |parallel sections| * |
1094 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001095 // | master | taskyield | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001096 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001097 // | simd | parallel | |
1098 // | simd | for | |
Alexander Musman80c22892014-07-17 08:54:58 +00001099 // | simd | master | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001100 // | simd | simd | |
1101 // | simd | sections | |
1102 // | simd | section | |
1103 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001104 // | simd | parallel for | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001105 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001106 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001107 // | simd | taskyield | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001108 // +------------------+-----------------+------------------------------------+
1109 // | sections | parallel | * |
1110 // | sections | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001111 // | sections | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001112 // | sections | simd | * |
1113 // | sections | sections | + |
1114 // | sections | section | * |
1115 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001116 // | sections | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001117 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001118 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001119 // | sections | taskyield | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001120 // +------------------+-----------------+------------------------------------+
1121 // | section | parallel | * |
1122 // | section | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001123 // | section | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001124 // | section | simd | * |
1125 // | section | sections | + |
1126 // | section | section | + |
1127 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001128 // | section | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001129 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001130 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001131 // | section | taskyield | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001132 // +------------------+-----------------+------------------------------------+
1133 // | single | parallel | * |
1134 // | single | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001135 // | single | master | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001136 // | single | simd | * |
1137 // | single | sections | + |
1138 // | single | section | + |
1139 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001140 // | single | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001141 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001142 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001143 // | single | taskyield | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001144 // +------------------+-----------------+------------------------------------+
1145 // | parallel for | parallel | * |
1146 // | parallel for | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001147 // | parallel for | master | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001148 // | parallel for | simd | * |
1149 // | parallel for | sections | + |
1150 // | parallel for | section | + |
1151 // | parallel for | single | + |
1152 // | parallel for | parallel for | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001153 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001154 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001155 // | parallel for | taskyield | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001156 // +------------------+-----------------+------------------------------------+
1157 // | parallel sections| parallel | * |
1158 // | parallel sections| for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001159 // | parallel sections| master | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001160 // | parallel sections| simd | * |
1161 // | parallel sections| sections | + |
1162 // | parallel sections| section | * |
1163 // | parallel sections| single | + |
1164 // | parallel sections| parallel for | * |
1165 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001166 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001167 // | parallel sections| taskyield | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001168 // +------------------+-----------------+------------------------------------+
1169 // | task | parallel | * |
1170 // | task | for | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001171 // | task | master | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001172 // | task | simd | * |
1173 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001174 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001175 // | task | single | + |
1176 // | task | parallel for | * |
1177 // | task |parallel sections| * |
1178 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001179 // | task | taskyield | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001180 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001181 if (Stack->getCurScope()) {
1182 auto ParentRegion = Stack->getParentDirective();
1183 bool NestingProhibited = false;
1184 bool CloseNesting = true;
1185 bool ShouldBeInParallelRegion = false;
1186 if (isOpenMPSimdDirective(ParentRegion)) {
1187 // OpenMP [2.16, Nesting of Regions]
1188 // OpenMP constructs may not be nested inside a simd region.
1189 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1190 return true;
1191 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001192 if (CurrentRegion == OMPD_section) {
1193 // OpenMP [2.7.2, sections Construct, Restrictions]
1194 // Orphaned section directives are prohibited. That is, the section
1195 // directives must appear within the sections construct and must not be
1196 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001197 if (ParentRegion != OMPD_sections &&
1198 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001199 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1200 << (ParentRegion != OMPD_unknown)
1201 << getOpenMPDirectiveName(ParentRegion);
1202 return true;
1203 }
1204 return false;
1205 }
Alexander Musman80c22892014-07-17 08:54:58 +00001206 if (CurrentRegion == OMPD_master) {
1207 // OpenMP [2.16, Nesting of Regions]
1208 // A master region may not be closely nested inside a worksharing,
1209 // atomic (TODO), or explicit task region.
1210 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1211 ParentRegion == OMPD_task;
1212 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
1213 !isOpenMPParallelDirective(CurrentRegion) &&
1214 !isOpenMPSimdDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001215 // OpenMP [2.16, Nesting of Regions]
1216 // A worksharing region may not be closely nested inside a worksharing,
1217 // explicit task, critical, ordered, atomic, or master region.
1218 // TODO
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001219 NestingProhibited = (isOpenMPWorksharingDirective(ParentRegion) &&
1220 !isOpenMPSimdDirective(ParentRegion)) ||
Alexander Musman80c22892014-07-17 08:54:58 +00001221 ParentRegion == OMPD_task ||
1222 ParentRegion == OMPD_master;
Alexey Bataev549210e2014-06-24 04:39:47 +00001223 ShouldBeInParallelRegion = true;
1224 }
1225 if (NestingProhibited) {
1226 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev41b97322014-07-02 03:04:53 +00001227 << CloseNesting << getOpenMPDirectiveName(ParentRegion)
1228 << ShouldBeInParallelRegion << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001229 return true;
1230 }
1231 }
1232 return false;
1233}
1234
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001235StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1236 ArrayRef<OMPClause *> Clauses,
1237 Stmt *AStmt,
1238 SourceLocation StartLoc,
1239 SourceLocation EndLoc) {
1240 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001241 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1242 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001243
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001244 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001245 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246 bool ErrorFound = false;
Alexey Bataev68446b72014-07-18 07:47:19 +00001247 if (AStmt) {
1248 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1249
1250 // Check default data sharing attributes for referenced variables.
1251 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1252 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1253 if (DSAChecker.isErrorFound())
1254 return StmtError();
1255 // Generate list of implicitly defined firstprivate variables.
1256 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
1257 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1258
1259 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1260 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1261 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1262 SourceLocation(), SourceLocation())) {
1263 ClausesWithImplicit.push_back(Implicit);
1264 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1265 DSAChecker.getImplicitFirstprivate().size();
1266 } else
1267 ErrorFound = true;
1268 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001269 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001270
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001271 switch (Kind) {
1272 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001273 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1274 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001275 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001276 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001277 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1278 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001279 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001280 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001281 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1282 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001283 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001284 case OMPD_sections:
1285 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1286 EndLoc);
1287 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001288 case OMPD_section:
1289 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001290 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001291 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1292 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001293 case OMPD_single:
1294 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1295 EndLoc);
1296 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001297 case OMPD_master:
1298 assert(ClausesWithImplicit.empty() &&
1299 "No clauses are allowed for 'omp master' directive");
1300 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1301 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001302 case OMPD_parallel_for:
1303 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1304 EndLoc, VarsWithInheritedDSA);
1305 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001306 case OMPD_parallel_sections:
1307 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1308 StartLoc, EndLoc);
1309 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001310 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001311 Res =
1312 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1313 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001314 case OMPD_taskyield:
1315 assert(ClausesWithImplicit.empty() &&
1316 "No clauses are allowed for 'omp taskyield' directive");
1317 assert(AStmt == nullptr &&
1318 "No associated statement allowed for 'omp taskyield' directive");
1319 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1320 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001321 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001322 llvm_unreachable("OpenMP Directive is not allowed");
1323 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001324 llvm_unreachable("Unknown OpenMP directive");
1325 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001326
Alexey Bataev4acb8592014-07-07 13:01:15 +00001327 for (auto P : VarsWithInheritedDSA) {
1328 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1329 << P.first << P.second->getSourceRange();
1330 }
1331 if (!VarsWithInheritedDSA.empty())
1332 return StmtError();
1333
Alexey Bataeved09d242014-05-28 05:53:51 +00001334 if (ErrorFound)
1335 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001336 return Res;
1337}
1338
1339StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1340 Stmt *AStmt,
1341 SourceLocation StartLoc,
1342 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001343 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1344 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1345 // 1.2.2 OpenMP Language Terminology
1346 // Structured block - An executable statement with a single entry at the
1347 // top and a single exit at the bottom.
1348 // The point of exit cannot be a branch out of the structured block.
1349 // longjmp() and throw() must not violate the entry/exit criteria.
1350 CS->getCapturedDecl()->setNothrow();
1351
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001352 getCurFunction()->setHasBranchProtectedScope();
1353
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001354 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1355 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001356}
1357
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001358namespace {
1359/// \brief Helper class for checking canonical form of the OpenMP loops and
1360/// extracting iteration space of each loop in the loop nest, that will be used
1361/// for IR generation.
1362class OpenMPIterationSpaceChecker {
1363 /// \brief Reference to Sema.
1364 Sema &SemaRef;
1365 /// \brief A location for diagnostics (when there is no some better location).
1366 SourceLocation DefaultLoc;
1367 /// \brief A location for diagnostics (when increment is not compatible).
1368 SourceLocation ConditionLoc;
1369 /// \brief A source location for referring to condition later.
1370 SourceRange ConditionSrcRange;
1371 /// \brief Loop variable.
1372 VarDecl *Var;
1373 /// \brief Lower bound (initializer for the var).
1374 Expr *LB;
1375 /// \brief Upper bound.
1376 Expr *UB;
1377 /// \brief Loop step (increment).
1378 Expr *Step;
1379 /// \brief This flag is true when condition is one of:
1380 /// Var < UB
1381 /// Var <= UB
1382 /// UB > Var
1383 /// UB >= Var
1384 bool TestIsLessOp;
1385 /// \brief This flag is true when condition is strict ( < or > ).
1386 bool TestIsStrictOp;
1387 /// \brief This flag is true when step is subtracted on each iteration.
1388 bool SubtractStep;
1389
1390public:
1391 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1392 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1393 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1394 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1395 SubtractStep(false) {}
1396 /// \brief Check init-expr for canonical loop form and save loop counter
1397 /// variable - #Var and its initialization value - #LB.
1398 bool CheckInit(Stmt *S);
1399 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1400 /// for less/greater and for strict/non-strict comparison.
1401 bool CheckCond(Expr *S);
1402 /// \brief Check incr-expr for canonical loop form and return true if it
1403 /// does not conform, otherwise save loop step (#Step).
1404 bool CheckInc(Expr *S);
1405 /// \brief Return the loop counter variable.
1406 VarDecl *GetLoopVar() const { return Var; }
1407 /// \brief Return true if any expression is dependent.
1408 bool Dependent() const;
1409
1410private:
1411 /// \brief Check the right-hand side of an assignment in the increment
1412 /// expression.
1413 bool CheckIncRHS(Expr *RHS);
1414 /// \brief Helper to set loop counter variable and its initializer.
1415 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1416 /// \brief Helper to set upper bound.
1417 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1418 const SourceLocation &SL);
1419 /// \brief Helper to set loop increment.
1420 bool SetStep(Expr *NewStep, bool Subtract);
1421};
1422
1423bool OpenMPIterationSpaceChecker::Dependent() const {
1424 if (!Var) {
1425 assert(!LB && !UB && !Step);
1426 return false;
1427 }
1428 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1429 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1430}
1431
1432bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1433 // State consistency checking to ensure correct usage.
1434 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1435 !TestIsLessOp && !TestIsStrictOp);
1436 if (!NewVar || !NewLB)
1437 return true;
1438 Var = NewVar;
1439 LB = NewLB;
1440 return false;
1441}
1442
1443bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1444 const SourceRange &SR,
1445 const SourceLocation &SL) {
1446 // State consistency checking to ensure correct usage.
1447 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1448 !TestIsLessOp && !TestIsStrictOp);
1449 if (!NewUB)
1450 return true;
1451 UB = NewUB;
1452 TestIsLessOp = LessOp;
1453 TestIsStrictOp = StrictOp;
1454 ConditionSrcRange = SR;
1455 ConditionLoc = SL;
1456 return false;
1457}
1458
1459bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1460 // State consistency checking to ensure correct usage.
1461 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1462 if (!NewStep)
1463 return true;
1464 if (!NewStep->isValueDependent()) {
1465 // Check that the step is integer expression.
1466 SourceLocation StepLoc = NewStep->getLocStart();
1467 ExprResult Val =
1468 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1469 if (Val.isInvalid())
1470 return true;
1471 NewStep = Val.get();
1472
1473 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1474 // If test-expr is of form var relational-op b and relational-op is < or
1475 // <= then incr-expr must cause var to increase on each iteration of the
1476 // loop. If test-expr is of form var relational-op b and relational-op is
1477 // > or >= then incr-expr must cause var to decrease on each iteration of
1478 // the loop.
1479 // If test-expr is of form b relational-op var and relational-op is < or
1480 // <= then incr-expr must cause var to decrease on each iteration of the
1481 // loop. If test-expr is of form b relational-op var and relational-op is
1482 // > or >= then incr-expr must cause var to increase on each iteration of
1483 // the loop.
1484 llvm::APSInt Result;
1485 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1486 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1487 bool IsConstNeg =
1488 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1489 bool IsConstZero = IsConstant && !Result.getBoolValue();
1490 if (UB && (IsConstZero ||
1491 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1492 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1493 SemaRef.Diag(NewStep->getExprLoc(),
1494 diag::err_omp_loop_incr_not_compatible)
1495 << Var << TestIsLessOp << NewStep->getSourceRange();
1496 SemaRef.Diag(ConditionLoc,
1497 diag::note_omp_loop_cond_requres_compatible_incr)
1498 << TestIsLessOp << ConditionSrcRange;
1499 return true;
1500 }
1501 }
1502
1503 Step = NewStep;
1504 SubtractStep = Subtract;
1505 return false;
1506}
1507
1508bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1509 // Check init-expr for canonical loop form and save loop counter
1510 // variable - #Var and its initialization value - #LB.
1511 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1512 // var = lb
1513 // integer-type var = lb
1514 // random-access-iterator-type var = lb
1515 // pointer-type var = lb
1516 //
1517 if (!S) {
1518 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1519 return true;
1520 }
1521 if (Expr *E = dyn_cast<Expr>(S))
1522 S = E->IgnoreParens();
1523 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1524 if (BO->getOpcode() == BO_Assign)
1525 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1526 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1527 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1528 if (DS->isSingleDecl()) {
1529 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1530 if (Var->hasInit()) {
1531 // Accept non-canonical init form here but emit ext. warning.
1532 if (Var->getInitStyle() != VarDecl::CInit)
1533 SemaRef.Diag(S->getLocStart(),
1534 diag::ext_omp_loop_not_canonical_init)
1535 << S->getSourceRange();
1536 return SetVarAndLB(Var, Var->getInit());
1537 }
1538 }
1539 }
1540 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1541 if (CE->getOperator() == OO_Equal)
1542 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1543 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1544
1545 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1546 << S->getSourceRange();
1547 return true;
1548}
1549
Alexey Bataev23b69422014-06-18 07:08:49 +00001550/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001551/// variable (which may be the loop variable) if possible.
1552static const VarDecl *GetInitVarDecl(const Expr *E) {
1553 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001554 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001555 E = E->IgnoreParenImpCasts();
1556 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1557 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1558 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1559 CE->getArg(0) != nullptr)
1560 E = CE->getArg(0)->IgnoreParenImpCasts();
1561 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1562 if (!DRE)
1563 return nullptr;
1564 return dyn_cast<VarDecl>(DRE->getDecl());
1565}
1566
1567bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1568 // Check test-expr for canonical form, save upper-bound UB, flags for
1569 // less/greater and for strict/non-strict comparison.
1570 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1571 // var relational-op b
1572 // b relational-op var
1573 //
1574 if (!S) {
1575 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1576 return true;
1577 }
1578 S = S->IgnoreParenImpCasts();
1579 SourceLocation CondLoc = S->getLocStart();
1580 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1581 if (BO->isRelationalOp()) {
1582 if (GetInitVarDecl(BO->getLHS()) == Var)
1583 return SetUB(BO->getRHS(),
1584 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1585 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1586 BO->getSourceRange(), BO->getOperatorLoc());
1587 if (GetInitVarDecl(BO->getRHS()) == Var)
1588 return SetUB(BO->getLHS(),
1589 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1590 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1591 BO->getSourceRange(), BO->getOperatorLoc());
1592 }
1593 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1594 if (CE->getNumArgs() == 2) {
1595 auto Op = CE->getOperator();
1596 switch (Op) {
1597 case OO_Greater:
1598 case OO_GreaterEqual:
1599 case OO_Less:
1600 case OO_LessEqual:
1601 if (GetInitVarDecl(CE->getArg(0)) == Var)
1602 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1603 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1604 CE->getOperatorLoc());
1605 if (GetInitVarDecl(CE->getArg(1)) == Var)
1606 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1607 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1608 CE->getOperatorLoc());
1609 break;
1610 default:
1611 break;
1612 }
1613 }
1614 }
1615 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1616 << S->getSourceRange() << Var;
1617 return true;
1618}
1619
1620bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1621 // RHS of canonical loop form increment can be:
1622 // var + incr
1623 // incr + var
1624 // var - incr
1625 //
1626 RHS = RHS->IgnoreParenImpCasts();
1627 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1628 if (BO->isAdditiveOp()) {
1629 bool IsAdd = BO->getOpcode() == BO_Add;
1630 if (GetInitVarDecl(BO->getLHS()) == Var)
1631 return SetStep(BO->getRHS(), !IsAdd);
1632 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1633 return SetStep(BO->getLHS(), false);
1634 }
1635 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1636 bool IsAdd = CE->getOperator() == OO_Plus;
1637 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1638 if (GetInitVarDecl(CE->getArg(0)) == Var)
1639 return SetStep(CE->getArg(1), !IsAdd);
1640 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1641 return SetStep(CE->getArg(0), false);
1642 }
1643 }
1644 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1645 << RHS->getSourceRange() << Var;
1646 return true;
1647}
1648
1649bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1650 // Check incr-expr for canonical loop form and return true if it
1651 // does not conform.
1652 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1653 // ++var
1654 // var++
1655 // --var
1656 // var--
1657 // var += incr
1658 // var -= incr
1659 // var = var + incr
1660 // var = incr + var
1661 // var = var - incr
1662 //
1663 if (!S) {
1664 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1665 return true;
1666 }
1667 S = S->IgnoreParens();
1668 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1669 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1670 return SetStep(
1671 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1672 (UO->isDecrementOp() ? -1 : 1)).get(),
1673 false);
1674 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1675 switch (BO->getOpcode()) {
1676 case BO_AddAssign:
1677 case BO_SubAssign:
1678 if (GetInitVarDecl(BO->getLHS()) == Var)
1679 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1680 break;
1681 case BO_Assign:
1682 if (GetInitVarDecl(BO->getLHS()) == Var)
1683 return CheckIncRHS(BO->getRHS());
1684 break;
1685 default:
1686 break;
1687 }
1688 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1689 switch (CE->getOperator()) {
1690 case OO_PlusPlus:
1691 case OO_MinusMinus:
1692 if (GetInitVarDecl(CE->getArg(0)) == Var)
1693 return SetStep(
1694 SemaRef.ActOnIntegerConstant(
1695 CE->getLocStart(),
1696 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1697 false);
1698 break;
1699 case OO_PlusEqual:
1700 case OO_MinusEqual:
1701 if (GetInitVarDecl(CE->getArg(0)) == Var)
1702 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1703 break;
1704 case OO_Equal:
1705 if (GetInitVarDecl(CE->getArg(0)) == Var)
1706 return CheckIncRHS(CE->getArg(1));
1707 break;
1708 default:
1709 break;
1710 }
1711 }
1712 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1713 << S->getSourceRange() << Var;
1714 return true;
1715}
Alexey Bataev23b69422014-06-18 07:08:49 +00001716} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001717
1718/// \brief Called on a for stmt to check and extract its iteration space
1719/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001720static bool CheckOpenMPIterationSpace(
1721 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1722 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1723 Expr *NestedLoopCountExpr,
1724 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001725 // OpenMP [2.6, Canonical Loop Form]
1726 // for (init-expr; test-expr; incr-expr) structured-block
1727 auto For = dyn_cast_or_null<ForStmt>(S);
1728 if (!For) {
1729 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001730 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1731 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1732 << CurrentNestedLoopCount;
1733 if (NestedLoopCount > 1)
1734 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1735 diag::note_omp_collapse_expr)
1736 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001737 return true;
1738 }
1739 assert(For->getBody());
1740
1741 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1742
1743 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001744 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001745 if (ISC.CheckInit(Init)) {
1746 return true;
1747 }
1748
1749 bool HasErrors = false;
1750
1751 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001752 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001753
1754 // OpenMP [2.6, Canonical Loop Form]
1755 // Var is one of the following:
1756 // A variable of signed or unsigned integer type.
1757 // For C++, a variable of a random access iterator type.
1758 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001759 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001760 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1761 !VarType->isPointerType() &&
1762 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1763 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1764 << SemaRef.getLangOpts().CPlusPlus;
1765 HasErrors = true;
1766 }
1767
Alexey Bataev4acb8592014-07-07 13:01:15 +00001768 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
1769 // Construct
1770 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1771 // parallel for construct is (are) private.
1772 // The loop iteration variable in the associated for-loop of a simd construct
1773 // with just one associated for-loop is linear with a constant-linear-step
1774 // that is the increment of the associated for-loop.
1775 // Exclude loop var from the list of variables with implicitly defined data
1776 // sharing attributes.
1777 while (VarsWithImplicitDSA.count(Var) > 0)
1778 VarsWithImplicitDSA.erase(Var);
1779
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001780 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1781 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001782 // The loop iteration variable in the associated for-loop of a simd construct
1783 // with just one associated for-loop may be listed in a linear clause with a
1784 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001785 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1786 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001787 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001788 auto PredeterminedCKind =
1789 isOpenMPSimdDirective(DKind)
1790 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
1791 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001792 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001793 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00001794 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1795 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001796 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001797 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00001798 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
1799 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001800 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001801 HasErrors = true;
1802 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001803 // Make the loop iteration variable private (for worksharing constructs),
1804 // linear (for simd directives with the only one associated loop) or
1805 // lastprivate (for simd directives with several collapsed loops).
1806 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001807 }
1808
Alexey Bataev7ff55242014-06-19 09:13:45 +00001809 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001810
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001811 // Check test-expr.
1812 HasErrors |= ISC.CheckCond(For->getCond());
1813
1814 // Check incr-expr.
1815 HasErrors |= ISC.CheckInc(For->getInc());
1816
1817 if (ISC.Dependent())
1818 return HasErrors;
1819
1820 // FIXME: Build loop's iteration space representation.
1821 return HasErrors;
1822}
1823
1824/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1825/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1826/// to get the first for loop.
1827static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1828 if (IgnoreCaptured)
1829 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1830 S = CapS->getCapturedStmt();
1831 // OpenMP [2.8.1, simd construct, Restrictions]
1832 // All loops associated with the construct must be perfectly nested; that is,
1833 // there must be no intervening code nor any OpenMP directive between any two
1834 // loops.
1835 while (true) {
1836 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1837 S = AS->getSubStmt();
1838 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1839 if (CS->size() != 1)
1840 break;
1841 S = CS->body_back();
1842 } else
1843 break;
1844 }
1845 return S;
1846}
1847
1848/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001849/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1850/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001851static unsigned
1852CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
1853 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
1854 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001855 unsigned NestedLoopCount = 1;
1856 if (NestedLoopCountExpr) {
1857 // Found 'collapse' clause - calculate collapse number.
1858 llvm::APSInt Result;
1859 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1860 NestedLoopCount = Result.getLimitedValue();
1861 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001862 // This is helper routine for loop directives (e.g., 'for', 'simd',
1863 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001864 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1865 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001866 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00001867 NestedLoopCount, NestedLoopCountExpr,
1868 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001869 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001870 // Move on to the next nested for loop, or to the loop body.
1871 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1872 }
1873
1874 // FIXME: Build resulting iteration space for IR generation (collapsing
1875 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001876 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001877}
1878
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001879static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001880 auto CollapseFilter = [](const OMPClause *C) -> bool {
1881 return C->getClauseKind() == OMPC_collapse;
1882 };
1883 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1884 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001885 if (I)
1886 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1887 return nullptr;
1888}
1889
Alexey Bataev4acb8592014-07-07 13:01:15 +00001890StmtResult Sema::ActOnOpenMPSimdDirective(
1891 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1892 SourceLocation EndLoc,
1893 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001894 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001895 unsigned NestedLoopCount =
1896 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
1897 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001898 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001899 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001900
1901 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001902 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1903 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001904}
1905
Alexey Bataev4acb8592014-07-07 13:01:15 +00001906StmtResult Sema::ActOnOpenMPForDirective(
1907 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1908 SourceLocation EndLoc,
1909 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001910 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001911 unsigned NestedLoopCount =
1912 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
1913 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001914 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001915 return StmtError();
1916
1917 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001918 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1919 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001920}
1921
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001922StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1923 Stmt *AStmt,
1924 SourceLocation StartLoc,
1925 SourceLocation EndLoc) {
1926 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1927 auto BaseStmt = AStmt;
1928 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1929 BaseStmt = CS->getCapturedStmt();
1930 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1931 auto S = C->children();
1932 if (!S)
1933 return StmtError();
1934 // All associated statements must be '#pragma omp section' except for
1935 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001936 for (++S; S; ++S) {
1937 auto SectionStmt = *S;
1938 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1939 if (SectionStmt)
1940 Diag(SectionStmt->getLocStart(),
1941 diag::err_omp_sections_substmt_not_section);
1942 return StmtError();
1943 }
1944 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001945 } else {
1946 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1947 return StmtError();
1948 }
1949
1950 getCurFunction()->setHasBranchProtectedScope();
1951
1952 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1953 AStmt);
1954}
1955
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001956StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1957 SourceLocation StartLoc,
1958 SourceLocation EndLoc) {
1959 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1960
1961 getCurFunction()->setHasBranchProtectedScope();
1962
1963 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1964}
1965
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001966StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1967 Stmt *AStmt,
1968 SourceLocation StartLoc,
1969 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00001970 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1971
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001972 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00001973
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001974 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1975}
1976
Alexander Musman80c22892014-07-17 08:54:58 +00001977StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
1978 SourceLocation StartLoc,
1979 SourceLocation EndLoc) {
1980 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1981
1982 getCurFunction()->setHasBranchProtectedScope();
1983
1984 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
1985}
1986
Alexey Bataev4acb8592014-07-07 13:01:15 +00001987StmtResult Sema::ActOnOpenMPParallelForDirective(
1988 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1989 SourceLocation EndLoc,
1990 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
1991 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1992 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1993 // 1.2.2 OpenMP Language Terminology
1994 // Structured block - An executable statement with a single entry at the
1995 // top and a single exit at the bottom.
1996 // The point of exit cannot be a branch out of the structured block.
1997 // longjmp() and throw() must not violate the entry/exit criteria.
1998 CS->getCapturedDecl()->setNothrow();
1999
2000 // In presence of clause 'collapse', it will define the nested loops number.
2001 unsigned NestedLoopCount =
2002 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
2003 *this, *DSAStack, VarsWithImplicitDSA);
2004 if (NestedLoopCount == 0)
2005 return StmtError();
2006
2007 getCurFunction()->setHasBranchProtectedScope();
2008 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
2009 NestedLoopCount, Clauses, AStmt);
2010}
2011
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002012StmtResult
2013Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
2014 Stmt *AStmt, SourceLocation StartLoc,
2015 SourceLocation EndLoc) {
2016 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2017 auto BaseStmt = AStmt;
2018 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2019 BaseStmt = CS->getCapturedStmt();
2020 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2021 auto S = C->children();
2022 if (!S)
2023 return StmtError();
2024 // All associated statements must be '#pragma omp section' except for
2025 // the first one.
2026 for (++S; S; ++S) {
2027 auto SectionStmt = *S;
2028 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2029 if (SectionStmt)
2030 Diag(SectionStmt->getLocStart(),
2031 diag::err_omp_parallel_sections_substmt_not_section);
2032 return StmtError();
2033 }
2034 }
2035 } else {
2036 Diag(AStmt->getLocStart(),
2037 diag::err_omp_parallel_sections_not_compound_stmt);
2038 return StmtError();
2039 }
2040
2041 getCurFunction()->setHasBranchProtectedScope();
2042
2043 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
2044 Clauses, AStmt);
2045}
2046
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002047StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
2048 Stmt *AStmt, SourceLocation StartLoc,
2049 SourceLocation EndLoc) {
2050 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2051 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2052 // 1.2.2 OpenMP Language Terminology
2053 // Structured block - An executable statement with a single entry at the
2054 // top and a single exit at the bottom.
2055 // The point of exit cannot be a branch out of the structured block.
2056 // longjmp() and throw() must not violate the entry/exit criteria.
2057 CS->getCapturedDecl()->setNothrow();
2058
2059 getCurFunction()->setHasBranchProtectedScope();
2060
2061 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
2062}
2063
Alexey Bataev68446b72014-07-18 07:47:19 +00002064StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
2065 SourceLocation EndLoc) {
2066 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
2067}
2068
Alexey Bataeved09d242014-05-28 05:53:51 +00002069OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002070 SourceLocation StartLoc,
2071 SourceLocation LParenLoc,
2072 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002073 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002074 switch (Kind) {
2075 case OMPC_if:
2076 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
2077 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00002078 case OMPC_final:
2079 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
2080 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00002081 case OMPC_num_threads:
2082 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
2083 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002084 case OMPC_safelen:
2085 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
2086 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00002087 case OMPC_collapse:
2088 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
2089 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002090 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002091 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002092 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002093 case OMPC_private:
2094 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002095 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002096 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002097 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002098 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002099 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002100 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002101 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002102 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002103 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002104 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002105 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002106 case OMPC_threadprivate:
2107 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002108 llvm_unreachable("Clause is not allowed.");
2109 }
2110 return Res;
2111}
2112
Alexey Bataeved09d242014-05-28 05:53:51 +00002113OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002114 SourceLocation LParenLoc,
2115 SourceLocation EndLoc) {
2116 Expr *ValExpr = Condition;
2117 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2118 !Condition->isInstantiationDependent() &&
2119 !Condition->containsUnexpandedParameterPack()) {
2120 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00002121 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002122 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002123 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002124
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002125 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002126 }
2127
2128 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2129}
2130
Alexey Bataev3778b602014-07-17 07:32:53 +00002131OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
2132 SourceLocation StartLoc,
2133 SourceLocation LParenLoc,
2134 SourceLocation EndLoc) {
2135 Expr *ValExpr = Condition;
2136 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
2137 !Condition->isInstantiationDependent() &&
2138 !Condition->containsUnexpandedParameterPack()) {
2139 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
2140 Condition->getExprLoc(), Condition);
2141 if (Val.isInvalid())
2142 return nullptr;
2143
2144 ValExpr = Val.get();
2145 }
2146
2147 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
2148}
2149
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002150ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
2151 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002152 if (!Op)
2153 return ExprError();
2154
2155 class IntConvertDiagnoser : public ICEConvertDiagnoser {
2156 public:
2157 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00002158 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00002159 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
2160 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002161 return S.Diag(Loc, diag::err_omp_not_integral) << T;
2162 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002163 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
2164 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002165 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
2166 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002167 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
2168 QualType T,
2169 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002170 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
2171 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002172 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
2173 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002174 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002175 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002176 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002177 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
2178 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002179 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
2180 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002181 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
2182 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002183 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00002184 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00002185 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002186 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
2187 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00002188 llvm_unreachable("conversion functions are permitted");
2189 }
2190 } ConvertDiagnoser;
2191 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
2192}
2193
2194OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
2195 SourceLocation StartLoc,
2196 SourceLocation LParenLoc,
2197 SourceLocation EndLoc) {
2198 Expr *ValExpr = NumThreads;
2199 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
2200 !NumThreads->isInstantiationDependent() &&
2201 !NumThreads->containsUnexpandedParameterPack()) {
2202 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
2203 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002204 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00002205 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002206 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002207
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002208 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00002209
2210 // OpenMP [2.5, Restrictions]
2211 // The num_threads expression must evaluate to a positive integer value.
2212 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00002213 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
2214 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00002215 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
2216 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002217 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00002218 }
2219 }
2220
Alexey Bataeved09d242014-05-28 05:53:51 +00002221 return new (Context)
2222 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00002223}
2224
Alexey Bataev62c87d22014-03-21 04:51:18 +00002225ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
2226 OpenMPClauseKind CKind) {
2227 if (!E)
2228 return ExprError();
2229 if (E->isValueDependent() || E->isTypeDependent() ||
2230 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002231 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002232 llvm::APSInt Result;
2233 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
2234 if (ICE.isInvalid())
2235 return ExprError();
2236 if (!Result.isStrictlyPositive()) {
2237 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
2238 << getOpenMPClauseName(CKind) << E->getSourceRange();
2239 return ExprError();
2240 }
2241 return ICE;
2242}
2243
2244OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
2245 SourceLocation LParenLoc,
2246 SourceLocation EndLoc) {
2247 // OpenMP [2.8.1, simd construct, Description]
2248 // The parameter of the safelen clause must be a constant
2249 // positive integer expression.
2250 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
2251 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002252 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00002253 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002254 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00002255}
2256
Alexander Musman64d33f12014-06-04 07:53:32 +00002257OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2258 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002259 SourceLocation LParenLoc,
2260 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002261 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002262 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002263 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002264 // The parameter of the collapse clause must be a constant
2265 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002266 ExprResult NumForLoopsResult =
2267 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2268 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002269 return nullptr;
2270 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002271 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002272}
2273
Alexey Bataeved09d242014-05-28 05:53:51 +00002274OMPClause *Sema::ActOnOpenMPSimpleClause(
2275 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2276 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002277 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002278 switch (Kind) {
2279 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002280 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002281 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2282 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002283 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002284 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002285 Res = ActOnOpenMPProcBindClause(
2286 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2287 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002288 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002289 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002290 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002291 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002292 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002293 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002294 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002295 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002296 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002297 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002298 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002299 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002300 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002301 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002302 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002303 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002304 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002305 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002306 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002307 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002308 case OMPC_threadprivate:
2309 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002310 llvm_unreachable("Clause is not allowed.");
2311 }
2312 return Res;
2313}
2314
2315OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2316 SourceLocation KindKwLoc,
2317 SourceLocation StartLoc,
2318 SourceLocation LParenLoc,
2319 SourceLocation EndLoc) {
2320 if (Kind == OMPC_DEFAULT_unknown) {
2321 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002322 static_assert(OMPC_DEFAULT_unknown > 0,
2323 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002324 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002325 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002326 Values += "'";
2327 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2328 Values += "'";
2329 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002330 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002331 Values += " or ";
2332 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002333 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002334 break;
2335 default:
2336 Values += Sep;
2337 break;
2338 }
2339 }
2340 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002341 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002342 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002343 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002344 switch (Kind) {
2345 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002346 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002347 break;
2348 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002349 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002350 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002351 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002352 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002353 break;
2354 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002355 return new (Context)
2356 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002357}
2358
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002359OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2360 SourceLocation KindKwLoc,
2361 SourceLocation StartLoc,
2362 SourceLocation LParenLoc,
2363 SourceLocation EndLoc) {
2364 if (Kind == OMPC_PROC_BIND_unknown) {
2365 std::string Values;
2366 std::string Sep(", ");
2367 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2368 Values += "'";
2369 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2370 Values += "'";
2371 switch (i) {
2372 case OMPC_PROC_BIND_unknown - 2:
2373 Values += " or ";
2374 break;
2375 case OMPC_PROC_BIND_unknown - 1:
2376 break;
2377 default:
2378 Values += Sep;
2379 break;
2380 }
2381 }
2382 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002383 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002384 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002385 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002386 return new (Context)
2387 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002388}
2389
Alexey Bataev56dafe82014-06-20 07:16:17 +00002390OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2391 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2392 SourceLocation StartLoc, SourceLocation LParenLoc,
2393 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2394 SourceLocation EndLoc) {
2395 OMPClause *Res = nullptr;
2396 switch (Kind) {
2397 case OMPC_schedule:
2398 Res = ActOnOpenMPScheduleClause(
2399 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2400 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2401 break;
2402 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002403 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002404 case OMPC_num_threads:
2405 case OMPC_safelen:
2406 case OMPC_collapse:
2407 case OMPC_default:
2408 case OMPC_proc_bind:
2409 case OMPC_private:
2410 case OMPC_firstprivate:
2411 case OMPC_lastprivate:
2412 case OMPC_shared:
2413 case OMPC_reduction:
2414 case OMPC_linear:
2415 case OMPC_aligned:
2416 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002417 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002418 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002419 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002420 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002421 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002422 case OMPC_threadprivate:
2423 case OMPC_unknown:
2424 llvm_unreachable("Clause is not allowed.");
2425 }
2426 return Res;
2427}
2428
2429OMPClause *Sema::ActOnOpenMPScheduleClause(
2430 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2431 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2432 SourceLocation EndLoc) {
2433 if (Kind == OMPC_SCHEDULE_unknown) {
2434 std::string Values;
2435 std::string Sep(", ");
2436 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2437 Values += "'";
2438 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2439 Values += "'";
2440 switch (i) {
2441 case OMPC_SCHEDULE_unknown - 2:
2442 Values += " or ";
2443 break;
2444 case OMPC_SCHEDULE_unknown - 1:
2445 break;
2446 default:
2447 Values += Sep;
2448 break;
2449 }
2450 }
2451 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2452 << Values << getOpenMPClauseName(OMPC_schedule);
2453 return nullptr;
2454 }
2455 Expr *ValExpr = ChunkSize;
2456 if (ChunkSize) {
2457 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2458 !ChunkSize->isInstantiationDependent() &&
2459 !ChunkSize->containsUnexpandedParameterPack()) {
2460 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2461 ExprResult Val =
2462 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2463 if (Val.isInvalid())
2464 return nullptr;
2465
2466 ValExpr = Val.get();
2467
2468 // OpenMP [2.7.1, Restrictions]
2469 // chunk_size must be a loop invariant integer expression with a positive
2470 // value.
2471 llvm::APSInt Result;
2472 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2473 Result.isSigned() && !Result.isStrictlyPositive()) {
2474 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2475 << "schedule" << ChunkSize->getSourceRange();
2476 return nullptr;
2477 }
2478 }
2479 }
2480
2481 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2482 EndLoc, Kind, ValExpr);
2483}
2484
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002485OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2486 SourceLocation StartLoc,
2487 SourceLocation EndLoc) {
2488 OMPClause *Res = nullptr;
2489 switch (Kind) {
2490 case OMPC_ordered:
2491 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2492 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002493 case OMPC_nowait:
2494 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2495 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002496 case OMPC_untied:
2497 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
2498 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002499 case OMPC_mergeable:
2500 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
2501 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002502 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002503 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002504 case OMPC_num_threads:
2505 case OMPC_safelen:
2506 case OMPC_collapse:
2507 case OMPC_schedule:
2508 case OMPC_private:
2509 case OMPC_firstprivate:
2510 case OMPC_lastprivate:
2511 case OMPC_shared:
2512 case OMPC_reduction:
2513 case OMPC_linear:
2514 case OMPC_aligned:
2515 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002516 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002517 case OMPC_default:
2518 case OMPC_proc_bind:
2519 case OMPC_threadprivate:
2520 case OMPC_unknown:
2521 llvm_unreachable("Clause is not allowed.");
2522 }
2523 return Res;
2524}
2525
2526OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2527 SourceLocation EndLoc) {
2528 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2529}
2530
Alexey Bataev236070f2014-06-20 11:19:47 +00002531OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2532 SourceLocation EndLoc) {
2533 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2534}
2535
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002536OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
2537 SourceLocation EndLoc) {
2538 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
2539}
2540
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002541OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
2542 SourceLocation EndLoc) {
2543 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
2544}
2545
Alexey Bataevc5e02582014-06-16 07:08:35 +00002546OMPClause *Sema::ActOnOpenMPVarListClause(
2547 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2548 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2549 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2550 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002551 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002552 switch (Kind) {
2553 case OMPC_private:
2554 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2555 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002556 case OMPC_firstprivate:
2557 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2558 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002559 case OMPC_lastprivate:
2560 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2561 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002562 case OMPC_shared:
2563 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2564 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002565 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002566 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2567 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002568 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002569 case OMPC_linear:
2570 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2571 ColonLoc, EndLoc);
2572 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002573 case OMPC_aligned:
2574 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2575 ColonLoc, EndLoc);
2576 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002577 case OMPC_copyin:
2578 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2579 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002580 case OMPC_copyprivate:
2581 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2582 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002583 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00002584 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00002585 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002586 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002587 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002588 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002589 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002590 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002591 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002592 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00002593 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00002594 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002595 case OMPC_threadprivate:
2596 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002597 llvm_unreachable("Clause is not allowed.");
2598 }
2599 return Res;
2600}
2601
2602OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2603 SourceLocation StartLoc,
2604 SourceLocation LParenLoc,
2605 SourceLocation EndLoc) {
2606 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002607 for (auto &RefExpr : VarList) {
2608 assert(RefExpr && "NULL expr in OpenMP private clause.");
2609 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002610 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002611 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002612 continue;
2613 }
2614
Alexey Bataeved09d242014-05-28 05:53:51 +00002615 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002616 // OpenMP [2.1, C/C++]
2617 // A list item is a variable name.
2618 // OpenMP [2.9.3.3, Restrictions, p.1]
2619 // A variable that is part of another variable (as an array or
2620 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002621 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002622 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002623 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002624 continue;
2625 }
2626 Decl *D = DE->getDecl();
2627 VarDecl *VD = cast<VarDecl>(D);
2628
2629 QualType Type = VD->getType();
2630 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2631 // It will be analyzed later.
2632 Vars.push_back(DE);
2633 continue;
2634 }
2635
2636 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2637 // A variable that appears in a private clause must not have an incomplete
2638 // type or a reference type.
2639 if (RequireCompleteType(ELoc, Type,
2640 diag::err_omp_private_incomplete_type)) {
2641 continue;
2642 }
2643 if (Type->isReferenceType()) {
2644 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002645 << getOpenMPClauseName(OMPC_private) << Type;
2646 bool IsDecl =
2647 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2648 Diag(VD->getLocation(),
2649 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2650 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002651 continue;
2652 }
2653
2654 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2655 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002656 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002657 // class type.
2658 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002659 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2660 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002661 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002662 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2663 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2664 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002665 // FIXME This code must be replaced by actual constructing/destructing of
2666 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002667 if (RD) {
2668 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2669 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002670 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002671 if (!CD ||
2672 CheckConstructorAccess(ELoc, CD,
2673 InitializedEntity::InitializeTemporary(Type),
2674 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002675 CD->isDeleted()) {
2676 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002677 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002678 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2679 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002680 Diag(VD->getLocation(),
2681 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2682 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002683 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2684 continue;
2685 }
2686 MarkFunctionReferenced(ELoc, CD);
2687 DiagnoseUseOfDecl(CD, ELoc);
2688
2689 CXXDestructorDecl *DD = RD->getDestructor();
2690 if (DD) {
2691 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2692 DD->isDeleted()) {
2693 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002694 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002695 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2696 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002697 Diag(VD->getLocation(),
2698 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2699 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002700 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2701 continue;
2702 }
2703 MarkFunctionReferenced(ELoc, DD);
2704 DiagnoseUseOfDecl(DD, ELoc);
2705 }
2706 }
2707
Alexey Bataev758e55e2013-09-06 18:03:48 +00002708 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2709 // in a Construct]
2710 // Variables with the predetermined data-sharing attributes may not be
2711 // listed in data-sharing attributes clauses, except for the cases
2712 // listed below. For these exceptions only, listing a predetermined
2713 // variable in a data-sharing attribute clause is allowed and overrides
2714 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002715 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002716 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002717 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2718 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002719 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002720 continue;
2721 }
2722
2723 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002724 Vars.push_back(DE);
2725 }
2726
Alexey Bataeved09d242014-05-28 05:53:51 +00002727 if (Vars.empty())
2728 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002729
2730 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2731}
2732
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002733OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2734 SourceLocation StartLoc,
2735 SourceLocation LParenLoc,
2736 SourceLocation EndLoc) {
2737 SmallVector<Expr *, 8> Vars;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002738 bool IsImplicitClause =
2739 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
2740 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
2741
Alexey Bataeved09d242014-05-28 05:53:51 +00002742 for (auto &RefExpr : VarList) {
2743 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2744 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002745 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002746 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002747 continue;
2748 }
2749
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002750 SourceLocation ELoc = IsImplicitClause ? ImplicitClauseLoc
2751 : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002752 // OpenMP [2.1, C/C++]
2753 // A list item is a variable name.
2754 // OpenMP [2.9.3.3, Restrictions, p.1]
2755 // A variable that is part of another variable (as an array or
2756 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002757 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002758 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002759 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002760 continue;
2761 }
2762 Decl *D = DE->getDecl();
2763 VarDecl *VD = cast<VarDecl>(D);
2764
2765 QualType Type = VD->getType();
2766 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2767 // It will be analyzed later.
2768 Vars.push_back(DE);
2769 continue;
2770 }
2771
2772 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2773 // A variable that appears in a private clause must not have an incomplete
2774 // type or a reference type.
2775 if (RequireCompleteType(ELoc, Type,
2776 diag::err_omp_firstprivate_incomplete_type)) {
2777 continue;
2778 }
2779 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002780 if (IsImplicitClause) {
2781 Diag(ImplicitClauseLoc,
2782 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
2783 << Type;
2784 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2785 } else {
2786 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2787 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2788 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002789 bool IsDecl =
2790 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2791 Diag(VD->getLocation(),
2792 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2793 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002794 continue;
2795 }
2796
2797 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2798 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002799 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002800 // class type.
2801 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002802 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2803 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2804 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002805 // FIXME This code must be replaced by actual constructing/destructing of
2806 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002807 if (RD) {
2808 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2809 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002810 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002811 if (!CD ||
2812 CheckConstructorAccess(ELoc, CD,
2813 InitializedEntity::InitializeTemporary(Type),
2814 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002815 CD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002816 if (IsImplicitClause) {
2817 Diag(ImplicitClauseLoc,
2818 diag::err_omp_task_predetermined_firstprivate_required_method)
2819 << 0;
2820 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2821 } else {
2822 Diag(ELoc, diag::err_omp_required_method)
2823 << getOpenMPClauseName(OMPC_firstprivate) << 1;
2824 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002825 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2826 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002827 Diag(VD->getLocation(),
2828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2829 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002830 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2831 continue;
2832 }
2833 MarkFunctionReferenced(ELoc, CD);
2834 DiagnoseUseOfDecl(CD, ELoc);
2835
2836 CXXDestructorDecl *DD = RD->getDestructor();
2837 if (DD) {
2838 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2839 DD->isDeleted()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002840 if (IsImplicitClause) {
2841 Diag(ImplicitClauseLoc,
2842 diag::err_omp_task_predetermined_firstprivate_required_method)
2843 << 1;
2844 Diag(RefExpr->getExprLoc(), diag::note_used_here);
2845 } else {
2846 Diag(ELoc, diag::err_omp_required_method)
2847 << getOpenMPClauseName(OMPC_firstprivate) << 4;
2848 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002849 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2850 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002851 Diag(VD->getLocation(),
2852 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2853 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002854 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2855 continue;
2856 }
2857 MarkFunctionReferenced(ELoc, DD);
2858 DiagnoseUseOfDecl(DD, ELoc);
2859 }
2860 }
2861
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002862 // If an implicit firstprivate variable found it was checked already.
2863 if (!IsImplicitClause) {
2864 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002865 Type = Type.getNonReferenceType().getCanonicalType();
2866 bool IsConstant = Type.isConstant(Context);
2867 Type = Context.getBaseElementType(Type);
2868 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2869 // A list item that specifies a given variable may not appear in more
2870 // than one clause on the same directive, except that a variable may be
2871 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002872 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002873 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002874 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002875 << getOpenMPClauseName(DVar.CKind)
2876 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002877 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002878 continue;
2879 }
2880
2881 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2882 // in a Construct]
2883 // Variables with the predetermined data-sharing attributes may not be
2884 // listed in data-sharing attributes clauses, except for the cases
2885 // listed below. For these exceptions only, listing a predetermined
2886 // variable in a data-sharing attribute clause is allowed and overrides
2887 // the variable's predetermined data-sharing attributes.
2888 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2889 // in a Construct, C/C++, p.2]
2890 // Variables with const-qualified type having no mutable member may be
2891 // listed in a firstprivate clause, even if they are static data members.
2892 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2893 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2894 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002895 << getOpenMPClauseName(DVar.CKind)
2896 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002897 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002898 continue;
2899 }
2900
Alexey Bataevf29276e2014-06-18 04:14:57 +00002901 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002902 // OpenMP [2.9.3.4, Restrictions, p.2]
2903 // A list item that is private within a parallel region must not appear
2904 // in a firstprivate clause on a worksharing construct if any of the
2905 // worksharing regions arising from the worksharing construct ever bind
2906 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002907 if (isOpenMPWorksharingDirective(CurrDir) &&
2908 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002909 DVar = DSAStack->getImplicitDSA(VD, true);
2910 if (DVar.CKind != OMPC_shared &&
2911 (isOpenMPParallelDirective(DVar.DKind) ||
2912 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002913 Diag(ELoc, diag::err_omp_required_access)
2914 << getOpenMPClauseName(OMPC_firstprivate)
2915 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002916 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002917 continue;
2918 }
2919 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002920 // OpenMP [2.9.3.4, Restrictions, p.3]
2921 // A list item that appears in a reduction clause of a parallel construct
2922 // must not appear in a firstprivate clause on a worksharing or task
2923 // construct if any of the worksharing or task regions arising from the
2924 // worksharing or task construct ever bind to any of the parallel regions
2925 // arising from the parallel construct.
2926 // OpenMP [2.9.3.4, Restrictions, p.4]
2927 // A list item that appears in a reduction clause in worksharing
2928 // construct must not appear in a firstprivate clause in a task construct
2929 // encountered during execution of any of the worksharing regions arising
2930 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002931 if (CurrDir == OMPD_task) {
2932 DVar =
2933 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
2934 [](OpenMPDirectiveKind K) -> bool {
2935 return isOpenMPParallelDirective(K) ||
2936 isOpenMPWorksharingDirective(K);
2937 },
2938 false);
2939 if (DVar.CKind == OMPC_reduction &&
2940 (isOpenMPParallelDirective(DVar.DKind) ||
2941 isOpenMPWorksharingDirective(DVar.DKind))) {
2942 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
2943 << getOpenMPDirectiveName(DVar.DKind);
2944 ReportOriginalDSA(*this, DSAStack, VD, DVar);
2945 continue;
2946 }
2947 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002948 }
2949
2950 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2951 Vars.push_back(DE);
2952 }
2953
Alexey Bataeved09d242014-05-28 05:53:51 +00002954 if (Vars.empty())
2955 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002956
2957 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2958 Vars);
2959}
2960
Alexander Musman1bb328c2014-06-04 13:06:39 +00002961OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2962 SourceLocation StartLoc,
2963 SourceLocation LParenLoc,
2964 SourceLocation EndLoc) {
2965 SmallVector<Expr *, 8> Vars;
2966 for (auto &RefExpr : VarList) {
2967 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2968 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2969 // It will be analyzed later.
2970 Vars.push_back(RefExpr);
2971 continue;
2972 }
2973
2974 SourceLocation ELoc = RefExpr->getExprLoc();
2975 // OpenMP [2.1, C/C++]
2976 // A list item is a variable name.
2977 // OpenMP [2.14.3.5, Restrictions, p.1]
2978 // A variable that is part of another variable (as an array or structure
2979 // element) cannot appear in a lastprivate clause.
2980 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2981 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2982 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2983 continue;
2984 }
2985 Decl *D = DE->getDecl();
2986 VarDecl *VD = cast<VarDecl>(D);
2987
2988 QualType Type = VD->getType();
2989 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2990 // It will be analyzed later.
2991 Vars.push_back(DE);
2992 continue;
2993 }
2994
2995 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2996 // A variable that appears in a lastprivate clause must not have an
2997 // incomplete type or a reference type.
2998 if (RequireCompleteType(ELoc, Type,
2999 diag::err_omp_lastprivate_incomplete_type)) {
3000 continue;
3001 }
3002 if (Type->isReferenceType()) {
3003 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3004 << getOpenMPClauseName(OMPC_lastprivate) << Type;
3005 bool IsDecl =
3006 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3007 Diag(VD->getLocation(),
3008 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3009 << VD;
3010 continue;
3011 }
3012
3013 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3014 // in a Construct]
3015 // Variables with the predetermined data-sharing attributes may not be
3016 // listed in data-sharing attributes clauses, except for the cases
3017 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003018 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003019 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
3020 DVar.CKind != OMPC_firstprivate &&
3021 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
3022 Diag(ELoc, diag::err_omp_wrong_dsa)
3023 << getOpenMPClauseName(DVar.CKind)
3024 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003025 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003026 continue;
3027 }
3028
Alexey Bataevf29276e2014-06-18 04:14:57 +00003029 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
3030 // OpenMP [2.14.3.5, Restrictions, p.2]
3031 // A list item that is private within a parallel region, or that appears in
3032 // the reduction clause of a parallel construct, must not appear in a
3033 // lastprivate clause on a worksharing construct if any of the corresponding
3034 // worksharing regions ever binds to any of the corresponding parallel
3035 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00003036 if (isOpenMPWorksharingDirective(CurrDir) &&
3037 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003038 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003039 if (DVar.CKind != OMPC_shared) {
3040 Diag(ELoc, diag::err_omp_required_access)
3041 << getOpenMPClauseName(OMPC_lastprivate)
3042 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003043 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003044 continue;
3045 }
3046 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003047 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00003048 // A variable of class type (or array thereof) that appears in a
3049 // lastprivate clause requires an accessible, unambiguous default
3050 // constructor for the class type, unless the list item is also specified
3051 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003052 // A variable of class type (or array thereof) that appears in a
3053 // lastprivate clause requires an accessible, unambiguous copy assignment
3054 // operator for the class type.
3055 while (Type.getNonReferenceType()->isArrayType())
3056 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3057 ->getElementType();
3058 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3059 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3060 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003061 // FIXME This code must be replaced by actual copying and destructing of the
3062 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00003063 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00003064 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3065 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003066 if (MD) {
3067 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3068 MD->isDeleted()) {
3069 Diag(ELoc, diag::err_omp_required_method)
3070 << getOpenMPClauseName(OMPC_lastprivate) << 2;
3071 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3072 VarDecl::DeclarationOnly;
3073 Diag(VD->getLocation(),
3074 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3075 << VD;
3076 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3077 continue;
3078 }
3079 MarkFunctionReferenced(ELoc, MD);
3080 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003081 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00003082
3083 CXXDestructorDecl *DD = RD->getDestructor();
3084 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003085 PartialDiagnostic PD =
3086 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00003087 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3088 DD->isDeleted()) {
3089 Diag(ELoc, diag::err_omp_required_method)
3090 << getOpenMPClauseName(OMPC_lastprivate) << 4;
3091 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3092 VarDecl::DeclarationOnly;
3093 Diag(VD->getLocation(),
3094 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3095 << VD;
3096 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3097 continue;
3098 }
3099 MarkFunctionReferenced(ELoc, DD);
3100 DiagnoseUseOfDecl(DD, ELoc);
3101 }
3102 }
3103
Alexey Bataevf29276e2014-06-18 04:14:57 +00003104 if (DVar.CKind != OMPC_firstprivate)
3105 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00003106 Vars.push_back(DE);
3107 }
3108
3109 if (Vars.empty())
3110 return nullptr;
3111
3112 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
3113 Vars);
3114}
3115
Alexey Bataev758e55e2013-09-06 18:03:48 +00003116OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
3117 SourceLocation StartLoc,
3118 SourceLocation LParenLoc,
3119 SourceLocation EndLoc) {
3120 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003121 for (auto &RefExpr : VarList) {
3122 assert(RefExpr && "NULL expr in OpenMP shared clause.");
3123 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00003124 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003125 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003126 continue;
3127 }
3128
Alexey Bataeved09d242014-05-28 05:53:51 +00003129 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003130 // OpenMP [2.1, C/C++]
3131 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00003132 // OpenMP [2.14.3.2, Restrictions, p.1]
3133 // A variable that is part of another variable (as an array or structure
3134 // element) cannot appear in a shared unless it is a static data member
3135 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00003136 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003137 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003138 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003139 continue;
3140 }
3141 Decl *D = DE->getDecl();
3142 VarDecl *VD = cast<VarDecl>(D);
3143
3144 QualType Type = VD->getType();
3145 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3146 // It will be analyzed later.
3147 Vars.push_back(DE);
3148 continue;
3149 }
3150
3151 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3152 // in a Construct]
3153 // Variables with the predetermined data-sharing attributes may not be
3154 // listed in data-sharing attributes clauses, except for the cases
3155 // listed below. For these exceptions only, listing a predetermined
3156 // variable in a data-sharing attribute clause is allowed and overrides
3157 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003158 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00003159 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
3160 DVar.RefExpr) {
3161 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3162 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003163 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003164 continue;
3165 }
3166
3167 DSAStack->addDSA(VD, DE, OMPC_shared);
3168 Vars.push_back(DE);
3169 }
3170
Alexey Bataeved09d242014-05-28 05:53:51 +00003171 if (Vars.empty())
3172 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003173
3174 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3175}
3176
Alexey Bataevc5e02582014-06-16 07:08:35 +00003177namespace {
3178class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
3179 DSAStackTy *Stack;
3180
3181public:
3182 bool VisitDeclRefExpr(DeclRefExpr *E) {
3183 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003184 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003185 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
3186 return false;
3187 if (DVar.CKind != OMPC_unknown)
3188 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003189 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003190 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003191 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00003192 return true;
3193 return false;
3194 }
3195 return false;
3196 }
3197 bool VisitStmt(Stmt *S) {
3198 for (auto Child : S->children()) {
3199 if (Child && Visit(Child))
3200 return true;
3201 }
3202 return false;
3203 }
Alexey Bataev23b69422014-06-18 07:08:49 +00003204 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00003205};
Alexey Bataev23b69422014-06-18 07:08:49 +00003206} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00003207
3208OMPClause *Sema::ActOnOpenMPReductionClause(
3209 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
3210 SourceLocation ColonLoc, SourceLocation EndLoc,
3211 CXXScopeSpec &ReductionIdScopeSpec,
3212 const DeclarationNameInfo &ReductionId) {
3213 // TODO: Allow scope specification search when 'declare reduction' is
3214 // supported.
3215 assert(ReductionIdScopeSpec.isEmpty() &&
3216 "No support for scoped reduction identifiers yet.");
3217
3218 auto DN = ReductionId.getName();
3219 auto OOK = DN.getCXXOverloadedOperator();
3220 BinaryOperatorKind BOK = BO_Comma;
3221
3222 // OpenMP [2.14.3.6, reduction clause]
3223 // C
3224 // reduction-identifier is either an identifier or one of the following
3225 // operators: +, -, *, &, |, ^, && and ||
3226 // C++
3227 // reduction-identifier is either an id-expression or one of the following
3228 // operators: +, -, *, &, |, ^, && and ||
3229 // FIXME: Only 'min' and 'max' identifiers are supported for now.
3230 switch (OOK) {
3231 case OO_Plus:
3232 case OO_Minus:
3233 BOK = BO_AddAssign;
3234 break;
3235 case OO_Star:
3236 BOK = BO_MulAssign;
3237 break;
3238 case OO_Amp:
3239 BOK = BO_AndAssign;
3240 break;
3241 case OO_Pipe:
3242 BOK = BO_OrAssign;
3243 break;
3244 case OO_Caret:
3245 BOK = BO_XorAssign;
3246 break;
3247 case OO_AmpAmp:
3248 BOK = BO_LAnd;
3249 break;
3250 case OO_PipePipe:
3251 BOK = BO_LOr;
3252 break;
3253 default:
3254 if (auto II = DN.getAsIdentifierInfo()) {
3255 if (II->isStr("max"))
3256 BOK = BO_GT;
3257 else if (II->isStr("min"))
3258 BOK = BO_LT;
3259 }
3260 break;
3261 }
3262 SourceRange ReductionIdRange;
3263 if (ReductionIdScopeSpec.isValid()) {
3264 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
3265 }
3266 ReductionIdRange.setEnd(ReductionId.getEndLoc());
3267 if (BOK == BO_Comma) {
3268 // Not allowed reduction identifier is found.
3269 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
3270 << ReductionIdRange;
3271 return nullptr;
3272 }
3273
3274 SmallVector<Expr *, 8> Vars;
3275 for (auto RefExpr : VarList) {
3276 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
3277 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3278 // It will be analyzed later.
3279 Vars.push_back(RefExpr);
3280 continue;
3281 }
3282
3283 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
3284 RefExpr->isInstantiationDependent() ||
3285 RefExpr->containsUnexpandedParameterPack()) {
3286 // It will be analyzed later.
3287 Vars.push_back(RefExpr);
3288 continue;
3289 }
3290
3291 auto ELoc = RefExpr->getExprLoc();
3292 auto ERange = RefExpr->getSourceRange();
3293 // OpenMP [2.1, C/C++]
3294 // A list item is a variable or array section, subject to the restrictions
3295 // specified in Section 2.4 on page 42 and in each of the sections
3296 // describing clauses and directives for which a list appears.
3297 // OpenMP [2.14.3.3, Restrictions, p.1]
3298 // A variable that is part of another variable (as an array or
3299 // structure element) cannot appear in a private clause.
3300 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
3301 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3302 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
3303 continue;
3304 }
3305 auto D = DE->getDecl();
3306 auto VD = cast<VarDecl>(D);
3307 auto Type = VD->getType();
3308 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3309 // A variable that appears in a private clause must not have an incomplete
3310 // type or a reference type.
3311 if (RequireCompleteType(ELoc, Type,
3312 diag::err_omp_reduction_incomplete_type))
3313 continue;
3314 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3315 // Arrays may not appear in a reduction clause.
3316 if (Type.getNonReferenceType()->isArrayType()) {
3317 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
3318 bool IsDecl =
3319 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3320 Diag(VD->getLocation(),
3321 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3322 << VD;
3323 continue;
3324 }
3325 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3326 // A list item that appears in a reduction clause must not be
3327 // const-qualified.
3328 if (Type.getNonReferenceType().isConstant(Context)) {
3329 Diag(ELoc, diag::err_omp_const_variable)
3330 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3331 bool IsDecl =
3332 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3333 Diag(VD->getLocation(),
3334 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3335 << VD;
3336 continue;
3337 }
3338 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3339 // If a list-item is a reference type then it must bind to the same object
3340 // for all threads of the team.
3341 VarDecl *VDDef = VD->getDefinition();
3342 if (Type->isReferenceType() && VDDef) {
3343 DSARefChecker Check(DSAStack);
3344 if (Check.Visit(VDDef->getInit())) {
3345 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3346 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3347 continue;
3348 }
3349 }
3350 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3351 // The type of a list item that appears in a reduction clause must be valid
3352 // for the reduction-identifier. For a max or min reduction in C, the type
3353 // of the list item must be an allowed arithmetic data type: char, int,
3354 // float, double, or _Bool, possibly modified with long, short, signed, or
3355 // unsigned. For a max or min reduction in C++, the type of the list item
3356 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3357 // double, or bool, possibly modified with long, short, signed, or unsigned.
3358 if ((BOK == BO_GT || BOK == BO_LT) &&
3359 !(Type->isScalarType() ||
3360 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3361 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3362 << getLangOpts().CPlusPlus;
3363 bool IsDecl =
3364 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3365 Diag(VD->getLocation(),
3366 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3367 << VD;
3368 continue;
3369 }
3370 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3371 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3372 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3373 bool IsDecl =
3374 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3375 Diag(VD->getLocation(),
3376 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3377 << VD;
3378 continue;
3379 }
3380 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3381 getDiagnostics().setSuppressAllDiagnostics(true);
3382 ExprResult ReductionOp =
3383 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3384 RefExpr, RefExpr);
3385 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3386 if (ReductionOp.isInvalid()) {
3387 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003388 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003389 bool IsDecl =
3390 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3391 Diag(VD->getLocation(),
3392 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3393 << VD;
3394 continue;
3395 }
3396
3397 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3398 // in a Construct]
3399 // Variables with the predetermined data-sharing attributes may not be
3400 // listed in data-sharing attributes clauses, except for the cases
3401 // listed below. For these exceptions only, listing a predetermined
3402 // variable in a data-sharing attribute clause is allowed and overrides
3403 // the variable's predetermined data-sharing attributes.
3404 // OpenMP [2.14.3.6, Restrictions, p.3]
3405 // Any number of reduction clauses can be specified on the directive,
3406 // but a list item can appear only once in the reduction clauses for that
3407 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003408 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003409 if (DVar.CKind == OMPC_reduction) {
3410 Diag(ELoc, diag::err_omp_once_referenced)
3411 << getOpenMPClauseName(OMPC_reduction);
3412 if (DVar.RefExpr) {
3413 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3414 }
3415 } else if (DVar.CKind != OMPC_unknown) {
3416 Diag(ELoc, diag::err_omp_wrong_dsa)
3417 << getOpenMPClauseName(DVar.CKind)
3418 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003419 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003420 continue;
3421 }
3422
3423 // OpenMP [2.14.3.6, Restrictions, p.1]
3424 // A list item that appears in a reduction clause of a worksharing
3425 // construct must be shared in the parallel regions to which any of the
3426 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003427 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003428 if (isOpenMPWorksharingDirective(CurrDir) &&
3429 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003430 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003431 if (DVar.CKind != OMPC_shared) {
3432 Diag(ELoc, diag::err_omp_required_access)
3433 << getOpenMPClauseName(OMPC_reduction)
3434 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003435 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003436 continue;
3437 }
3438 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003439
3440 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3441 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3442 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003443 // FIXME This code must be replaced by actual constructing/destructing of
3444 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003445 if (RD) {
3446 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3447 PartialDiagnostic PD =
3448 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003449 if (!CD ||
3450 CheckConstructorAccess(ELoc, CD,
3451 InitializedEntity::InitializeTemporary(Type),
3452 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003453 CD->isDeleted()) {
3454 Diag(ELoc, diag::err_omp_required_method)
3455 << getOpenMPClauseName(OMPC_reduction) << 0;
3456 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3457 VarDecl::DeclarationOnly;
3458 Diag(VD->getLocation(),
3459 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3460 << VD;
3461 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3462 continue;
3463 }
3464 MarkFunctionReferenced(ELoc, CD);
3465 DiagnoseUseOfDecl(CD, ELoc);
3466
3467 CXXDestructorDecl *DD = RD->getDestructor();
3468 if (DD) {
3469 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3470 DD->isDeleted()) {
3471 Diag(ELoc, diag::err_omp_required_method)
3472 << getOpenMPClauseName(OMPC_reduction) << 4;
3473 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3474 VarDecl::DeclarationOnly;
3475 Diag(VD->getLocation(),
3476 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3477 << VD;
3478 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3479 continue;
3480 }
3481 MarkFunctionReferenced(ELoc, DD);
3482 DiagnoseUseOfDecl(DD, ELoc);
3483 }
3484 }
3485
3486 DSAStack->addDSA(VD, DE, OMPC_reduction);
3487 Vars.push_back(DE);
3488 }
3489
3490 if (Vars.empty())
3491 return nullptr;
3492
3493 return OMPReductionClause::Create(
3494 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3495 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3496}
3497
Alexander Musman8dba6642014-04-22 13:09:42 +00003498OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3499 SourceLocation StartLoc,
3500 SourceLocation LParenLoc,
3501 SourceLocation ColonLoc,
3502 SourceLocation EndLoc) {
3503 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003504 for (auto &RefExpr : VarList) {
3505 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3506 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003507 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003508 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003509 continue;
3510 }
3511
3512 // OpenMP [2.14.3.7, linear clause]
3513 // A list item that appears in a linear clause is subject to the private
3514 // clause semantics described in Section 2.14.3.3 on page 159 except as
3515 // noted. In addition, the value of the new list item on each iteration
3516 // of the associated loop(s) corresponds to the value of the original
3517 // list item before entering the construct plus the logical number of
3518 // the iteration times linear-step.
3519
Alexey Bataeved09d242014-05-28 05:53:51 +00003520 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003521 // OpenMP [2.1, C/C++]
3522 // A list item is a variable name.
3523 // OpenMP [2.14.3.3, Restrictions, p.1]
3524 // A variable that is part of another variable (as an array or
3525 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003526 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003527 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003528 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003529 continue;
3530 }
3531
3532 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3533
3534 // OpenMP [2.14.3.7, linear clause]
3535 // A list-item cannot appear in more than one linear clause.
3536 // A list-item that appears in a linear clause cannot appear in any
3537 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003538 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00003539 if (DVar.RefExpr) {
3540 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3541 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003542 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003543 continue;
3544 }
3545
3546 QualType QType = VD->getType();
3547 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3548 // It will be analyzed later.
3549 Vars.push_back(DE);
3550 continue;
3551 }
3552
3553 // A variable must not have an incomplete type or a reference type.
3554 if (RequireCompleteType(ELoc, QType,
3555 diag::err_omp_linear_incomplete_type)) {
3556 continue;
3557 }
3558 if (QType->isReferenceType()) {
3559 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3560 << getOpenMPClauseName(OMPC_linear) << QType;
3561 bool IsDecl =
3562 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3563 Diag(VD->getLocation(),
3564 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3565 << VD;
3566 continue;
3567 }
3568
3569 // A list item must not be const-qualified.
3570 if (QType.isConstant(Context)) {
3571 Diag(ELoc, diag::err_omp_const_variable)
3572 << getOpenMPClauseName(OMPC_linear);
3573 bool IsDecl =
3574 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3575 Diag(VD->getLocation(),
3576 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3577 << VD;
3578 continue;
3579 }
3580
3581 // A list item must be of integral or pointer type.
3582 QType = QType.getUnqualifiedType().getCanonicalType();
3583 const Type *Ty = QType.getTypePtrOrNull();
3584 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3585 !Ty->isPointerType())) {
3586 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3587 bool IsDecl =
3588 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3589 Diag(VD->getLocation(),
3590 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3591 << VD;
3592 continue;
3593 }
3594
3595 DSAStack->addDSA(VD, DE, OMPC_linear);
3596 Vars.push_back(DE);
3597 }
3598
3599 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003600 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003601
3602 Expr *StepExpr = Step;
3603 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3604 !Step->isInstantiationDependent() &&
3605 !Step->containsUnexpandedParameterPack()) {
3606 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003607 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003608 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003609 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003610 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003611
3612 // Warn about zero linear step (it would be probably better specified as
3613 // making corresponding variables 'const').
3614 llvm::APSInt Result;
3615 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3616 !Result.isNegative() && !Result.isStrictlyPositive())
3617 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3618 << (Vars.size() > 1);
3619 }
3620
3621 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3622 Vars, StepExpr);
3623}
3624
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003625OMPClause *Sema::ActOnOpenMPAlignedClause(
3626 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3627 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3628
3629 SmallVector<Expr *, 8> Vars;
3630 for (auto &RefExpr : VarList) {
3631 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3632 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3633 // It will be analyzed later.
3634 Vars.push_back(RefExpr);
3635 continue;
3636 }
3637
3638 SourceLocation ELoc = RefExpr->getExprLoc();
3639 // OpenMP [2.1, C/C++]
3640 // A list item is a variable name.
3641 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3642 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3643 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3644 continue;
3645 }
3646
3647 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3648
3649 // OpenMP [2.8.1, simd construct, Restrictions]
3650 // The type of list items appearing in the aligned clause must be
3651 // array, pointer, reference to array, or reference to pointer.
3652 QualType QType = DE->getType()
3653 .getNonReferenceType()
3654 .getUnqualifiedType()
3655 .getCanonicalType();
3656 const Type *Ty = QType.getTypePtrOrNull();
3657 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3658 !Ty->isPointerType())) {
3659 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3660 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3661 bool IsDecl =
3662 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3663 Diag(VD->getLocation(),
3664 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3665 << VD;
3666 continue;
3667 }
3668
3669 // OpenMP [2.8.1, simd construct, Restrictions]
3670 // A list-item cannot appear in more than one aligned clause.
3671 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3672 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3673 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3674 << getOpenMPClauseName(OMPC_aligned);
3675 continue;
3676 }
3677
3678 Vars.push_back(DE);
3679 }
3680
3681 // OpenMP [2.8.1, simd construct, Description]
3682 // The parameter of the aligned clause, alignment, must be a constant
3683 // positive integer expression.
3684 // If no optional parameter is specified, implementation-defined default
3685 // alignments for SIMD instructions on the target platforms are assumed.
3686 if (Alignment != nullptr) {
3687 ExprResult AlignResult =
3688 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3689 if (AlignResult.isInvalid())
3690 return nullptr;
3691 Alignment = AlignResult.get();
3692 }
3693 if (Vars.empty())
3694 return nullptr;
3695
3696 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3697 EndLoc, Vars, Alignment);
3698}
3699
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003700OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3701 SourceLocation StartLoc,
3702 SourceLocation LParenLoc,
3703 SourceLocation EndLoc) {
3704 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003705 for (auto &RefExpr : VarList) {
3706 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3707 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003708 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003709 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003710 continue;
3711 }
3712
Alexey Bataeved09d242014-05-28 05:53:51 +00003713 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003714 // OpenMP [2.1, C/C++]
3715 // A list item is a variable name.
3716 // OpenMP [2.14.4.1, Restrictions, p.1]
3717 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003718 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003719 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003720 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003721 continue;
3722 }
3723
3724 Decl *D = DE->getDecl();
3725 VarDecl *VD = cast<VarDecl>(D);
3726
3727 QualType Type = VD->getType();
3728 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3729 // It will be analyzed later.
3730 Vars.push_back(DE);
3731 continue;
3732 }
3733
3734 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3735 // A list item that appears in a copyin clause must be threadprivate.
3736 if (!DSAStack->isThreadPrivate(VD)) {
3737 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003738 << getOpenMPClauseName(OMPC_copyin)
3739 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003740 continue;
3741 }
3742
3743 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3744 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003745 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003746 // operator for the class type.
3747 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003748 CXXRecordDecl *RD =
3749 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003750 // FIXME This code must be replaced by actual assignment of the
3751 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003752 if (RD) {
3753 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3754 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003755 if (MD) {
3756 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3757 MD->isDeleted()) {
3758 Diag(ELoc, diag::err_omp_required_method)
3759 << getOpenMPClauseName(OMPC_copyin) << 2;
3760 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3761 VarDecl::DeclarationOnly;
3762 Diag(VD->getLocation(),
3763 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3764 << VD;
3765 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3766 continue;
3767 }
3768 MarkFunctionReferenced(ELoc, MD);
3769 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003770 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003771 }
3772
3773 DSAStack->addDSA(VD, DE, OMPC_copyin);
3774 Vars.push_back(DE);
3775 }
3776
Alexey Bataeved09d242014-05-28 05:53:51 +00003777 if (Vars.empty())
3778 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003779
3780 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3781}
3782
Alexey Bataevbae9a792014-06-27 10:37:06 +00003783OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3784 SourceLocation StartLoc,
3785 SourceLocation LParenLoc,
3786 SourceLocation EndLoc) {
3787 SmallVector<Expr *, 8> Vars;
3788 for (auto &RefExpr : VarList) {
3789 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3790 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3791 // It will be analyzed later.
3792 Vars.push_back(RefExpr);
3793 continue;
3794 }
3795
3796 SourceLocation ELoc = RefExpr->getExprLoc();
3797 // OpenMP [2.1, C/C++]
3798 // A list item is a variable name.
3799 // OpenMP [2.14.4.1, Restrictions, p.1]
3800 // A list item that appears in a copyin clause must be threadprivate.
3801 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3802 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3803 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3804 continue;
3805 }
3806
3807 Decl *D = DE->getDecl();
3808 VarDecl *VD = cast<VarDecl>(D);
3809
3810 QualType Type = VD->getType();
3811 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3812 // It will be analyzed later.
3813 Vars.push_back(DE);
3814 continue;
3815 }
3816
3817 // OpenMP [2.14.4.2, Restrictions, p.2]
3818 // A list item that appears in a copyprivate clause may not appear in a
3819 // private or firstprivate clause on the single construct.
3820 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003821 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003822 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3823 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3824 Diag(ELoc, diag::err_omp_wrong_dsa)
3825 << getOpenMPClauseName(DVar.CKind)
3826 << getOpenMPClauseName(OMPC_copyprivate);
3827 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3828 continue;
3829 }
3830
3831 // OpenMP [2.11.4.2, Restrictions, p.1]
3832 // All list items that appear in a copyprivate clause must be either
3833 // threadprivate or private in the enclosing context.
3834 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003835 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00003836 if (DVar.CKind == OMPC_shared) {
3837 Diag(ELoc, diag::err_omp_required_access)
3838 << getOpenMPClauseName(OMPC_copyprivate)
3839 << "threadprivate or private in the enclosing context";
3840 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3841 continue;
3842 }
3843 }
3844 }
3845
3846 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3847 // A variable of class type (or array thereof) that appears in a
3848 // copyin clause requires an accessible, unambiguous copy assignment
3849 // operator for the class type.
3850 Type = Context.getBaseElementType(Type);
3851 CXXRecordDecl *RD =
3852 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3853 // FIXME This code must be replaced by actual assignment of the
3854 // threadprivate variable.
3855 if (RD) {
3856 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3857 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3858 if (MD) {
3859 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3860 MD->isDeleted()) {
3861 Diag(ELoc, diag::err_omp_required_method)
3862 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3863 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3864 VarDecl::DeclarationOnly;
3865 Diag(VD->getLocation(),
3866 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3867 << VD;
3868 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3869 continue;
3870 }
3871 MarkFunctionReferenced(ELoc, MD);
3872 DiagnoseUseOfDecl(MD, ELoc);
3873 }
3874 }
3875
3876 // No need to mark vars as copyprivate, they are already threadprivate or
3877 // implicitly private.
3878 Vars.push_back(DE);
3879 }
3880
3881 if (Vars.empty())
3882 return nullptr;
3883
3884 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3885}
3886
Alexey Bataev758e55e2013-09-06 18:03:48 +00003887#undef DSAStack