blob: 5ce0c84f84a7322ffd1da0fc4aaaa2c59e3caa4e [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.
142 DSAVarData getTopDSA(VarDecl *D);
143 /// \brief Returns data-sharing attributes for the specified declaration.
144 DSAVarData getImplicitDSA(VarDecl *D);
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,
150 DirectivesPredicate DPred);
151 /// \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,
156 DirectivesPredicate DPred);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000157
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 /// \brief Returns currently analyzed directive.
159 OpenMPDirectiveKind getCurrentDirective() const {
160 return Stack.back().Directive;
161 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000162 /// \brief Returns parent directive.
163 OpenMPDirectiveKind getParentDirective() const {
164 if (Stack.size() > 2)
165 return Stack[Stack.size() - 2].Directive;
166 return OMPD_unknown;
167 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000168
169 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000170 void setDefaultDSANone(SourceLocation Loc) {
171 Stack.back().DefaultAttr = DSA_none;
172 Stack.back().DefaultAttrLoc = Loc;
173 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000174 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000175 void setDefaultDSAShared(SourceLocation Loc) {
176 Stack.back().DefaultAttr = DSA_shared;
177 Stack.back().DefaultAttrLoc = Loc;
178 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000179
180 DefaultDataSharingAttributes getDefaultDSA() const {
181 return Stack.back().DefaultAttr;
182 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000183 SourceLocation getDefaultDSALocation() const {
184 return Stack.back().DefaultAttrLoc;
185 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186
Alexey Bataevf29276e2014-06-18 04:14:57 +0000187 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000188 bool isThreadPrivate(VarDecl *D) {
189 DSAVarData DVar = getTopDSA(D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000190 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000191 }
192
193 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000194 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000195 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000196};
Alexey Bataeved09d242014-05-28 05:53:51 +0000197} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000198
199DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
200 VarDecl *D) {
201 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000202 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000203 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
204 // in a region but not in construct]
205 // File-scope or namespace-scope variables referenced in called routines
206 // in the region are shared unless they appear in a threadprivate
207 // directive.
Alexey Bataev750a58b2014-03-18 12:19:12 +0000208 if (!D->isFunctionOrMethodVarDecl())
209 DVar.CKind = OMPC_shared;
210
211 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
212 // in a region but not in construct]
213 // Variables with static storage duration that are declared in called
214 // routines in the region are shared.
215 if (D->hasGlobalStorage())
216 DVar.CKind = OMPC_shared;
217
Alexey Bataev758e55e2013-09-06 18:03:48 +0000218 return DVar;
219 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000220
Alexey Bataev758e55e2013-09-06 18:03:48 +0000221 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000222 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
223 // in a Construct, C/C++, predetermined, p.1]
224 // Variables with automatic storage duration that are declared in a scope
225 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000226 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
227 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
228 DVar.CKind = OMPC_private;
229 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000230 }
231
Alexey Bataev758e55e2013-09-06 18:03:48 +0000232 // Explicitly specified attributes and local variables with predetermined
233 // attributes.
234 if (Iter->SharingMap.count(D)) {
235 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
236 DVar.CKind = Iter->SharingMap[D].Attributes;
237 return DVar;
238 }
239
240 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
241 // in a Construct, C/C++, implicitly determined, p.1]
242 // In a parallel or task construct, the data-sharing attributes of these
243 // variables are determined by the default clause, if present.
244 switch (Iter->DefaultAttr) {
245 case DSA_shared:
246 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000247 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000248 return DVar;
249 case DSA_none:
250 return DVar;
251 case DSA_unspecified:
252 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
253 // in a Construct, implicitly determined, p.2]
254 // In a parallel construct, if no default clause is present, these
255 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000256 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000257 if (isOpenMPParallelDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 DVar.CKind = OMPC_shared;
259 return DVar;
260 }
261
262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263 // in a Construct, implicitly determined, p.4]
264 // In a task construct, if no default clause is present, a variable that in
265 // the enclosing context is determined to be shared by all implicit tasks
266 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000267 if (DVar.DKind == OMPD_task) {
268 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000269 for (StackTy::reverse_iterator I = std::next(Iter),
270 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000271 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000272 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
273 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000274 // in a Construct, implicitly determined, p.6]
275 // In a task construct, if no default clause is present, a variable
276 // whose data-sharing attribute is not determined by the rules above is
277 // firstprivate.
278 DVarTemp = getDSA(I, D);
279 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000280 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000281 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000282 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000283 return DVar;
284 }
Alexey Bataevcefffae2014-06-23 08:21:53 +0000285 if (isOpenMPParallelDirective(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000286 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000287 }
288 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000290 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000291 return DVar;
292 }
293 }
294 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
295 // in a Construct, implicitly determined, p.3]
296 // For constructs other than task, if no default clause is present, these
297 // variables inherit their data-sharing attributes from the enclosing
298 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000299 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000300}
301
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000302DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
303 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
304 auto It = Stack.back().AlignedMap.find(D);
305 if (It == Stack.back().AlignedMap.end()) {
306 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
307 Stack.back().AlignedMap[D] = NewDE;
308 return nullptr;
309 } else {
310 assert(It->second && "Unexpected nullptr expr in the aligned map");
311 return It->second;
312 }
313 return nullptr;
314}
315
Alexey Bataev758e55e2013-09-06 18:03:48 +0000316void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
317 if (A == OMPC_threadprivate) {
318 Stack[0].SharingMap[D].Attributes = A;
319 Stack[0].SharingMap[D].RefExpr = E;
320 } else {
321 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
322 Stack.back().SharingMap[D].Attributes = A;
323 Stack.back().SharingMap[D].RefExpr = E;
324 }
325}
326
Alexey Bataeved09d242014-05-28 05:53:51 +0000327bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000328 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000329 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000330 Scope *TopScope = nullptr;
Alexey Bataevcefffae2014-06-23 08:21:53 +0000331 while (I != E && !isOpenMPParallelDirective(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000332 ++I;
333 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000334 if (I == E)
335 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000336 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000337 Scope *CurScope = getCurScope();
338 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000340 }
341 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000342 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000343 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000344}
345
346DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
347 DSAVarData DVar;
348
349 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
350 // in a Construct, C/C++, predetermined, p.1]
351 // Variables appearing in threadprivate directives are threadprivate.
352 if (D->getTLSKind() != VarDecl::TLS_None) {
353 DVar.CKind = OMPC_threadprivate;
354 return DVar;
355 }
356 if (Stack[0].SharingMap.count(D)) {
357 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
358 DVar.CKind = OMPC_threadprivate;
359 return DVar;
360 }
361
362 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
363 // in a Construct, C/C++, predetermined, p.1]
364 // Variables with automatic storage duration that are declared in a scope
365 // inside the construct are private.
Alexey Bataevec3da872014-01-31 05:15:34 +0000366 OpenMPDirectiveKind Kind = getCurrentDirective();
Alexey Bataevcefffae2014-06-23 08:21:53 +0000367 if (!isOpenMPParallelDirective(Kind)) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000368 if (isOpenMPLocal(D, std::next(Stack.rbegin())) && D->isLocalVarDecl() &&
Alexey Bataeved09d242014-05-28 05:53:51 +0000369 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000370 DVar.CKind = OMPC_private;
371 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000372 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 }
374
375 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
376 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000377 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000378 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000379 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000380 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ff55242014-06-19 09:13:45 +0000381 DSAVarData DVarTemp =
382 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000383 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
384 return DVar;
385
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 DVar.CKind = OMPC_shared;
387 return DVar;
388 }
389
390 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000391 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392 while (Type->isArrayType()) {
393 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
394 Type = ElemType.getNonReferenceType().getCanonicalType();
395 }
396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
397 // in a Construct, C/C++, predetermined, p.6]
398 // Variables with const qualified type having no mutable member are
399 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000400 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000401 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000402 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000403 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 // Variables with const-qualified type having no mutable member may be
405 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev7ff55242014-06-19 09:13:45 +0000406 DSAVarData DVarTemp =
407 hasDSA(D, MatchesAnyClause(OMPC_firstprivate), MatchesAlways());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000408 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
409 return DVar;
410
Alexey Bataev758e55e2013-09-06 18:03:48 +0000411 DVar.CKind = OMPC_shared;
412 return DVar;
413 }
414
415 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
416 // in a Construct, C/C++, predetermined, p.7]
417 // Variables with static storage duration that are declared in a scope
418 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000419 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000420 DVar.CKind = OMPC_shared;
421 return DVar;
422 }
423
424 // Explicitly specified attributes and local variables with predetermined
425 // attributes.
426 if (Stack.back().SharingMap.count(D)) {
427 DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
428 DVar.CKind = Stack.back().SharingMap[D].Attributes;
429 }
430
431 return DVar;
432}
433
434DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000435 return getDSA(std::next(Stack.rbegin()), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436}
437
Alexey Bataevf29276e2014-06-18 04:14:57 +0000438template <class ClausesPredicate, class DirectivesPredicate>
439DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
440 DirectivesPredicate DPred) {
Benjamin Kramer167e9992014-03-02 12:20:24 +0000441 for (StackTy::reverse_iterator I = std::next(Stack.rbegin()),
442 E = std::prev(Stack.rend());
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000443 I != E; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000444 if (!DPred(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000445 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000446 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000447 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000448 return DVar;
449 }
450 return DSAVarData();
451}
452
Alexey Bataevf29276e2014-06-18 04:14:57 +0000453template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataevc5e02582014-06-16 07:08:35 +0000454DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(VarDecl *D,
Alexey Bataevf29276e2014-06-18 04:14:57 +0000455 ClausesPredicate CPred,
456 DirectivesPredicate DPred) {
Alexey Bataevc5e02582014-06-16 07:08:35 +0000457 for (auto I = Stack.rbegin(), EE = std::prev(Stack.rend()); I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000458 if (!DPred(I->Directive))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000459 continue;
460 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000461 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000462 return DVar;
463 return DSAVarData();
464 }
465 return DSAVarData();
466}
467
Alexey Bataev758e55e2013-09-06 18:03:48 +0000468void Sema::InitDataSharingAttributesStack() {
469 VarDataSharingAttributesStack = new DSAStackTy(*this);
470}
471
472#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
473
Alexey Bataeved09d242014-05-28 05:53:51 +0000474void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000475
476void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
477 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000478 Scope *CurScope, SourceLocation Loc) {
479 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 PushExpressionEvaluationContext(PotentiallyEvaluated);
481}
482
483void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000484 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
485 // A variable of class type (or array thereof) that appears in a lastprivate
486 // clause requires an accessible, unambiguous default constructor for the
487 // class type, unless the list item is also specified in a firstprivate
488 // clause.
489 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
490 for (auto C : D->clauses()) {
491 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
492 for (auto VarRef : Clause->varlists()) {
493 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
494 continue;
495 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
496 auto DVar = DSAStack->getTopDSA(VD);
497 if (DVar.CKind == OMPC_lastprivate) {
498 SourceLocation ELoc = VarRef->getExprLoc();
499 auto Type = VarRef->getType();
500 if (Type->isArrayType())
501 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
502 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000503 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
504 // FIXME This code must be replaced by actual constructing of the
505 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000506 if (RD) {
507 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
508 PartialDiagnostic PD =
509 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
510 if (!CD ||
511 CheckConstructorAccess(
512 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
513 CD->getAccess(), PD) == AR_inaccessible ||
514 CD->isDeleted()) {
515 Diag(ELoc, diag::err_omp_required_method)
516 << getOpenMPClauseName(OMPC_lastprivate) << 0;
517 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
518 VarDecl::DeclarationOnly;
519 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
520 : diag::note_defined_here)
521 << VD;
522 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
523 continue;
524 }
525 MarkFunctionReferenced(ELoc, CD);
526 DiagnoseUseOfDecl(CD, ELoc);
527 }
528 }
529 }
530 }
531 }
532 }
533
Alexey Bataev758e55e2013-09-06 18:03:48 +0000534 DSAStack->pop();
535 DiscardCleanupsInEvaluationContext();
536 PopExpressionEvaluationContext();
537}
538
Alexey Bataeva769e072013-03-22 06:34:35 +0000539namespace {
540
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000541class VarDeclFilterCCC : public CorrectionCandidateCallback {
542private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000543 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000544
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000545public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000546 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000547 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000548 NamedDecl *ND = Candidate.getCorrectionDecl();
549 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
550 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000551 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
552 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000553 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000554 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000555 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000556};
Alexey Bataeved09d242014-05-28 05:53:51 +0000557} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000558
559ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
560 CXXScopeSpec &ScopeSpec,
561 const DeclarationNameInfo &Id) {
562 LookupResult Lookup(*this, Id, LookupOrdinaryName);
563 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
564
565 if (Lookup.isAmbiguous())
566 return ExprError();
567
568 VarDecl *VD;
569 if (!Lookup.isSingleResult()) {
570 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000571 if (TypoCorrection Corrected =
572 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
573 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000574 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000575 PDiag(Lookup.empty()
576 ? diag::err_undeclared_var_use_suggest
577 : diag::err_omp_expected_var_arg_suggest)
578 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000579 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000580 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000581 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
582 : diag::err_omp_expected_var_arg)
583 << Id.getName();
584 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000585 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000586 } else {
587 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000588 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000589 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
590 return ExprError();
591 }
592 }
593 Lookup.suppressDiagnostics();
594
595 // OpenMP [2.9.2, Syntax, C/C++]
596 // Variables must be file-scope, namespace-scope, or static block-scope.
597 if (!VD->hasGlobalStorage()) {
598 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000599 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
600 bool IsDecl =
601 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000602 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000603 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
604 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000605 return ExprError();
606 }
607
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000608 VarDecl *CanonicalVD = VD->getCanonicalDecl();
609 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000610 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
611 // A threadprivate directive for file-scope variables must appear outside
612 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000613 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
614 !getCurLexicalContext()->isTranslationUnit()) {
615 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000616 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
617 bool IsDecl =
618 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
619 Diag(VD->getLocation(),
620 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
621 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000622 return ExprError();
623 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000624 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
625 // A threadprivate directive for static class member variables must appear
626 // in the class definition, in the same scope in which the member
627 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000628 if (CanonicalVD->isStaticDataMember() &&
629 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
630 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000631 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
632 bool IsDecl =
633 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
634 Diag(VD->getLocation(),
635 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
636 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000637 return ExprError();
638 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000639 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
640 // A threadprivate directive for namespace-scope variables must appear
641 // outside any definition or declaration other than the namespace
642 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000643 if (CanonicalVD->getDeclContext()->isNamespace() &&
644 (!getCurLexicalContext()->isFileContext() ||
645 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
646 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000647 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
648 bool IsDecl =
649 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
650 Diag(VD->getLocation(),
651 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
652 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000653 return ExprError();
654 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000655 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
656 // A threadprivate directive for static block-scope variables must appear
657 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000658 if (CanonicalVD->isStaticLocal() && CurScope &&
659 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000660 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000661 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
662 bool IsDecl =
663 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
664 Diag(VD->getLocation(),
665 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
666 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000667 return ExprError();
668 }
669
670 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
671 // A threadprivate directive must lexically precede all references to any
672 // of the variables in its list.
673 if (VD->isUsed()) {
674 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000675 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000676 return ExprError();
677 }
678
679 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000680 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000681 return DE;
682}
683
Alexey Bataeved09d242014-05-28 05:53:51 +0000684Sema::DeclGroupPtrTy
685Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
686 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000687 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000688 CurContext->addDecl(D);
689 return DeclGroupPtrTy::make(DeclGroupRef(D));
690 }
691 return DeclGroupPtrTy();
692}
693
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000694namespace {
695class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
696 Sema &SemaRef;
697
698public:
699 bool VisitDeclRefExpr(const DeclRefExpr *E) {
700 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
701 if (VD->hasLocalStorage()) {
702 SemaRef.Diag(E->getLocStart(),
703 diag::err_omp_local_var_in_threadprivate_init)
704 << E->getSourceRange();
705 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
706 << VD << VD->getSourceRange();
707 return true;
708 }
709 }
710 return false;
711 }
712 bool VisitStmt(const Stmt *S) {
713 for (auto Child : S->children()) {
714 if (Child && Visit(Child))
715 return true;
716 }
717 return false;
718 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000719 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000720};
721} // namespace
722
Alexey Bataeved09d242014-05-28 05:53:51 +0000723OMPThreadPrivateDecl *
724Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000725 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 for (auto &RefExpr : VarList) {
727 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000728 VarDecl *VD = cast<VarDecl>(DE->getDecl());
729 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000730
731 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
732 // A threadprivate variable must not have an incomplete type.
733 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000734 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000735 continue;
736 }
737
738 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
739 // A threadprivate variable must not have a reference type.
740 if (VD->getType()->isReferenceType()) {
741 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000742 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
743 bool IsDecl =
744 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
745 Diag(VD->getLocation(),
746 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
747 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000748 continue;
749 }
750
Richard Smithfd3834f2013-04-13 02:43:54 +0000751 // Check if this is a TLS variable.
752 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000753 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000754 bool IsDecl =
755 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
756 Diag(VD->getLocation(),
757 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
758 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000759 continue;
760 }
761
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000762 // Check if initial value of threadprivate variable reference variable with
763 // local storage (it is not supported by runtime).
764 if (auto Init = VD->getAnyInitializer()) {
765 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000766 if (Checker.Visit(Init))
767 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000768 }
769
Alexey Bataeved09d242014-05-28 05:53:51 +0000770 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000771 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000772 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000773 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000774 if (!Vars.empty()) {
775 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
776 Vars);
777 D->setAccess(AS_public);
778 }
779 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000780}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000781
Alexey Bataev7ff55242014-06-19 09:13:45 +0000782static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
783 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
784 bool IsLoopIterVar = false) {
785 if (DVar.RefExpr) {
786 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
787 << getOpenMPClauseName(DVar.CKind);
788 return;
789 }
790 enum {
791 PDSA_StaticMemberShared,
792 PDSA_StaticLocalVarShared,
793 PDSA_LoopIterVarPrivate,
794 PDSA_LoopIterVarLinear,
795 PDSA_LoopIterVarLastprivate,
796 PDSA_ConstVarShared,
797 PDSA_GlobalVarShared,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000798 PDSA_LocalVarPrivate,
799 PDSA_Implicit
800 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000801 bool ReportHint = false;
802 if (IsLoopIterVar) {
803 if (DVar.CKind == OMPC_private)
804 Reason = PDSA_LoopIterVarPrivate;
805 else if (DVar.CKind == OMPC_lastprivate)
806 Reason = PDSA_LoopIterVarLastprivate;
807 else
808 Reason = PDSA_LoopIterVarLinear;
809 } else if (VD->isStaticLocal())
810 Reason = PDSA_StaticLocalVarShared;
811 else if (VD->isStaticDataMember())
812 Reason = PDSA_StaticMemberShared;
813 else if (VD->isFileVarDecl())
814 Reason = PDSA_GlobalVarShared;
815 else if (VD->getType().isConstant(SemaRef.getASTContext()))
816 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000817 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000818 ReportHint = true;
819 Reason = PDSA_LocalVarPrivate;
820 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000821 if (Reason != PDSA_Implicit) {
822 SemaRef.Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
823 << Reason << ReportHint
824 << getOpenMPDirectiveName(Stack->getCurrentDirective());
825 } else if (DVar.ImplicitDSALoc.isValid()) {
826 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
827 << getOpenMPClauseName(DVar.CKind);
828 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000829}
830
Alexey Bataev758e55e2013-09-06 18:03:48 +0000831namespace {
832class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
833 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000834 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000835 bool ErrorFound;
836 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000837 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000838 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000839
Alexey Bataev758e55e2013-09-06 18:03:48 +0000840public:
841 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000842 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000843 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000844 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
845 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000846
847 SourceLocation ELoc = E->getExprLoc();
848
849 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
850 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
851 if (DVar.CKind != OMPC_unknown) {
852 if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000853 !Stack->isThreadPrivate(VD) && !DVar.RefExpr)
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000854 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000855 return;
856 }
857 // The default(none) clause requires that each variable that is referenced
858 // in the construct, and does not have a predetermined data-sharing
859 // attribute, must have its data-sharing attribute explicitly determined
860 // by being listed in a data-sharing attribute clause.
861 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000862 (isOpenMPParallelDirective(DKind) || DKind == OMPD_task) &&
863 VarsWithInheritedDSA.count(VD) == 0) {
864 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000865 return;
866 }
867
868 // OpenMP [2.9.3.6, Restrictions, p.2]
869 // A list item that appears in a reduction clause of the innermost
870 // enclosing worksharing or parallel construct may not be accessed in an
871 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000872 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev23b69422014-06-18 07:08:49 +0000873 MatchesAlways());
Alexey Bataevc5e02582014-06-16 07:08:35 +0000874 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
875 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000876 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
877 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000878 return;
879 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000880
881 // Define implicit data-sharing attributes for task.
882 DVar = Stack->getImplicitDSA(VD);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000883 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
884 ImplicitFirstprivate.push_back(DVar.RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000885 }
886 }
887 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000888 for (auto C : S->clauses())
889 if (C)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000890 for (StmtRange R = C->children(); R; ++R)
891 if (Stmt *Child = *R)
892 Visit(Child);
893 }
894 void VisitStmt(Stmt *S) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000895 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I != E;
896 ++I)
Alexey Bataev758e55e2013-09-06 18:03:48 +0000897 if (Stmt *Child = *I)
898 if (!isa<OMPExecutableDirective>(Child))
899 Visit(Child);
Alexey Bataeved09d242014-05-28 05:53:51 +0000900 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000901
902 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000903 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000904 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
905 return VarsWithInheritedDSA;
906 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000907
Alexey Bataev7ff55242014-06-19 09:13:45 +0000908 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
909 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000910};
Alexey Bataeved09d242014-05-28 05:53:51 +0000911} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000912
Alexey Bataevbae9a792014-06-27 10:37:06 +0000913void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +0000914 switch (DKind) {
915 case OMPD_parallel: {
916 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
917 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000918 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000919 std::make_pair(".global_tid.", KmpInt32PtrTy),
920 std::make_pair(".bound_tid.", KmpInt32PtrTy),
921 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000922 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000923 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
924 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000925 break;
926 }
927 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000928 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000929 std::make_pair(StringRef(), QualType()) // __context with shared vars
930 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000931 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
932 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000933 break;
934 }
935 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000936 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000937 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +0000938 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000939 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
940 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +0000941 break;
942 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000943 case OMPD_sections: {
944 Sema::CapturedParamNameType Params[] = {
945 std::make_pair(StringRef(), QualType()) // __context with shared vars
946 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000947 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
948 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000949 break;
950 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000951 case OMPD_section: {
952 Sema::CapturedParamNameType Params[] = {
953 std::make_pair(StringRef(), QualType()) // __context with shared vars
954 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000955 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
956 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000957 break;
958 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000959 case OMPD_single: {
960 Sema::CapturedParamNameType Params[] = {
961 std::make_pair(StringRef(), QualType()) // __context with shared vars
962 };
Alexey Bataevbae9a792014-06-27 10:37:06 +0000963 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
964 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000965 break;
966 }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000967 case OMPD_parallel_for: {
968 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
969 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
970 Sema::CapturedParamNameType Params[] = {
971 std::make_pair(".global_tid.", KmpInt32PtrTy),
972 std::make_pair(".bound_tid.", KmpInt32PtrTy),
973 std::make_pair(StringRef(), QualType()) // __context with shared vars
974 };
975 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
976 Params);
977 break;
978 }
Alexey Bataev9959db52014-05-06 10:08:46 +0000979 case OMPD_threadprivate:
980 case OMPD_task:
981 llvm_unreachable("OpenMP Directive is not allowed");
982 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +0000983 llvm_unreachable("Unknown OpenMP directive");
984 }
985}
986
Alexey Bataev549210e2014-06-24 04:39:47 +0000987bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
988 OpenMPDirectiveKind CurrentRegion,
989 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +0000990 // Allowed nesting of constructs
991 // +------------------+-----------------+------------------------------------+
992 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
993 // +------------------+-----------------+------------------------------------+
994 // | parallel | parallel | * |
995 // | parallel | for | * |
996 // | parallel | simd | * |
997 // | parallel | sections | * |
998 // | parallel | section | + |
999 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001000 // | parallel | parallel for | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001001 // +------------------+-----------------+------------------------------------+
1002 // | for | parallel | * |
1003 // | for | for | + |
1004 // | for | simd | * |
1005 // | for | sections | + |
1006 // | for | section | + |
1007 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001008 // | for | parallel for | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001009 // +------------------+-----------------+------------------------------------+
1010 // | simd | parallel | |
1011 // | simd | for | |
1012 // | simd | simd | |
1013 // | simd | sections | |
1014 // | simd | section | |
1015 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001016 // | simd | parallel for | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001017 // +------------------+-----------------+------------------------------------+
1018 // | sections | parallel | * |
1019 // | sections | for | + |
1020 // | sections | simd | * |
1021 // | sections | sections | + |
1022 // | sections | section | * |
1023 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001024 // | sections | parallel for | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001025 // +------------------+-----------------+------------------------------------+
1026 // | section | parallel | * |
1027 // | section | for | + |
1028 // | section | simd | * |
1029 // | section | sections | + |
1030 // | section | section | + |
1031 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001032 // | section | parallel for | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001033 // +------------------+-----------------+------------------------------------+
1034 // | single | parallel | * |
1035 // | single | for | + |
1036 // | single | simd | * |
1037 // | single | sections | + |
1038 // | single | section | + |
1039 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001040 // | single | parallel for | * |
1041 // +------------------+-----------------+------------------------------------+
1042 // | parallel for | parallel | * |
1043 // | parallel for | for | + |
1044 // | parallel for | simd | * |
1045 // | parallel for | sections | + |
1046 // | parallel for | section | + |
1047 // | parallel for | single | + |
1048 // | parallel for | parallel for | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001049 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001050 if (Stack->getCurScope()) {
1051 auto ParentRegion = Stack->getParentDirective();
1052 bool NestingProhibited = false;
1053 bool CloseNesting = true;
1054 bool ShouldBeInParallelRegion = false;
1055 if (isOpenMPSimdDirective(ParentRegion)) {
1056 // OpenMP [2.16, Nesting of Regions]
1057 // OpenMP constructs may not be nested inside a simd region.
1058 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1059 return true;
1060 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001061 if (CurrentRegion == OMPD_section) {
1062 // OpenMP [2.7.2, sections Construct, Restrictions]
1063 // Orphaned section directives are prohibited. That is, the section
1064 // directives must appear within the sections construct and must not be
1065 // encountered elsewhere in the sections region.
1066 if (ParentRegion != OMPD_sections) {
1067 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1068 << (ParentRegion != OMPD_unknown)
1069 << getOpenMPDirectiveName(ParentRegion);
1070 return true;
1071 }
1072 return false;
1073 }
Alexey Bataev549210e2014-06-24 04:39:47 +00001074 if (isOpenMPWorksharingDirective(CurrentRegion) &&
1075 !isOpenMPParallelDirective(CurrentRegion) &&
1076 !isOpenMPSimdDirective(CurrentRegion)) {
1077 // OpenMP [2.16, Nesting of Regions]
1078 // A worksharing region may not be closely nested inside a worksharing,
1079 // explicit task, critical, ordered, atomic, or master region.
1080 // TODO
1081 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) &&
1082 !isOpenMPSimdDirective(ParentRegion);
1083 ShouldBeInParallelRegion = true;
1084 }
1085 if (NestingProhibited) {
1086 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev41b97322014-07-02 03:04:53 +00001087 << CloseNesting << getOpenMPDirectiveName(ParentRegion)
1088 << ShouldBeInParallelRegion << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001089 return true;
1090 }
1091 }
1092 return false;
1093}
1094
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001095StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
1096 ArrayRef<OMPClause *> Clauses,
1097 Stmt *AStmt,
1098 SourceLocation StartLoc,
1099 SourceLocation EndLoc) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001100 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1101
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001102 StmtResult Res = StmtError();
Alexey Bataev549210e2014-06-24 04:39:47 +00001103 if (CheckNestingOfRegions(*this, DSAStack, Kind, StartLoc))
1104 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001105
1106 // Check default data sharing attributes for referenced variables.
1107 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1108 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1109 if (DSAChecker.isErrorFound())
1110 return StmtError();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001111 // Generate list of implicitly defined firstprivate variables.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001112 auto &VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001113 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
1114 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
1115
1116 bool ErrorFound = false;
1117 if (!DSAChecker.getImplicitFirstprivate().empty()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001118 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1119 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1120 SourceLocation(), SourceLocation())) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001121 ClausesWithImplicit.push_back(Implicit);
1122 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataeved09d242014-05-28 05:53:51 +00001123 DSAChecker.getImplicitFirstprivate().size();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001124 } else
1125 ErrorFound = true;
1126 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001127
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001128 switch (Kind) {
1129 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001130 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1131 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001132 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001133 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001134 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1135 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001136 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001137 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001138 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1139 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001140 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001141 case OMPD_sections:
1142 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1143 EndLoc);
1144 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001145 case OMPD_section:
1146 assert(ClausesWithImplicit.empty() &&
1147 "No clauses is allowed for 'omp section' directive");
1148 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1149 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001150 case OMPD_single:
1151 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1152 EndLoc);
1153 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001154 case OMPD_parallel_for:
1155 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1156 EndLoc, VarsWithInheritedDSA);
1157 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001158 case OMPD_threadprivate:
1159 case OMPD_task:
1160 llvm_unreachable("OpenMP Directive is not allowed");
1161 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001162 llvm_unreachable("Unknown OpenMP directive");
1163 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001164
Alexey Bataev4acb8592014-07-07 13:01:15 +00001165 for (auto P : VarsWithInheritedDSA) {
1166 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1167 << P.first << P.second->getSourceRange();
1168 }
1169 if (!VarsWithInheritedDSA.empty())
1170 return StmtError();
1171
Alexey Bataeved09d242014-05-28 05:53:51 +00001172 if (ErrorFound)
1173 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001174 return Res;
1175}
1176
1177StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1178 Stmt *AStmt,
1179 SourceLocation StartLoc,
1180 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001181 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1182 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1183 // 1.2.2 OpenMP Language Terminology
1184 // Structured block - An executable statement with a single entry at the
1185 // top and a single exit at the bottom.
1186 // The point of exit cannot be a branch out of the structured block.
1187 // longjmp() and throw() must not violate the entry/exit criteria.
1188 CS->getCapturedDecl()->setNothrow();
1189
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001190 getCurFunction()->setHasBranchProtectedScope();
1191
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001192 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1193 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001194}
1195
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001196namespace {
1197/// \brief Helper class for checking canonical form of the OpenMP loops and
1198/// extracting iteration space of each loop in the loop nest, that will be used
1199/// for IR generation.
1200class OpenMPIterationSpaceChecker {
1201 /// \brief Reference to Sema.
1202 Sema &SemaRef;
1203 /// \brief A location for diagnostics (when there is no some better location).
1204 SourceLocation DefaultLoc;
1205 /// \brief A location for diagnostics (when increment is not compatible).
1206 SourceLocation ConditionLoc;
1207 /// \brief A source location for referring to condition later.
1208 SourceRange ConditionSrcRange;
1209 /// \brief Loop variable.
1210 VarDecl *Var;
1211 /// \brief Lower bound (initializer for the var).
1212 Expr *LB;
1213 /// \brief Upper bound.
1214 Expr *UB;
1215 /// \brief Loop step (increment).
1216 Expr *Step;
1217 /// \brief This flag is true when condition is one of:
1218 /// Var < UB
1219 /// Var <= UB
1220 /// UB > Var
1221 /// UB >= Var
1222 bool TestIsLessOp;
1223 /// \brief This flag is true when condition is strict ( < or > ).
1224 bool TestIsStrictOp;
1225 /// \brief This flag is true when step is subtracted on each iteration.
1226 bool SubtractStep;
1227
1228public:
1229 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1230 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
1231 ConditionSrcRange(SourceRange()), Var(nullptr), LB(nullptr),
1232 UB(nullptr), Step(nullptr), TestIsLessOp(false), TestIsStrictOp(false),
1233 SubtractStep(false) {}
1234 /// \brief Check init-expr for canonical loop form and save loop counter
1235 /// variable - #Var and its initialization value - #LB.
1236 bool CheckInit(Stmt *S);
1237 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1238 /// for less/greater and for strict/non-strict comparison.
1239 bool CheckCond(Expr *S);
1240 /// \brief Check incr-expr for canonical loop form and return true if it
1241 /// does not conform, otherwise save loop step (#Step).
1242 bool CheckInc(Expr *S);
1243 /// \brief Return the loop counter variable.
1244 VarDecl *GetLoopVar() const { return Var; }
1245 /// \brief Return true if any expression is dependent.
1246 bool Dependent() const;
1247
1248private:
1249 /// \brief Check the right-hand side of an assignment in the increment
1250 /// expression.
1251 bool CheckIncRHS(Expr *RHS);
1252 /// \brief Helper to set loop counter variable and its initializer.
1253 bool SetVarAndLB(VarDecl *NewVar, Expr *NewLB);
1254 /// \brief Helper to set upper bound.
1255 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1256 const SourceLocation &SL);
1257 /// \brief Helper to set loop increment.
1258 bool SetStep(Expr *NewStep, bool Subtract);
1259};
1260
1261bool OpenMPIterationSpaceChecker::Dependent() const {
1262 if (!Var) {
1263 assert(!LB && !UB && !Step);
1264 return false;
1265 }
1266 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1267 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1268}
1269
1270bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar, Expr *NewLB) {
1271 // State consistency checking to ensure correct usage.
1272 assert(Var == nullptr && LB == nullptr && UB == nullptr && Step == nullptr &&
1273 !TestIsLessOp && !TestIsStrictOp);
1274 if (!NewVar || !NewLB)
1275 return true;
1276 Var = NewVar;
1277 LB = NewLB;
1278 return false;
1279}
1280
1281bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1282 const SourceRange &SR,
1283 const SourceLocation &SL) {
1284 // State consistency checking to ensure correct usage.
1285 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1286 !TestIsLessOp && !TestIsStrictOp);
1287 if (!NewUB)
1288 return true;
1289 UB = NewUB;
1290 TestIsLessOp = LessOp;
1291 TestIsStrictOp = StrictOp;
1292 ConditionSrcRange = SR;
1293 ConditionLoc = SL;
1294 return false;
1295}
1296
1297bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1298 // State consistency checking to ensure correct usage.
1299 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1300 if (!NewStep)
1301 return true;
1302 if (!NewStep->isValueDependent()) {
1303 // Check that the step is integer expression.
1304 SourceLocation StepLoc = NewStep->getLocStart();
1305 ExprResult Val =
1306 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1307 if (Val.isInvalid())
1308 return true;
1309 NewStep = Val.get();
1310
1311 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1312 // If test-expr is of form var relational-op b and relational-op is < or
1313 // <= then incr-expr must cause var to increase on each iteration of the
1314 // loop. If test-expr is of form var relational-op b and relational-op is
1315 // > or >= then incr-expr must cause var to decrease on each iteration of
1316 // the loop.
1317 // If test-expr is of form b relational-op var and relational-op is < or
1318 // <= then incr-expr must cause var to decrease on each iteration of the
1319 // loop. If test-expr is of form b relational-op var and relational-op is
1320 // > or >= then incr-expr must cause var to increase on each iteration of
1321 // the loop.
1322 llvm::APSInt Result;
1323 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
1324 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
1325 bool IsConstNeg =
1326 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
1327 bool IsConstZero = IsConstant && !Result.getBoolValue();
1328 if (UB && (IsConstZero ||
1329 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
1330 : (!IsConstNeg || (IsUnsigned && !Subtract))))) {
1331 SemaRef.Diag(NewStep->getExprLoc(),
1332 diag::err_omp_loop_incr_not_compatible)
1333 << Var << TestIsLessOp << NewStep->getSourceRange();
1334 SemaRef.Diag(ConditionLoc,
1335 diag::note_omp_loop_cond_requres_compatible_incr)
1336 << TestIsLessOp << ConditionSrcRange;
1337 return true;
1338 }
1339 }
1340
1341 Step = NewStep;
1342 SubtractStep = Subtract;
1343 return false;
1344}
1345
1346bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
1347 // Check init-expr for canonical loop form and save loop counter
1348 // variable - #Var and its initialization value - #LB.
1349 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
1350 // var = lb
1351 // integer-type var = lb
1352 // random-access-iterator-type var = lb
1353 // pointer-type var = lb
1354 //
1355 if (!S) {
1356 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
1357 return true;
1358 }
1359 if (Expr *E = dyn_cast<Expr>(S))
1360 S = E->IgnoreParens();
1361 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1362 if (BO->getOpcode() == BO_Assign)
1363 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
1364 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), BO->getLHS());
1365 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
1366 if (DS->isSingleDecl()) {
1367 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
1368 if (Var->hasInit()) {
1369 // Accept non-canonical init form here but emit ext. warning.
1370 if (Var->getInitStyle() != VarDecl::CInit)
1371 SemaRef.Diag(S->getLocStart(),
1372 diag::ext_omp_loop_not_canonical_init)
1373 << S->getSourceRange();
1374 return SetVarAndLB(Var, Var->getInit());
1375 }
1376 }
1377 }
1378 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
1379 if (CE->getOperator() == OO_Equal)
1380 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
1381 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), CE->getArg(1));
1382
1383 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
1384 << S->getSourceRange();
1385 return true;
1386}
1387
Alexey Bataev23b69422014-06-18 07:08:49 +00001388/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001389/// variable (which may be the loop variable) if possible.
1390static const VarDecl *GetInitVarDecl(const Expr *E) {
1391 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00001392 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001393 E = E->IgnoreParenImpCasts();
1394 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
1395 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
1396 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
1397 CE->getArg(0) != nullptr)
1398 E = CE->getArg(0)->IgnoreParenImpCasts();
1399 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
1400 if (!DRE)
1401 return nullptr;
1402 return dyn_cast<VarDecl>(DRE->getDecl());
1403}
1404
1405bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
1406 // Check test-expr for canonical form, save upper-bound UB, flags for
1407 // less/greater and for strict/non-strict comparison.
1408 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1409 // var relational-op b
1410 // b relational-op var
1411 //
1412 if (!S) {
1413 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
1414 return true;
1415 }
1416 S = S->IgnoreParenImpCasts();
1417 SourceLocation CondLoc = S->getLocStart();
1418 if (auto BO = dyn_cast<BinaryOperator>(S)) {
1419 if (BO->isRelationalOp()) {
1420 if (GetInitVarDecl(BO->getLHS()) == Var)
1421 return SetUB(BO->getRHS(),
1422 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
1423 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1424 BO->getSourceRange(), BO->getOperatorLoc());
1425 if (GetInitVarDecl(BO->getRHS()) == Var)
1426 return SetUB(BO->getLHS(),
1427 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
1428 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
1429 BO->getSourceRange(), BO->getOperatorLoc());
1430 }
1431 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1432 if (CE->getNumArgs() == 2) {
1433 auto Op = CE->getOperator();
1434 switch (Op) {
1435 case OO_Greater:
1436 case OO_GreaterEqual:
1437 case OO_Less:
1438 case OO_LessEqual:
1439 if (GetInitVarDecl(CE->getArg(0)) == Var)
1440 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
1441 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1442 CE->getOperatorLoc());
1443 if (GetInitVarDecl(CE->getArg(1)) == Var)
1444 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
1445 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
1446 CE->getOperatorLoc());
1447 break;
1448 default:
1449 break;
1450 }
1451 }
1452 }
1453 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
1454 << S->getSourceRange() << Var;
1455 return true;
1456}
1457
1458bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
1459 // RHS of canonical loop form increment can be:
1460 // var + incr
1461 // incr + var
1462 // var - incr
1463 //
1464 RHS = RHS->IgnoreParenImpCasts();
1465 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
1466 if (BO->isAdditiveOp()) {
1467 bool IsAdd = BO->getOpcode() == BO_Add;
1468 if (GetInitVarDecl(BO->getLHS()) == Var)
1469 return SetStep(BO->getRHS(), !IsAdd);
1470 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
1471 return SetStep(BO->getLHS(), false);
1472 }
1473 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
1474 bool IsAdd = CE->getOperator() == OO_Plus;
1475 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
1476 if (GetInitVarDecl(CE->getArg(0)) == Var)
1477 return SetStep(CE->getArg(1), !IsAdd);
1478 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
1479 return SetStep(CE->getArg(0), false);
1480 }
1481 }
1482 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1483 << RHS->getSourceRange() << Var;
1484 return true;
1485}
1486
1487bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
1488 // Check incr-expr for canonical loop form and return true if it
1489 // does not conform.
1490 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
1491 // ++var
1492 // var++
1493 // --var
1494 // var--
1495 // var += incr
1496 // var -= incr
1497 // var = var + incr
1498 // var = incr + var
1499 // var = var - incr
1500 //
1501 if (!S) {
1502 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
1503 return true;
1504 }
1505 S = S->IgnoreParens();
1506 if (auto UO = dyn_cast<UnaryOperator>(S)) {
1507 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
1508 return SetStep(
1509 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
1510 (UO->isDecrementOp() ? -1 : 1)).get(),
1511 false);
1512 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
1513 switch (BO->getOpcode()) {
1514 case BO_AddAssign:
1515 case BO_SubAssign:
1516 if (GetInitVarDecl(BO->getLHS()) == Var)
1517 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
1518 break;
1519 case BO_Assign:
1520 if (GetInitVarDecl(BO->getLHS()) == Var)
1521 return CheckIncRHS(BO->getRHS());
1522 break;
1523 default:
1524 break;
1525 }
1526 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
1527 switch (CE->getOperator()) {
1528 case OO_PlusPlus:
1529 case OO_MinusMinus:
1530 if (GetInitVarDecl(CE->getArg(0)) == Var)
1531 return SetStep(
1532 SemaRef.ActOnIntegerConstant(
1533 CE->getLocStart(),
1534 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
1535 false);
1536 break;
1537 case OO_PlusEqual:
1538 case OO_MinusEqual:
1539 if (GetInitVarDecl(CE->getArg(0)) == Var)
1540 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
1541 break;
1542 case OO_Equal:
1543 if (GetInitVarDecl(CE->getArg(0)) == Var)
1544 return CheckIncRHS(CE->getArg(1));
1545 break;
1546 default:
1547 break;
1548 }
1549 }
1550 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
1551 << S->getSourceRange() << Var;
1552 return true;
1553}
Alexey Bataev23b69422014-06-18 07:08:49 +00001554} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001555
1556/// \brief Called on a for stmt to check and extract its iteration space
1557/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00001558static bool CheckOpenMPIterationSpace(
1559 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
1560 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
1561 Expr *NestedLoopCountExpr,
1562 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001563 // OpenMP [2.6, Canonical Loop Form]
1564 // for (init-expr; test-expr; incr-expr) structured-block
1565 auto For = dyn_cast_or_null<ForStmt>(S);
1566 if (!For) {
1567 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001568 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
1569 << NestedLoopCount << (CurrentNestedLoopCount > 0)
1570 << CurrentNestedLoopCount;
1571 if (NestedLoopCount > 1)
1572 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
1573 diag::note_omp_collapse_expr)
1574 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001575 return true;
1576 }
1577 assert(For->getBody());
1578
1579 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
1580
1581 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001582 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001583 if (ISC.CheckInit(Init)) {
1584 return true;
1585 }
1586
1587 bool HasErrors = false;
1588
1589 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001590 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001591
1592 // OpenMP [2.6, Canonical Loop Form]
1593 // Var is one of the following:
1594 // A variable of signed or unsigned integer type.
1595 // For C++, a variable of a random access iterator type.
1596 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001597 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001598 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
1599 !VarType->isPointerType() &&
1600 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
1601 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
1602 << SemaRef.getLangOpts().CPlusPlus;
1603 HasErrors = true;
1604 }
1605
Alexey Bataev4acb8592014-07-07 13:01:15 +00001606 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
1607 // Construct
1608 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1609 // parallel for construct is (are) private.
1610 // The loop iteration variable in the associated for-loop of a simd construct
1611 // with just one associated for-loop is linear with a constant-linear-step
1612 // that is the increment of the associated for-loop.
1613 // Exclude loop var from the list of variables with implicitly defined data
1614 // sharing attributes.
1615 while (VarsWithImplicitDSA.count(Var) > 0)
1616 VarsWithImplicitDSA.erase(Var);
1617
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001618 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
1619 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00001620 // The loop iteration variable in the associated for-loop of a simd construct
1621 // with just one associated for-loop may be listed in a linear clause with a
1622 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001623 // The loop iteration variable(s) in the associated for-loop(s) of a for or
1624 // parallel for construct may be listed in a private or lastprivate clause.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001625 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00001626 auto PredeterminedCKind =
1627 isOpenMPSimdDirective(DKind)
1628 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
1629 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001630 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001631 DVar.CKind != PredeterminedCKind) ||
Alexey Bataevf29276e2014-06-18 04:14:57 +00001632 (isOpenMPWorksharingDirective(DKind) && DVar.CKind != OMPC_unknown &&
1633 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00001634 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001635 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00001636 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
1637 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001638 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001639 HasErrors = true;
1640 } else {
Alexey Bataev4acb8592014-07-07 13:01:15 +00001641 // Make the loop iteration variable private (for worksharing constructs),
1642 // linear (for simd directives with the only one associated loop) or
1643 // lastprivate (for simd directives with several collapsed loops).
1644 DSA.addDSA(Var, nullptr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001645 }
1646
Alexey Bataev7ff55242014-06-19 09:13:45 +00001647 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00001648
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001649 // Check test-expr.
1650 HasErrors |= ISC.CheckCond(For->getCond());
1651
1652 // Check incr-expr.
1653 HasErrors |= ISC.CheckInc(For->getInc());
1654
1655 if (ISC.Dependent())
1656 return HasErrors;
1657
1658 // FIXME: Build loop's iteration space representation.
1659 return HasErrors;
1660}
1661
1662/// \brief A helper routine to skip no-op (attributed, compound) stmts get the
1663/// next nested for loop. If \a IgnoreCaptured is true, it skips captured stmt
1664/// to get the first for loop.
1665static Stmt *IgnoreContainerStmts(Stmt *S, bool IgnoreCaptured) {
1666 if (IgnoreCaptured)
1667 if (auto CapS = dyn_cast_or_null<CapturedStmt>(S))
1668 S = CapS->getCapturedStmt();
1669 // OpenMP [2.8.1, simd construct, Restrictions]
1670 // All loops associated with the construct must be perfectly nested; that is,
1671 // there must be no intervening code nor any OpenMP directive between any two
1672 // loops.
1673 while (true) {
1674 if (auto AS = dyn_cast_or_null<AttributedStmt>(S))
1675 S = AS->getSubStmt();
1676 else if (auto CS = dyn_cast_or_null<CompoundStmt>(S)) {
1677 if (CS->size() != 1)
1678 break;
1679 S = CS->body_back();
1680 } else
1681 break;
1682 }
1683 return S;
1684}
1685
1686/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001687/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
1688/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001689static unsigned
1690CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
1691 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
1692 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001693 unsigned NestedLoopCount = 1;
1694 if (NestedLoopCountExpr) {
1695 // Found 'collapse' clause - calculate collapse number.
1696 llvm::APSInt Result;
1697 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
1698 NestedLoopCount = Result.getLimitedValue();
1699 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001700 // This is helper routine for loop directives (e.g., 'for', 'simd',
1701 // 'for simd', etc.).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001702 Stmt *CurStmt = IgnoreContainerStmts(AStmt, true);
1703 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001704 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00001705 NestedLoopCount, NestedLoopCountExpr,
1706 VarsWithImplicitDSA))
Alexey Bataevabfc0692014-06-25 06:52:00 +00001707 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001708 // Move on to the next nested for loop, or to the loop body.
1709 CurStmt = IgnoreContainerStmts(cast<ForStmt>(CurStmt)->getBody(), false);
1710 }
1711
1712 // FIXME: Build resulting iteration space for IR generation (collapsing
1713 // iteration spaces when loop count > 1 ('collapse' clause)).
Alexey Bataevabfc0692014-06-25 06:52:00 +00001714 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001715}
1716
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001717static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001718 auto CollapseFilter = [](const OMPClause *C) -> bool {
1719 return C->getClauseKind() == OMPC_collapse;
1720 };
1721 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
1722 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00001723 if (I)
1724 return cast<OMPCollapseClause>(*I)->getNumForLoops();
1725 return nullptr;
1726}
1727
Alexey Bataev4acb8592014-07-07 13:01:15 +00001728StmtResult Sema::ActOnOpenMPSimdDirective(
1729 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1730 SourceLocation EndLoc,
1731 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001732 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001733 unsigned NestedLoopCount =
1734 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
1735 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001736 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001737 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001738
1739 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001740 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1741 Clauses, AStmt);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001742}
1743
Alexey Bataev4acb8592014-07-07 13:01:15 +00001744StmtResult Sema::ActOnOpenMPForDirective(
1745 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1746 SourceLocation EndLoc,
1747 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001748 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00001749 unsigned NestedLoopCount =
1750 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
1751 *DSAStack, VarsWithImplicitDSA);
Alexey Bataevabfc0692014-06-25 06:52:00 +00001752 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00001753 return StmtError();
1754
1755 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataevabfc0692014-06-25 06:52:00 +00001756 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
1757 Clauses, AStmt);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001758}
1759
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001760StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
1761 Stmt *AStmt,
1762 SourceLocation StartLoc,
1763 SourceLocation EndLoc) {
1764 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1765 auto BaseStmt = AStmt;
1766 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
1767 BaseStmt = CS->getCapturedStmt();
1768 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
1769 auto S = C->children();
1770 if (!S)
1771 return StmtError();
1772 // All associated statements must be '#pragma omp section' except for
1773 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001774 for (++S; S; ++S) {
1775 auto SectionStmt = *S;
1776 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
1777 if (SectionStmt)
1778 Diag(SectionStmt->getLocStart(),
1779 diag::err_omp_sections_substmt_not_section);
1780 return StmtError();
1781 }
1782 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001783 } else {
1784 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
1785 return StmtError();
1786 }
1787
1788 getCurFunction()->setHasBranchProtectedScope();
1789
1790 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
1791 AStmt);
1792}
1793
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001794StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
1795 SourceLocation StartLoc,
1796 SourceLocation EndLoc) {
1797 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1798
1799 getCurFunction()->setHasBranchProtectedScope();
1800
1801 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
1802}
1803
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001804StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
1805 Stmt *AStmt,
1806 SourceLocation StartLoc,
1807 SourceLocation EndLoc) {
1808 getCurFunction()->setHasBranchProtectedScope();
1809 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
1810}
1811
Alexey Bataev4acb8592014-07-07 13:01:15 +00001812StmtResult Sema::ActOnOpenMPParallelForDirective(
1813 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
1814 SourceLocation EndLoc,
1815 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
1816 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1817 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1818 // 1.2.2 OpenMP Language Terminology
1819 // Structured block - An executable statement with a single entry at the
1820 // top and a single exit at the bottom.
1821 // The point of exit cannot be a branch out of the structured block.
1822 // longjmp() and throw() must not violate the entry/exit criteria.
1823 CS->getCapturedDecl()->setNothrow();
1824
1825 // In presence of clause 'collapse', it will define the nested loops number.
1826 unsigned NestedLoopCount =
1827 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
1828 *this, *DSAStack, VarsWithImplicitDSA);
1829 if (NestedLoopCount == 0)
1830 return StmtError();
1831
1832 getCurFunction()->setHasBranchProtectedScope();
1833 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
1834 NestedLoopCount, Clauses, AStmt);
1835}
1836
Alexey Bataeved09d242014-05-28 05:53:51 +00001837OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001838 SourceLocation StartLoc,
1839 SourceLocation LParenLoc,
1840 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001841 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001842 switch (Kind) {
1843 case OMPC_if:
1844 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
1845 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00001846 case OMPC_num_threads:
1847 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
1848 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001849 case OMPC_safelen:
1850 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
1851 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00001852 case OMPC_collapse:
1853 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
1854 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001855 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00001856 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00001857 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001858 case OMPC_private:
1859 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00001860 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001861 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00001862 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00001863 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00001864 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00001865 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00001866 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00001867 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00001868 case OMPC_nowait:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001869 case OMPC_threadprivate:
1870 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001871 llvm_unreachable("Clause is not allowed.");
1872 }
1873 return Res;
1874}
1875
Alexey Bataeved09d242014-05-28 05:53:51 +00001876OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001877 SourceLocation LParenLoc,
1878 SourceLocation EndLoc) {
1879 Expr *ValExpr = Condition;
1880 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
1881 !Condition->isInstantiationDependent() &&
1882 !Condition->containsUnexpandedParameterPack()) {
1883 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001884 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001885 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001886 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001887
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001888 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00001889 }
1890
1891 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
1892}
1893
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001894ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
1895 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001896 if (!Op)
1897 return ExprError();
1898
1899 class IntConvertDiagnoser : public ICEConvertDiagnoser {
1900 public:
1901 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00001902 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001903 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
1904 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001905 return S.Diag(Loc, diag::err_omp_not_integral) << T;
1906 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001907 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
1908 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001909 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
1910 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001911 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
1912 QualType T,
1913 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001914 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
1915 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001916 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
1917 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001918 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001919 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001920 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001921 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
1922 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001923 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
1924 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001925 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
1926 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001927 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00001928 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00001929 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001930 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
1931 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00001932 llvm_unreachable("conversion functions are permitted");
1933 }
1934 } ConvertDiagnoser;
1935 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
1936}
1937
1938OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
1939 SourceLocation StartLoc,
1940 SourceLocation LParenLoc,
1941 SourceLocation EndLoc) {
1942 Expr *ValExpr = NumThreads;
1943 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
1944 !NumThreads->isInstantiationDependent() &&
1945 !NumThreads->containsUnexpandedParameterPack()) {
1946 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
1947 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001948 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00001949 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001950 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001951
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001952 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00001953
1954 // OpenMP [2.5, Restrictions]
1955 // The num_threads expression must evaluate to a positive integer value.
1956 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00001957 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
1958 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00001959 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
1960 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001961 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00001962 }
1963 }
1964
Alexey Bataeved09d242014-05-28 05:53:51 +00001965 return new (Context)
1966 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00001967}
1968
Alexey Bataev62c87d22014-03-21 04:51:18 +00001969ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
1970 OpenMPClauseKind CKind) {
1971 if (!E)
1972 return ExprError();
1973 if (E->isValueDependent() || E->isTypeDependent() ||
1974 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001975 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001976 llvm::APSInt Result;
1977 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
1978 if (ICE.isInvalid())
1979 return ExprError();
1980 if (!Result.isStrictlyPositive()) {
1981 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
1982 << getOpenMPClauseName(CKind) << E->getSourceRange();
1983 return ExprError();
1984 }
1985 return ICE;
1986}
1987
1988OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
1989 SourceLocation LParenLoc,
1990 SourceLocation EndLoc) {
1991 // OpenMP [2.8.1, simd construct, Description]
1992 // The parameter of the safelen clause must be a constant
1993 // positive integer expression.
1994 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
1995 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001996 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00001997 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001998 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00001999}
2000
Alexander Musman64d33f12014-06-04 07:53:32 +00002001OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
2002 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00002003 SourceLocation LParenLoc,
2004 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00002005 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002006 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00002007 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00002008 // The parameter of the collapse clause must be a constant
2009 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00002010 ExprResult NumForLoopsResult =
2011 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
2012 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00002013 return nullptr;
2014 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00002015 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00002016}
2017
Alexey Bataeved09d242014-05-28 05:53:51 +00002018OMPClause *Sema::ActOnOpenMPSimpleClause(
2019 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
2020 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002021 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002022 switch (Kind) {
2023 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002024 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00002025 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
2026 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002027 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002028 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00002029 Res = ActOnOpenMPProcBindClause(
2030 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
2031 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002032 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002033 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002034 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002035 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002036 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002037 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002038 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002039 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00002040 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00002041 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00002042 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00002043 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002044 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002045 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002046 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002047 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002048 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002049 case OMPC_threadprivate:
2050 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002051 llvm_unreachable("Clause is not allowed.");
2052 }
2053 return Res;
2054}
2055
2056OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
2057 SourceLocation KindKwLoc,
2058 SourceLocation StartLoc,
2059 SourceLocation LParenLoc,
2060 SourceLocation EndLoc) {
2061 if (Kind == OMPC_DEFAULT_unknown) {
2062 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002063 static_assert(OMPC_DEFAULT_unknown > 0,
2064 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00002065 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002066 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002067 Values += "'";
2068 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
2069 Values += "'";
2070 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002071 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002072 Values += " or ";
2073 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00002074 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002075 break;
2076 default:
2077 Values += Sep;
2078 break;
2079 }
2080 }
2081 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002082 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002083 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002084 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002085 switch (Kind) {
2086 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002087 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002088 break;
2089 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002090 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002091 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002092 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002093 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00002094 break;
2095 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002096 return new (Context)
2097 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002098}
2099
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002100OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
2101 SourceLocation KindKwLoc,
2102 SourceLocation StartLoc,
2103 SourceLocation LParenLoc,
2104 SourceLocation EndLoc) {
2105 if (Kind == OMPC_PROC_BIND_unknown) {
2106 std::string Values;
2107 std::string Sep(", ");
2108 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
2109 Values += "'";
2110 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
2111 Values += "'";
2112 switch (i) {
2113 case OMPC_PROC_BIND_unknown - 2:
2114 Values += " or ";
2115 break;
2116 case OMPC_PROC_BIND_unknown - 1:
2117 break;
2118 default:
2119 Values += Sep;
2120 break;
2121 }
2122 }
2123 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00002124 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002125 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002126 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002127 return new (Context)
2128 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002129}
2130
Alexey Bataev56dafe82014-06-20 07:16:17 +00002131OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
2132 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
2133 SourceLocation StartLoc, SourceLocation LParenLoc,
2134 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
2135 SourceLocation EndLoc) {
2136 OMPClause *Res = nullptr;
2137 switch (Kind) {
2138 case OMPC_schedule:
2139 Res = ActOnOpenMPScheduleClause(
2140 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
2141 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
2142 break;
2143 case OMPC_if:
2144 case OMPC_num_threads:
2145 case OMPC_safelen:
2146 case OMPC_collapse:
2147 case OMPC_default:
2148 case OMPC_proc_bind:
2149 case OMPC_private:
2150 case OMPC_firstprivate:
2151 case OMPC_lastprivate:
2152 case OMPC_shared:
2153 case OMPC_reduction:
2154 case OMPC_linear:
2155 case OMPC_aligned:
2156 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002157 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002158 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002159 case OMPC_nowait:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002160 case OMPC_threadprivate:
2161 case OMPC_unknown:
2162 llvm_unreachable("Clause is not allowed.");
2163 }
2164 return Res;
2165}
2166
2167OMPClause *Sema::ActOnOpenMPScheduleClause(
2168 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
2169 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
2170 SourceLocation EndLoc) {
2171 if (Kind == OMPC_SCHEDULE_unknown) {
2172 std::string Values;
2173 std::string Sep(", ");
2174 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
2175 Values += "'";
2176 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
2177 Values += "'";
2178 switch (i) {
2179 case OMPC_SCHEDULE_unknown - 2:
2180 Values += " or ";
2181 break;
2182 case OMPC_SCHEDULE_unknown - 1:
2183 break;
2184 default:
2185 Values += Sep;
2186 break;
2187 }
2188 }
2189 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
2190 << Values << getOpenMPClauseName(OMPC_schedule);
2191 return nullptr;
2192 }
2193 Expr *ValExpr = ChunkSize;
2194 if (ChunkSize) {
2195 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
2196 !ChunkSize->isInstantiationDependent() &&
2197 !ChunkSize->containsUnexpandedParameterPack()) {
2198 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
2199 ExprResult Val =
2200 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
2201 if (Val.isInvalid())
2202 return nullptr;
2203
2204 ValExpr = Val.get();
2205
2206 // OpenMP [2.7.1, Restrictions]
2207 // chunk_size must be a loop invariant integer expression with a positive
2208 // value.
2209 llvm::APSInt Result;
2210 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
2211 Result.isSigned() && !Result.isStrictlyPositive()) {
2212 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
2213 << "schedule" << ChunkSize->getSourceRange();
2214 return nullptr;
2215 }
2216 }
2217 }
2218
2219 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
2220 EndLoc, Kind, ValExpr);
2221}
2222
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002223OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
2224 SourceLocation StartLoc,
2225 SourceLocation EndLoc) {
2226 OMPClause *Res = nullptr;
2227 switch (Kind) {
2228 case OMPC_ordered:
2229 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
2230 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00002231 case OMPC_nowait:
2232 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
2233 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002234 case OMPC_if:
2235 case OMPC_num_threads:
2236 case OMPC_safelen:
2237 case OMPC_collapse:
2238 case OMPC_schedule:
2239 case OMPC_private:
2240 case OMPC_firstprivate:
2241 case OMPC_lastprivate:
2242 case OMPC_shared:
2243 case OMPC_reduction:
2244 case OMPC_linear:
2245 case OMPC_aligned:
2246 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00002247 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002248 case OMPC_default:
2249 case OMPC_proc_bind:
2250 case OMPC_threadprivate:
2251 case OMPC_unknown:
2252 llvm_unreachable("Clause is not allowed.");
2253 }
2254 return Res;
2255}
2256
2257OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
2258 SourceLocation EndLoc) {
2259 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
2260}
2261
Alexey Bataev236070f2014-06-20 11:19:47 +00002262OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
2263 SourceLocation EndLoc) {
2264 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
2265}
2266
Alexey Bataevc5e02582014-06-16 07:08:35 +00002267OMPClause *Sema::ActOnOpenMPVarListClause(
2268 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
2269 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
2270 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
2271 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002272 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002273 switch (Kind) {
2274 case OMPC_private:
2275 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2276 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002277 case OMPC_firstprivate:
2278 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2279 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00002280 case OMPC_lastprivate:
2281 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2282 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002283 case OMPC_shared:
2284 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
2285 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00002286 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00002287 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
2288 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002289 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00002290 case OMPC_linear:
2291 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
2292 ColonLoc, EndLoc);
2293 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00002294 case OMPC_aligned:
2295 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
2296 ColonLoc, EndLoc);
2297 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00002298 case OMPC_copyin:
2299 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
2300 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00002301 case OMPC_copyprivate:
2302 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
2303 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00002304 case OMPC_if:
Alexey Bataev568a8332014-03-06 06:15:19 +00002305 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00002306 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00002307 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002308 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00002309 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00002310 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00002311 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00002312 case OMPC_nowait:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002313 case OMPC_threadprivate:
2314 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002315 llvm_unreachable("Clause is not allowed.");
2316 }
2317 return Res;
2318}
2319
2320OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
2321 SourceLocation StartLoc,
2322 SourceLocation LParenLoc,
2323 SourceLocation EndLoc) {
2324 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002325 for (auto &RefExpr : VarList) {
2326 assert(RefExpr && "NULL expr in OpenMP private clause.");
2327 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002328 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002329 Vars.push_back(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002330 continue;
2331 }
2332
Alexey Bataeved09d242014-05-28 05:53:51 +00002333 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002334 // OpenMP [2.1, C/C++]
2335 // A list item is a variable name.
2336 // OpenMP [2.9.3.3, Restrictions, p.1]
2337 // A variable that is part of another variable (as an array or
2338 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002339 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002340 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002341 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002342 continue;
2343 }
2344 Decl *D = DE->getDecl();
2345 VarDecl *VD = cast<VarDecl>(D);
2346
2347 QualType Type = VD->getType();
2348 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2349 // It will be analyzed later.
2350 Vars.push_back(DE);
2351 continue;
2352 }
2353
2354 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2355 // A variable that appears in a private clause must not have an incomplete
2356 // type or a reference type.
2357 if (RequireCompleteType(ELoc, Type,
2358 diag::err_omp_private_incomplete_type)) {
2359 continue;
2360 }
2361 if (Type->isReferenceType()) {
2362 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002363 << getOpenMPClauseName(OMPC_private) << Type;
2364 bool IsDecl =
2365 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2366 Diag(VD->getLocation(),
2367 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2368 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002369 continue;
2370 }
2371
2372 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
2373 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002374 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002375 // class type.
2376 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002377 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2378 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002379 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002380 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2381 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2382 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002383 // FIXME This code must be replaced by actual constructing/destructing of
2384 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002385 if (RD) {
2386 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
2387 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002388 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002389 if (!CD ||
2390 CheckConstructorAccess(ELoc, CD,
2391 InitializedEntity::InitializeTemporary(Type),
2392 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002393 CD->isDeleted()) {
2394 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002395 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002396 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2397 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002398 Diag(VD->getLocation(),
2399 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2400 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002401 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2402 continue;
2403 }
2404 MarkFunctionReferenced(ELoc, CD);
2405 DiagnoseUseOfDecl(CD, ELoc);
2406
2407 CXXDestructorDecl *DD = RD->getDestructor();
2408 if (DD) {
2409 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2410 DD->isDeleted()) {
2411 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002412 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002413 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2414 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002415 Diag(VD->getLocation(),
2416 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2417 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002418 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2419 continue;
2420 }
2421 MarkFunctionReferenced(ELoc, DD);
2422 DiagnoseUseOfDecl(DD, ELoc);
2423 }
2424 }
2425
Alexey Bataev758e55e2013-09-06 18:03:48 +00002426 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2427 // in a Construct]
2428 // Variables with the predetermined data-sharing attributes may not be
2429 // listed in data-sharing attributes clauses, except for the cases
2430 // listed below. For these exceptions only, listing a predetermined
2431 // variable in a data-sharing attribute clause is allowed and overrides
2432 // the variable's predetermined data-sharing attributes.
2433 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2434 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002435 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2436 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002437 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002438 continue;
2439 }
2440
2441 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002442 Vars.push_back(DE);
2443 }
2444
Alexey Bataeved09d242014-05-28 05:53:51 +00002445 if (Vars.empty())
2446 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002447
2448 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2449}
2450
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002451OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
2452 SourceLocation StartLoc,
2453 SourceLocation LParenLoc,
2454 SourceLocation EndLoc) {
2455 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002456 for (auto &RefExpr : VarList) {
2457 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
2458 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002459 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002460 Vars.push_back(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002461 continue;
2462 }
2463
Alexey Bataeved09d242014-05-28 05:53:51 +00002464 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002465 // OpenMP [2.1, C/C++]
2466 // A list item is a variable name.
2467 // OpenMP [2.9.3.3, Restrictions, p.1]
2468 // A variable that is part of another variable (as an array or
2469 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00002470 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002471 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002472 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002473 continue;
2474 }
2475 Decl *D = DE->getDecl();
2476 VarDecl *VD = cast<VarDecl>(D);
2477
2478 QualType Type = VD->getType();
2479 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2480 // It will be analyzed later.
2481 Vars.push_back(DE);
2482 continue;
2483 }
2484
2485 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2486 // A variable that appears in a private clause must not have an incomplete
2487 // type or a reference type.
2488 if (RequireCompleteType(ELoc, Type,
2489 diag::err_omp_firstprivate_incomplete_type)) {
2490 continue;
2491 }
2492 if (Type->isReferenceType()) {
2493 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002494 << getOpenMPClauseName(OMPC_firstprivate) << Type;
2495 bool IsDecl =
2496 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2497 Diag(VD->getLocation(),
2498 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2499 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002500 continue;
2501 }
2502
2503 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
2504 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00002505 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002506 // class type.
2507 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002508 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2509 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2510 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002511 // FIXME This code must be replaced by actual constructing/destructing of
2512 // the firstprivate variable.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002513 if (RD) {
2514 CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
2515 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00002516 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002517 if (!CD ||
2518 CheckConstructorAccess(ELoc, CD,
2519 InitializedEntity::InitializeTemporary(Type),
2520 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002521 CD->isDeleted()) {
2522 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002523 << getOpenMPClauseName(OMPC_firstprivate) << 1;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002524 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2525 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002526 Diag(VD->getLocation(),
2527 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2528 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002529 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2530 continue;
2531 }
2532 MarkFunctionReferenced(ELoc, CD);
2533 DiagnoseUseOfDecl(CD, ELoc);
2534
2535 CXXDestructorDecl *DD = RD->getDestructor();
2536 if (DD) {
2537 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2538 DD->isDeleted()) {
2539 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00002540 << getOpenMPClauseName(OMPC_firstprivate) << 4;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002541 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2542 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00002543 Diag(VD->getLocation(),
2544 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2545 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002546 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2547 continue;
2548 }
2549 MarkFunctionReferenced(ELoc, DD);
2550 DiagnoseUseOfDecl(DD, ELoc);
2551 }
2552 }
2553
2554 // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
2555 // variable and it was checked already.
2556 if (StartLoc.isValid() && EndLoc.isValid()) {
2557 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2558 Type = Type.getNonReferenceType().getCanonicalType();
2559 bool IsConstant = Type.isConstant(Context);
2560 Type = Context.getBaseElementType(Type);
2561 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
2562 // A list item that specifies a given variable may not appear in more
2563 // than one clause on the same directive, except that a variable may be
2564 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002565 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00002566 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002567 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002568 << getOpenMPClauseName(DVar.CKind)
2569 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002570 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002571 continue;
2572 }
2573
2574 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2575 // in a Construct]
2576 // Variables with the predetermined data-sharing attributes may not be
2577 // listed in data-sharing attributes clauses, except for the cases
2578 // listed below. For these exceptions only, listing a predetermined
2579 // variable in a data-sharing attribute clause is allowed and overrides
2580 // the variable's predetermined data-sharing attributes.
2581 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2582 // in a Construct, C/C++, p.2]
2583 // Variables with const-qualified type having no mutable member may be
2584 // listed in a firstprivate clause, even if they are static data members.
2585 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
2586 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
2587 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00002588 << getOpenMPClauseName(DVar.CKind)
2589 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002590 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002591 continue;
2592 }
2593
Alexey Bataevf29276e2014-06-18 04:14:57 +00002594 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002595 // OpenMP [2.9.3.4, Restrictions, p.2]
2596 // A list item that is private within a parallel region must not appear
2597 // in a firstprivate clause on a worksharing construct if any of the
2598 // worksharing regions arising from the worksharing construct ever bind
2599 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00002600 if (isOpenMPWorksharingDirective(CurrDir) &&
2601 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002602 DVar = DSAStack->getImplicitDSA(VD);
2603 if (DVar.CKind != OMPC_shared) {
2604 Diag(ELoc, diag::err_omp_required_access)
2605 << getOpenMPClauseName(OMPC_firstprivate)
2606 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002607 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002608 continue;
2609 }
2610 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002611 // OpenMP [2.9.3.4, Restrictions, p.3]
2612 // A list item that appears in a reduction clause of a parallel construct
2613 // must not appear in a firstprivate clause on a worksharing or task
2614 // construct if any of the worksharing or task regions arising from the
2615 // worksharing or task construct ever bind to any of the parallel regions
2616 // arising from the parallel construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002617 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002618 // OpenMP [2.9.3.4, Restrictions, p.4]
2619 // A list item that appears in a reduction clause in worksharing
2620 // construct must not appear in a firstprivate clause in a task construct
2621 // encountered during execution of any of the worksharing regions arising
2622 // from the worksharing construct.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002623 // TODO
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002624 }
2625
2626 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
2627 Vars.push_back(DE);
2628 }
2629
Alexey Bataeved09d242014-05-28 05:53:51 +00002630 if (Vars.empty())
2631 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002632
2633 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2634 Vars);
2635}
2636
Alexander Musman1bb328c2014-06-04 13:06:39 +00002637OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
2638 SourceLocation StartLoc,
2639 SourceLocation LParenLoc,
2640 SourceLocation EndLoc) {
2641 SmallVector<Expr *, 8> Vars;
2642 for (auto &RefExpr : VarList) {
2643 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
2644 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2645 // It will be analyzed later.
2646 Vars.push_back(RefExpr);
2647 continue;
2648 }
2649
2650 SourceLocation ELoc = RefExpr->getExprLoc();
2651 // OpenMP [2.1, C/C++]
2652 // A list item is a variable name.
2653 // OpenMP [2.14.3.5, Restrictions, p.1]
2654 // A variable that is part of another variable (as an array or structure
2655 // element) cannot appear in a lastprivate clause.
2656 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
2657 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2658 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
2659 continue;
2660 }
2661 Decl *D = DE->getDecl();
2662 VarDecl *VD = cast<VarDecl>(D);
2663
2664 QualType Type = VD->getType();
2665 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2666 // It will be analyzed later.
2667 Vars.push_back(DE);
2668 continue;
2669 }
2670
2671 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
2672 // A variable that appears in a lastprivate clause must not have an
2673 // incomplete type or a reference type.
2674 if (RequireCompleteType(ELoc, Type,
2675 diag::err_omp_lastprivate_incomplete_type)) {
2676 continue;
2677 }
2678 if (Type->isReferenceType()) {
2679 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
2680 << getOpenMPClauseName(OMPC_lastprivate) << Type;
2681 bool IsDecl =
2682 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2683 Diag(VD->getLocation(),
2684 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2685 << VD;
2686 continue;
2687 }
2688
2689 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
2690 // in a Construct]
2691 // Variables with the predetermined data-sharing attributes may not be
2692 // listed in data-sharing attributes clauses, except for the cases
2693 // listed below.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002694 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
2695 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
2696 DVar.CKind != OMPC_firstprivate &&
2697 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
2698 Diag(ELoc, diag::err_omp_wrong_dsa)
2699 << getOpenMPClauseName(DVar.CKind)
2700 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002701 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002702 continue;
2703 }
2704
Alexey Bataevf29276e2014-06-18 04:14:57 +00002705 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
2706 // OpenMP [2.14.3.5, Restrictions, p.2]
2707 // A list item that is private within a parallel region, or that appears in
2708 // the reduction clause of a parallel construct, must not appear in a
2709 // lastprivate clause on a worksharing construct if any of the corresponding
2710 // worksharing regions ever binds to any of the corresponding parallel
2711 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00002712 if (isOpenMPWorksharingDirective(CurrDir) &&
2713 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002714 DVar = DSAStack->getImplicitDSA(VD);
2715 if (DVar.CKind != OMPC_shared) {
2716 Diag(ELoc, diag::err_omp_required_access)
2717 << getOpenMPClauseName(OMPC_lastprivate)
2718 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002719 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002720 continue;
2721 }
2722 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002723 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00002724 // A variable of class type (or array thereof) that appears in a
2725 // lastprivate clause requires an accessible, unambiguous default
2726 // constructor for the class type, unless the list item is also specified
2727 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002728 // A variable of class type (or array thereof) that appears in a
2729 // lastprivate clause requires an accessible, unambiguous copy assignment
2730 // operator for the class type.
2731 while (Type.getNonReferenceType()->isArrayType())
2732 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
2733 ->getElementType();
2734 CXXRecordDecl *RD = getLangOpts().CPlusPlus
2735 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
2736 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00002737 // FIXME This code must be replaced by actual copying and destructing of the
2738 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00002739 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00002740 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
2741 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002742 if (MD) {
2743 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
2744 MD->isDeleted()) {
2745 Diag(ELoc, diag::err_omp_required_method)
2746 << getOpenMPClauseName(OMPC_lastprivate) << 2;
2747 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2748 VarDecl::DeclarationOnly;
2749 Diag(VD->getLocation(),
2750 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2751 << VD;
2752 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2753 continue;
2754 }
2755 MarkFunctionReferenced(ELoc, MD);
2756 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002757 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00002758
2759 CXXDestructorDecl *DD = RD->getDestructor();
2760 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002761 PartialDiagnostic PD =
2762 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00002763 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
2764 DD->isDeleted()) {
2765 Diag(ELoc, diag::err_omp_required_method)
2766 << getOpenMPClauseName(OMPC_lastprivate) << 4;
2767 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
2768 VarDecl::DeclarationOnly;
2769 Diag(VD->getLocation(),
2770 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2771 << VD;
2772 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
2773 continue;
2774 }
2775 MarkFunctionReferenced(ELoc, DD);
2776 DiagnoseUseOfDecl(DD, ELoc);
2777 }
2778 }
2779
Alexey Bataevf29276e2014-06-18 04:14:57 +00002780 if (DVar.CKind != OMPC_firstprivate)
2781 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00002782 Vars.push_back(DE);
2783 }
2784
2785 if (Vars.empty())
2786 return nullptr;
2787
2788 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
2789 Vars);
2790}
2791
Alexey Bataev758e55e2013-09-06 18:03:48 +00002792OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
2793 SourceLocation StartLoc,
2794 SourceLocation LParenLoc,
2795 SourceLocation EndLoc) {
2796 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00002797 for (auto &RefExpr : VarList) {
2798 assert(RefExpr && "NULL expr in OpenMP shared clause.");
2799 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002800 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00002801 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002802 continue;
2803 }
2804
Alexey Bataeved09d242014-05-28 05:53:51 +00002805 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002806 // OpenMP [2.1, C/C++]
2807 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00002808 // OpenMP [2.14.3.2, Restrictions, p.1]
2809 // A variable that is part of another variable (as an array or structure
2810 // element) cannot appear in a shared unless it is a static data member
2811 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00002812 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002813 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00002814 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002815 continue;
2816 }
2817 Decl *D = DE->getDecl();
2818 VarDecl *VD = cast<VarDecl>(D);
2819
2820 QualType Type = VD->getType();
2821 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
2822 // It will be analyzed later.
2823 Vars.push_back(DE);
2824 continue;
2825 }
2826
2827 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
2828 // in a Construct]
2829 // Variables with the predetermined data-sharing attributes may not be
2830 // listed in data-sharing attributes clauses, except for the cases
2831 // listed below. For these exceptions only, listing a predetermined
2832 // variable in a data-sharing attribute clause is allowed and overrides
2833 // the variable's predetermined data-sharing attributes.
2834 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
Alexey Bataeved09d242014-05-28 05:53:51 +00002835 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
2836 DVar.RefExpr) {
2837 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
2838 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002839 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002840 continue;
2841 }
2842
2843 DSAStack->addDSA(VD, DE, OMPC_shared);
2844 Vars.push_back(DE);
2845 }
2846
Alexey Bataeved09d242014-05-28 05:53:51 +00002847 if (Vars.empty())
2848 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002849
2850 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
2851}
2852
Alexey Bataevc5e02582014-06-16 07:08:35 +00002853namespace {
2854class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
2855 DSAStackTy *Stack;
2856
2857public:
2858 bool VisitDeclRefExpr(DeclRefExpr *E) {
2859 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
2860 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
2861 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
2862 return false;
2863 if (DVar.CKind != OMPC_unknown)
2864 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002865 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev23b69422014-06-18 07:08:49 +00002866 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways());
Alexey Bataevf29276e2014-06-18 04:14:57 +00002867 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00002868 return true;
2869 return false;
2870 }
2871 return false;
2872 }
2873 bool VisitStmt(Stmt *S) {
2874 for (auto Child : S->children()) {
2875 if (Child && Visit(Child))
2876 return true;
2877 }
2878 return false;
2879 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002880 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00002881};
Alexey Bataev23b69422014-06-18 07:08:49 +00002882} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00002883
2884OMPClause *Sema::ActOnOpenMPReductionClause(
2885 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
2886 SourceLocation ColonLoc, SourceLocation EndLoc,
2887 CXXScopeSpec &ReductionIdScopeSpec,
2888 const DeclarationNameInfo &ReductionId) {
2889 // TODO: Allow scope specification search when 'declare reduction' is
2890 // supported.
2891 assert(ReductionIdScopeSpec.isEmpty() &&
2892 "No support for scoped reduction identifiers yet.");
2893
2894 auto DN = ReductionId.getName();
2895 auto OOK = DN.getCXXOverloadedOperator();
2896 BinaryOperatorKind BOK = BO_Comma;
2897
2898 // OpenMP [2.14.3.6, reduction clause]
2899 // C
2900 // reduction-identifier is either an identifier or one of the following
2901 // operators: +, -, *, &, |, ^, && and ||
2902 // C++
2903 // reduction-identifier is either an id-expression or one of the following
2904 // operators: +, -, *, &, |, ^, && and ||
2905 // FIXME: Only 'min' and 'max' identifiers are supported for now.
2906 switch (OOK) {
2907 case OO_Plus:
2908 case OO_Minus:
2909 BOK = BO_AddAssign;
2910 break;
2911 case OO_Star:
2912 BOK = BO_MulAssign;
2913 break;
2914 case OO_Amp:
2915 BOK = BO_AndAssign;
2916 break;
2917 case OO_Pipe:
2918 BOK = BO_OrAssign;
2919 break;
2920 case OO_Caret:
2921 BOK = BO_XorAssign;
2922 break;
2923 case OO_AmpAmp:
2924 BOK = BO_LAnd;
2925 break;
2926 case OO_PipePipe:
2927 BOK = BO_LOr;
2928 break;
2929 default:
2930 if (auto II = DN.getAsIdentifierInfo()) {
2931 if (II->isStr("max"))
2932 BOK = BO_GT;
2933 else if (II->isStr("min"))
2934 BOK = BO_LT;
2935 }
2936 break;
2937 }
2938 SourceRange ReductionIdRange;
2939 if (ReductionIdScopeSpec.isValid()) {
2940 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
2941 }
2942 ReductionIdRange.setEnd(ReductionId.getEndLoc());
2943 if (BOK == BO_Comma) {
2944 // Not allowed reduction identifier is found.
2945 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
2946 << ReductionIdRange;
2947 return nullptr;
2948 }
2949
2950 SmallVector<Expr *, 8> Vars;
2951 for (auto RefExpr : VarList) {
2952 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
2953 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
2954 // It will be analyzed later.
2955 Vars.push_back(RefExpr);
2956 continue;
2957 }
2958
2959 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
2960 RefExpr->isInstantiationDependent() ||
2961 RefExpr->containsUnexpandedParameterPack()) {
2962 // It will be analyzed later.
2963 Vars.push_back(RefExpr);
2964 continue;
2965 }
2966
2967 auto ELoc = RefExpr->getExprLoc();
2968 auto ERange = RefExpr->getSourceRange();
2969 // OpenMP [2.1, C/C++]
2970 // A list item is a variable or array section, subject to the restrictions
2971 // specified in Section 2.4 on page 42 and in each of the sections
2972 // describing clauses and directives for which a list appears.
2973 // OpenMP [2.14.3.3, Restrictions, p.1]
2974 // A variable that is part of another variable (as an array or
2975 // structure element) cannot appear in a private clause.
2976 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
2977 if (!DE || !isa<VarDecl>(DE->getDecl())) {
2978 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
2979 continue;
2980 }
2981 auto D = DE->getDecl();
2982 auto VD = cast<VarDecl>(D);
2983 auto Type = VD->getType();
2984 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
2985 // A variable that appears in a private clause must not have an incomplete
2986 // type or a reference type.
2987 if (RequireCompleteType(ELoc, Type,
2988 diag::err_omp_reduction_incomplete_type))
2989 continue;
2990 // OpenMP [2.14.3.6, reduction clause, Restrictions]
2991 // Arrays may not appear in a reduction clause.
2992 if (Type.getNonReferenceType()->isArrayType()) {
2993 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
2994 bool IsDecl =
2995 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2996 Diag(VD->getLocation(),
2997 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2998 << VD;
2999 continue;
3000 }
3001 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3002 // A list item that appears in a reduction clause must not be
3003 // const-qualified.
3004 if (Type.getNonReferenceType().isConstant(Context)) {
3005 Diag(ELoc, diag::err_omp_const_variable)
3006 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
3007 bool IsDecl =
3008 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3009 Diag(VD->getLocation(),
3010 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3011 << VD;
3012 continue;
3013 }
3014 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
3015 // If a list-item is a reference type then it must bind to the same object
3016 // for all threads of the team.
3017 VarDecl *VDDef = VD->getDefinition();
3018 if (Type->isReferenceType() && VDDef) {
3019 DSARefChecker Check(DSAStack);
3020 if (Check.Visit(VDDef->getInit())) {
3021 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
3022 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
3023 continue;
3024 }
3025 }
3026 // OpenMP [2.14.3.6, reduction clause, Restrictions]
3027 // The type of a list item that appears in a reduction clause must be valid
3028 // for the reduction-identifier. For a max or min reduction in C, the type
3029 // of the list item must be an allowed arithmetic data type: char, int,
3030 // float, double, or _Bool, possibly modified with long, short, signed, or
3031 // unsigned. For a max or min reduction in C++, the type of the list item
3032 // must be an allowed arithmetic data type: char, wchar_t, int, float,
3033 // double, or bool, possibly modified with long, short, signed, or unsigned.
3034 if ((BOK == BO_GT || BOK == BO_LT) &&
3035 !(Type->isScalarType() ||
3036 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
3037 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
3038 << getLangOpts().CPlusPlus;
3039 bool IsDecl =
3040 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3041 Diag(VD->getLocation(),
3042 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3043 << VD;
3044 continue;
3045 }
3046 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
3047 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
3048 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
3049 bool IsDecl =
3050 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3051 Diag(VD->getLocation(),
3052 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3053 << VD;
3054 continue;
3055 }
3056 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
3057 getDiagnostics().setSuppressAllDiagnostics(true);
3058 ExprResult ReductionOp =
3059 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
3060 RefExpr, RefExpr);
3061 getDiagnostics().setSuppressAllDiagnostics(Suppress);
3062 if (ReductionOp.isInvalid()) {
3063 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00003064 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003065 bool IsDecl =
3066 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3067 Diag(VD->getLocation(),
3068 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3069 << VD;
3070 continue;
3071 }
3072
3073 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
3074 // in a Construct]
3075 // Variables with the predetermined data-sharing attributes may not be
3076 // listed in data-sharing attributes clauses, except for the cases
3077 // listed below. For these exceptions only, listing a predetermined
3078 // variable in a data-sharing attribute clause is allowed and overrides
3079 // the variable's predetermined data-sharing attributes.
3080 // OpenMP [2.14.3.6, Restrictions, p.3]
3081 // Any number of reduction clauses can be specified on the directive,
3082 // but a list item can appear only once in the reduction clauses for that
3083 // directive.
3084 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
3085 if (DVar.CKind == OMPC_reduction) {
3086 Diag(ELoc, diag::err_omp_once_referenced)
3087 << getOpenMPClauseName(OMPC_reduction);
3088 if (DVar.RefExpr) {
3089 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
3090 }
3091 } else if (DVar.CKind != OMPC_unknown) {
3092 Diag(ELoc, diag::err_omp_wrong_dsa)
3093 << getOpenMPClauseName(DVar.CKind)
3094 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003095 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003096 continue;
3097 }
3098
3099 // OpenMP [2.14.3.6, Restrictions, p.1]
3100 // A list item that appears in a reduction clause of a worksharing
3101 // construct must be shared in the parallel regions to which any of the
3102 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003103 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00003104 if (isOpenMPWorksharingDirective(CurrDir) &&
3105 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00003106 DVar = DSAStack->getImplicitDSA(VD);
3107 if (DVar.CKind != OMPC_shared) {
3108 Diag(ELoc, diag::err_omp_required_access)
3109 << getOpenMPClauseName(OMPC_reduction)
3110 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003111 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003112 continue;
3113 }
3114 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00003115
3116 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3117 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3118 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003119 // FIXME This code must be replaced by actual constructing/destructing of
3120 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00003121 if (RD) {
3122 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3123 PartialDiagnostic PD =
3124 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00003125 if (!CD ||
3126 CheckConstructorAccess(ELoc, CD,
3127 InitializedEntity::InitializeTemporary(Type),
3128 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00003129 CD->isDeleted()) {
3130 Diag(ELoc, diag::err_omp_required_method)
3131 << getOpenMPClauseName(OMPC_reduction) << 0;
3132 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3133 VarDecl::DeclarationOnly;
3134 Diag(VD->getLocation(),
3135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3136 << VD;
3137 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3138 continue;
3139 }
3140 MarkFunctionReferenced(ELoc, CD);
3141 DiagnoseUseOfDecl(CD, ELoc);
3142
3143 CXXDestructorDecl *DD = RD->getDestructor();
3144 if (DD) {
3145 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
3146 DD->isDeleted()) {
3147 Diag(ELoc, diag::err_omp_required_method)
3148 << getOpenMPClauseName(OMPC_reduction) << 4;
3149 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3150 VarDecl::DeclarationOnly;
3151 Diag(VD->getLocation(),
3152 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3153 << VD;
3154 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3155 continue;
3156 }
3157 MarkFunctionReferenced(ELoc, DD);
3158 DiagnoseUseOfDecl(DD, ELoc);
3159 }
3160 }
3161
3162 DSAStack->addDSA(VD, DE, OMPC_reduction);
3163 Vars.push_back(DE);
3164 }
3165
3166 if (Vars.empty())
3167 return nullptr;
3168
3169 return OMPReductionClause::Create(
3170 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
3171 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
3172}
3173
Alexander Musman8dba6642014-04-22 13:09:42 +00003174OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
3175 SourceLocation StartLoc,
3176 SourceLocation LParenLoc,
3177 SourceLocation ColonLoc,
3178 SourceLocation EndLoc) {
3179 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003180 for (auto &RefExpr : VarList) {
3181 assert(RefExpr && "NULL expr in OpenMP linear clause.");
3182 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00003183 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003184 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003185 continue;
3186 }
3187
3188 // OpenMP [2.14.3.7, linear clause]
3189 // A list item that appears in a linear clause is subject to the private
3190 // clause semantics described in Section 2.14.3.3 on page 159 except as
3191 // noted. In addition, the value of the new list item on each iteration
3192 // of the associated loop(s) corresponds to the value of the original
3193 // list item before entering the construct plus the logical number of
3194 // the iteration times linear-step.
3195
Alexey Bataeved09d242014-05-28 05:53:51 +00003196 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00003197 // OpenMP [2.1, C/C++]
3198 // A list item is a variable name.
3199 // OpenMP [2.14.3.3, Restrictions, p.1]
3200 // A variable that is part of another variable (as an array or
3201 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003202 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00003203 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003204 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00003205 continue;
3206 }
3207
3208 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3209
3210 // OpenMP [2.14.3.7, linear clause]
3211 // A list-item cannot appear in more than one linear clause.
3212 // A list-item that appears in a linear clause cannot appear in any
3213 // other data-sharing attribute clause.
3214 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
3215 if (DVar.RefExpr) {
3216 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3217 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003218 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00003219 continue;
3220 }
3221
3222 QualType QType = VD->getType();
3223 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
3224 // It will be analyzed later.
3225 Vars.push_back(DE);
3226 continue;
3227 }
3228
3229 // A variable must not have an incomplete type or a reference type.
3230 if (RequireCompleteType(ELoc, QType,
3231 diag::err_omp_linear_incomplete_type)) {
3232 continue;
3233 }
3234 if (QType->isReferenceType()) {
3235 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
3236 << getOpenMPClauseName(OMPC_linear) << QType;
3237 bool IsDecl =
3238 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3239 Diag(VD->getLocation(),
3240 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3241 << VD;
3242 continue;
3243 }
3244
3245 // A list item must not be const-qualified.
3246 if (QType.isConstant(Context)) {
3247 Diag(ELoc, diag::err_omp_const_variable)
3248 << getOpenMPClauseName(OMPC_linear);
3249 bool IsDecl =
3250 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3251 Diag(VD->getLocation(),
3252 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3253 << VD;
3254 continue;
3255 }
3256
3257 // A list item must be of integral or pointer type.
3258 QType = QType.getUnqualifiedType().getCanonicalType();
3259 const Type *Ty = QType.getTypePtrOrNull();
3260 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
3261 !Ty->isPointerType())) {
3262 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
3263 bool IsDecl =
3264 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3265 Diag(VD->getLocation(),
3266 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3267 << VD;
3268 continue;
3269 }
3270
3271 DSAStack->addDSA(VD, DE, OMPC_linear);
3272 Vars.push_back(DE);
3273 }
3274
3275 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003276 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00003277
3278 Expr *StepExpr = Step;
3279 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3280 !Step->isInstantiationDependent() &&
3281 !Step->containsUnexpandedParameterPack()) {
3282 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003283 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00003284 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003285 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003286 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00003287
3288 // Warn about zero linear step (it would be probably better specified as
3289 // making corresponding variables 'const').
3290 llvm::APSInt Result;
3291 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
3292 !Result.isNegative() && !Result.isStrictlyPositive())
3293 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
3294 << (Vars.size() > 1);
3295 }
3296
3297 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
3298 Vars, StepExpr);
3299}
3300
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003301OMPClause *Sema::ActOnOpenMPAlignedClause(
3302 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
3303 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
3304
3305 SmallVector<Expr *, 8> Vars;
3306 for (auto &RefExpr : VarList) {
3307 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
3308 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3309 // It will be analyzed later.
3310 Vars.push_back(RefExpr);
3311 continue;
3312 }
3313
3314 SourceLocation ELoc = RefExpr->getExprLoc();
3315 // OpenMP [2.1, C/C++]
3316 // A list item is a variable name.
3317 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3318 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3319 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3320 continue;
3321 }
3322
3323 VarDecl *VD = cast<VarDecl>(DE->getDecl());
3324
3325 // OpenMP [2.8.1, simd construct, Restrictions]
3326 // The type of list items appearing in the aligned clause must be
3327 // array, pointer, reference to array, or reference to pointer.
3328 QualType QType = DE->getType()
3329 .getNonReferenceType()
3330 .getUnqualifiedType()
3331 .getCanonicalType();
3332 const Type *Ty = QType.getTypePtrOrNull();
3333 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
3334 !Ty->isPointerType())) {
3335 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
3336 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
3337 bool IsDecl =
3338 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3339 Diag(VD->getLocation(),
3340 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3341 << VD;
3342 continue;
3343 }
3344
3345 // OpenMP [2.8.1, simd construct, Restrictions]
3346 // A list-item cannot appear in more than one aligned clause.
3347 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
3348 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
3349 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
3350 << getOpenMPClauseName(OMPC_aligned);
3351 continue;
3352 }
3353
3354 Vars.push_back(DE);
3355 }
3356
3357 // OpenMP [2.8.1, simd construct, Description]
3358 // The parameter of the aligned clause, alignment, must be a constant
3359 // positive integer expression.
3360 // If no optional parameter is specified, implementation-defined default
3361 // alignments for SIMD instructions on the target platforms are assumed.
3362 if (Alignment != nullptr) {
3363 ExprResult AlignResult =
3364 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
3365 if (AlignResult.isInvalid())
3366 return nullptr;
3367 Alignment = AlignResult.get();
3368 }
3369 if (Vars.empty())
3370 return nullptr;
3371
3372 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
3373 EndLoc, Vars, Alignment);
3374}
3375
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003376OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
3377 SourceLocation StartLoc,
3378 SourceLocation LParenLoc,
3379 SourceLocation EndLoc) {
3380 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003381 for (auto &RefExpr : VarList) {
3382 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
3383 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003384 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003385 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003386 continue;
3387 }
3388
Alexey Bataeved09d242014-05-28 05:53:51 +00003389 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003390 // OpenMP [2.1, C/C++]
3391 // A list item is a variable name.
3392 // OpenMP [2.14.4.1, Restrictions, p.1]
3393 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00003394 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003395 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003396 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003397 continue;
3398 }
3399
3400 Decl *D = DE->getDecl();
3401 VarDecl *VD = cast<VarDecl>(D);
3402
3403 QualType Type = VD->getType();
3404 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3405 // It will be analyzed later.
3406 Vars.push_back(DE);
3407 continue;
3408 }
3409
3410 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
3411 // A list item that appears in a copyin clause must be threadprivate.
3412 if (!DSAStack->isThreadPrivate(VD)) {
3413 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00003414 << getOpenMPClauseName(OMPC_copyin)
3415 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003416 continue;
3417 }
3418
3419 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3420 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00003421 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003422 // operator for the class type.
3423 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003424 CXXRecordDecl *RD =
3425 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003426 // FIXME This code must be replaced by actual assignment of the
3427 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003428 if (RD) {
3429 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3430 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003431 if (MD) {
3432 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3433 MD->isDeleted()) {
3434 Diag(ELoc, diag::err_omp_required_method)
3435 << getOpenMPClauseName(OMPC_copyin) << 2;
3436 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3437 VarDecl::DeclarationOnly;
3438 Diag(VD->getLocation(),
3439 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3440 << VD;
3441 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3442 continue;
3443 }
3444 MarkFunctionReferenced(ELoc, MD);
3445 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003446 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003447 }
3448
3449 DSAStack->addDSA(VD, DE, OMPC_copyin);
3450 Vars.push_back(DE);
3451 }
3452
Alexey Bataeved09d242014-05-28 05:53:51 +00003453 if (Vars.empty())
3454 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003455
3456 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3457}
3458
Alexey Bataevbae9a792014-06-27 10:37:06 +00003459OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
3460 SourceLocation StartLoc,
3461 SourceLocation LParenLoc,
3462 SourceLocation EndLoc) {
3463 SmallVector<Expr *, 8> Vars;
3464 for (auto &RefExpr : VarList) {
3465 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
3466 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
3467 // It will be analyzed later.
3468 Vars.push_back(RefExpr);
3469 continue;
3470 }
3471
3472 SourceLocation ELoc = RefExpr->getExprLoc();
3473 // OpenMP [2.1, C/C++]
3474 // A list item is a variable name.
3475 // OpenMP [2.14.4.1, Restrictions, p.1]
3476 // A list item that appears in a copyin clause must be threadprivate.
3477 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
3478 if (!DE || !isa<VarDecl>(DE->getDecl())) {
3479 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
3480 continue;
3481 }
3482
3483 Decl *D = DE->getDecl();
3484 VarDecl *VD = cast<VarDecl>(D);
3485
3486 QualType Type = VD->getType();
3487 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3488 // It will be analyzed later.
3489 Vars.push_back(DE);
3490 continue;
3491 }
3492
3493 // OpenMP [2.14.4.2, Restrictions, p.2]
3494 // A list item that appears in a copyprivate clause may not appear in a
3495 // private or firstprivate clause on the single construct.
3496 if (!DSAStack->isThreadPrivate(VD)) {
3497 auto DVar = DSAStack->getTopDSA(VD);
3498 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
3499 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
3500 Diag(ELoc, diag::err_omp_wrong_dsa)
3501 << getOpenMPClauseName(DVar.CKind)
3502 << getOpenMPClauseName(OMPC_copyprivate);
3503 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3504 continue;
3505 }
3506
3507 // OpenMP [2.11.4.2, Restrictions, p.1]
3508 // All list items that appear in a copyprivate clause must be either
3509 // threadprivate or private in the enclosing context.
3510 if (DVar.CKind == OMPC_unknown) {
3511 DVar = DSAStack->getImplicitDSA(VD);
3512 if (DVar.CKind == OMPC_shared) {
3513 Diag(ELoc, diag::err_omp_required_access)
3514 << getOpenMPClauseName(OMPC_copyprivate)
3515 << "threadprivate or private in the enclosing context";
3516 ReportOriginalDSA(*this, DSAStack, VD, DVar);
3517 continue;
3518 }
3519 }
3520 }
3521
3522 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
3523 // A variable of class type (or array thereof) that appears in a
3524 // copyin clause requires an accessible, unambiguous copy assignment
3525 // operator for the class type.
3526 Type = Context.getBaseElementType(Type);
3527 CXXRecordDecl *RD =
3528 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
3529 // FIXME This code must be replaced by actual assignment of the
3530 // threadprivate variable.
3531 if (RD) {
3532 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
3533 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
3534 if (MD) {
3535 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
3536 MD->isDeleted()) {
3537 Diag(ELoc, diag::err_omp_required_method)
3538 << getOpenMPClauseName(OMPC_copyprivate) << 2;
3539 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3540 VarDecl::DeclarationOnly;
3541 Diag(VD->getLocation(),
3542 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3543 << VD;
3544 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3545 continue;
3546 }
3547 MarkFunctionReferenced(ELoc, MD);
3548 DiagnoseUseOfDecl(MD, ELoc);
3549 }
3550 }
3551
3552 // No need to mark vars as copyprivate, they are already threadprivate or
3553 // implicitly private.
3554 Vars.push_back(DE);
3555 }
3556
3557 if (Vars.empty())
3558 return nullptr;
3559
3560 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
3561}
3562
Alexey Bataev758e55e2013-09-06 18:03:48 +00003563#undef DSAStack